Kaynağa Gözat

Make constructors work with a list internally, instead of relying on input()

Yentl Van Tendeloo 8 yıl önce
ebeveyn
işleme
5dfe2c511a

+ 97 - 84
bootstrap/constructors.alc

@@ -3,11 +3,27 @@ include "conformance_scd.alh"
 include "library.alh"
 include "io.alh"
 include "modelling.alh"
+include "utils.alh"
 
 Element while_stack = ?
 Element variable_map = ?
 
 Element function construct_function():
+	Integer len
+	Element list
+
+	len = input()
+	list = list_create()
+
+	log("Waiting for " + cast_v2s(len))
+	while (list_len(list) < len):
+		log("Current length: " + cast_v2s(list_len(list)))
+		list_append(list, input())
+	log("Ready!")
+
+	return construct_function_list(list_reverse(list))!
+
+Element function construct_function_list(list : Element):
 	String command
 	Element result
 	Element main_function
@@ -25,19 +41,19 @@ Element function construct_function():
 
 	continue = True
 	while (continue):
-		command = input()
+		command = list_pop_final(list)
 		if (command == "global"):
-			result = construct_global()
+			result = construct_global(list)
 		elif (command == "funcdef"):
-			result = construct_funcdef(False)
+			result = construct_funcdef(False, list)
 		elif (command == "mutable_funcdef"):
-			result = construct_funcdef(True)
+			result = construct_funcdef(True, list)
 		else:
 			log("ERROR (1): did not understand command " + cast_e2s(command))
 			output("ERROR: compiled code not understood: " + cast_e2s(command))
 			return read_root()!
 
-		continue = input()
+		continue = list_pop_final(list)
 		if (prev_element != read_root()):
 			dict_add_fast(prev_element, "next", result["start"])
 		else:
@@ -67,16 +83,16 @@ Element function construct_function():
 	
 	return main_function!
 
-Action function construct_global():
+Action function construct_global(list : Element):
 	Action this_element
 	String declared_element
 	String op
 
 	this_element = create_value(!global)
-	declared_element = input()
+	declared_element = list_pop_final(list)
 	dict_add_fast(this_element, "var", declared_element)
 
-	op = input()
+	op = list_pop_final(list)
 	if (op != "none"):
 		// Defines
 		Action assign
@@ -89,7 +105,7 @@ Action function construct_global():
 		dict_add_fast(resolve, "var", declared_element)
 		if (op == "deref"):
 			value = create_value(!constant)
-			dict_add_fast(value, "node", import_node(input()))
+			dict_add_fast(value, "node", import_node(list_pop_final(list)))
 		elif (op == "empty"):
 			value = create_value(!call)
 			Element res
@@ -101,7 +117,7 @@ Action function construct_global():
 			dict_add_fast(res, "var", "create_node")
 		elif (op == "const"):
 			value = create_value(!constant)
-			dict_add_fast(value, "node", input())
+			dict_add_fast(value, "node", list_pop_final(list))
 		dict_add_fast(assign, "value", value)
 
 		Element result
@@ -120,7 +136,7 @@ Action function construct_global():
 		dict_add_fast(result, "end", this_element)
 		return result!
 
-Action function construct_funcdef(mutable : Boolean):
+Action function construct_funcdef(mutable : Boolean, list : Element):
 	Action assign
 	Action resolve
 	Action constant
@@ -134,7 +150,7 @@ Action function construct_funcdef(mutable : Boolean):
 	assign = create_value(!assign)
 	resolve = create_value(!resolve)
 	constant = create_value(!constant)
-	name = input()
+	name = list_pop_final(list)
 	if (dict_in(variable_map, name)):
 		formal = dict_read(variable_map, name)
 	else:
@@ -153,7 +169,7 @@ Action function construct_funcdef(mutable : Boolean):
 		dict_add_fast(func, "mutable", create_node())
 
 	Integer nrParams
-	nrParams = input()
+	nrParams = list_pop_final(list)
 	Integer counter
 	counter = 0
 	Element param
