mirror of
https://github.com/BoardWare-Genius/jarvis-models.git
synced 2025-12-13 16:53:24 +00:00
feat: runtime boolean
This commit is contained in:
@ -1,5 +1,5 @@
|
|||||||
# Runtime
|
# Runtime
|
||||||
一台VM運行時,用於隔離租戶環境與服務器環境,提供腿本用於調用模型流。
|
虛擬運行時,用於隔離租戶環境與服務器環境,提供腿本用於調用模型,組成工作流。
|
||||||
# 語法
|
# 語法
|
||||||
```
|
```
|
||||||
// 使用//註解
|
// 使用//註解
|
||||||
|
|||||||
@ -93,7 +93,7 @@ class Parser:
|
|||||||
return self.current_token["type"]
|
return self.current_token["type"]
|
||||||
|
|
||||||
def _is_literal(self):
|
def _is_literal(self):
|
||||||
return self.current_token["type"] in ["NUMBER", "STRING", "FLOAT"]
|
return self.current_token["type"] in ["NUMBER", "STRING", "FLOAT", "true", "false"]
|
||||||
|
|
||||||
# variable
|
# variable
|
||||||
def variable_statement(self):
|
def variable_statement(self):
|
||||||
@ -126,6 +126,8 @@ class Parser:
|
|||||||
|
|
||||||
def literal(self):
|
def literal(self):
|
||||||
token_type = self.current_token["type"]
|
token_type = self.current_token["type"]
|
||||||
|
if token_type == "true" or token_type == "false":
|
||||||
|
return self.boolean_literal()
|
||||||
if token_type == "NUMBER":
|
if token_type == "NUMBER":
|
||||||
return self.numberic_literal()
|
return self.numberic_literal()
|
||||||
if token_type == "STRING":
|
if token_type == "STRING":
|
||||||
@ -134,6 +136,18 @@ class Parser:
|
|||||||
return self.float_literal()
|
return self.float_literal()
|
||||||
raise Exception("Unexpected token: " + token_type)
|
raise Exception("Unexpected token: " + token_type)
|
||||||
|
|
||||||
|
def boolean_literal(self):
|
||||||
|
if self.token_type() == "true":
|
||||||
|
self.eat('true')
|
||||||
|
value = "True"
|
||||||
|
else:
|
||||||
|
self.eat("false")
|
||||||
|
value = "False"
|
||||||
|
return {
|
||||||
|
"type": 'BooleanLiteral',
|
||||||
|
"value": value,
|
||||||
|
}
|
||||||
|
|
||||||
def numberic_literal(self):
|
def numberic_literal(self):
|
||||||
token = self.eat('NUMBER')
|
token = self.eat('NUMBER')
|
||||||
return {
|
return {
|
||||||
|
|||||||
@ -69,12 +69,14 @@ class Runtime:
|
|||||||
return int(ast.get("value"))
|
return int(ast.get("value"))
|
||||||
elif ast.get("type") == "FloatLiteral":
|
elif ast.get("type") == "FloatLiteral":
|
||||||
return float(ast.get("value"))
|
return float(ast.get("value"))
|
||||||
|
elif ast.get("type") == "BooleanLiteral":
|
||||||
|
return bool(ast.get("value"))
|
||||||
|
|
||||||
def _is_identifier(self, ast):
|
def _is_identifier(self, ast):
|
||||||
return ast["type"] == "Identifier"
|
return ast["type"] == "Identifier"
|
||||||
|
|
||||||
def _is_literal(self, ast):
|
def _is_literal(self, ast):
|
||||||
return ast["type"] in ["NumericLiteral", "StringLiteral", "FloatLiteral"]
|
return ast["type"] in ["NumericLiteral", "StringLiteral", "FloatLiteral", "BooleanLiteral"]
|
||||||
|
|
||||||
def exec_return(self, ast):
|
def exec_return(self, ast):
|
||||||
v = ast.get("value")
|
v = ast.get("value")
|
||||||
|
|||||||
@ -7,10 +7,25 @@ specs = (
|
|||||||
# Comments:
|
# Comments:
|
||||||
(re.compile(r"^//.*"), None),
|
(re.compile(r"^//.*"), None),
|
||||||
|
|
||||||
|
# Symbols:
|
||||||
|
(re.compile(r"^\("), "("),
|
||||||
|
(re.compile(r"^\)"), ")"),
|
||||||
|
(re.compile(r"^\,"), ","),
|
||||||
|
(re.compile(r"^\{"), "{"),
|
||||||
|
(re.compile(r"^\}"), "}"),
|
||||||
|
(re.compile(r"^;"), ";"),
|
||||||
|
|
||||||
# Keywords:
|
# Keywords:
|
||||||
(re.compile(r"^\blet\b"), "let"),
|
(re.compile(r"^\blet\b"), "let"),
|
||||||
(re.compile(r"^\breturn\b"), "return"),
|
(re.compile(r"^\breturn\b"), "return"),
|
||||||
(re.compile(r"^;"), ";"),
|
(re.compile(r"^\bif\b"), "if"),
|
||||||
|
(re.compile(r"^\belse\b"), "else"),
|
||||||
|
(re.compile(r"^\bwhile\b"), "while"),
|
||||||
|
(re.compile(r"^\bfor\b"), "for"),
|
||||||
|
(re.compile(r"^\def\b"), "def"),
|
||||||
|
(re.compile(r"^\btrue\b"), "true"),
|
||||||
|
(re.compile(r"^\bfalse\b"), "false"),
|
||||||
|
|
||||||
|
|
||||||
# Floats:
|
# Floats:
|
||||||
(re.compile(r"^[-+]?[0-9]+\.[0-9]+"), "FLOAT"),
|
(re.compile(r"^[-+]?[0-9]+\.[0-9]+"), "FLOAT"),
|
||||||
@ -27,13 +42,6 @@ specs = (
|
|||||||
# Double-quoted strings
|
# Double-quoted strings
|
||||||
(re.compile(r"^\"[^\"]*\""), "STRING"),
|
(re.compile(r"^\"[^\"]*\""), "STRING"),
|
||||||
|
|
||||||
# Symbols:
|
|
||||||
(re.compile(r"^\("), "("),
|
|
||||||
(re.compile(r"^\)"), ")"),
|
|
||||||
(re.compile(r"^\,"), ","),
|
|
||||||
(re.compile(r"^\{"), "{"),
|
|
||||||
(re.compile(r"^\}"), "}"),
|
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
class Tokenizer:
|
class Tokenizer:
|
||||||
|
|||||||
51
runtime_example.py
Normal file
51
runtime_example.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
from src.blackbox.audio_to_text import AudioToText
|
||||||
|
from src.blackbox.text_to_audio import TextToAudio
|
||||||
|
from runtime.ast.parser import Parser
|
||||||
|
from runtime.ast.runtime import Runtime
|
||||||
|
|
||||||
|
script = """
|
||||||
|
let hello = true
|
||||||
|
let text = audio_to_text(audio)
|
||||||
|
print(hello)
|
||||||
|
return tts(text)
|
||||||
|
"""
|
||||||
|
|
||||||
|
def version():
|
||||||
|
return "0.0.1"
|
||||||
|
|
||||||
|
def add(a,b):
|
||||||
|
return a+b
|
||||||
|
|
||||||
|
def div(a,b):
|
||||||
|
return a/b
|
||||||
|
|
||||||
|
def minus(a,b):
|
||||||
|
return a-b
|
||||||
|
|
||||||
|
def mul(a,b):
|
||||||
|
return a*b
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
f = open("./test_data/testone.wav", "rb")
|
||||||
|
audio_data = f.read()
|
||||||
|
f.close()
|
||||||
|
tts = TextToAudio()
|
||||||
|
audio_to_text = AudioToText()
|
||||||
|
# 注入函数
|
||||||
|
runtime = Runtime(records={
|
||||||
|
"add": add,
|
||||||
|
"div": div,
|
||||||
|
"minus": minus,
|
||||||
|
"mul": mul,
|
||||||
|
"audio_to_text": audio_to_text,
|
||||||
|
"tts": tts,
|
||||||
|
"version": version,
|
||||||
|
"print": print
|
||||||
|
})
|
||||||
|
# 注入數據
|
||||||
|
script_output = runtime.run(Parser().parse(script), {
|
||||||
|
"audio": audio_data
|
||||||
|
})
|
||||||
|
f = open("./test_data/runtime_output.mp3", "wb")
|
||||||
|
f.write(script_output.read())
|
||||||
|
f.close()
|
||||||
BIN
test_data/runtime_output.mp3
Normal file
BIN
test_data/runtime_output.mp3
Normal file
Binary file not shown.
Reference in New Issue
Block a user