-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
118 lines (93 loc) · 3.92 KB
/
cli.py
File metadata and controls
118 lines (93 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from typer import Typer, Argument
from visitors.string_rep_visitor import FormatVisitor
from visitors.semantic_check_visitor import SemanticCheckerVisitor
from visitors.high_level_ir_generator_visitor import TzScriptToHighLevelIrVisitor
from utils import run_tzscript_ast_building_pipeline
from visitors.michelson_generator_visitor import MichelsonGenerator
from visitors.hl_string_repre import HLReprVisitor
from visitors.code_runner import CodeRunnerVisitor
import typer
app = Typer()
def process(script: str, num_steps=3):
with typer.progressbar(length=num_steps) as progress:
# Tokenize Script
print("\nBuilding TZScript AST", end="")
ast = run_tzscript_ast_building_pipeline(script)
failed = False
print("\nPerforming Semantic Check", end="")
semantic_checker = SemanticCheckerVisitor()
semantic_result = semantic_checker.visit(ast)
if len(semantic_result) > 0:
failed = True
print("\nSomething Went Wrong")
for err in semantic_result:
print(err)
else:
print("... OK")
if not failed:
progress.update(1)
print("\nGenerating Intermediate Representation", end="")
high_level_ir = TzScriptToHighLevelIrVisitor()
ir = high_level_ir.visit(ast)
print("...OK")
progress.update(1)
return ast, ir, progress
return None, None, None
@app.command()
def build(file: str = Argument("", help="tzscript file to be parsed"),
out_file: str = Argument(None, help='michelson file to be generated')):
""" generates the .tz michelson script from the tzscript file specified """
with open(file, "r", encoding='utf-8') as f:
script = f.read()
ast, ir, progress = process(script, 3)
print("\nGenerating Michelson Code", end="")
# TODO uncomment this when code generation is working OK
visit_generator = MichelsonGenerator()
visit_generator.visit(ir)
# michelson_result = michelson_geneator.result
michelson_result = visit_generator.code
if out_file is None:
out_file = file[:file.find(".tzs")]+".tz"
with open(out_file, "w") as f:
f.write(michelson_result)
print(f"\nGenerated {out_file}")
progress.update(1)
@app.command()
def run(file: str = Argument("", help="tzscript file to be parsed")):
""" Runs AST generated by the Parser, calling the last entry defined (this behavior is restricted to rhis option) """
with open(file, "r", encoding='utf-8') as f:
script = f.read()
ast, ir, progress = process(script, 2)
if ast is not None:
print("\nRunning Code\n", end="")
runner = CodeRunnerVisitor()
runner.visit(ast)
@app.command()
def represent(file: str = Argument("", help="tzscript file to be parsed"),
out_file: str = Argument(None, help='michelson file to be generated')):
""" generates the .tzs.repr the string representation of the code in the input """
# with typer.progressbar(length=total) as progress:
with open(file, "r") as f:
script = f.read()
ast, ir, progress = process(script, 4)
if ast is not None:
print("\nGenerating String representation Code for base AST")
string_repr = FormatVisitor()
result = str(string_repr.visit(ast))
if out_file is None:
out_file = file[:file.find(".tzs")]+".tzs.rep"
with open(out_file, "w") as f:
f.write(result)
progress.update(1)
print("\nGenerating String representation Code for intermediate AST")
hl_repr = HLReprVisitor()
result = hl_repr.visit(ir)
if out_file is None:
out_file = file[:file.find(".tzs")]+".tzs.rep"
with open(out_file, "a+") as f:
f.write("\n\n")
f.write(str(result))
progress.update(1)
print(f"\nGenerated {out_file}")
if __name__ == "__main__":
app()