compilation_manager.alc 3.1 KB

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