def __init__(self, tw=None): super().__init__() # declare instance attributes self.cond = None self.then_st = S_Empty() self.else_st = S_Empty() # if created without a TW if tw is None: return # keyword tw.consume(T_IF) # paren with condition paren = tw.consume_paren(ParenType.EXPR) self.cond = expressions.parse( paren.expression ) # the "then" branch self.then_st = tw.consume_statement() if tw.has(T_ELSE): # "else" branch tw.consume(T_ELSE) self.else_st = tw.consume_statement() else: # there is no "else" branch # add empty statement instead. self.else_st = S_Empty()
def __init__(self, tw=None): super().__init__() # declare instance attributes self.cond = None self.body_st = None # if created without a TW if tw is None: return # keyword tw.consume(T_DO) # the loop body self.body_st = tw.consume_statement() # keyword tw.consume(T_WHILE) # paren with condition paren = tw.consume_paren(ParenType.EXPR) self.cond = expressions.parse( paren.expression ) # end of the statement. tw.consume(T_Semicolon)
def __init__(self, tw=None): super().__init__() # declare instance attributes self.name = None self.args = None # if created without a TW if tw is None: return # keyword tw.consume(T_CALL) # func name self.name = tw.consume(T_Name).value # arguments paren paren = tw.consume_paren(ParenType.ARGVALS) # collect all argument names atw = StTokenWalker(paren.tokens) self.args = [] while atw.has_next(): val = atw.consume(T_Expression) expr = expressions.parse(val) self.args.append(expr) tw.consume(T_Semicolon)
def __init__(self, tw=None): super().__init__() # declare instance attributes self.var = None self.op = T_AssignOperator('=') self.value = None # if created without a TW if tw is None: return # keyword tw.consume(T_SET) # variable name name = tw.consume(T_Name).value index = None if tw.has(T_Bracket): br = tw.consume(T_Bracket) index = br.index self.var = E_Variable(name, index) # operator and value rv = tw.consume(T_Rvalue) self.op = rv.tokens[0] # convert T_Expression to Expression self.value = expressions.parse( rv.tokens[1] ) # end of statement tw.consume(T_Semicolon)
def __init__(self, parent, stmt): super(If, self).__init__(parent) # Create sub-scope self.get_ctx().sub_scope() self.condition = expressions.parse(self, stmt.children[0]) self.if_stmt = parse.parse(self, stmt.children[1]) self.else_stmt = parse.parse(self, stmt.children[2]) if len( stmt.children) == 3 else None
def __init__(self, parent, stmt): super(Assignment, self).__init__(parent) if stmt.children[0].children[0].type != 'identifier': raise Exception("Expression not yet supported on left side of assignment") if len(stmt.children[0].children) == 2: self.left = Definition(self, stmt.children[0], terminator='') else: self.left = expressions.parse(self, stmt.children[0].children[0]) # mark as type as to allow proper casts self.get_ctx().set('type', self.left.get_type()) self.right = expressions.parse(self, stmt.children[1]) print(self.left.identifier, self.left.get_type(), self.right.get_type()) if self.left.get_type() != self.right.get_type(): raise Exception("Assigning {assign} to variable of type {var}".format( var = self.left.get_type(), assign = self.right.get_type(), ))
def __init__(self, tw=None): super().__init__() # declare instance attributes self.value = None # if created without a TW if tw is None: return # keyword tw.consume(T_CASE) # the value expr = tw.consume(T_Expression) self.value = expressions.parse(expr) # a colon tw.consume(T_Colon)
def __init__(self, tw=None): super().__init__() # declare instance attributes self.value = None self.body_st = None # if created without a TW if tw is None: return # keyword tw.consume(T_SWITCH) # paren with condition paren = tw.consume_paren(ParenType.EXPR) self.value = expressions.parse( paren.expression ) # the switch body self.body_st = S_Block(tw)
def __init__(self, tw=None): super().__init__() # declare instance attributes self.var = None self.value = None # if created without a TW if tw is None: return # keyword tw.consume(T_VAR) # variable name name = tw.consume(T_Name).value self.var = E_Variable(name) # optional rvalue if tw.has(T_Rvalue): # operator and value rv = tw.consume(T_Rvalue) assignOp = rv.tokens[0] expr = rv.tokens[1] # cannot use other than simple = in declaration # the variable isn't defined yet. if assignOp.value != '=': raise Exception( 'Cannot use %s in variable declaration!' % assignOp.value ) self.value = expressions.parse(expr) else: # synthetic zero value self.value = None tw.consume(T_Semicolon)
def __init__(self, tw=None): super().__init__() # declare instance attributes self.value = None # if created without a TW if tw is None: return # keyword tw.consume(T_RETURN) if tw.has(T_Expression): # explicitly given return value expr = tw.consume(T_Expression) self.value = expressions.parse(expr) else: # a defualt return value self.value = E_Literal(T_Number('0')) tw.consume(T_Semicolon)
def __init__(self, tw=None): super().__init__() # declare instance attributes self.init = None self.cond = None self.iter = None self.body_st = None # if created without a TW if tw is None: return # keyword tw.consume(T_FOR) # paren with init, cond, iter paren = tw.consume_paren(ParenType.FOR) self.cond = expressions.parse( paren.for_cond ) # init statements itw = StTokenWalker(paren.for_init) self.init = [] while itw.has_next(): s = itw.consume_statement() self.init.append(s) # iter statements itw = StTokenWalker(paren.for_iter) self.iter = [] while itw.has_next(): s = itw.consume_statement() self.iter.append(s) # the loop body self.body_st = tw.consume_statement()
def set_clause(self, str_): self.expr = exp.parse(str_)
def __init__(self, parent, stmt): super(ExprStatement, self).__init__(parent) self.expr = expressions.parse(self, stmt)