Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ############################################################
- ## Imports
- ############################################################
- from random import randint
- ############################################################
- ## Data Structures
- ############################################################
- # Prog =
- # Class int [Struct]
- #
- # Struct =
- # Code [Stmt]
- # If parent Cond [Struct]
- # Ifelse parent Cond [Struct] [Struct]
- # For parent int int [Struct]
- #
- # parent = (ref, pos)
- # pos = [int]
- #
- # Stmt =
- # Assignadd str Exp
- #
- # Cond =
- # Neg Cond
- # And Cond Cond
- # Or Cond Cond
- # Eq Exp Exp
- # Neq Exp Exp
- # Ge Exp Exp
- # Gt Exp Exp
- # Le Exp Exp
- # Lt Exp Exp
- #
- # Exp =
- # Const int
- # Ref str
- # Add Exp Exp
- # Sub Exp Exp
- # Mul Exp Exp
- #
- #
- # object = (kind, attributes)
- # kind = str
- # attributes = depends on object and kind
- ################################################################
- ## Constants
- ################################################################
- indentstr = "\t"
- variables = "abcd"
- maxstructs = 4
- # 4 gives 10 structs total on average.
- # 5 gives 20 structs total on average.
- # 6 gives 80 structs total on average.
- maxstmts = 4
- maxforloops = 3
- maxconds = 2
- maxexps = 3
- constmin = -100
- constmax = 100
- ################################################################
- ## Utility Functions
- ################################################################
- def randItem(l):
- return l[randint(0,len(l)-1)]
- def countForStructs((kind, attrs)):
- if kind not in "If Ifelse For":
- return 0
- else:
- parentref = attrs[0][0]
- if kind=="For":
- return 1+countForStructs(parentref)
- else:
- return countForStructs(parentref)
- ################################################################
- ## Generating
- ################################################################
- # Prog =
- # Class int [Struct]
- def genProg(n):
- structs = []
- prog = ("Class", (n, structs))
- for i in range(randint(1, maxstructs)):
- struct = genStruct((prog, [1,i]), 0)
- structs.append(struct)
- return prog
- # Struct =
- # Code [Stmt]
- # If parent Cond [Struct]
- # Ifelse parent Cond [Struct] [Struct]
- # For parent int int [Struct]
- def genStruct(parent, depth):
- if depth >= maxstructs:
- kind = "Code"
- else:
- kind = randItem("Code If Ifelse For".split())
- if kind=="Code":
- stmts = []
- struct = (kind, (stmts,))
- for i in range(randint(1, maxstmts)):
- stmt = genStmt()
- stmts.append(stmt)
- elif kind=="If":
- thenstructs = []
- cond = genCond(0)
- struct = (kind, (parent, cond, thenstructs))
- for i in range(randint(1, maxstructs-depth)):
- thenstruct = genStruct((struct, [2, i]), depth+1)
- thenstructs.append(thenstruct)
- elif kind=="Ifelse":
- thenstructs = []
- elsestructs = []
- cond = genCond(0)
- struct = (kind, (parent, cond, thenstructs, elsestructs))
- for i in range(randint(1, maxstructs-depth)):
- thenstruct = genStruct((struct, [2, i]), depth+1)
- thenstructs.append(thenstruct)
- for i in range(randint(1, maxstructs-depth)):
- elsestruct = genStruct((struct, [3, i]), depth+1)
- elsestructs.append(elsestruct)
- elif kind=="For":
- loopstructs = []
- loops = randint(1, maxforloops)
- i = countForStructs(parent[0])
- struct = (kind, (parent, i, loops, loopstructs))
- for i in range(randint(1, maxstructs-depth)):
- loopstruct = genStruct((struct, [3, i]), depth+1)
- loopstructs.append(loopstruct)
- else:
- assert False, "Generated invalid Struct: " + kind
- return struct
- # Stmt =
- # Assignadd str Exp
- def genStmt():
- kind = "Assignadd"
- var = randItem(variables)
- exp = genExp(0)
- return (kind, (var, exp))
- # Cond =
- # Neg Cond
- # And Cond Cond
- # Or Cond Cond
- # Eq Exp Exp
- # Neq Exp Exp
- # Ge Exp Exp
- # Gt Exp Exp
- # Le Exp Exp
- # Lt Exp Exp
- def genCond(depth):
- if depth >= maxconds:
- kind = randItem("Eq Neq Ge Gt Le Lt".split())
- else:
- kind = randItem("Neg And Or Eq Neq Ge Gt Le Lt".split())
- if kind=="Neg":
- innercond = genCond(depth+1)
- return (kind, (innercond,))
- elif kind in "And Or".split():
- innercond1 = genCond(depth+1)
- innercond2 = genCond(depth+1)
- return (kind, (innercond1, innercond2))
- elif kind in "Eq Neq Ge Gt Le Lt":
- exp1 = genExp(0)
- exp2 = genExp(0)
- return (kind, (exp1, exp2))
- else:
- assert False, "Generated invalid Cond: " + kind
- # Exp =
- # Const int
- # Ref str
- # Add Exp Exp
- # Sub Exp Exp
- # Mul Exp Exp
- def genExp(depth):
- if depth >= maxexps:
- kind = randItem("Const Ref".split())
- else:
- kind = randItem("Const Ref Add Sub Mul".split())
- if kind=="Const":
- return (kind, (randint(constmin, constmax),))
- elif kind=="Ref":
- return (kind, (randItem(variables),))
- elif kind in "Add Sub Mul":
- exp1 = genExp(depth+1)
- exp2 = genExp(depth+1)
- return (kind, (exp1, exp2))
- else:
- assert False, "Generated invalid Exp: " + kind
- ################################################################
- ## Printing
- ################################################################
- # Prog =
- # Class int [Struct]
- def printProg((kind, attrs)):
- n, structs = attrs
- print "class TestClass%s implements TestInterface {" % n
- print indentstr+"public int testFunction(%s) {" % (", ".join(["int "+c for c in variables]))
- for struct in structs:
- printStruct(struct, 2)
- print indentstr*2 + "return %s;" % (" + ".join(variables))
- print indentstr + "}"
- print "}"
- # Struct =
- # Code [Stmt]
- # If parent Cond [Struct]
- # Ifelse parent Cond [Struct] [Struct]
- # For parent int [Struct]
- def printStruct((kind, attrs), indent):
- if kind=="Code":
- stmts = attrs[0]
- for stmt in stmts:
- printStmt(stmt, indent)
- elif kind=="If":
- parent, cond, thenprog = attrs
- print indentstr*indent + "if (%s) {" % strCond(cond)
- for struct in thenprog:
- printStruct(struct, indent+1)
- print indentstr*indent + "}"
- elif kind=="Ifelse":
- parent, cond, thenprog, elseprog = attrs
- print indentstr*indent + "if (%s) {" % strCond(cond)
- for struct in thenprog:
- printStruct(struct, indent+1)
- print indentstr*indent + "} else {"
- for struct in elseprog:
- printStruct(struct, indent+1)
- print indentstr*indent + "}"
- elif kind=="For":
- parent, i, n, prog = attrs
- print indentstr*indent + "for (int i%s=0;i%s<%s;i%s++) {" % (i, i, n, i)
- for struct in prog:
- printStruct(struct, indent+1)
- print indentstr*indent + "}"
- else:
- assert False, "Printing invalid Struct: " + kind
- # Stmt =
- # Assignadd str Exp
- def printStmt((kind, attrs), indent):
- if kind=="Assignadd":
- var, exp = attrs
- print indentstr*indent + "%s += %s;" % (var, strExp(exp))
- else:
- assert False, "Printing invalid Stmt: " + kind
- # Cond =
- # Neg Cond
- # And Cond Cond
- # Or Cond Cond
- # Eq Exp Exp
- # Neq Exp Exp
- # Ge Exp Exp
- # Gt Exp Exp
- # Le Exp Exp
- # Lt Exp Exp
- def strCond((kind, attrs)):
- if kind=="Neg":
- cond1 = attrs[0]
- return "!(%s)" % (strCond(cond1))
- elif kind=="And":
- cond1, cond2 = attrs
- return "(%s)&&(%s)" % (strCond(cond1), strCond(cond2))
- elif kind=="Or":
- cond1, cond2 = attrs
- return "(%s)||(%s)" % (strCond(cond1), strCond(cond2))
- elif kind=="Eq":
- exp1, exp2 = attrs
- return "(%s)==(%s)" % (strExp(exp1), strExp(exp2))
- elif kind=="Neq":
- exp1, exp2 = attrs
- return "(%s)!=(%s)" % (strExp(exp1), strExp(exp2))
- elif kind=="Ge":
- exp1, exp2 = attrs
- return "(%s)>=(%s)" % (strExp(exp1), strExp(exp2))
- elif kind=="Gt":
- exp1, exp2 = attrs
- return "(%s)>(%s)" % (strExp(exp1), strExp(exp2))
- elif kind=="Le":
- exp1, exp2 = attrs
- return "(%s)<=(%s)" % (strExp(exp1), strExp(exp2))
- elif kind=="Lt":
- exp1, exp2 = attrs
- return "(%s)<(%s)" % (strExp(exp1), strExp(exp2))
- else:
- assert False, "Printing invalid Cond: "+kind
- # Exp =
- # Const int
- # Ref str
- # Add Exp Exp
- # Sub Exp Exp
- # Mul Exp Exp
- def strExp((kind, attrs)):
- if kind == "Const":
- i = attrs[0]
- return str(i)
- elif kind == "Ref":
- var = attrs[0]
- return var
- elif kind == "Add":
- exp1, exp2 = attrs
- return "(%s)+(%s)" % (strExp(exp1), strExp(exp2))
- elif kind == "Sub":
- exp1, exp2 = attrs
- return "(%s)-(%s)" % (strExp(exp1), strExp(exp2))
- elif kind == "Mul":
- exp1, exp2 = attrs
- return "(%s)*(%s)" % (strExp(exp1), strExp(exp2))
- else:
- assert False, "Printing invalid Exp: "+kind
- ################################################################
- ## Entry Point
- ################################################################
- printProg(genProg(0))
Add Comment
Please, Sign In to add comment