From 3b73978f40c5b198b1769ffc6c67547afe535ec0 Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 31 May 2019 23:37:45 -0300 Subject: [PATCH 1/4] add make_exprs.py script --- scripts/make_exprs.py | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 scripts/make_exprs.py diff --git a/scripts/make_exprs.py b/scripts/make_exprs.py new file mode 100755 index 0000000..97ed36f --- /dev/null +++ b/scripts/make_exprs.py @@ -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() From 8007df6853ce4832ce5e7dc20a4253efc7d49a6f Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 31 May 2019 23:38:21 -0300 Subject: [PATCH 2/4] add mypy_cache to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 3cef7be..63487f0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ zig-cache/ +*.mypy_cache* From 6ff75a09267f078127c03616c8da2909f791672b Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 31 May 2019 23:43:46 -0300 Subject: [PATCH 3/4] finish make_exprs.py script - add src/expr.zig --- scripts/make_exprs.py | 22 ++++++++++++++++------ src/expr.zig | 28 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 src/expr.zig diff --git a/scripts/make_exprs.py b/scripts/make_exprs.py index 97ed36f..df4020f 100755 --- a/scripts/make_exprs.py +++ b/scripts/make_exprs.py @@ -4,7 +4,7 @@ from pathlib import Path EXPR_TYPES = { 'Binary': ('left: Expr', 'operator: Token', 'right: Expr'), - 'Grouping': ('expression: Expr'), + 'Grouping': ('expression: Expr',), 'Unary': ('operator: Token', 'right: Expr'), # NOTE: when adding new Literals, add new Literal types, instead of just @@ -18,7 +18,9 @@ def _gen_expr_decls(): res.append(f'pub const {expr_type} = struct {{') for param in expr_params: - res.append(f'{param},') + res.append(f' {param},') + + res.append('};\n') return '\n'.join(res) @@ -28,19 +30,27 @@ def do_base_union(union_name: str): 'const Token = @import("token.zig").Token;\n' ] - res.extend(_gen_expr_decls()) - res.append(f'pub const {union_name} = union {{') + res.append(_gen_expr_decls()) + + res.append(f'pub const {union_name}Type = enum {{') + for expr_type in EXPR_TYPES: + res.append(f' {expr_type},') + res.append('};\n') + + + res.append(f'pub const {union_name} = union({union_name}Type) {{') for expr_type in EXPR_TYPES: - res.append(f'{expr_type}: {expr_type},') + res.append(f' {expr_type}: {expr_type},') - res.append('}\n') + res.append('};\n') return '\n'.join(res) def main(): expr_file = Path('./src') / 'expr.zig' + expr_file.touch() expr_file.unlink() expr_file.write_text(do_base_union('Expr')) diff --git a/src/expr.zig b/src/expr.zig new file mode 100644 index 0000000..e00e76f --- /dev/null +++ b/src/expr.zig @@ -0,0 +1,28 @@ +const Token = @import("token.zig").Token; + +pub const Binary = struct { + left: Expr, + operator: Token, + right: Expr, +}; + +pub const Grouping = struct { + expression: Expr, +}; + +pub const Unary = struct { + operator: Token, + right: Expr, +}; + +pub const ExprType = enum { + Binary, + Grouping, + Unary, +}; + +pub const Expr = union(ExprType) { + Binary: Binary, + Grouping: Grouping, + Unary: Unary, +}; From 727a25963896416ddb49adff324e46316e3386bc Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 1 Jun 2019 00:04:04 -0300 Subject: [PATCH 4/4] rm make_exprs.py, moving to the c part --- .gitignore | 1 - scripts/make_exprs.py | 59 ------------------------------------------- 2 files changed, 60 deletions(-) delete mode 100755 scripts/make_exprs.py diff --git a/.gitignore b/.gitignore index 63487f0..3cef7be 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ zig-cache/ -*.mypy_cache* diff --git a/scripts/make_exprs.py b/scripts/make_exprs.py deleted file mode 100755 index df4020f..0000000 --- a/scripts/make_exprs.py +++ /dev/null @@ -1,59 +0,0 @@ -#!/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},') - - res.append('};\n') - - return '\n'.join(res) - - -def do_base_union(union_name: str): - res = [ - 'const Token = @import("token.zig").Token;\n' - ] - - res.append(_gen_expr_decls()) - - res.append(f'pub const {union_name}Type = enum {{') - for expr_type in EXPR_TYPES: - res.append(f' {expr_type},') - res.append('};\n') - - - res.append(f'pub const {union_name} = union({union_name}Type) {{') - - 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.touch() - expr_file.unlink() - expr_file.write_text(do_base_union('Expr')) - - -if __name__ == '__main__': - main()