compilation_manager.alc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. include "primitives.alh"
  2. include "constructors.alh"
  3. Element function compilation_manager():
  4. String operation
  5. String object_name
  6. Element root
  7. Element mv_root
  8. Element node
  9. mv_root = read_root()
  10. if (dict_in(mv_root["__hierarchy"], "objects")):
  11. root = mv_root["__hierarchy"]["objects"]
  12. else:
  13. root = create_node()
  14. dict_add(mv_root["__hierarchy"], "objects", root)
  15. operation = input()
  16. if (operation == "upload"):
  17. object_name = input()
  18. node = create_node()
  19. dict_add(root, object_name, node)
  20. dict_add(node, "hash_md5", input())
  21. if (input()):
  22. dict_add(node, "initializers", construct_top())
  23. else:
  24. dict_add(node, "initializers", deserialize(input()))
  25. Element symbols
  26. symbols = create_node()
  27. dict_add(node, "symbols", symbols)
  28. while (input()):
  29. dict_add(symbols, input(), input())
  30. elif (operation == "remove_obj"):
  31. dict_delete(root, input())
  32. elif (operation == "read_initializers"):
  33. node = root[input()]["initializers"]
  34. output(node)
  35. elif (operation == "link_and_load"):
  36. Element objs
  37. String obj
  38. objs = create_node()
  39. obj = input()
  40. while (obj != ""):
  41. if (dict_in(root, obj)):
  42. set_add(objs, obj)
  43. else:
  44. log("ERROR: couldn't find obj " + obj)
  45. obj = input()
  46. link_and_load(root, objs)
  47. elif (operation == "is_defined"):
  48. object_name = input()
  49. if (dict_in(root, object_name)):
  50. output(root[object_name]["hash_md5"])
  51. else:
  52. output(create_node())
  53. else:
  54. log("Failed to understand command")
  55. return operation
  56. Boolean function check_symbols(root : Element, objs : Element):
  57. Element symbols
  58. String obj
  59. Element keys
  60. String key
  61. Element copy_objs
  62. // We always need a main variable
  63. symbols = create_node()
  64. dict_add(symbols, "main", False)
  65. // Resolve all symbols
  66. copy_objs = set_copy(objs)
  67. while (0 < list_len(copy_objs)):
  68. obj = set_pop(copy_objs)
  69. keys = dict_keys(root[obj]["symbols"])
  70. while (0 < list_len(keys)):
  71. key = set_pop(keys)
  72. if (root[obj]["symbols"][key]):
  73. // Defines
  74. if (bool_not(dict_in(symbols, key))):
  75. // Not yet in dictionary
  76. dict_add(symbols, key, True)
  77. elif (symbols[key]):
  78. // Already in dictionary, and it was already defined
  79. log("ERROR: multiple definition of symbol " + key)
  80. return False
  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. log("ERROR: undefined symbol " + key)
  96. return False
  97. return True
  98. Void function link_and_load(root : Element, objs : Element):
  99. String obj
  100. Element func
  101. if (check_symbols(root, objs)):
  102. output(True)
  103. while (0 < list_len(objs)):
  104. obj = set_pop(objs)
  105. func = root[obj]["initializers"]
  106. exec(func)
  107. main()
  108. else:
  109. output(False)
  110. return