Commit b3b896b5 authored by dbarbera's avatar dbarbera
Browse files

Refactor expression nodes creation

parent 475a4c4e
...@@ -271,6 +271,14 @@ def warning(root, msg): ...@@ -271,6 +271,14 @@ def warning(root, msg):
return '{} - line {}'.format(msg, root.getLine()) return '{} - line {}'.format(msg, root.getLine())
def tmp():
''' Return a temporary variable name '''
global TMPVAR
varname = TMPVAR
TMPVAR += 1
return varname
def get_state_list(process_root): def get_state_list(process_root):
''' Return the list of states of a process ''' ''' Return the list of states of a process '''
# 1) get all STATE statements # 1) get all STATE statements
...@@ -905,31 +913,18 @@ def primary_variable(root, context): ...@@ -905,31 +913,18 @@ def primary_variable(root, context):
return prim, errors, warnings return prim, errors, warnings
def tmp(): def binary_expression(root, context):
''' Return a temporary variable name ''' ''' Binary expression analysys '''
global TMPVAR errors, warnings = [], []
varname = TMPVAR
TMPVAR += 1
return varname
def expr_ast(root): ExprNode = EXPR_NODE[root.type]
''' Create an AST node from a Parse Tree node ''' expr = ExprNode(
Node = EXPR_NODE[root.type]
node = Node(
get_input_string(root), get_input_string(root),
root.getLine(), root.getLine(),
root.getCharPositionInLine() root.getCharPositionInLine()
) )
node.exprType = UNKNOWN_TYPE expr.exprType = UNKNOWN_TYPE
node.tmpVar = tmp() expr.tmpVar = tmp()
return node
def binary_expression(root, context):
''' Binary expression analysys '''
expr, errors, warnings = expr_ast(root), [], []
left, right = root.children left, right = root.children
expr.left, err_left, warn_left = expression(left, context) expr.left, err_left, warn_left = expression(left, context)
...@@ -944,10 +939,16 @@ def binary_expression(root, context): ...@@ -944,10 +939,16 @@ def binary_expression(root, context):
def unary_expression(root, context): def unary_expression(root, context):
''' Unary expression analysys ''' ''' Unary expression analysys '''
expr = expr_ast(root) ExprNode = EXPR_NODE[root.type]
expr = ExprNode(
get_input_string(root),
root.getLine(),
root.getCharPositionInLine()
)
expr.exprType = UNKNOWN_TYPE
expr.tmpVar = tmp()
child = root.children[0] expr.expr, errors, warnings = expression(root.children[0], context)
expr.expr, errors, warnings = expression(child, context)
return expr, errors, warnings return expr, errors, warnings
...@@ -1178,7 +1179,15 @@ def neg_expression(root, context): ...@@ -1178,7 +1179,15 @@ def neg_expression(root, context):
def if_then_else_expression(root, context): def if_then_else_expression(root, context):
''' If Then Else expression analysis ''' ''' If Then Else expression analysis '''
expr, errors, warnings = expr_ast(root), [], [] errors, warnings = [], []
expr = ogAST.PrimIfThenElse(
get_input_string(root),
root.getLine(),
root.getCharPositionInLine()
)
expr.exprType = UNKNOWN_TYPE
expr.tmpVar = tmp()
if_part, then_part, else_part = root.getChildren() if_part, then_part, else_part = root.getChildren()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment