Browse Source

Added compiler code in bootstrap

Yentl Van Tendeloo 8 years ago
parent
commit
c474025c10

+ 53 - 0
bootstrap/compiler.alc

@@ -0,0 +1,53 @@
+include "primitives.alh"
+include "services.alh"
+include "constructors.alh"
+include "modelling.alh"
+
+Element function generic_compile(code : String, port : String):
+	comm_set(port, code)
+	
+	String response
+	response = comm_get(port)
+	if (response == "OK"):
+		Integer count
+		Element lst
+
+		lst = list_create()
+		count = comm_get(port)
+		while (count > 0):
+			list_append(lst, comm_get(port))
+
+		comm_close(port)
+		return lst!
+	else:
+		log("Compilation error: " + response)
+		comm_close(port)
+		return read_root()!
+
+Element function compile_code(code : String):
+	String port
+	port = comm_connect("compiler")
+	comm_set(port, "code")
+
+	Element list
+	list = generic_compile(code, port)
+
+	if (element_eq(list, read_root())):
+		return read_root()!
+
+	else:
+		return construct_function_list(list)!
+
+Element function compile_model(code : String, metamodel : Element):
+	String port
+	port = comm_connect("compiler")
+	comm_set(port, "model")
+
+	Element list
+	list = generic_compile(code, port)
+
+	if (element_eq(list, read_root())):
+		return read_root()!
+
+	else:
+		return construct_model_list(metamodel, list)!

+ 1 - 0
bootstrap/initial_code_task.alc

@@ -23,5 +23,6 @@ Void mutable function __main():
 	exec(root["bootstrap/io.alc"]["initializers"])
 	exec(root["bootstrap/conformance_finding.alc"]["initializers"])
 	exec(root["bootstrap/typing.alc"]["initializers"])
+	exec(root["bootstrap/compiler.alc"]["initializers"])
 	new_task()
 	return!

+ 60 - 28
bootstrap/modelling.alc

@@ -5,6 +5,7 @@ include "constructors.alh"
 include "metamodels.alh"
 include "library.alh"
 include "typing.alh"
+include "utils.alh"
 
 Element global_models = ?
 
@@ -537,34 +538,18 @@ Element function construct_model_raw(metamodel : Element):
 	String command
 	Element model
 
-	model = instantiate_model(metamodel)
-
-	while (True):
-		command = input()
-		if (command == "add_node"):
-			model_add_node(model, input())
-		elif (command == "add_value"):
-			model_add_value(model, input(), input())
-		elif (command == "add_edge"):
-			model_add_edge(model, input(), input(), input())
-		elif (command == "exit"):
-			return model!
-		elif (command == "instantiate_node"):
-			instantiate_node(model, input(), input())
-		elif (command == "model_define_attribute"):
-			model_define_attribute(model, input(), input(), input(), input())
-		elif (command == "instantiate_attribute"):
-			instantiate_attribute(model, input(), input(), input())
-		elif (command == "instantiate_attribute_ref"):
-			instantiate_attribute_ref(model, input(), input(), input())
-		elif (command == "instantiate_attribute_code"):
-			instantiate_attribute_code(model, input(), input(), construct_function())
-		elif (command == "instantiate_link"):
-			instantiate_link(model, input(), input(), input(), input())
-		elif (command == "add_code_model"):
-			add_code_model(model, input(), construct_function())
-		else:
-			log("Modelling error: did not understand command " + command)
+	Integer i
+	Integer count
+	Element lst
+	i = 0
+	count = input()
+	lst = list_create()
+
+	while (i < count):
+		list_append(lst, input())
+		i = i + 1
+	
+	return construct_model_list(metamodel, lst)!
 
 Element function get_func_AL_model(al_model : Element):
 	Element initial_function
@@ -580,3 +565,50 @@ Element function get_func_AL_model(al_model : Element):
 		return read_root()!
 	else:
 		return al_model["model"][set_pop(allAssociationDestinations(al_model, set_pop(initial_function), "initial_funcdef"))]!