@@ -165,12 +181,12 @@ Action function construct_funcdef(mutable : Boolean):
 		param = create_node()
 		dict_add_fast(params, string_get(arg_names_decl, counter), param)
 		dict_add_fast(param, "name", string_get(arg_names_decl, counter))
-		dict_add_fast(variable_map, input(), param)
+		dict_add_fast(variable_map, list_pop_final(list), param)
 		// Output each parameter in turn
 		counter = counter + 1
 
 	// Now add the body
-	dict_add_fast(func, "body", construct_unknown())
+	dict_add_fast(func, "body", construct_unknown(list))
 
 	Element result
 	result = dict_create()
@@ -180,85 +196,82 @@ Action function construct_funcdef(mutable : Boolean):
 	dict_add_fast(result, "end", assign)
 	return result!
 
-Element function construct_unknown():
+Element function construct_unknown(list : Element):
 	String elem
 	Element new_model
 	Element new_model_model
-	elem = input()
+	elem = list_pop_final(list)
 
 	if (elem == "if"):
-		return construct_if()!
+		return construct_if(list)!
 	elif (elem == "while"):
-		return construct_while()!
+		return construct_while(list)!
 	elif (elem == "access"):
-		return construct_access()!
+		return construct_access(list)!
 	elif (elem == "resolve"):
-		return construct_resolve()!
+		return construct_resolve(list)!
 	elif (elem == "assign"):
-		return construct_assign()!
+		return construct_assign(list)!
 	elif (elem == "call"):
-		return construct_call()!
+		return construct_call(list)!
 	elif (elem == "return"):
-		return construct_return()!
+		return construct_return(list)!
 	elif (elem == "const"):
-		return construct_const()!
+		return construct_const(list)!
 	elif (elem == "declare"):
-		return construct_declare()!
+		return construct_declare(list)!
 	elif (elem == "output"):
-		return construct_output()!
+		return construct_output(list)!
 	elif (elem == "input"):
-		return construct_input()!
+		return construct_input(list)!
 	elif (elem == "deref"):
-		return construct_deref()!
+		return construct_deref(list)!
 	elif (elem == "break"):
-		return construct_break()!
+		return construct_break(list)!
 	elif (elem == "continue"):
-		return construct_continue()!
-	elif (elem == "model"):
-		construct_model()
-		return construct_unknown()!
+		return construct_continue(list)!
 	else:
 		log("ERROR (2): did not understand command " + cast_e2s(elem))
 		output("ERROR: compiled code not understood: " + cast_e2s(elem))
 		return read_root()!
 
-Action function construct_if():
+Action function construct_if(list : Element):
 	Action this_element
 	this_element = create_value(!if)
-	dict_add_fast(this_element, "cond", construct_unknown())
-	dict_add_fast(this_element, "then", construct_unknown())
-	if (input()):
-		dict_add_fast(this_element, "else", construct_unknown())
-	if (input()):
-		dict_add_fast(this_element, "next", construct_unknown())
+	dict_add_fast(this_element, "cond", construct_unknown(list))
+	dict_add_fast(this_element, "then", construct_unknown(list))
+	if (list_pop_final(list)):
+		dict_add_fast(this_element, "else", construct_unknown(list))
+	if (list_pop_final(list)):
+		dict_add_fast(this_element, "next", construct_unknown(list))
 	return this_element!
 
-Action function construct_while():
+Action function construct_while(list : Element):
 	Action this_element
 	this_element = create_value(!while)
-	dict_add_fast(this_element, "cond", construct_unknown())
+	dict_add_fast(this_element, "cond", construct_unknown(list))
 
 	list_append(while_stack, this_element)
-	dict_add_fast(this_element, "body", construct_unknown())
+	dict_add_fast(this_element, "body", construct_unknown(list))
 	list_delete(while_stack, list_len(while_stack) - 1)
 
-	if (input()):
-		dict_add_fast(this_element, "next", construct_unknown())
+	if (list_pop_final(list)):
+		dict_add_fast(this_element, "next", construct_unknown(list))
 	return this_element!
 
-Action function construct_access():
+Action function construct_access(list : Element):
 	Action this_element
 	this_element = create_value(!access)
