AL_to_py.alc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. include "primitives.alh"
  2. include "modelling.alh"
  3. include "object_operations.alh"
  4. // def print_instruction(self, inst, indent=0):
  5. // intrinsics = {"integer_addition": (lambda x, y: "(%s + %s)" % (x, y)),
  6. // #"string_join": (lambda x, y: "(str(%s) + str(%s))" % (x, y)),
  7. // #"dict_read": (lambda x, y: "(%s[%s])" % (x, y)),
  8. // #"dict_overwrite": (lambda x, y, z: "(%s[%s] = %s)" % (x, y, z)),
  9. // #"dict_add_fast": (lambda x, y, z: "(%s[%s] = %s)" % (x, y, z)),
  10. // #"dict_add_fast": (lambda x, y, z: "(%s[%s] = %s)" % (x, y, z)),
  11. // }
  12. //
  13. // inst_type, = yield [("RV", [inst])]
  14. // instruction = "(no_printer_for_%s)" % inst_type["value"]
  15. //
  16. // if inst_type["value"] == "if":
  17. // cond, true, false = yield [("RD", [inst, "cond"]),
  18. // ("RD", [inst, "then"]),
  19. // ("RD", [inst, "else"])]
  20. // cond, = yield [("CALL_ARGS", [self.print_instruction, (cond, 0)])]
  21. // true, = yield [("CALL_ARGS", [self.print_instruction, (true, indent+1)])]
  22. // if false:
  23. // false, = yield [("CALL_ARGS", [self.print_instruction, (false, indent+1)])]
  24. // false = (" " * indent + "else:\n%s\n") % false
  25. // else:
  26. // false = ""
  27. //
  28. // instruction = " " * indent + "if (" + cond + "):\n" + true + "\n" + false
  29. // #TODO fix for inline calls
  30. // elif inst_type["value"] == "constant":
  31. // node, = yield [("RD", [inst, "node"])]
  32. // node, = yield [("RV", [node])]
  33. // if isinstance(node, str):
  34. // instruction = " " * indent + '"%s"' % node
  35. // else:
  36. // instruction = " " * indent + str(node)
  37. // elif inst_type["value"] == "return":
  38. // value, = yield [("RD", [inst, "value"])]
  39. // if value:
  40. // value, = yield [("CALL_ARGS", [self.print_instruction, (value, 0)])]
  41. // instruction = " " * indent + "return %s\n" % value
  42. // else:
  43. // instruction = " " * indent + "return\n"
  44. // #TODO fix for inline calls
  45. // elif inst_type["value"] == "declare":
  46. // instruction = ""
  47. // elif inst_type["value"] == "resolve":
  48. // value, = yield [("RD", [inst, "var"])]
  49. // str_value, = yield [("RV", [value])]
  50. // if str_value:
  51. // instruction = str_value
  52. // else:
  53. // instruction = "var_%s" % value
  54. // elif inst_type["value"] == "assign":
  55. // var, val = yield [("RD", [inst, "var"]),
  56. // ("RD", [inst, "value"])]
  57. // var, val = yield [("CALL_ARGS", [self.print_instruction, (var, 0)]),
  58. // ("CALL_ARGS", [self.print_instruction, (val, 0)])]
  59. // instruction = " " * indent + var + " = " + val + "\n"
  60. // #TODO fix for inline calls
  61. // elif inst_type["value"] == "call":
  62. // func_name, = yield [("RD", [inst, "func"])]
  63. // func_name, = yield [("CALL_ARGS", [self.print_instruction, (func_name, 0)])]
  64. // param_list = []
  65. //
  66. // param, = yield [("RD", [inst, "params"])]
  67. // while param:
  68. // value, = yield [("RD", [param, "value"])]
  69. // res, param = yield [("CALL_ARGS", [self.print_instruction, (value, 0)]),
  70. // ("RD", [param, "next_param"])]
  71. // param_list.append(res)
  72. //
  73. // if func_name in intrinsics:
  74. // instruction = " " * indent + intrinsics[func_name](*param_list)
  75. // else:
  76. // instruction = " " * indent + func_name + "(" + ", ".join(param_list) + ")"
  77. // """
  78. // if indent == 0:
  79. // #TODO fix for inline calls
  80. // instruction = 'result, = yield [("CALL_ARGS", (%s, (' % func_name + ",".join(param_list) + ',)))]'
  81. // else:
  82. // instruction = " " * indent + 'yield [("CALL_ARGS", (%s, (' % func_name + ",".join(param_list) + ',)))]'
  83. // """
  84. // if indent:
  85. // instruction += "\n"
  86. // elif inst_type["value"] == "access":
  87. // value, = yield [("RD", [inst, "var"])]
  88. // instruction, = yield [("CALL_ARGS", [self.print_instruction, (value, 0)])]
  89. // elif inst_type["value"] == "while":
  90. // cond, body = yield [("RD", [inst, "cond"]),
  91. // ("RD", [inst, "body"])]
  92. // cond, body = yield [("CALL_ARGS", [self.print_instruction, (cond, 0)]),
  93. // ("CALL_ARGS", [self.print_instruction, (body, indent+1)])]
  94. // instruction = " " * indent + "while (%s):\n" % cond + body
  95. // #TODO fix for inline calls
  96. //
  97. // next_inst, = yield [("RD", [inst, "next"])]
  98. // if next_inst:
  99. // next_inst, = yield [("CALL_ARGS", [self.print_instruction, (next_inst, indent)])]
  100. // else:
  101. // next_inst = ""
  102. //
  103. // raise primitive_functions.PrimitiveFinished(instruction + next_inst)
  104. Boolean function main(model : Element):
  105. // Read out the main function
  106. String initial_function
  107. String al_node
  108. initial_function = set_pop(allInstances(model, "AL/Initial"))
  109. initial_function = set_pop(allAssociationDestinations(model, initial_function))
  110. al_node = cast_value(initial_function)
  111. log("Read out initial node: " + initial_function)
  112. log("Value: " + al_node)
  113. // Ready, now branch out
  114. // TODO
  115. return True!