Forráskód Böngészése

Add symbols to the bootstrap file

Yentl Van Tendeloo 8 éve
szülő
commit
8706a156c6

A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 5464 - 0
bootstrap/bootstrap.m


+ 16 - 3
bootstrap/bootstrap.py

@@ -223,7 +223,7 @@ def bootstrap():
                 f.write("Edge _new_user(root, user_root)\n")
                 f.write("Edge __new_user(_new_user, ___new_user)\n")
 
-                def compile_code_AL(code, target, prepend="", is_file=False, main=False):
+                def compile_code_AL(code, target, prepend="", is_file=False, main=False, symbols=None):
                     import sys
                     sys.path.append("interface/HUTN/")
                     from hutn_compiler.compiler import main as compile_code
@@ -234,7 +234,7 @@ def bootstrap():
                         filename = "bootstrap/bootstrap.al"
                     else:
                         filename = code
-                    code = compile_code(filename, "interface/HUTN/grammars/actionlanguage.g", "BS", ["--debug", "--prepend:%s" % prepend, "--main" if main else "--not-main"])
+                    code = compile_code(filename, "interface/HUTN/grammars/actionlanguage.g", "BS", ["--debug", "--prepend:%s" % prepend, "--main" if main else "--not-main"], symbols=symbols)
                     if not is_file:
                         os.remove("bootstrap/bootstrap.al")
                     return code.replace("auto_initial_IP", target)
@@ -250,8 +250,10 @@ def bootstrap():
                 for bootstrap_file in bootstrap_files:
                     # Compile the subfile
                     print("[COMP] %s" % bootstrap_file)
-                    result = compile_code_AL(bootstrap_file, "initial_IP", prepend=bootstrap_file, is_file=True)
+                    symbols = {}
+                    result = compile_code_AL(bootstrap_file, "initial_IP", prepend=bootstrap_file, is_file=True, symbols=symbols)
                     f.write(result, both=False)
+
                     # Now link the code with the compilation manager structure
                     f.write("Node elem()\n", both=False)
                     f.write('Node initializers("initializers")\n', both=False)
@@ -266,7 +268,18 @@ def bootstrap():
                     f.write('Node hash_value("%s")\n' % md5.hexdigest(), both=False)
                     f.write("Edge _(elem, hash_value)\n", both=False)
                     f.write("Edge _(_, hash)\n", both=False)
+
                     #TODO add the symbols as well!
+                    f.write('Node symbols("symbols")\n', both=False)
+                    f.write('Node __symbols()\n', both=False)
+                    f.write('Edge _(elem, __symbols)\n', both=False)
+                    f.write('Edge _(_, symbols)\n', both=False)
+
+                    for k, v in symbols.items():
+                        f.write('Node v(%s)\n' % v, both=False)
+                        f.write('Node k("%s")\n' % k, both=False)
+                        f.write('Edge _(__symbols, v)\n', both=False)
+                        f.write('Edge _(_, k)\n', both=False)
 
                 # Create code for initial user
                 print("[BOOT] initial_user")

+ 27 - 0
interface/HUTN/hutn_compiler/bootstrap_visitor.py

@@ -13,6 +13,8 @@ class BootstrapVisitor(PrimitivesVisitor):
         else:
             self.prepend_name = "auto"
 
+        self.object_symbols = {}
+
     def rename(self, name):
         if name == "initial_IP":
             return "%s_%s" % (self.prepend_name, name)
@@ -61,3 +63,28 @@ class BootstrapVisitor(PrimitivesVisitor):
                 target = self.rename(target)
                 output.append("Edge _%s(_%s, _%s)\n" % (name, source, target))
         return ''.join(output)
+
+    def visit_definition(self, tree):
+        for a in tree.get_children("ID"):
+            name = a.get_tail()[0]
+            self.object_symbols[name] = True
+            return PrimitivesVisitor.visit_definition(self, tree)
+
+    def visit_vardecl(self, tree):
+        if len(tree.get_tail()) > 2:
+            for a in tree.get_children("ID"):
+                name = a.get_tail()[0]
+                self.object_symbols.setdefault(name, False)
+                return PrimitivesVisitor.visit_vardecl(self, tree)
+        else:
+            return PrimitivesVisitor.visit_vardecl(self, tree)
+
+    def visit_funcdecl(self, tree):
+        for a in tree.get_children("func_name"):
+            for b in a.get_children("ID"):
+                name = b.get_tail()[0]
+                if tree.get_children("func_body") or tree.get_children("ASSIGN"):
+                    self.object_symbols[name] = True
+                else:
+                    self.object_symbols.setdefault(name, False)
+                return PrimitivesVisitor.visit_funcdecl(self, tree)

+ 6 - 2
interface/HUTN/hutn_compiler/compiler.py

@@ -134,7 +134,7 @@ def do_compile(inputfile, grammarfile, visitors=[], include_paths = []):
     if visitors:
         return visitors[-1].dump()
 
-def main(input_file, grammar_file, mode, args=[]):
+def main(input_file, grammar_file, mode, args=[], symbols=None):
     from prettyprint_visitor import PrettyPrintVisitor
     from prettyprint_visitor import PrintVisitor
     from semantics_visitor import SemanticsVisitor
@@ -159,7 +159,11 @@ def main(input_file, grammar_file, mode, args=[]):
         "M" : [ModelVisitor],
         "MO" : [ModelObjectVisitor],
     }
-    return do_compile(input_file, grammar_file, [v(args) for v in modes[mode]])
+    visitors = [v(args) for v in modes[mode]]
+    result = do_compile(input_file, grammar_file, visitors)
+    if symbols is not None and getattr(visitors[-1], "object_symbols", None) is not None:
+        symbols.update(visitors[-1].object_symbols)
+    return result
 
 if __name__ == "__main__":
     if len(sys.argv) <= 2: