Selaa lähdekoodia

Partial migration

Yentl Van Tendeloo 7 vuotta sitten
vanhempi
commit
92cb1b6448

+ 0 - 2
bootstrap/primitives.alc

@@ -40,8 +40,6 @@ Integer function integer_subtraction(a: Integer, b: Integer) = ?primitives/integ
 Integer function integer_multiplication(a: Integer, b: Integer) = ?primitives/integer_multiplication
 Integer function integer_division(a: Integer, b: Integer) = ?primitives/integer_division
 Boolean function integer_lt(a: Integer, b: Integer) = ?primitives/integer_lt
-Element function list_insert(a: Element, b: Element, c: Integer) = ?primitives/list_insert
-Element function list_delete(a: Element, b: Integer) = ?primitives/list_delete
 String function string_join(a: String, b: String) = ?primitives/string_join
 String function string_get(a: String, b: Integer) = ?primitives/string_get
 Element function string_split(a: String, b: String) = ?primitives/string_split

+ 30 - 0
bootstrap/semi_primitives.alc

@@ -553,3 +553,33 @@ String function spawn(function : Element, arguments : Element):
 	dict_add_fast(read_root(), taskname, task_root)
 
 	return taskname!
+
+Element function list_insert(a : Element, b : Element, c : Integer):
+	Integer current
+	Element prev
+	Element new_prev
+
+	current = c
+	prev = b
+
+	while (current < list_len(a)):
+		new_prev = list_get(a, current)
+		dict_delete(a, current)
+		dict_add_fast(a, current, prev)
+		prev = new_prev
+		current = current + 1
+	
+	return a!
+
+Element function list_delete(a : Element, b : Integer):
+	Integer current
+
+	current = c
+	
+	while (current < (list_len(a) - 1)):
+		dict_delete(a, current)
+		dict_add_fast(a, current, a[current + 1])
+		current = current + 1
+
+	dict_delete(a, current)
+	return a!

+ 27 - 0
kernel/modelverse_kernel/compiled.py

@@ -276,4 +276,31 @@ def instantiate_node(a, b, c, **remainder):
     yield [("CD", [typing, name, b])]
 
     raise PrimitiveFinished(name_node)
