From d61bbaeef041d665b62bebb5a4f8a4e7fa141ed2 Mon Sep 17 00:00:00 2001 From: ignis Date: Sun, 7 Jul 2019 11:43:19 +0900 Subject: [PATCH] Boolean type field attribute syntax --- code/code_gen/code_gen.py | 20 ++++++++++++++++++-- code/code_gen/terms.input | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/code/code_gen/code_gen.py b/code/code_gen/code_gen.py index 37b7755..2b10cf4 100644 --- a/code/code_gen/code_gen.py +++ b/code/code_gen/code_gen.py @@ -26,7 +26,7 @@ calc_grammar = """ ?attr_list: attr_pair ("," attr_pair)* - ?attr_pair: NAME "=" NAME + ?attr_pair: NAME "=" BOOL | NAME "=" INT ?sum: product @@ -71,12 +71,19 @@ calc_grammar = """ %import common.INT %import common.WS + BOOL: "true" | "false" + COMMENT: /#.*/ %ignore COMMENT %ignore WS """ +def tok_to_bool(tok): + "Convert the value of `tok` from string to bool, while maintaining line number & column." + # tok.type == 'BOOL' + return Token.new_borrow_pos(tok.type, tok.value == "true", tok) + def tok_to_int(tok): "Convert the value of `tok` from string to int, while maintaining line number & column." # tok.type == 'INT' @@ -122,7 +129,16 @@ class VersionInfo(object): def test(terms_raw, report=False): - tree = Lark(calc_grammar, parser='lalr', lexer_callbacks = {'INT': tok_to_int}).parse(terms_raw) + parser = Lark(calc_grammar, + parser='lalr', + lexer_callbacks = + { + 'INT': tok_to_int, + 'BOOL': tok_to_bool + } + ) + + tree = parser.parse(terms_raw) ir1 = Stage1(tree) diff --git a/code/code_gen/terms.input b/code/code_gen/terms.input index a92b61d..4daf6ee 100644 --- a/code/code_gen/terms.input +++ b/code/code_gen/terms.input @@ -10,6 +10,6 @@ nx = - ddx(c) / fsd ny = - ddy(c) / fsd nz = - ddz(c) / fsd -divn (a=1, b=kill, xlow=128, xupp=320) = ddx(nx) + ddy(ny) + ddz(nz) +divn (export=true, x0=128, x1=320) = ddx(nx) + ddy(ny) + ddz(nz) avg { c, fsd, divn }