Guest User

Untitled

a guest
Jan 12th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.45 KB | None | 0 0
  1. ############################################################
  2. ## Imports
  3. ############################################################
  4.  
  5. from random import randint
  6.  
  7.  
  8.  
  9.  
  10. ############################################################
  11. ## Data Structures
  12. ############################################################
  13. # Prog =
  14. #   Class int [Struct]
  15. #
  16. # Struct =
  17. #   Code [Stmt]
  18. #   If parent Cond [Struct]
  19. #   Ifelse parent Cond [Struct] [Struct]
  20. #   For parent int int [Struct]
  21. #
  22. # parent = (ref, pos)
  23. # pos = [int]
  24. #
  25. # Stmt =
  26. #   Assignadd str Exp
  27. #
  28. # Cond =
  29. #   Neg Cond
  30. #   And Cond Cond
  31. #   Or Cond Cond
  32. #   Eq Exp Exp
  33. #   Neq Exp Exp
  34. #   Ge Exp Exp
  35. #   Gt Exp Exp
  36. #   Le Exp Exp
  37. #   Lt Exp Exp
  38. #
  39. # Exp =
  40. #   Const int
  41. #   Ref str
  42. #   Add Exp Exp
  43. #   Sub Exp Exp
  44. #   Mul Exp Exp
  45. #
  46. #
  47. # object = (kind, attributes)
  48. # kind = str
  49. # attributes = depends on object and kind
  50.  
  51.  
  52.  
  53.  
  54. ################################################################
  55. ## Constants
  56. ################################################################
  57.  
  58. indentstr = "\t"
  59. variables = "abcd"
  60. maxstructs = 4
  61. # 4 gives 10 structs total on average.
  62. # 5 gives 20 structs total on average.
  63. # 6 gives 80 structs total on average.
  64. maxstmts = 4
  65. maxforloops = 3
  66. maxconds = 2
  67. maxexps = 3
  68. constmin = -100
  69. constmax = 100
  70.  
  71.  
  72.  
  73.  
  74. ################################################################
  75. ## Utility Functions
  76. ################################################################
  77.  
  78. def randItem(l):
  79.   return l[randint(0,len(l)-1)]
  80.  
  81. def countForStructs((kind, attrs)):
  82.   if kind not in "If Ifelse For":
  83.     return 0
  84.   else:
  85.     parentref = attrs[0][0]
  86.     if kind=="For":
  87.       return 1+countForStructs(parentref)
  88.     else:
  89.       return countForStructs(parentref)
  90.  
  91.  
  92.  
  93.  
  94. ################################################################
  95. ## Generating
  96. ################################################################
  97.  
  98. # Prog =
  99. #   Class int [Struct]
  100. def genProg(n):
  101.   structs = []
  102.   prog = ("Class", (n, structs))
  103.   for i in range(randint(1, maxstructs)):
  104.     struct = genStruct((prog, [1,i]), 0)
  105.     structs.append(struct)
  106.   return prog
  107.  
  108. # Struct =
  109. #   Code [Stmt]
  110. #   If parent Cond [Struct]
  111. #   Ifelse parent Cond [Struct] [Struct]
  112. #   For parent int int [Struct]
  113. def genStruct(parent, depth):
  114.   if depth >= maxstructs:
  115.     kind = "Code"
  116.   else:
  117.     kind = randItem("Code If Ifelse For".split())
  118.  
  119.   if kind=="Code":
  120.     stmts = []
  121.     struct = (kind, (stmts,))
  122.     for i in range(randint(1, maxstmts)):
  123.       stmt = genStmt()
  124.       stmts.append(stmt)
  125.   elif kind=="If":
  126.     thenstructs = []
  127.     cond = genCond(0)
  128.     struct = (kind, (parent, cond, thenstructs))
  129.     for i in range(randint(1, maxstructs-depth)):
  130.       thenstruct = genStruct((struct, [2, i]), depth+1)
  131.       thenstructs.append(thenstruct)
  132.   elif kind=="Ifelse":
  133.     thenstructs = []
  134.     elsestructs = []
  135.     cond = genCond(0)
  136.     struct = (kind, (parent, cond, thenstructs, elsestructs))
  137.     for i in range(randint(1, maxstructs-depth)):
  138.       thenstruct = genStruct((struct, [2, i]), depth+1)
  139.       thenstructs.append(thenstruct)
  140.     for i in range(randint(1, maxstructs-depth)):
  141.       elsestruct = genStruct((struct, [3, i]), depth+1)
  142.       elsestructs.append(elsestruct)
  143.   elif kind=="For":
  144.     loopstructs = []
  145.     loops = randint(1, maxforloops)
  146.     i = countForStructs(parent[0])
  147.     struct = (kind, (parent, i, loops, loopstructs))
  148.     for i in range(randint(1, maxstructs-depth)):
  149.       loopstruct = genStruct((struct, [3, i]), depth+1)
  150.       loopstructs.append(loopstruct)
  151.   else:
  152.     assert False, "Generated invalid Struct: " + kind
  153.   return struct
  154.  
  155. # Stmt =
  156. #   Assignadd str Exp
  157. def genStmt():
  158.   kind = "Assignadd"
  159.   var = randItem(variables)
  160.   exp = genExp(0)
  161.   return (kind, (var, exp))
  162.  
  163. # Cond =
  164. #   Neg Cond
  165. #   And Cond Cond
  166. #   Or Cond Cond
  167. #   Eq Exp Exp
  168. #   Neq Exp Exp
  169. #   Ge Exp Exp
  170. #   Gt Exp Exp
  171. #   Le Exp Exp
  172. #   Lt Exp Exp
  173. def genCond(depth):
  174.   if depth >= maxconds:
  175.     kind = randItem("Eq Neq Ge Gt Le Lt".split())
  176.   else:
  177.     kind = randItem("Neg And Or Eq Neq Ge Gt Le Lt".split())
  178.  
  179.   if kind=="Neg":
  180.     innercond = genCond(depth+1)
  181.     return (kind, (innercond,))
  182.   elif kind in "And Or".split():
  183.     innercond1 = genCond(depth+1)
  184.     innercond2 = genCond(depth+1)
  185.     return (kind, (innercond1, innercond2))
  186.   elif kind in "Eq Neq Ge Gt Le Lt":
  187.     exp1 = genExp(0)
  188.     exp2 = genExp(0)
  189.     return (kind, (exp1, exp2))
  190.   else:
  191.     assert False, "Generated invalid Cond: " + kind
  192.  
  193. # Exp =
  194. #   Const int
  195. #   Ref str
  196. #   Add Exp Exp
  197. #   Sub Exp Exp
  198. #   Mul Exp Exp
  199. def genExp(depth):
  200.   if depth >= maxexps:
  201.     kind = randItem("Const Ref".split())
  202.   else:
  203.     kind = randItem("Const Ref Add Sub Mul".split())
  204.   if kind=="Const":
  205.     return (kind, (randint(constmin, constmax),))
  206.   elif kind=="Ref":
  207.     return (kind, (randItem(variables),))
  208.   elif kind in "Add Sub Mul":
  209.     exp1 = genExp(depth+1)
  210.     exp2 = genExp(depth+1)
  211.     return (kind, (exp1, exp2))
  212.   else:
  213.     assert False, "Generated invalid Exp: " + kind
  214.  
  215.  
  216.  
  217. ################################################################
  218. ## Printing
  219. ################################################################
  220.  
  221. # Prog =
  222. #   Class int [Struct]
  223. def printProg((kind, attrs)):
  224.   n, structs = attrs
  225.   print "class TestClass%s implements TestInterface {" % n
  226.   print indentstr+"public int testFunction(%s) {" % (", ".join(["int "+c for c in variables]))
  227.   for struct in structs:
  228.     printStruct(struct, 2)
  229.   print indentstr*2 + "return %s;" % (" + ".join(variables))
  230.   print indentstr + "}"
  231.   print "}"
  232.  
  233. # Struct =
  234. #   Code [Stmt]
  235. #   If parent Cond [Struct]
  236. #   Ifelse parent Cond [Struct] [Struct]
  237. #   For parent int [Struct]
  238. def printStruct((kind, attrs), indent):
  239.   if kind=="Code":
  240.     stmts = attrs[0]
  241.     for stmt in stmts:
  242.       printStmt(stmt, indent)
  243.   elif kind=="If":
  244.     parent, cond, thenprog = attrs
  245.     print indentstr*indent + "if (%s) {" % strCond(cond)
  246.     for struct in thenprog:
  247.       printStruct(struct, indent+1)
  248.     print indentstr*indent + "}"
  249.   elif kind=="Ifelse":
  250.     parent, cond, thenprog, elseprog = attrs
  251.     print indentstr*indent + "if (%s) {" % strCond(cond)
  252.     for struct in thenprog:
  253.       printStruct(struct, indent+1)
  254.     print indentstr*indent + "} else {"
  255.     for struct in elseprog:
  256.       printStruct(struct, indent+1)
  257.     print indentstr*indent + "}"
  258.   elif kind=="For":
  259.     parent, i, n, prog = attrs
  260.     print indentstr*indent + "for (int i%s=0;i%s<%s;i%s++) {" % (i, i, n, i)
  261.     for struct in prog:
  262.       printStruct(struct, indent+1)
  263.     print indentstr*indent + "}"
  264.   else:
  265.     assert False, "Printing invalid Struct: " + kind
  266.  
  267. # Stmt =
  268. #   Assignadd str Exp
  269. def printStmt((kind, attrs), indent):
  270.   if kind=="Assignadd":
  271.     var, exp = attrs
  272.     print indentstr*indent + "%s += %s;" % (var, strExp(exp))
  273.   else:
  274.     assert False, "Printing invalid Stmt: " + kind
  275.  
  276. # Cond =
  277. #   Neg Cond
  278. #   And Cond Cond
  279. #   Or Cond Cond
  280. #   Eq Exp Exp
  281. #   Neq Exp Exp
  282. #   Ge Exp Exp
  283. #   Gt Exp Exp
  284. #   Le Exp Exp
  285. #   Lt Exp Exp
  286. def strCond((kind, attrs)):
  287.   if kind=="Neg":
  288.     cond1 = attrs[0]
  289.     return "!(%s)" % (strCond(cond1))
  290.   elif kind=="And":
  291.     cond1, cond2 = attrs
  292.     return "(%s)&&(%s)" % (strCond(cond1), strCond(cond2))
  293.   elif kind=="Or":
  294.     cond1, cond2 = attrs
  295.     return "(%s)||(%s)" % (strCond(cond1), strCond(cond2))
  296.   elif kind=="Eq":
  297.     exp1, exp2 = attrs
  298.     return "(%s)==(%s)" % (strExp(exp1), strExp(exp2))
  299.   elif kind=="Neq":
  300.     exp1, exp2 = attrs
  301.     return "(%s)!=(%s)" % (strExp(exp1), strExp(exp2))
  302.   elif kind=="Ge":
  303.     exp1, exp2 = attrs
  304.     return "(%s)>=(%s)" % (strExp(exp1), strExp(exp2))
  305.   elif kind=="Gt":
  306.     exp1, exp2 = attrs
  307.     return "(%s)>(%s)" % (strExp(exp1), strExp(exp2))
  308.   elif kind=="Le":
  309.     exp1, exp2 = attrs
  310.     return "(%s)<=(%s)" % (strExp(exp1), strExp(exp2))
  311.   elif kind=="Lt":
  312.     exp1, exp2 = attrs
  313.     return "(%s)<(%s)" % (strExp(exp1), strExp(exp2))
  314.   else:
  315.     assert False, "Printing invalid Cond: "+kind
  316.  
  317. # Exp =
  318. #   Const int
  319. #   Ref str
  320. #   Add Exp Exp
  321. #   Sub Exp Exp
  322. #   Mul Exp Exp
  323. def strExp((kind, attrs)):
  324.   if kind == "Const":
  325.     i = attrs[0]
  326.     return str(i)
  327.   elif kind == "Ref":
  328.     var = attrs[0]
  329.     return var
  330.   elif kind == "Add":
  331.     exp1, exp2 = attrs
  332.     return "(%s)+(%s)" % (strExp(exp1), strExp(exp2))
  333.   elif kind == "Sub":
  334.     exp1, exp2 = attrs
  335.     return "(%s)-(%s)" % (strExp(exp1), strExp(exp2))
  336.   elif kind == "Mul":
  337.     exp1, exp2 = attrs
  338.     return "(%s)*(%s)" % (strExp(exp1), strExp(exp2))
  339.   else:
  340.     assert False, "Printing invalid Exp: "+kind
  341.  
  342.  
  343.  
  344.  
  345. ################################################################
  346. ## Entry Point
  347. ################################################################
  348.  
  349. printProg(genProg(0))
Add Comment
Please, Sign In to add comment