-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·176 lines (156 loc) · 4.32 KB
/
main.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from lexer import ChLexer
from cparser import ChParser
from compiler import Compiler
from exceptions import *
from modules import (
Module,
PutCharMod,
FunctionCallMod,
ResolutionMod,
VariableAssignMod,
FunctionDecMod,
VariableReAssignMod,
ConditionalMod,
ForLoopCompileTimeMod,
EmbedMod,
VariableReAssignAtIndexMod,
ClassDeclarationMod,
WhileMod,
AdvancedWriteMod,
ValueAtPointerMod,
IncrementMod,
GotoMod,
ReturnMod
)
import pprint
pprint = pprint.PrettyPrinter(indent=2).pprint
### Rich definitions ###
std_print = print
from rich import print
from rich.console import Console
console = Console()
from rich.progress import Progress
from rich.traceback import install
from rich.syntax import Syntax
install()
########################
### Args proc ###
from sys import argv
flags = ''
if len(argv) < 1:
raise Exception("Usage: chonky <input>")
if len(argv) >= 4:
flags = argv[3]
#################
### Read file ###
text = open(argv[1],"r").read()
#################
### Construct tree ###
lexer = ChLexer()
parser = ChParser(argv[1])
######################
### Tasks ###
# Works only on functions that support tasks
def construct_task(
progress,
task,
target,
*args,
tasklen = 100
):
def _run_task():
return target(
*args,
task = task,
progress = progress,
tasklen = tasklen
)
return _run_task
with Progress(
refresh_per_second = 100
) as total_progress:
# Lex
lex = total_progress.add_task("Lex...", total=100)
lexed_tokens = lexer.tokenize(text)
# Increases time it takes, but makes it possible to track tree building
lexed_len = len(list(lexer.tokenize(text)))
total_progress.update(lex, advance=100)
# Build tree task
build_tree = total_progress.add_task("Building tree...", total=1000)
build_tree_task = construct_task(
total_progress,
build_tree,
parser.parse,
lexed_tokens,
tasklen = lexed_len
)
tree = build_tree_task()
# Force finish
total_progress.update(build_tree, advance=1000)
# Build root node
root_compiler_instance = Compiler(
tree, flags = flags, filename = argv[1]
)
root_compiler_instance.populate_modules_actions([
PutCharMod,
FunctionCallMod,
ResolutionMod,
VariableAssignMod,
FunctionDecMod,
VariableReAssignMod,
ConditionalMod,
ForLoopCompileTimeMod,
EmbedMod,
VariableReAssignAtIndexMod,
ClassDeclarationMod,
WhileMod,
AdvancedWriteMod,
ValueAtPointerMod,
IncrementMod,
GotoMod,
ReturnMod
])
print(f"\n[bold green]Tree complete, compiling...[/bold green]")
failed = False
try:
root_compiler_instance.run()
except TranspilerExceptions.UnknownActionReference as e:
root_compiler_instance.guide()
console.print_exception()
print(f"[bold]Possible actions:[/bold]\n{root_compiler_instance.actions}")
failed = True
if failed:
print(f"\n[bold red]Assembly build failed :cross_mark: [/bold red]")
quit(1)
else:
print(f"\n[bold green]Assembly build complete :heavy_check_mark: [/bold green]")
final_asm = '\n'.join(root_compiler_instance.finished)
final_flags_asm = '\n'.join(root_compiler_instance.flags)
# final_asm_pretty = Syntax(
# final_asm,
# "Asm",
# padding=1,
# line_numbers=True
# )
# print(final_asm_pretty)
final_func_asm = '\n'.join(root_compiler_instance.finished_funcs)
# final_asm_pretty = Syntax(
# "$ FUNCTIONS" + final_func_asm,
# "Asm",
# padding=1,
# line_numbers=True
# )
# print(final_asm_pretty)
# Once we are done put everything together
base = open("base.asm","r").read().format(flags=final_flags_asm,code=final_asm, func_code=final_func_asm)
# Write
open('out.asm', 'w+').write(base)
#############
# # Assemble -- OUTDATED
# from asm import Loader
# l = Loader(
# 'out.asm',
# argv[2]
# )
# open(argv[2], 'wb+').write(l.Processed)
# print(f"\n[bold green]Binary build complete with {root_compiler_instance.warnings} warnings :heavy_check_mark: [/bold green]")