Browse Source

Bootstrap file can now be compiled (though doesn't work yet...)

Yentl Van Tendeloo 8 years ago
parent
commit
c097c8bc1f

+ 4 - 5
bootstrap/bootstrap.py

@@ -200,12 +200,12 @@ def bootstrap():
                 f.write("Node __objects()\n", both=False)
                 f.write('Dict (__hierarchy, "objects", __objects)\n', both=False)
 
-                def compile_code_MO(filename, model_name)
+                def compile_code_MO(filename, model_name):
                     import sys
                     sys.path.append("interface/HUTN/")
                     from hutn_compiler.compiler import main as compile_code
-                    model_constructors = compile_code(filename, "interface/HUTN/grammars/modelling.g", "MB", ["--modelname:%s" % model_name])
-                    print("Got model construction code: " + str(model_constructors))
+                    model_code = compile_code(filename, "interface/HUTN/grammars/modelling.g", "MB", ["--modelname:%s" % model_name])
+                    return model_code + "\n"
 
                 # Compile all model definitions to ALC directly
                 total_alc = ""
@@ -216,9 +216,8 @@ def bootstrap():
                     # Compile the subfile
                     bootstrap_model = bootstrap_model.replace("\\", "/")
                     model_name = bootstrap_model.rsplit(".mvc", 1)[0].rsplit("/", 1)[1]
-                    print("[MVC] %s" % bootstrap_name)
+                    print("[MVC] %s" % model_name)
                     alc = compile_code_MO(bootstrap_model, model_name)
-                    print("ALC: " + str(alc))
                     total_alc += alc
                     binding_alc += "\tinitialize_%s()\n" % model_name
 

+ 0 - 2
bootstrap/modelling.alc

@@ -595,8 +595,6 @@ Void function construct_model():
 			instantiate_link(global_models[input()], input(), input(), input(), input())
 		elif (command == "model_define_attribute"):
 			model_define_attribute(global_models[input()], input(), input(), input(), input())
-		elif (command == "initialize_bottom"):
-			initialize_bottom(input())
 		elif (command == "export_node"):
 			String local_name
 			String location

+ 12 - 10
interface/HUTN/hutn_compiler/model_bootstrap_visitor.py

@@ -37,20 +37,22 @@ class ModelBootstrapVisitor(Visitor):
         url = tree.get_children("MV_URL")[0]
         target = tree.get_children("MODEL_ID")[0]
         #self.constructors.extend(["import_node", url.get_text(), target.get_text()])
-        self.code += "\timport_node(%s, %s)\n" % (url.get_text(), target.get_text())
+        self.code += '\tElement %s\n' % target.get_text()
+        self.code += '\t%s = import_node("%s")\n' % (target.get_text(), url.get_text())
 
     def visit_export(self, tree):
         url = tree.get_children("MV_URL")[0]
         target = tree.get_children("MODEL_ID")[0]
         #self.constructors.extend(["export_node", target.get_text(), url.get_text()])
-        self.code += "\texport_node(%s, %s)\n" % (target.get_text(), url.get_text())
+        self.code += '\texport_node(%s, "%s")\n' % (target.get_text(), url.get_text())
 
     def visit_model(self, tree):
         children = tree.get_children("MODEL_ID")
         model_type = children[0].get_text()
         model_name = children[-1].get_text()
         #self.constructors.extend(["instantiate_model", model_type, model_name])
-        self.code += "\tinstantiate_model(%s, %s)\n" % (model_type, model_name)
+        self.code += "\tElement %s\n" % model_name
+        self.code += '\t%s = instantiate_model("%s")\n' % (model_name, model_type)
         self.current_model = model_name
         self.names = set()
         for element in tree.get_children("model_element"):
@@ -78,10 +80,10 @@ class ModelBootstrapVisitor(Visitor):
                 raise Exception("Target of link %s unknown: %s" % (element_name, target_name))
 
             #self.constructors.extend(["instantiate_link", self.current_model, element_type, element_name, source_name, target_name])
-            self.code += "\tinstantiate_link(%s, %s, %s, %s, %s)\n" % (self.current_model, element_type, element_name, source_name, target_name)
+            self.code += '\tinstantiate_link(%s, "%s", "%s", "%s", "%s")\n' % (self.current_model, element_type, element_name, source_name, target_name)
         else:
             #self.constructors.extend(["instantiate_node", self.current_model, element_type, element_name])
-            self.code += "\tinstantiate_node(%s, %s, %s)\n" % (self.current_model, element_type, element_name)
+            self.code += '\tinstantiate_node(%s, "%s", "%s")\n' % (self.current_model, element_type, element_name)
 
         self.names.add(element_name)
         self.current_element.append(element_name)
@@ -103,7 +105,7 @@ class ModelBootstrapVisitor(Visitor):
                 raise Exception("Superclass %s is undefined" % superclass)
 
             #self.constructors.extend(["instantiate_link", self.current_model, "Inheritance", "%s_inherits_from_%s" % (self.current_element[-1], superclass), self.current_element[-1], superclass])
-            self.code += '\tinstantiate_link(%s, "Inheritance", "%s_inherits_from_%s", %s, %s)\n' % (self.current_model, self.current_element[-1], superclass, self.current_element[-1], superclass)
+            self.code += '\tinstantiate_link(%s, "Inheritance", "%s_inherits_from_%s", "%s", "%s")\n' % (self.current_model, self.current_element[-1], superclass, self.current_element[-1], superclass)
             self.names.add("%s_inherits_from_%s" % (self.current_element[-1], superclass))
 
     def visit_model_attribute(self, tree):
@@ -117,7 +119,7 @@ class ModelBootstrapVisitor(Visitor):
             attr_optional = len(tree.get_children("OPTIONAL")) > 0
             attr_type = children[1].get_text()
             #self.constructors.extend(["model_define_attribute", self.current_model, self.current_element[-1], attr_name, attr_optional, attr_type])
-            self.code += "\tmodel_define_attribute(%s, %s, %s, %s, %s)\n" % (self.current_model, self.current_element[-1], attr_name, attr_optional, attr_type)
+            self.code += '\tmodel_define_attribute(%s, "%s", "%s", "%s", %s)\n' % (self.current_model, self.current_element[-1], attr_name, attr_optional, attr_type)
             full_attribute_name = self.current_element[-1] + "_" + attr_name
 
             if is_assign:
@@ -176,7 +178,7 @@ class ModelBootstrapVisitor(Visitor):
             else:
                 raise Exception(attr_value.head)
             #self.constructors.extend(["instantiate_attribute", self.current_model, self.current_element[-1], attr_name, attr_value])
-            self.code += "\tinstantiate_attribute(%s, %s, %s, %s)\n" % (self.current_model, self.current_element[-1], attr_name, attr_value)
+            self.code += '\tinstantiate_attribute(%s, "%s", "%s", %s)\n' % (self.current_model, self.current_element[-1], attr_name, attr_value)
         elif tree.get_children("DOLLAR"):
             # Coded attribute
             #self.constructors.extend(["instantiate_attribute_code", self.current_model, self.current_element[-1], attr_name])
@@ -192,14 +194,14 @@ class ModelBootstrapVisitor(Visitor):
             code = "\n".join(code_fragments)
 
             func_name = "__code__%s" % abs(hash(code))
-            self.code += "\tinstantiate_attribute_code(%s, %s, %s, %s)\n" % (self.current_model, self.current_element[-1], attr_name, func_name)
+            self.code += '\tinstantiate_attribute_code(%s, "%s", "%s", %s)\n' % (self.current_model, self.current_element[-1], attr_name, func_name)
 
             # Rewrite the name of the function to func_name
             prepend = code
             before, after = prepend.split("function ", 1)
             before = before + "function "
             after = "(" + after.split("(", 1)[1]
-            prepend = before + func_name + after
+            prepend = before + func_name + after + "\n"
 
             # And prepend the actual code block
             self.code = prepend + self.code

+ 6 - 10
interface/HUTN/includes/metamodels.alh

@@ -1,10 +1,6 @@
-Void function create_metamodels()
-Element function initialize_SCD(location : String)
-Element function initialize_PN(location_SCD : String, location_PN : String)
-Element function initialize_bottom(location : String)
-Element function initialize_AL(location_SCD : String, location_AL : String)
-Element function constraint_Natural(params : Element)
-Element function constraint_String(params : Element)
-Element function constraint_Location(params : Element)
-Element function constraint_Boolean(params : Element)
-Element function constraint_ActionLanguage(params : Element)
+Void function initialize_MMs()
+String function constraint_Natural(model : Element, name : String)
+String function constraint_String(model : Element, name : String)
+String function constraint_Location(model : Element, name : String)
+String function constraint_Boolean(model : Element, name : String)
+String function constraint_ActionLanguage(model : Element, name : String)