Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for CONST #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/slimit/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions src/slimit/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
13 changes: 11 additions & 2 deletions src/slimit/visitors/ecmavisitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
13 changes: 11 additions & 2 deletions src/slimit/visitors/minvisitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,26 @@ 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))
if node.initializer is not None:
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

Expand Down
3 changes: 3 additions & 0 deletions src/slimit/visitors/scopevisitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down