+
+def list_insert(a, b, c, **remainder):
+    # TODO make non-primitive, though compiled
+    a_outgoing, c_value = yield [("RO", [a]), ("RV", [c])] 
+    links = yield [("RD", [a, i]) for i in range(c_value, len(a_outgoing))] + \
+                  [("RDE", [a, i]) for i in range(c_value, len(a_outgoing))]
+
+    values = links[:len(links) // 2]
+    edges = links[len(links) // 2:] 
+
+    yield [("CD", [a, c_value, b])] + \
+          [("CD", [a, c_value + 1 + index, value]) for index, value in enumerate(values)] + \
+          [("DE", [i]) for i in edges]
+    raise PrimitiveFinished(a)
+
+def list_delete(a, b, **remainder):
+    # TODO make non-primitive, though compiled
+    a_outgoing, b_value = yield [("RO", [a]), ("RV", [b])]
+    links = yield [("RD", [a, i]) for i in range(b_value, len(a_outgoing))] + \
+                  [("RDE", [a, i]) for i in range(b_value, len(a_outgoing))]
+
+    values = links[:len(links) // 2]
+    edges = links[len(links) // 2:]
+
+    yield [("CD", [a, b_value + index, value]) for index, value in enumerate(values[1:])] + \
+          [("DE", [i]) for i in edges]
+    raise PrimitiveFinished(a)
 """

+ 67 - 104
kernel/modelverse_kernel/new_primitives.py

@@ -46,120 +46,99 @@ def integer_subtraction(a, b, **remainder):
         a['value'], = yield [("RV", [a['id']])]
     if 'value' not in b:
         b['value'], = yield [("RV", [b['id']])]
-    result = {'value': a['value'] - b['value']}
-    raise PrimitiveFinished(result)
+    raise PrimitiveFinished({'value': a['value'] - b['value']})
 
 def integer_addition(a, b, **remainder):
     if 'value' not in a:
         a['value'], = yield [("RV", [a['id']])]
     if 'value' not in b:
         b['value'], = yield [("RV", [b['id']])]
-    result = {'value': a['value'] + b['value']}
-    raise PrimitiveFinished(result)
+    raise PrimitiveFinished({'value': a['value'] + b['value']})
 
 def integer_multiplication(a, b, **remainder):
     if 'value' not in a:
         a['value'], = yield [("RV", [a['id']])]
     if 'value' not in b:
         b['value'], = yield [("RV", [b['id']])]
-    result = {'value': a['value'] * b['value']}
-    raise PrimitiveFinished(result)
+    raise PrimitiveFinished({'value': a['value'] * b['value']})
 
 def integer_division(a, b, **remainder):
     if 'value' not in a:
         a['value'], = yield [("RV", [a['id']])]
     if 'value' not in b:
         b['value'], = yield [("RV", [b['id']])]
-    result = {'value': int(a['value']) // b['value']}
-    raise PrimitiveFinished(result)
+    raise PrimitiveFinished({'value': int(a['value']) // b['value']})
 
 def integer_lt(a, b, **remainder):
     if 'value' not in a:
         a['value'], = yield [("RV", [a['id']])]
     if 'value' not in b:
         b['value'], = yield [("RV", [b['id']])]
-    result = {'value': a['value'] < b['value']}
-    raise PrimitiveFinished(result)
+    raise PrimitiveFinished({'value': a['value'] < b['value']})
 
 def bool_and(a, b, **remainder):
     if 'value' not in a:
         a['value'], = yield [("RV", [a['id']])]
     if 'value' not in b:
         b['value'], = yield [("RV", [b['id']])]
-    result = {'value': a['value'] and b['value']}
-    raise PrimitiveFinished(result)
+    raise PrimitiveFinished({'value': a['value'] and b['value']})
 
 def bool_or(a, b, **remainder):
     if 'value' not in a:
         a['value'], = yield [("RV", [a['id']])]
     if 'value' not in b:
         b['value'], = yield [("RV", [b['id']])]
-    result = {'value': a['value'] or b['value']}
-    raise PrimitiveFinished(result)
+    raise PrimitiveFinished({'value': a['value'] or b['value']})
 
 def bool_not(a, **remainder):
     if 'value' not in a:
         a['value'], = yield [("RV", [a['id']])]
-    result = {'value': not a['value']}
-    raise PrimitiveFinished(result)
+    raise PrimitiveFinished({'value': not a['value']})
 
 def float_subtraction(a, b, **remainder):
     if 'value' not in a:
         a['value'], = yield [("RV", [a['id']])]
     if 'value' not in b:
         b['value'], = yield [("RV", [b['id']])]
-    result = {'value': a['value'] - b['value']}
-    raise PrimitiveFinished(result)
+    raise PrimitiveFinished({'value': a['value'] - b['value']})
 
 def float_addition(a, b, **remainder):
     if 'value' not in a:
         a['value'], = yield [("RV", [a['id']])]
     if 'value' not in b:
         b['value'], = yield [("RV", [b['id']])]
-    result = {'value': a['value'] + b['value']}
-    raise PrimitiveFinished(result)
+    raise PrimitiveFinished({'value': a['value'] + b['value']})
 
 def float_multiplication(a, b, **remainder):
     if 'value' not in a:
         a['value'], = yield [("RV", [a['id']])]
     if 'value' not in b:
         b['value'], = yield [("RV", [b['id']])]
-    result = {'value': a['value'] * b['value']}
-    raise PrimitiveFinished(result)
+    raise PrimitiveFinished({'value': a['value'] * b['value']})
 
 def float_division(a, b, **remainder):
     if 'value' not in a:
         a['value'], = yield [("RV", [a['id']])]
     if 'value' not in b:
         b['value'], = yield [("RV", [b['id']])]
-    result = {'value': float(a['value']) / float(b['value'])}
-    raise PrimitiveFinished(result)
+    raise PrimitiveFinished({'value': float(a['value']) / float(b['value'])})
 
 def float_lt(a, b, **remainder):
     if 'value' not in a:
         a['value'], = yield [("RV", [a['id']])]
     if 'value' not in b:
         b['value'], = yield [("RV", [b['id']])]
-    result = {'value': a['value'] < b['value']}
-    raise PrimitiveFinished(result)
+    raise PrimitiveFinished({'value': a['value'] < b['value']})
 
 def string_join(a, b, **remainder):
     if 'value' not in a:
         a['value'], = yield [("RV", [a['id']])]
     if 'value' not in b:
         b['value'], = yield [("RV", [b['id']])]
-    result = {'value': str(a['value']) + str(b['value'])}
-    raise PrimitiveFinished(result)
+    raise PrimitiveFinished({'value': str(a['value']) + str(b['value'])})
 
 def string_split(a, b, **remainder):
     # TODO make non-primitive, though compiled
-    a_value, b_value = yield [("RV", [a]), ("RV", [b])]
-    result = a_value.split(b_value)
-    elems = yield [("CN", [])] + [("CNV", [v]) for v in result]
-    new_val = elems[0]
-    yield [("CD", [new_val, i, v]) for i, v in enumerate(elems[1:])]
-    raise PrimitiveFinished(new_val)
-
     if 'value' not in a:
         a['value'], = yield [("RV", [a['id']])]
     if 'value' not in b:
@@ -171,100 +150,84 @@ def string_split(a, b, **remainder):
     raise PrimitiveFinished({'id': new_val})
 
 def string_get(a, b, **remainder):
-    a_value, b_value =  yield [("RV", [a]), ("RV", [b])]
-    result, = yield [("CNV", [a_value[b_value]])]
-    raise PrimitiveFinished(result)
+    if 'value' not in a:
+        a['value'], = yield [("RV", [a['id']])]
+    if 'value' not in b:
+        b['value'], = yield [("RV", [b['id']])]
+    raise PrimitiveFinished({'value': a['value'][b['value']]})
 
 def string_len(a, **remainder):
-    a_value, = yield [("RV", [a])]
-    result, = yield [("CNV", [len(a_value)])]
-    raise PrimitiveFinished(result)
+    if 'value' not in a:
+        a['value'], = yield [("RV", [a['id']])]
+    raise PrimitiveFinished({'value': len(a['value'])})
 
 def value_eq(a, b, **remainder):
-    a_value, b_value =  yield [("RV", [a]), ("RV", [b])]
-    result, = yield [("CNV", [a_value == b_value])]
-    raise PrimitiveFinished(result)
+    if 'value' not in a:
+        a['value'], = yield [("RV", [a['id']])]
+    if 'value' not in b:
+        b['value'], = yield [("RV", [b['id']])]
+    raise PrimitiveFinished({'value': a['value'] == b['value']})
 
 def element_eq(a, b, **remainder):
-    result, = yield [("CNV", [a == b])]
-    raise PrimitiveFinished(result)
+    if "id" not in a:
+        print("MATERIALIZING A element_eq")
+        if "value" in a:
+            a['id'], = yield [("CNV", [a['value']])]
+    if "id" not in b:
+        print("MATERIALIZING B element_eq")
+        if "value" in b:
+            b['id'], = yield [("CNV", [b['value']])]
+    raise PrimitiveFinished({'value': a['id'] == b['id']})
 
 def cast_string(a, **remainder):
-    a_value, = yield [("RV", [a])]
+    if 'value' not in a:
+        a['value'], = yield [("RV", [a['id']])]
     if isinstance(a_value, dict):
-        result, = yield [("CNV", [str(a_value["value"])])]
+        raise PrimitiveFinished({'value': str(a['value']['value'])})
     else:
-        result, = yield [("CNV", [str(a_value)])]
-    raise PrimitiveFinished(result)
+        raise PrimitiveFinished({'value': str(a['value'])})
 
 def cast_float(a, **remainder):
-    a_value, = yield [("RV", [a])]
-    result, = yield [("CNV", [float(a_value)])]
-    raise PrimitiveFinished(result)
+    if 'value' not in a:
+        a['value'], = yield [("RV", [a['id']])]
+    raise PrimitiveFinished({'value': float(a['value'])})
 
 def cast_boolean(a, **remainder):
-    a_value, = yield [("RV", [a])]
-    result, = yield [("CNV", [bool(a_value)])]
-    raise PrimitiveFinished(result)
+    if 'value' not in a:
+        a['value'], = yield [("RV", [a['id']])]
+    raise PrimitiveFinished({'value': bool(a['value'])})
 
 def cast_integer(a, **remainder):
-    a_value, = yield [("RV", [a])]
-    result, = yield [("CNV", [int(a_value)])]
-    raise PrimitiveFinished(result)
+    if 'value' not in a:
+        a['value'], = yield [("RV", [a['id']])]
+    raise PrimitiveFinished({'value': int(a['value'])})
 
 def cast_value(a, **remainder):
-    a_value, = yield [("RV", [a])]
+    if 'value' not in a:
+        a['value'], = yield [("RV", [a['id']])]
     if isinstance(a_value, dict):
-        # Action or type
-        value = a_value["value"]
+        raise PrimitiveFinished({'value': str(a['value']['value'])})
     else:
-        value = json.dumps(a_value)
-    result, = yield [("CNV", [value])]
-    raise PrimitiveFinished(result)
+        raise PrimitiveFinished({'value': json.dumps(a['value'])})
 
 def cast_id(a, **remainder):
-    result, = yield [("CNV", ["%s" % (a)])]
-    raise PrimitiveFinished(result)
-
-def list_insert(a, b, c, **remainder):
-    # TODO make non-primitive, though compiled
-    a_outgoing, c_value = yield [("RO", [a]), ("RV", [c])]
-    links = yield [("RD", [a, i]) for i in range(c_value, len(a_outgoing))] + \
-                  [("RDE", [a, i]) for i in range(c_value, len(a_outgoing))]
-
-    if sys.version_info[0] < 3:
-        values = links[:len(links)/2]
-        edges = links[len(links)/2:]
-    else:
-        values = links[:len(links) // 2]
-        edges = links[len(links) // 2:]
-
-    yield [("CD", [a, c_value, b])] + \
-          [("CD", [a, c_value + 1 + index, value]) for index, value in enumerate(values)] + \
-          [("DE", [i]) for i in edges]
-    raise PrimitiveFinished(a)
-
-def list_delete(a, b, **remainder):
-    # TODO make non-primitive, though compiled
-    a_outgoing, b_value = yield [("RO", [a]), ("RV", [b])]
-    links = yield [("RD", [a, i]) for i in range(b_value, len(a_outgoing))] + \
-                  [("RDE", [a, i]) for i in range(b_value, len(a_outgoing))]
-
-    if sys.version_info[0] < 3:
-        values = links[:len(links) / 2]
-        edges = links[len(links) / 2:]
-    else:
-        values = links[:len(links) // 2]
-        edges = links[len(links) // 2:]
-
-    yield [("CD", [a, b_value + index, value]) for index, value in enumerate(values[1:])] + \
-          [("DE", [i]) for i in edges]
-    raise PrimitiveFinished(a)
+    if "id" not in a:
+        print("MATERIALIZING A cast_id")
+        if "value" in a:
+            a['id'], = yield [("CNV", [a['value']])]
+        else:
+            a['id'], = yield [("CN", [])]
+    raise PrimitiveFinished({'value': str(a['id'])})
 
 def dict_add_fast(a, b, c, **remainder):
     # TODO deprecate, as dict_add is now also efficient
-    v, = yield [("RV", [b])]
-    yield [("CD", [a, v, c])]
+    if "value" not in b:
+        b['value'], = yield [("RV", [b['id']])]
+    if "id" not in c:
+        print("MATERIALIZING C dict_add_fast")
+        c['id'], = yield [("CNV", [c['value']])]
+
+    yield [("CD", [a['id'], b['value'], c['id']])]
     raise PrimitiveFinished(a)
 
 def dict_delete(a, b, **remainder):