def vUnaryOp(self, node): if node.oper == '++': if node.right.resolveType().type == 'int': id = 'Increment' if node.pre: id = 'pre' + id else: id = 'post' + id if node.isNodeStatement(): id = 'p' + id func = ast.FunctionCall(id, ast.ActualParametersList(node.right)) func.symtab = node.symtab if node.isNodeStatement(): func.isStatement = True self.visit(func) else: binaryOp = ast.BinaryOp(node.right, '+', ast.IntLiteral(1)) binaryOp.symtab = node.symtab assignment = ast.AssignmentStatement(node.right, '=', binaryOp) assignment.symtab = node.symtab self.visit(assignment) elif node.oper == '--': if node.right.resolveType().isInt(): id = 'Decrement' if node.pre: id = 'pre' + id else: id = 'post' + id if node.isNodeStatement(): id = 'p' + id func = ast.FunctionCall(id, ast.ActualParametersList(node.right)) func.symtab = node.symtab if node.isNodeStatement(): func.isStatement = True self.visit(func) else: binaryOp = ast.BinaryOp(node.right, '-', ast.IntLiteral(1)) binaryOp.symtab = node.symtab assignment = ast.AssignmentStatement(node.right, '=', binaryOp) assignment.symtab = node.symtab self.visit(assignment) #binaryOp = ast.BinaryOp(node.right, '-', ast.IntLiteral(1)) #binaryOp.symtab = node.symtab #assignment = ast.AssignmentStatement(node.right, '=', binaryOp) #assignment.symtab = node.symtab #self.visit(assignment) else: self.w(self.t[node.oper]) self.visit(node.right)
def doCondition(self, node, cond): if not node.cond.resolveType().isBool(): newcond = ast.BinaryOp(node.cond, '!=', ast.IntLiteral('0')) newcond.symtab = node.symtab self.visit(newcond) else: self.visit(node.cond)
def vFunction(self, node): self.current_function_id = node.id self.p() proc = node.returnType.type == 'void' if proc: self.w('PROCEDURE') else: self.w('FUNCTION') self.w(node.id) self.visit(node.params) if not proc: self.w(':') self.visit(node.returnType) self.p(';') if node.id == 'main': # hack for main block = copy.deepcopy(node.block) ret = ast.ReturnStatement(ast.IntLiteral(0)) ret.symtab = node.symtab block.add(ret) self.visit(block) else: self.visit(node.block) self.p(';') self.p() self.current_function_id = None
def vCastExpression(self, node): tpe = node.expression.resolveType() if tpe.isInt() and node.type.isFloatingPoint(): self.w('castint2%s(' % node.type.type) self.visit(node.expression) self.w(')') elif node.type.resolveType().isInt() and tpe.isFloatingPoint(): self.w('cast%s2int(' % tpe.type) self.visit(node.expression) self.w(')') elif node.type.resolveType().isString(): self.visit(node.expression) elif node.type.resolveType().isBool() and tpe.isInt(): op = ast.BinaryOp(node.expression, '!=', ast.IntLiteral('0')) op.symtab = node.symtab op.right.symtab = node.symtab self.visit(op) else: self.visit(node.type.resolveType()) self.w('(') self.visit(node.expression) self.w(')')
def parse_expr(self, comments_allowed=True): self.skip_whitespace() char = '' print(char) name = char while True: char = self.source[self.loc] self.loc += 1 name += char print(name) if char == '#': if comments_allowed: print('Skipping comment') name = '' self.skip_line() else: return None if self.isalone(): name = name.strip() print name + ' is alone' if isint(name): return ast.IntLiteral(name) elif isfloat(name): return ast.FloatLiteral(name) elif name == 'actor': return self.parse_actor() elif name == 'class': return self.parse_class() elif name == 'var': return self.parse_var() elif name == 'val': return self.parse_val() elif isname(name): return self.parse_name(name) else: print('Parse Error: unrecognized token at location ' + str(self.loc) + ': \n' + self.source[self.loc:self.loc+20]) sys.exit(1)
def nud(self, token): if isinstance(token, tokens.IDENT): if isinstance(self.next, tokens.LPARAN): return self.parse_function_call() else: return ast.Identifier(token) if isinstance(token, tokens.LPARAN): e = self.expr() if not isinstance(self.next, tokens.RPARAN): raise ParserError(f"Expected `)` found {self.next}") self.step() return e if isinstance(token, tokens.INT): return ast.IntLiteral(token) if isinstance(token, tokens.FUNC): func = self.parse_function_literal() self.step_back() return func if isinstance(token, (tokens.TRUE, tokens.FALSE)): return ast.BoolLiteral(token) if isinstance(token, tokens.MINUS): return ast.PrefixOp(token, self.nud(self.read_char())) else: raise ParserError(f"No prefix parse function found for {token}")
def p_constant(p): '''constant : NUMBER''' if '.' in p[1]: p[0] = ast.FloatLiteral(p[1]).addloc(p.lineno(1)) else: p[0] = ast.IntLiteral(p[1]).addloc(p.lineno(1))
def p_literal_01(t): '''literal : INTEGER_LIT''' t[0] = ast.IntLiteral(t[1])
def visitIntLiteral(self, ctx: ZeamplParser.IntLiteralContext): value = int(ctx.getText()) return ast.IntLiteral(self.get_range(ctx), value)