add make_exprs.py script

This commit is contained in:
Luna 2019-05-31 23:37:45 -03:00
parent 756f85d77d
commit 3b73978f40
1 changed files with 49 additions and 0 deletions

49
scripts/make_exprs.py Executable file
View File

@ -0,0 +1,49 @@
#!/usr/bin/python3.6
from pathlib import Path
EXPR_TYPES = {
'Binary': ('left: Expr', 'operator: Token', 'right: Expr'),
'Grouping': ('expression: Expr'),
'Unary': ('operator: Token', 'right: Expr'),
# NOTE: when adding new Literals, add new Literal types, instead of just
# doing Literal with an 'Object value'. it won't work.
}
def _gen_expr_decls():
res = []
for expr_type, expr_params in EXPR_TYPES.items():
res.append(f'pub const {expr_type} = struct {{')
for param in expr_params:
res.append(f'{param},')
return '\n'.join(res)
def do_base_union(union_name: str):
res = [
'const Token = @import("token.zig").Token;\n'
]
res.extend(_gen_expr_decls())
res.append(f'pub const {union_name} = union {{')
for expr_type in EXPR_TYPES:
res.append(f'{expr_type}: {expr_type},')
res.append('}\n')
return '\n'.join(res)
def main():
expr_file = Path('./src') / 'expr.zig'
expr_file.unlink()
expr_file.write_text(do_base_union('Expr'))
if __name__ == '__main__':
main()