compilation_manager.alc 3.3 KB

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