From d34cca1c4acf0018b2366974e835e923cf4a8c58 Mon Sep 17 00:00:00 2001 From: fflexo Date: Wed, 25 Jan 2017 08:45:58 +0000 Subject: [PATCH] Added CONST token into grammar Added new AST types for CONST which inherit from VAR since transforms are generally applicable to both Fixed up visitors to still work with new types --- src/slimit/ast.py | 6 ++++++ src/slimit/parser.py | 28 ++++++++++++++++++++++++++++ src/slimit/visitors/ecmavisitor.py | 13 +++++++++++-- src/slimit/visitors/minvisitor.py | 13 +++++++++++-- src/slimit/visitors/scopevisitor.py | 3 +++ 5 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/slimit/ast.py b/src/slimit/ast.py index 7458967..8ec308b 100644 --- a/src/slimit/ast.py +++ b/src/slimit/ast.py @@ -178,6 +178,12 @@ def __init__(self, identifier, initializer=None): def children(self): return [self.identifier, self.initializer] +class ConstStatement(VarStatement): + pass + +class ConstDecl(VarDecl): + pass + class UnaryOp(Node): def __init__(self, op, value, postfix=False): self.op = op diff --git a/src/slimit/parser.py b/src/slimit/parser.py index d892166..691f0c8 100644 --- a/src/slimit/parser.py +++ b/src/slimit/parser.py @@ -155,6 +155,7 @@ def p_source_element(self, p): def p_statement(self, p): """statement : block | variable_statement + | const_statement | empty_statement | expr_statement | if_statement @@ -943,6 +944,33 @@ def p_variable_declaration_noin(self, p): else: p[0] = ast.VarDecl(p[1], p[2]) + # CONST + def p_const_statement(self, p): + """const_statement : CONST const_declaration_list SEMI + | CONST const_declaration_list auto_semi + """ + p[0] = ast.ConstStatement(p[2]) + + def p_const_declaration_list(self, p): + """ + const_declaration_list \ + : const_declaration + | const_declaration_list COMMA const_declaration + """ + if len(p) == 2: + p[0] = [p[1]] + else: + p[1].append(p[3]) + p[0] = p[1] + + def p_const_declaration(self, p): + """const_declaration : identifier initializer + """ + if len(p) == 2: + p[0] = ast.ConstDecl(p[1]) + else: + p[0] = ast.ConstDecl(p[1], p[2]) + def p_initializer(self, p): """initializer : EQ assignment_expr""" p[0] = p[2] diff --git a/src/slimit/visitors/ecmavisitor.py b/src/slimit/visitors/ecmavisitor.py index 1c98598..711feaf 100644 --- a/src/slimit/visitors/ecmavisitor.py +++ b/src/slimit/visitors/ecmavisitor.py @@ -54,10 +54,19 @@ def visit_Block(self, node): s += '\n' + self._make_indent() + '}' return s - def visit_VarStatement(self, node): - s = 'var %s;' % ', '.join(self.visit(child) for child in node) + def visit_VarStatement(self, node, name='var'): + s = '%s %s;' % (name,', '.join(self.visit(child) for child in node)) return s + def visit_ConstStatement(self, node): + return self.visit_VarStatement(node, 'const') + + def visit_ConstDecl(self, node): + output = [] + output.append(self.visit(node.identifier)) + output.append(' = %s' % self.visit(node.initializer)) + return ''.join(output) + def visit_VarDecl(self, node): output = [] output.append(self.visit(node.identifier)) diff --git a/src/slimit/visitors/minvisitor.py b/src/slimit/visitors/minvisitor.py index 1c6fd15..cda3110 100644 --- a/src/slimit/visitors/minvisitor.py +++ b/src/slimit/visitors/minvisitor.py @@ -58,10 +58,13 @@ def visit_Block(self, node): else: return '{%s}' % ''.join(children) - def visit_VarStatement(self, node): - s = 'var %s;' % ','.join(self.visit(child) for child in node) + def visit_VarStatement(self, node, name='var'): + s = '%s %s;' % (name,','.join(self.visit(child) for child in node)) return s + def visit_ConstStatement(self, node): + return self.visit_VarStatement(node, 'const') + def visit_VarDecl(self, node): output = [] output.append(self.visit(node.identifier)) @@ -69,6 +72,12 @@ def visit_VarDecl(self, node): output.append('=%s' % self.visit(node.initializer)) return ''.join(output) + def visit_ConstDecl(self, node): + output = [] + output.append(self.visit(node.identifier)) + output.append('=%s' % self.visit(node.initializer)) + return ''.join(output) + def visit_Identifier(self, node): return node.value diff --git a/src/slimit/visitors/scopevisitor.py b/src/slimit/visitors/scopevisitor.py index 10e55cb..d5fa2f2 100644 --- a/src/slimit/visitors/scopevisitor.py +++ b/src/slimit/visitors/scopevisitor.py @@ -59,6 +59,9 @@ def visit_VarDecl(self, node): ident.scope = self.current_scope self.visit(node.initializer) + def visit_ConstDecl(self, node): + return self.visit_VarDecl(node) + def visit_Identifier(self, node): node.scope = self.current_scope