compilation_manager.alc 3.2 KB

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