-	dict_add_fast(this_element, "var", construct_unknown())
+	dict_add_fast(this_element, "var", construct_unknown(list))
 	return this_element!
 
-Action function construct_resolve():
+Action function construct_resolve(list : Element):
 	Action this_element
 	Element linked_element
 	String name
 
 	this_element = create_value(!resolve)
-	name = input()
+	name = list_pop_final(list)
 	if dict_in(variable_map, name):
 		linked_element = variable_map[name]
 	else:
@@ -266,22 +279,22 @@ Action function construct_resolve():
 	dict_add_fast(this_element, "var", linked_element)
 	return this_element!
 
-Action function construct_assign():
+Action function construct_assign(list : Element):
 	Action this_element
 	this_element = create_value(!assign)
-	dict_add_fast(this_element, "var", construct_unknown())
-	dict_add_fast(this_element, "value", construct_unknown())
-	if (input()):
-		dict_add_fast(this_element, "next", construct_unknown())
+	dict_add_fast(this_element, "var", construct_unknown(list))
+	dict_add_fast(this_element, "value", construct_unknown(list))
+	if (list_pop_final(list)):
+		dict_add_fast(this_element, "next", construct_unknown(list))
 	return this_element!
 
-Action function construct_call():
+Action function construct_call(list : Element):
 	Action this_element
 	this_element = create_value(!call)
-	dict_add_fast(this_element, "func", construct_unknown())
+	dict_add_fast(this_element, "func", construct_unknown(list))
 
 	Integer nrParams
-	nrParams = input()
+	nrParams = list_pop_final(list)
 	Integer counter
 	counter = 0
 
@@ -294,7 +307,7 @@ Action function construct_call():
 		param = create_node()
 
 		dict_add_fast(param, "name", string_get(arg_names_call, counter))
-		dict_add_fast(param, "value", construct_unknown())
+		dict_add_fast(param, "value", construct_unknown(list))
 
 		if (counter == 0):
 			dict_add_fast(this_element, "params", param)
@@ -307,26 +320,26 @@ Action function construct_call():
 	if (nrParams > 0):
 		dict_add_fast(this_element, "last_param", prev_param)
 
-	if (input()):
-		dict_add_fast(this_element, "next", construct_unknown())
+	if (list_pop_final(list)):
+		dict_add_fast(this_element, "next", construct_unknown(list))
 	return this_element!
 
-Action function construct_return():
-	if (input()):
+Action function construct_return(list : Element):
+	if (list_pop_final(list)):
 		Action this_element
 		this_element = create_value(!return)
-		dict_add_fast(this_element, "value", construct_unknown())
+		dict_add_fast(this_element, "value", construct_unknown(list))
 		return this_element!
 	else:
 		return create_value(!return)!
 
-Action function construct_const():
+Action function construct_const(list : Element):
 	Action this_element
 	this_element = create_value(!constant)
-	dict_add_fast(this_element, "node", input())
+	dict_add_fast(this_element, "node", list_pop_final(list))
 	return this_element!
 
-Action function construct_declare():
+Action function construct_declare(list : Element):
 	Action this_element
 	Element declared_element
 	String name
@@ -334,12 +347,12 @@ Action function construct_declare():
 	this_element = create_value(!declare)
 	declared_element = create_node()
 	dict_add_fast(this_element, "var", declared_element)
-	name = input()
+	name = list_pop_final(list)
 	dict_add_fast(variable_map, name, declared_element)
 
 	// Assign value
 	String op
-	op = input()
+	op = list_pop_final(list)
 	if (op != "none"):
 		Action assign
 		Action resolve
@@ -351,7 +364,7 @@ Action function construct_declare():
 		dict_add_fast(resolve, "var", declared_element)
 		if (op == "deref"):
 			value = create_value(!constant)
-			dict_add_fast(value, "node", import_node(input()))
+			dict_add_fast(value, "node", import_node(list_pop_final(list)))
 		elif (op == "empty"):
 			value = create_value(!call)
 			Element res
@@ -363,42 +376,42 @@ Action function construct_declare():
 			dict_add_fast(res, "var", "create_node")
 		elif (op == "const"):
 			value = create_value(!constant)
