From 508cd1793e84ad5551c90ed534fcbfbbe881400c Mon Sep 17 00:00:00 2001 From: ignis Date: Sun, 7 Jul 2019 05:06:37 +0900 Subject: [PATCH] added syntax field array attribute --- code/code_gen/code_gen.py | 14 ++++++++++++-- code/code_gen/post.py | 18 +++++++++++++++--- code/code_gen/terms.input | 4 ++-- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/code/code_gen/code_gen.py b/code/code_gen/code_gen.py index 78ba446..37b7755 100644 --- a/code/code_gen/code_gen.py +++ b/code/code_gen/code_gen.py @@ -20,10 +20,15 @@ calc_grammar = """ ?start: statement* - ?statement: NAME "=" sum -> assign_var + ?statement: NAME [ "(" [attr_list] ")" ] "=" sum -> assign_var | avg "{" [NAME ("," NAME)*] "}" -> assign_avg_var | varlist + ?attr_list: attr_pair ("," attr_pair)* + + ?attr_pair: NAME "=" NAME + | NAME "=" INT + ?sum: product | sum "+" product -> add | sum "-" product -> sub @@ -63,6 +68,7 @@ calc_grammar = """ %import common.CNAME -> NAME %import common.NUMBER + %import common.INT %import common.WS COMMENT: /#.*/ @@ -71,6 +77,10 @@ calc_grammar = """ %ignore WS """ +def tok_to_int(tok): + "Convert the value of `tok` from string to int, while maintaining line number & column." + # tok.type == 'INT' + return Token.new_borrow_pos(tok.type, int(tok), tok) class VersionInfo(object): @@ -112,7 +122,7 @@ class VersionInfo(object): def test(terms_raw, report=False): - tree = Lark(calc_grammar, parser='lalr' ).parse(terms_raw) + tree = Lark(calc_grammar, parser='lalr', lexer_callbacks = {'INT': tok_to_int}).parse(terms_raw) ir1 = Stage1(tree) diff --git a/code/code_gen/post.py b/code/code_gen/post.py index 549e8f3..5217357 100644 --- a/code/code_gen/post.py +++ b/code/code_gen/post.py @@ -14,11 +14,22 @@ class CollectDefinitions(Visitor): self.derived[v.value] = PrimaryField(v.value, self.derived) def assign_var (self, tree): - lval, rval = tree.children + if len(tree.children) > 2: + lval, lattr, rval = tree.children + else: + lval, rval = tree.children + lattr = None + + attr_dict = {} + + if lattr is not None: + for t in lattr.children: + k, v = t.children + attr_dict[k.value] = v.value if lval.value in self.derived: raise StandardError("duplicate definition of " + lval) - self.derived[lval.value] = Field(lval.value, rval, self.derived) + self.derived[lval.value] = Field(lval.value, attr_dict, rval, self.derived) def assign_avg_var (self, tree): w = tree.children[0] @@ -205,8 +216,9 @@ class FieldBase (object): class Field (FieldBase): - def __init__ (self, name, exp, fdict): + def __init__ (self, name, attr, exp, fdict): super(Field,self).__init__(name, fdict) + self.attr = attr self.exp = exp self.fluc, self.dep, self.derivs = ExpInspector.inspect(exp) self.comment = ExpToCode(self.fdict).transform(self.exp) diff --git a/code/code_gen/terms.input b/code/code_gen/terms.input index 48dffff..a92b61d 100644 --- a/code/code_gen/terms.input +++ b/code/code_gen/terms.input @@ -4,12 +4,12 @@ c = 1.0 - y -fsd = sqrt (sqr(ddx(c)) + sqr(ddy(c)) + sqr(ddz(c))) +fsd () = sqrt (sqr(ddx(c)) + sqr(ddy(c)) + sqr(ddz(c))) nx = - ddx(c) / fsd ny = - ddy(c) / fsd nz = - ddz(c) / fsd -divn = ddx(nx) + ddy(ny) + ddz(nz) +divn (a=1, b=kill, xlow=128, xupp=320) = ddx(nx) + ddy(ny) + ddz(nz) avg { c, fsd, divn }