|
@@ -7,21 +7,68 @@ include "modelling.alh"
|
|
|
Element while_stack = ?
|
|
|
Element variable_map = ?
|
|
|
|
|
|
-Element function construct_top():
|
|
|
+Element function construct_function():
|
|
|
String command
|
|
|
- while (True):
|
|
|
+ Element result
|
|
|
+ Element main_function
|
|
|
+ Boolean continue
|
|
|
+ Element prev_element
|
|
|
+ Element first_element
|
|
|
+
|
|
|
+ // Initialize variables
|
|
|
+ prev_element = read_root()
|
|
|
+ main_function = read_root()
|
|
|
+
|
|
|
+ // Clear global variables
|
|
|
+ while_stack = create_node()
|
|
|
+ variable_map = create_node()
|
|
|
+
|
|
|
+ continue = True
|
|
|
+ while (continue):
|
|
|
command = input()
|
|
|
if (command == "global"):
|
|
|
- return construct_global()!
|
|
|
+ result = construct_global()
|
|
|
elif (command == "funcdef"):
|
|
|
- return construct_top_funcdef(False)!
|
|
|
+ result = construct_funcdef(False)
|
|
|
elif (command == "mutable_funcdef"):
|
|
|
- return construct_top_funcdef(True)!
|
|
|
+ result = construct_funcdef(True)
|
|
|
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()
|
|
|
+ if (prev_element != read_root()):
|
|
|
+ dict_add_fast(prev_element, "next", result["start"])
|
|
|
+ else:
|
|
|
+ first_element = result["start"]
|
|
|
+
|
|
|
+ if (bool_and(element_eq(main_function, read_root()), command != "global")):
|
|
|
+ // In case no main function is defined, it is the first defined function
|
|
|
+ // This is mostly there to ensure backwards compatibility
|
|
|
+ main_function = result["instruction"]
|
|
|
+
|
|
|
+ prev_element = result["end"]
|
|
|
+
|
|
|
+ if (value_eq(result["name"], "main")):
|
|
|
+ // It was the function that we want to call
|
|
|
+ main_function = result["instruction"]
|
|
|
+
|
|
|
+ if (element_eq(main_function, read_root())):
|
|
|
+ log("ERROR (2): no main function found")
|
|
|
+ output("ERROR: no main function found")
|
|
|
+ return read_root()!
|
|
|
+
|
|
|
+ // Overwrite the main function with our declaration function
|
|
|
+ prev_element = main_function["body"]
|
|
|
+ log("Got main function V: " + cast_e2s(main_function))
|
|
|
+ log("Got main function S: " + dict_to_string(main_function))
|
|
|
+ dict_delete(main_function, "body")
|
|
|
+ dict_add_fast(main_function, "body", first_element)
|
|
|
+ dict_add_fast(result["end"], "next", prev_element)
|
|
|
+
|
|
|
+ return main_function!
|
|
|
+
|
|
|
Action function construct_global():
|
|
|
Action this_element
|
|
|
String declared_element
|
|
@@ -58,21 +105,25 @@ Action function construct_global():
|
|
|
dict_add_fast(value, "node", input())
|
|
|
dict_add_fast(assign, "value", value)
|
|
|
|
|
|
- if (input()):
|
|
|
- dict_add_fast(assign, "next", construct_top())
|
|
|
- return this_element!
|
|
|
+ Element result
|
|
|
+ result = create_node()
|
|
|
+ dict_add_fast(result, "name", "")
|
|
|
+ dict_add_fast(result, "instruction", this_element)
|
|
|
+ dict_add_fast(result, "start", this_element)
|
|
|
+ dict_add_fast(result, "end", assign)
|
|
|
+ return result!
|
|
|
|
|
|
-Action function construct_top_funcdef(mutable : Boolean):
|
|
|
+Action function construct_funcdef(mutable : Boolean):
|
|
|
Action assign
|
|
|
Action resolve
|
|
|
Action constant
|
|
|
Element formal
|
|
|
Element func
|
|
|
Element params
|
|
|
- Action global
|
|
|
+ Action declare
|
|
|
String name
|
|
|
|
|
|
- global = create_value(!global)
|
|
|
+ declare = create_value(!global)
|
|
|
assign = create_value(!assign)
|
|
|
resolve = create_value(!resolve)
|
|
|
constant = create_value(!constant)
|
|
@@ -83,8 +134,8 @@ Action function construct_top_funcdef(mutable : Boolean):
|
|
|
formal = name
|
|
|
func = create_node()
|
|
|
params = create_node()
|
|
|
- dict_add_fast(global, "var", formal)
|
|
|
- dict_add_fast(global, "next", assign)
|
|
|
+ dict_add_fast(declare, "var", formal)
|
|
|
+ dict_add_fast(declare, "next", assign)
|
|
|
dict_add_fast(assign, "var", resolve)
|
|
|
dict_add_fast(assign, "value", constant)
|
|
|
dict_add_fast(resolve, "var", formal)
|
|
@@ -114,10 +165,13 @@ Action function construct_top_funcdef(mutable : Boolean):
|
|
|
// Now add the body
|
|
|
dict_add_fast(func, "body", construct_unknown())
|
|
|
|
|
|
- if (input()):
|
|
|
- dict_add_fast(assign, "next", construct_top())
|
|
|
-
|
|
|
- return global!
|
|
|
+ Element result
|
|
|
+ result = create_node()
|
|
|
+ dict_add_fast(result, "name", name)
|
|
|
+ dict_add_fast(result, "instruction", func)
|
|
|
+ dict_add_fast(result, "start", declare)
|
|
|
+ dict_add_fast(result, "end", assign)
|
|
|
+ return result!
|
|
|
|
|
|
Element function construct_unknown():
|
|
|
String elem
|
|
@@ -309,47 +363,3 @@ Action function construct_continue():
|
|
|
this_element = create_value(!continue)
|
|
|
dict_add_fast(this_element, "while", while_stack[list_len(while_stack) - 1])
|
|
|
return this_element!
|
|
|
-
|
|
|
-Action function construct_function():
|
|
|
- Action func
|
|
|
- Integer nrParams
|
|
|
- Integer counter
|
|
|
- Element param
|
|
|
- Element params
|
|
|
- String arg_names_decl
|
|
|
- String inp
|
|
|
-
|
|
|
- variable_map = create_node()
|
|
|
-
|
|
|
- inp = input()
|
|
|
- while (bool_and(inp != "funcdef", inp != "mutable_funcdef")):
|
|
|
- // We skip over everything that is not a funcdef, as these are all just definitions of global stuff
|
|
|
- inp = input()
|
|
|
-
|
|
|
- // Consume the name
|
|
|
- input()
|
|
|
-
|
|
|
- params = create_node()
|
|
|
- nrParams = input()
|
|
|
- counter = 0
|
|
|
- func = create_node()
|
|
|
- arg_names_decl = "abcdefghijklmnopqrstuvwxyz"
|
|
|
- dict_add_fast(func, "params", params)
|
|
|
-
|
|
|
- if (inp == "mutable_funcdef"):
|
|
|
- dict_add_fast(func, "mutable", create_node())
|
|
|
-
|
|
|
- while (counter < nrParams):
|
|
|
- param = create_node()
|
|
|
- dict_add_fast(params, string_get(arg_names_decl, counter), param)
|
|
|
- dict_add_fast(variable_map, input(), param)
|
|
|
- // Output each parameter in turn
|
|
|
- counter = counter + 1
|
|
|
-
|
|
|
- // Now add the body
|
|
|
- dict_add_fast(func, "body", construct_unknown())
|
|
|
-
|
|
|
- // Consume the final 'false', to indicate that no additional code will come
|
|
|
- input()
|
|
|
-
|
|
|
- return func!
|