+
+Element function trim_AL_constructors(list : Element):
+	Integer length
+	Element lst
+	Integer i
+	length = list_pop_final(list)
+	i = 0
+	lst = list_create()
+
+	while (i < length):
+		list_append(lst, list_pop_final(list))
+
+	return lst!
+
+Element function construct_model_list(metamodel : Element, list : Element):
+	String command
+	Element model
+
+	list = list_reverse(list)
+	model = instantiate_model(metamodel)
+
+	while (list_len(list) > 0):
+		command = list_pop_final(list)
+		if (command == "add_node"):
+			model_add_node(model, list_pop_final(list))
+		elif (command == "add_value"):
+			model_add_value(model, list_pop_final(list), list_pop_final(list))
+		elif (command == "add_edge"):
+			model_add_edge(model, list_pop_final(list), list_pop_final(list), list_pop_final(list))
+		elif (command == "instantiate_node"):
+			instantiate_node(model, list_pop_final(list), list_pop_final(list))
+		elif (command == "model_define_attribute"):
+			model_define_attribute(model, list_pop_final(list), list_pop_final(list), list_pop_final(list), list_pop_final(list))
+		elif (command == "instantiate_attribute"):
+			instantiate_attribute(model, list_pop_final(list), list_pop_final(list), list_pop_final(list))
+		elif (command == "instantiate_attribute_ref"):
+			instantiate_attribute_ref(model, list_pop_final(list), list_pop_final(list), list_pop_final(list))
+		elif (command == "instantiate_attribute_code"):
+			instantiate_attribute_code(model, list_pop_final(list), list_pop_final(list), construct_function_list(trim_AL_constructors(list)))
+		elif (command == "instantiate_link"):
+			instantiate_link(model, list_pop_final(list), list_pop_final(list), list_pop_final(list), list_pop_final(list))
+		elif (command == "add_code_model"):
+			add_code_model(model, list_pop_final(list), construct_function_list(trim_AL_constructors(list)))
+		else:
+			log("Modelling error: did not understand command " + command)
+
+	return model!

+ 1 - 1
interface/HUTN/hutn_compiler/model_visitor.py

@@ -15,7 +15,7 @@ class ModelVisitor(Visitor):
         self.includes = []
 
     def dump(self):
-        return self.constructors
+        return [len(self.constructors)] + self.constructors
 
     def __getattr__(self, attr):
         if attr.startswith("visit_"):

+ 2 - 0
interface/HUTN/includes/compiler.alh

@@ -0,0 +1,2 @@
+Element function compile_code(code : String)
+Element function compile_model(code : String, metamodel : Element)

+ 1 - 0
interface/HUTN/includes/modelling.alh

@@ -26,3 +26,4 @@ Element function construct_model_raw(metamodel : Element)
 Element function get_func_AL_model(model : Element)
 Void function add_code_model(model : Element, export_name : String, code : Element)
 String function read_type(model : Element, name : String)
+Element function construct_model_list(metamodel : Element, list : Element)

+ 3 - 1
scripts/HUTN_service.py

@@ -39,15 +39,17 @@ def compile_service(port):
             f.write(code)
             f.flush()
 
-        return do_compile(".model.mvc", COMPILER_PATH + "/grammars/modelling.g", "M") + ["exit"]
+        return do_compile(".model.mvc", COMPILER_PATH + "/grammars/modelling.g", "M")
 
     mode = service_get(port)
     code = service_get(port)
     try:
         if mode == "code":
+            service_set(port, "OK")
             compiled = compile_AL(code)
             [service_set(port, i) for i in compiled]
         elif mode == "model":
+            service_set(port, "OK")
             compiled = compile_model(code)
             [service_set(port, i) for i in compiled]
         else:

+ 3 - 3
wrappers/modelverse.py

@@ -159,7 +159,7 @@ def _compile_model(code):
         f.write(code)
         f.flush()
 
-    return do_compile(".model.mvc", COMPILER_PATH + "/grammars/modelling.g", "M") + ["exit"]
+    return do_compile(".model.mvc", COMPILER_PATH + "/grammars/modelling.g", "M")
 
 def _output(expected=None,port=None):
     if port is None:
@@ -310,7 +310,7 @@ def model_add(model_name, metamodel_name, model_code=None):
         except Exception as e:
             raise CompilationError(e)
     else:
-        compiled = ["exit"]
+        compiled = [0]
 
     _input(["model_add", metamodel_name, model_name])
     _handle_output("Waiting for model constructors...")
@@ -392,7 +392,7 @@ def model_overwrite(model_name, new_model=None, metamodel_name=None):
         except Exception as e:
             raise CompilationError(e)
     else:
-        compiled = ["exit"]
+        compiled = [0]
 
     _input("upload")
     _handle_output("Waiting for model constructors...")