compilation_manager.alc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. include "primitives.alh"
  2. include "constructors.alh"
  3. include "conformance_scd.alh"
  4. include "modelling.alh"
  5. include "metamodels.alh"
  6. include "library.alh"
  7. Element function compilation_manager():
  8. String operation
  9. String object_name
  10. Element root
  11. Element mv_root
  12. Element node
  13. mv_root = read_root()
  14. if (dict_in(mv_root["__hierarchy"], "models")):
  15. root = mv_root["__hierarchy"]["models"]
  16. else:
  17. root = create_node()
  18. dict_add(mv_root["__hierarchy"], "models", root)
  19. operation = input()
  20. if (operation == "upload"):
  21. object_name = input()
  22. node = instantiate_model(import_node("models/SimpleClassDiagrams"))
  23. dict_add(root, object_name, node)
  24. dict_add(node, "hash_md5", input())
  25. if (input()):
  26. dict_add(node, "initializers", construct_top())
  27. else:
  28. dict_add(node, "initializers", deserialize(input()))
  29. add_AL(node, node["initializers"], "")
  30. Element symbols
  31. symbols = create_node()
  32. dict_add(node, "symbols", symbols)
  33. while (input()):
  34. dict_add(symbols, input(), input())
  35. elif (operation == "remove_obj"):
  36. dict_delete(root, input())
  37. elif (operation == "read_initializers"):
  38. node = root[input()]["initializers"]
  39. output(node)
  40. elif (operation == "link_and_load"):
  41. Element objs
  42. String obj
  43. objs = create_node()
  44. obj = input()
  45. while (obj != ""):
  46. if (dict_in(root, obj)):
  47. set_add(objs, obj)
  48. else:
  49. log("ERROR: couldn't find obj " + obj)
  50. obj = input()
  51. link_and_load(root, input(), objs)
  52. elif (operation == "is_defined"):
  53. object_name = input()
  54. if (dict_in(root, object_name)):
  55. output(root[object_name]["hash_md5"])
  56. else:
  57. output(create_node())
  58. else:
  59. log("Failed to understand command")
  60. return operation
  61. Boolean function check_symbols(root : Element, main_function : String, objs : Element):
  62. Element symbols
  63. String obj
  64. Element keys
  65. String key
  66. Element copy_objs
  67. // We always need a main variable
  68. symbols = create_node()
  69. dict_add(symbols, main_function, False)
  70. // Resolve all symbols
  71. copy_objs = set_copy(objs)
  72. while (0 < list_len(copy_objs)):
  73. obj = set_pop(copy_objs)
  74. keys = dict_keys(root[obj]["symbols"])
  75. while (0 < list_len(keys)):
  76. key = set_pop(keys)
  77. if (root[obj]["symbols"][key]):
  78. // Defines
  79. if (bool_not(dict_in(symbols, key))):
  80. // Not yet in dictionary
  81. dict_add(symbols, key, True)
  82. elif (symbols[key]):
  83. // Already in dictionary, and it was already defined
  84. log("ERROR: multiple definition of symbol " + key)
  85. return False
  86. else:
  87. // Already in dictionary, but only used
  88. dict_delete(symbols, key)
  89. dict_add(symbols, key, True)
  90. else:
  91. // Uses
  92. if (bool_not(dict_in(symbols, key))):
  93. dict_add(symbols, key, False)
  94. // Check whether we have everything
  95. keys = dict_keys(symbols)
  96. while (0 < list_len(keys)):
  97. key = set_pop(keys)
  98. if (bool_not(symbols[key])):
  99. if (bool_not(bool_or(key == "output", key == "input"))):
  100. log("ERROR: undefined symbol " + key)
  101. return False
  102. return True
  103. Void function link_and_load(root : Element, main_function : String, objs : Element):
  104. String obj
  105. Element func
  106. Element main_f
  107. if (check_symbols(root, main_function, objs)):
  108. // Symbols verified OK, start execution
  109. output(True)
  110. // Call all initializers in turn
  111. while (0 < list_len(objs)):
  112. obj = set_pop(objs)
  113. func = root[obj]["initializers"]
  114. exec(func)
  115. // Resolve the main function, which should now be in (global) scope, and execute it
  116. main_f = resolve(main_function)
  117. main_f()
  118. else:
  119. // Symbol verification failed, so don't execute
  120. output(False)
  121. return