Kaynağa Gözat

Wrote some primitive functions explicit in action language

Yentl Van Tendeloo 9 yıl önce
ebeveyn
işleme
b55afb435b
1 değiştirilmiş dosya ile 37 ekleme ve 8 silme
  1. 37 8
      bootstrap/primitives.alc

+ 37 - 8
bootstrap/primitives.alc

@@ -68,6 +68,7 @@ Element function set_remove_node(a: Element, b: Element) = ?primitives/set_remov
 Element function set_in_node(a: Element, b: Element) = ?primitives/set_in_node
 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
 Integer function string_len(a: String) = ?primitives/string_len
 Element function deserialize(a: String) = ?primitives/deserialize
 Element function log(a: String) = ?primitives/log
@@ -102,7 +103,6 @@ Element function exec(first_instr : Element):
 
 	return n()
 
-//Boolean function string_startswith(a: String, b: String) = ?primitives/string_startswith
 Boolean function string_startswith(a: String, b: String):
 	Integer i
 	i = 0
@@ -116,14 +116,43 @@ Boolean function string_startswith(a: String, b: String):
 	
 	return True
 
-//Boolean function has_value(a: Element) = ?primitives/has_value
 Boolean function has_value(a: Element):
 	return bool_or(bool_or(bool_or(is_physical_action, is_physical_int(a)), is_physical_float(a)), bool_or(is_physical_string(a), is_physical_boolean(a)))
 
+Boolean function float_gte(a: Float, b: Float):
+	return bool_or(float_gt(a, b), value_eq(a, b))
+
+Boolean function float_lte(a: Float, b: Float):
+	return bool_or(float_lt(a, b), value_eq(a, b))
+
+Boolean function integer_lte(a: Integer, b: Integer):
+	return bool_or(integer_lt(a, b), value_eq(a, b))
+	
+Boolean function integer_gte(a: Integer, b: Integer):
+	return bool_or(integer_gt(a, b), value_eq(a, b))
+
 // TODO: implement these explicitly and split off this code from the primitives completely
-Boolean function float_gte(a: Float, b: Float) = ?primitives/float_gte
-Boolean function float_lte(a: Float, b: Float) = ?primitives/float_lte
-Boolean function integer_lte(a: Integer, b: Integer) = ?primitives/integer_lte
-Boolean function integer_gte(a: Integer, b: Integer) = ?primitives/integer_gte
-String function string_substr(a: String, b: Integer, c: Integer) = ?primitives/string_substr
-Element function string_split(a: String, b: String) = ?primitives/string_split
+String function string_substr(a: String, b: Integer, c: Integer):
+	String result
+	Integer i
+
+	// First handle corner cases!
+
+	// If the string is too short for b to even start, return empty
+	if (b > string_len(a)):
+		return ""
+
+	// If the part we want to snip is negative, we return empty
+	if (b > c):
+		return ""
+
+	i = 0
+	result = ""
+	while (i < string_len(a)):
+		if (bool_and(i > b, i < c)):
+			result = result + string_get(a, i)
+
+		if (i > c):
+			return result
+	return result
+