|
@@ -1,4 +1,6 @@
|
|
|
include "primitives.alh"
|
|
|
+include "utils.alh"
|
|
|
+include "random.alh"
|
|
|
|
|
|
// This function must be kept internally, only called through the "sleep" and "interruptable_sleep" functions
|
|
|
Float function __sleep(a : Float, b : Boolean) = ?primitives/__sleep
|
|
@@ -486,3 +488,69 @@ Element function range(max : Integer):
|
|
|
counter = counter + 1
|
|
|
|
|
|
return result!
|
|
|
+
|
|
|
+String function spawn(function : Element, arguments : Element):
|
|
|
+ // Define a new task, with all the required stack information
|
|
|
+ // This is taken from the "task_management" code
|
|
|
+ Element task_root
|
|
|
+ Element task_frame
|
|
|
+ Element output_value
|
|
|
+ Element input_value
|
|
|
+ Element root
|
|
|
+
|
|
|
+ root = read_root()
|
|
|
+ task_root = create_node()
|
|
|
+ task_frame = create_node()
|
|
|
+ output_value = create_node()
|
|
|
+ input_value = create_node()
|
|
|
+
|
|
|
+ dict_add_fast(task_root, "frame", task_frame)
|
|
|
+ dict_add_fast(task_root, "output", output_value)
|
|
|
+ dict_add_fast(task_root, "last_output", output_value)
|
|
|
+ dict_add_fast(task_root, "input", input_value)
|
|
|
+ dict_add_fast(task_root, "last_input", input_value)
|
|
|
+ dict_add_fast(task_frame, "evalstack", create_node())
|
|
|
+ dict_add_fast(task_frame, "returnvalue", create_node())
|
|
|
+ dict_add_fast(task_frame, "phase", "init")
|
|
|
+ dict_add_fast(task_frame, "symbols", create_node())
|
|
|
+
|
|
|
+ // Instead of just spawning, we set a different IP
|
|
|
+ dict_add_fast(task_frame, "IP", function["body"])
|
|
|
+
|
|
|
+ // Additionally, we copy over all globals that we previously had
|
|
|
+ dict_add_fast(task_root, "globals", dict_copy(root[get_taskname()]["globals"]))
|
|
|
+
|
|
|
+ log("Setting IP to " + cast_value(function["body"]))
|
|
|
+ log("Has outputs: " + set_to_string(dict_keys(function)))
|
|
|
+
|
|
|
+ if (dict_in(function, "params")):
|
|
|
+ // And add the arguments to the symbol table
|
|
|
+ Element symbols
|
|
|
+ String arg_names_call
|
|
|
+ Element param_dict
|
|
|
+ Integer arg_i
|
|
|
+ symbols = task_frame["symbols"]
|
|
|
+ arg_names_call = "abcdefghijklmnopqrstuvwxyz"
|
|
|
+ arg_i = 0
|
|
|
+ param_dict = function["params"]
|
|
|
+
|
|
|
+ arguments = list_copy(arguments)
|
|
|
+ Element t
|
|
|
+ Element entry
|
|
|
+ while (list_len(arguments) > 0):
|
|
|
+ entry = create_node()
|
|
|
+ dict_add(entry, "value", list_pop(arguments, 0))
|
|
|
+ t = create_edge(symbols, entry)
|
|
|
+ create_edge(t, param_dict[string_get(arg_names_call, arg_i)])
|
|
|
+ log("Adding to symbols: " + cast_value(string_get(arg_names_call, arg_i)))
|
|
|
+ arg_i = arg_i + 1
|
|
|
+
|
|
|
+ // Add this only at the end, as otherwise the task will already be detected and executed
|
|
|
+ String taskname
|
|
|
+ taskname = random_string(30)
|
|
|
+
|
|
|
+ while (dict_in(read_root(), taskname)):
|
|
|
+ taskname = random_string(30)
|
|
|
+ dict_add_fast(read_root(), taskname, task_root)
|
|
|
+
|
|
|
+ return taskname!
|