def ParameterDeclaratorTest(): k = VariableDeclaration(specifiers.chaR, VariableDeclarator(Identifier('a', None, False))) l = VariableDeclaration(specifiers.chaR, VariableDeclarator(Identifier('b', None, False))) r = ParameterDeclarator([k, l]) return r
def ForLoopTest2(symtab): # i = 0 init = AssignmentExpression(Identifier('i', None, False), assignmentOperator.EQUAL, IntegerLiteral(0)) # i++ step = UnaryExpression(unaryOperator.POST_INCREMENT, Identifier('i', None, False)) # i < 100 condition = ConditionalExpression(Identifier('i', None, False), conditionalOperator.COMPARE_LE, IntegerLiteral(100)) # A body for the for loop body = CompoundStatement() # a variable declaration: inT y decl1 = DeclarationStatement( VariableDeclaration( specifiers.inT, VariableDeclarator(Identifier('y', None, False, None, False)))) body.addStatement(decl1) # identifiers a, b id1 = Identifier('a', None, False) id2 = Identifier('y', None, False) as1 = ExpressionStatement( AssignmentExpression(id2, assignmentOperator.EQUAL, Identifier('i', None, False))) as2 = ExpressionStatement( AssignmentExpression(copy.deepcopy(id1), assignmentOperator.EQUAL, id2)) body.addStatement(as1) body.addStatement(as2) forloop = ForLoop(init, condition, step, body) return forloop
def CompoundStatementTest(parent): id1 = Identifier('a', parent, False) id2 = Identifier('b', parent, False) binexp1 = BinaryExpression(id1, binaryOperator.ADD, id2) binexp2 = BinaryExpression(id1, binaryOperator.ADD, id2) binexp3 = BinaryExpression(id1, binaryOperator.ADD, id2) decl1 = DeclarationStatement( VariableDeclaration(specifiers.int8, VariableDeclarator(Identifier('y', parent, False)))) Y = decl1 expstmt1 = ExpressionStatement(binexp1) expstmt2 = ExpressionStatement(binexp2) expstmt3 = ExpressionStatement(binexp3) compstmt = CompoundStatement() compstmt.addStatement(decl1) compstmt.addStatementAfter(decl1, expstmt1) print(compstmt) k = CompoundStatement() decl1 = DeclarationStatement( VariableDeclaration( specifiers.int8, VariableDeclarator(Identifier('x', compstmt, False)))) k.addStatement(decl1) k.addStatement(expstmt2) compstmt.addStatement(k) l = CompoundStatement() decl1 = DeclarationStatement( VariableDeclaration( specifiers.int8, VariableDeclarator(Identifier('z', compstmt, False)))) l.addStatement(decl1) l.addStatement(expstmt2) k.addStatement(l) compstmt.addStatement(expstmt3) expstmt1.detach() print(('next compstmt:\n', compstmt)) print('symtabl:') print(('parent symtabl:', k.getParentTables())) print(('symbol look up:', k.findSymbol(Y.getDeclaration().getDeclaredSymbols()[0]))) return compstmt
def NestedDeclaratorTest(): from hir.Identifier import Identifier from hir.PointerSpecifier import PointerSpecifier from hir.ArraySpecifier import ArraySpecifier from hir.VariableDeclarator import VariableDeclarator from hir.ParameterDeclarator import ParameterDeclaratorTest from hir.VariableDeclaration import VariableDeclaration from hir.DeclarationStatement import DeclarationStatement ns = VariableDeclarator(Identifier('a', None, False), None, PointerSpecifier([CONST])) k = VariableDeclaration(DOUBLE, VariableDeclarator(Identifier('xyz', None, False))) ns = NestedDeclarator(ns, None, None, [k]) ns = VariableDeclarator(ns, ArraySpecifier([3])) ns = VariableDeclarator(ns, None, PointerSpecifier()) ns = DeclarationStatement(VariableDeclaration(INT, ns)) return ns
def StructDeclarationTest(): from hir.VariableDeclarator import VariableDeclarator from hir.VariableDeclaration import VariableDeclaration from hir.Keyword import specifiers k = VariableDeclaration(specifiers.int8, VariableDeclarator(Identifier('y', None, False))) s = DeclarationStatement(k) id1 = Identifier('a', None, False) struct_dec = StructDeclaration(id1) struct_dec.add_field(s) k = VariableDeclaration(specifiers.int8, VariableDeclarator(Identifier('x', None, False))) s = DeclarationStatement(k) struct_dec.add_field(s) print(struct_dec.getDeclaredSymbols()) # id2 = Identifier('b', None, False) # struct_dec1 = StructDeclaration(id2) # struct_dec1.add_field(struct_dec) return struct_dec
def ForLoopTest(parent=None): doTest = False if parent is not None: doTest = True # i = 0 init = AssignmentExpression(Identifier('i', parent, doTest), assignmentOperator.EQUAL, IntegerLiteral(0)) # i++ step = UnaryExpression(unaryOperator.POST_INCREMENT, Identifier('i', parent, False)) # i < 100 condition = ConditionalExpression(Identifier('i', parent, doTest), conditionalOperator.COMPARE_LE, IntegerLiteral(100)) # A body for the for loop body = CompoundStatement() # a variable declaration: inT y decl1 = DeclarationStatement( VariableDeclaration(specifiers.inT, VariableDeclarator(Identifier('y', body, doTest)))) body.addStatement(decl1) # identifiers a, b id1 = Identifier('a', parent, doTest) id2 = Identifier('y', parent, doTest) # assignment expression, a += b, a = b, a %= b args1 = id1, assignmentOperator.ADD, id2 args2 = copy.deepcopy(id1), assignmentOperator.EQUAL, copy.deepcopy(id2) args3 = copy.deepcopy(id1), assignmentOperator.MODULUS, copy.deepcopy(id2) args4 = copy.deepcopy(args3) assignments = [ AssignmentExpression(*k) for k in [args1, args2, args3, args4] ] r = list(map(ExpressionStatement, assignments)) # adding the statements to the body of the ForLoop list(map(body.addStatement, r)) body.addStatement(IfStatementTest()) forloop = ForLoop(init, condition, step, body) return forloop
def IfStatementTest(): import copy control = ConditionalExpression(Identifier('a', None, False), conditionalOperator.COMPARE_GT, IntegerLiteral(0)) assignexp1 = AssignmentExpression(Identifier('a', None, False), assignmentOperator.ADD, Identifier('b', None, False)) assignStmt1 = ExpressionStatement(assignexp1) decl1 = DeclarationStatement( VariableDeclaration(specifiers.inT, VariableDeclarator(Identifier('y', None, False)))) body = CompoundStatement() body.addStatement(decl1) copy_ifbody = copy.deepcopy(body) body.addStatement(assignStmt1) assignStmt2 = copy.deepcopy(assignStmt1) copy_ifbody.addStatement(assignStmt2) ifconstruct = IfStatement(control, body) control = ConditionalExpression(Identifier('a', None, False), conditionalOperator.COMPARE_LT, IntegerLiteral(0)) ifconstruct = IfStatement(control, copy_ifbody, ifconstruct) return ifconstruct
given Declaration: can be either VariableDeclaration or ProcedureDeclaration. Raises Exception if not either type""" Statement.__init__(self) self.setNumChildren(1) if not isinstance(exp, Declaration): raise NotADeclarationError('Type: (%s)' % (type(exp))) self.setChild(0, exp) exp.setParent(self) def getDeclaration(self): """Returns the declaration associated with this DeclarationStatement.""" return self.getChild(0) def __repr__(self): retval = repr(self.getDeclaration()) + ';' return retval __str__ = __repr__ if __name__ == '__main__': k = VariableDeclaration(specifiers.int8, VariableDeclarator(Identifier('y', None, False))) s = DeclarationStatement(k) print((repr(s))) from hir.StructDeclaration import StructDeclaration, StructDeclarationTest s = DeclarationStatement(StructDeclarationTest()) print(s) s = DeclarationStatement(StructDeclaration(Identifier('s', None, False))) print(s)