to_python.alc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. include "primitives.alh"
  2. include "modelling.alh"
  3. include "object_operations.alh"
  4. String function get_indent(indentation : Integer):
  5. String code
  6. code = ""
  7. while (indentation > 0):
  8. code = code + " "
  9. indentation = indentation - 1
  10. return code!
  11. String function code_print(node : Element, indentation : Integer):
  12. String code
  13. String inst_type
  14. inst_type = cast_value(node)
  15. code = "(no_printer_for_" + inst_type + ")"
  16. if (inst_type == "if"):
  17. String cond_code
  18. String true_code
  19. String false_code
  20. cond_code = code_print(node["cond"], 0)
  21. true_code = code_print(node["then"], indentation + 1)
  22. if (dict_in(node, "else")):
  23. false_code = get_indent(indentation) + "else:\n"
  24. false_code = code_print(node["else"], indentation + 1)
  25. else:
  26. false_code = ""
  27. code = get_indent(indentation) + "if(" + cond_code + "):\n" + true_code + false_code
  28. elif (inst_type == "constant"):
  29. String value
  30. value = cast_value(node["node"])
  31. if (value == "true"):
  32. value = "True"
  33. elif (value == "false"):
  34. value = "False"
  35. code = get_indent(indentation) + value
  36. elif (inst_type == "return"):
  37. if (dict_in(node, "value")):
  38. code = get_indent(indentation) + "return " + code_print(node["value"], 0) + "\n"
  39. else:
  40. code = get_indent(indentation) + "return\n"
  41. elif (inst_type == "declare"):
  42. code = ""
  43. elif (inst_type == "resolve"):
  44. if (is_physical_string(node["var"])):
  45. code = cast_string(node["var"])
  46. else:
  47. code = "var_" + cast_id(node["var"])
  48. elif (inst_type == "assign"):
  49. code = get_indent(indentation) + code_print(node["var"], 0) + " = " + code_print(node["value"], 0) + "\n"
  50. elif (inst_type == "access"):
  51. code = code_print(node["var"], indentation)
  52. elif (inst_type == "while"):
  53. code = get_indent(indentation) + "while (" + code_print(node["cond"], 0) + "):\n" + code_print(node["body"], indentation + 1)
  54. elif (inst_type == "call"):
  55. String func_name
  56. String params
  57. params = ""
  58. func_name = code_print(node["func"], 0)
  59. Element param
  60. param = node["params"]
  61. Boolean continue
  62. continue = True
  63. while (continue):
  64. if (params == ""):
  65. params = code_print(param["value"], 0)
  66. else:
  67. params = params + ", " + code_print(param["value"], 0)
  68. if (bool_not(dict_in(param, "next_param"))):
  69. continue = False
  70. param = param["next_param"]
  71. code = func_name + "(" + params + ")"
  72. if (indentation > 0):
  73. code = get_indent(indentation) + code + "\n"
  74. if (dict_in(node, "next")):
  75. code = code + code_print(node["next"], indentation)
  76. return code!
  77. Boolean function main(model : Element):
  78. // Read out the main function
  79. log("Start up MAIN function")
  80. String al_node
  81. Element function_element
  82. String code
  83. Element funcdefs
  84. String function
  85. funcdefs = allInstances(model, "AL/funcdef")
  86. code = ""
  87. while (set_len(funcdefs) > 0):
  88. // Iterate over all functions
  89. function = set_pop(funcdefs)
  90. log("Starting at function " + function)
  91. log("Got keys: " + set_to_string(dict_keys(model["model"][function])))
  92. log("Parameters:")
  93. Element params
  94. String param
  95. params = dict_keys(model["model"][function]["params"])
  96. code = code + "def func_" + cast_id(model["model"][function]) + "("
  97. while (set_len(params) > 0):
  98. param = set_pop(params)
  99. log(" " + param + " --> " + cast_id(model["model"][function]["params"][param]))
  100. code = code + "var_" + cast_id(model["model"][function]["params"][param])
  101. if (set_len(params) > 0):
  102. code = code + ", "
  103. code = code + "):\n"
  104. function_element = model["model"][function]["body"]
  105. al_node = cast_value(function_element)
  106. while (cast_value(function_element) == "global"):
  107. log("Assigning to " + cast_value(function_element["next"]["var"]["var"]))
  108. log(" the element: " + cast_id(function_element["next"]["value"]["node"]))
  109. log(" keys: " + set_to_string(dict_keys(function_element["next"]["value"]["node"])))
  110. log("Function is element: " + cast_id(function_element["next"]["value"]["node"]))
  111. function_element = function_element["next"]["next"]
  112. log("Started execution at " + cast_value(function_element))
  113. log("Started REAL execution at " + cast_value(function_element))
  114. code = code + code_print(function_element, 1)
  115. log("Finding initial function...")
  116. function = set_pop(allInstances(model, "AL/Initial"))
  117. function = set_pop(allAssociationDestinations(model, function, "AL/initial_funcdef"))
  118. log("Initial function: " + cast_id(model["model"][function]))
  119. code = code + "main = func_" + cast_id(model["model"][function]) + "\n"
  120. // Found the main function, though we only have the name of the function to assign...
  121. // What we can do is reassign main to that function!
  122. log("Generated code:")
  123. log(code)
  124. log("Function block can now be invoked by calling the 'main' function!")
  125. return True!