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"):
		Element lst

		lst = comm_get_list(port)

		comm_close(port)
		return lst!
	else:
		comm_close(port)
		return response!

Element function compile_code(code : String):
	String port
	port = ""
	while (port == ""):
		port = comm_connect("compiler")

	comm_set(port, "code")

	Element list
	list = generic_compile(code, port)

	if (is_physical_string(list)):
		return list!
	else:
		Element model
		model = construct_function_list(list)
		dict_add(model, "__text", code)
		return model!

Element function compile_model(code : String, metamodel : Element):
	String port
	port = ""
	while (port == ""):
		port = comm_connect("compiler")

	comm_set(port, "model")

	Element list
	list = generic_compile(code, port)

	if (is_physical_string(list)):
		return list!
	else:
		return construct_model_list(instantiate_model(metamodel), list)!