-			dict_add_fast(value, "node", input())
+			dict_add_fast(value, "node", list_pop_final(list))
 		dict_add_fast(assign, "value", value)
 
-		if (input()):
-			dict_add_fast(assign, "next", construct_unknown())
+		if (list_pop_final(list)):
+			dict_add_fast(assign, "next", construct_unknown(list))
 	else:
-		if (input()):
-			dict_add_fast(this_element, "next", construct_unknown())
+		if (list_pop_final(list)):
+			dict_add_fast(this_element, "next", construct_unknown(list))
 	return this_element!
 
-Action function construct_input():
+Action function construct_input(list : Element):
 	Action this_element
 	this_element = create_value(!input)
 	return this_element!
 
-Action function construct_output():
+Action function construct_output(list : Element):
 	Action this_element
 	this_element = create_value(!output)
-	dict_add_fast(this_element, "value", construct_unknown())
-	if (input()):
-		dict_add_fast(this_element, "next", construct_unknown())
+	dict_add_fast(this_element, "value", construct_unknown(list))
+	if (list_pop_final(list)):
+		dict_add_fast(this_element, "next", construct_unknown(list))
 	return this_element!
 
-Action function construct_deref():
+Action function construct_deref(list : Element):
 	Action this_element
 	this_element = create_value(!constant)
-	dict_add_fast(this_element, "node", import_node(input()))
+	dict_add_fast(this_element, "node", import_node(list_pop_final(list)))
 	return this_element!
 
-Action function construct_break():
+Action function construct_break(list : Element):
 	Action this_element
 	this_element = create_value(!break)
 	dict_add_fast(this_element, "while", while_stack[list_len(while_stack) - 1])
 	return this_element!
 
-Action function construct_continue():
+Action function construct_continue(list : Element):
 	Action this_element
 	this_element = create_value(!continue)
 	dict_add_fast(this_element, "while", while_stack[list_len(while_stack) - 1])

+ 3 - 0
bootstrap/semi_primitives.alc

@@ -187,6 +187,9 @@ Element function list_pop(lst : Element, index : Integer):
 	list_delete(lst, index)
 	return v!
 
+Element function list_pop_final(lst : Element):
+	return list_pop(lst, list_len(lst) - 1)!
+
 Element function set_copy(a : Element):
 	return dict_keys(a)!
 

+ 2 - 2
bootstrap/utils.alc

@@ -59,8 +59,8 @@ Element function list_reverse(lst : Element):
 	result = list_create()
 
 	Integer i
-	i = list_len(lst)
-	while (i > 0):
+	i = list_len(lst) - 1
+	while (i >= 0):
 		list_append(result, list_read(lst, i))
 		i = i - 1
 

+ 1 - 2
interface/HUTN/hutn_compiler/constructors_visitor.py

@@ -8,8 +8,7 @@ class ConstructorsVisitor(Visitor):
         self.free_id = 0
 
     def dump(self):
-        return self.constructors
-        # return pickle.dumps(self.constructors, pickle.HIGHEST_PROTOCOL)
+        return [len(self.constructors)] + self.constructors
 
     def add_constructors(self, *constructors):
         self.constructors.extend(constructors)

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

@@ -1 +1,2 @@
 Action function construct_function()
+Action function construct_function_list(list : Element)

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

@@ -102,6 +102,7 @@ Element function exec(a : Element)
 Element function resolve(var_name : String)
 Boolean function has_input()
 Element function list_pop(lst : Element, index : Integer)
+Element function list_pop_final(lst : Element)
 Element function set_copy(set : Element)
 String function set_to_string(set : Element)
 String function list_to_string(set : Element)

+ 2 - 1
wrappers/modelverse.py

@@ -138,7 +138,8 @@ def _compile_AL(code):
         f.write(code)
         f.flush()
 
-    return do_compile(".code.alc", COMPILER_PATH + "/grammars/actionlanguage.g", "CS")
+    compiled = do_compile(".code.alc", COMPILER_PATH + "/grammars/actionlanguage.g", "CS")
+    return compiled
 
 def _compile_model(code):
     # Compile a model and send the compiled graph