mirror of
https://github.com/BoardWare-Genius/jarvis-models.git
synced 2025-12-13 16:53:24 +00:00
feat: runtime block value
This commit is contained in:
@ -55,7 +55,15 @@ class Parser:
|
|||||||
"body": body,
|
"body": body,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def if_statement(self):
|
||||||
|
self.eat('if')
|
||||||
|
return {
|
||||||
|
"type": 'IfStatement',
|
||||||
|
}
|
||||||
|
|
||||||
def expression_statement(self):
|
def expression_statement(self):
|
||||||
|
if self.token_type() == "if":
|
||||||
|
return self.if_statement()
|
||||||
if self.token_type() == "return":
|
if self.token_type() == "return":
|
||||||
return self.return_statement()
|
return self.return_statement()
|
||||||
if self._is_literal():
|
if self._is_literal():
|
||||||
|
|||||||
@ -32,8 +32,7 @@ class Runtime:
|
|||||||
def assignment_expression(self, ast):
|
def assignment_expression(self, ast):
|
||||||
id = ast.get("identifier").get("name")
|
id = ast.get("identifier").get("name")
|
||||||
v = ast.get("value")
|
v = ast.get("value")
|
||||||
l = self.literal(v)
|
self.set_identifier_value(id, self.unquote(v))
|
||||||
self.records[id] = self.unquote(v)
|
|
||||||
|
|
||||||
def _is_call_function(self, ast):
|
def _is_call_function(self, ast):
|
||||||
return ast["type"] == "CallExpression"
|
return ast["type"] == "CallExpression"
|
||||||
@ -44,14 +43,32 @@ class Runtime:
|
|||||||
unquoted_args = []
|
unquoted_args = []
|
||||||
for arg in args:
|
for arg in args:
|
||||||
unquoted_args.append(self.unquote(arg))
|
unquoted_args.append(self.unquote(arg))
|
||||||
fu = self.records.get(id)
|
fu = self.get_identifier_value(id)
|
||||||
if fu == None:
|
if fu == None:
|
||||||
raise Exception("Function not found: " + id)
|
raise Exception("Function not found: " + id)
|
||||||
return fu(*unquoted_args)
|
return fu(*unquoted_args)
|
||||||
|
|
||||||
|
def get_identifier_value(self, key):
|
||||||
|
v = self.records.get(key)
|
||||||
|
if v != None:
|
||||||
|
return v
|
||||||
|
if self.parent != None:
|
||||||
|
return self.parent.get_identifier_value(key)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def set_identifier_value(self, key, value):
|
||||||
|
v = self.records.get(key)
|
||||||
|
if v != None:
|
||||||
|
self.records[key] = value
|
||||||
|
return
|
||||||
|
if self.parent != None:
|
||||||
|
self.parent.set_identifier_value(key, value)
|
||||||
|
return
|
||||||
|
raise Exception("Identifier not found: " + key)
|
||||||
|
|
||||||
def unquote(self, ast):
|
def unquote(self, ast):
|
||||||
if self._is_identifier(ast):
|
if self._is_identifier(ast):
|
||||||
return self.records.get(ast.get("name"))
|
return self.get_identifier_value(ast.get("name"))
|
||||||
if self._is_literal(ast):
|
if self._is_literal(ast):
|
||||||
return self.literal(ast)
|
return self.literal(ast)
|
||||||
if self._is_call_function(ast):
|
if self._is_call_function(ast):
|
||||||
|
|||||||
@ -39,6 +39,15 @@ specs = (
|
|||||||
# Assignment:
|
# Assignment:
|
||||||
(re.compile(r"^="), "SIMPLE_ASSIGN"),
|
(re.compile(r"^="), "SIMPLE_ASSIGN"),
|
||||||
|
|
||||||
|
# Logical operators:
|
||||||
|
(re.compile(r"^&&"), "AND"),
|
||||||
|
(re.compile(r"^\|\|"), "OR"),
|
||||||
|
(re.compile(r"^!"), "NOT"),
|
||||||
|
|
||||||
|
# Math operators: +, -, *, /:
|
||||||
|
(re.compile(r"^[+-]"), "ADDITIVE_OPERATOR"),
|
||||||
|
(re.compile(r"^[*/]"), "MULTIPLICATIVE_OPERATOR"),
|
||||||
|
|
||||||
# Double-quoted strings
|
# Double-quoted strings
|
||||||
(re.compile(r"^\"[^\"]*\""), "STRING"),
|
(re.compile(r"^\"[^\"]*\""), "STRING"),
|
||||||
|
|
||||||
|
|||||||
@ -4,10 +4,9 @@ from runtime.ast.parser import Parser
|
|||||||
from runtime.ast.runtime import Runtime
|
from runtime.ast.runtime import Runtime
|
||||||
|
|
||||||
script = """
|
script = """
|
||||||
let hello = true
|
let hello = add(div(1,2),2)
|
||||||
let text = audio_to_text(audio)
|
|
||||||
print(hello)
|
return hello
|
||||||
return tts(text)
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def version():
|
def version():
|
||||||
@ -42,10 +41,7 @@ if __name__ == "__main__":
|
|||||||
"version": version,
|
"version": version,
|
||||||
"print": print
|
"print": print
|
||||||
})
|
})
|
||||||
|
ast = Parser().parse(script)
|
||||||
# 注入數據
|
# 注入數據
|
||||||
script_output = runtime.run(Parser().parse(script), {
|
script_output = runtime.run(ast, {})
|
||||||
"audio": audio_data
|
print("script:",script_output)
|
||||||
})
|
|
||||||
f = open("./test_data/runtime_output.mp3", "wb")
|
|
||||||
f.write(script_output.read())
|
|
||||||
f.close()
|
|
||||||
Reference in New Issue
Block a user