primitives.alc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. Boolean function value_eq(a: Element, b: Element) = ?primitives/value_eq
  2. Boolean function value_neq(a: Element, b: Element) = ?primitives/value_neq
  3. Boolean function bool_and(a: Boolean, b: Boolean) = ?primitives/bool_and
  4. Boolean function bool_or(a: Boolean, b: Boolean) = ?primitives/bool_or
  5. Boolean function bool_not(a: Boolean) = ?primitives/bool_not
  6. Element function create_node() = ?primitives/create_node
  7. Element function create_edge(a: Element, b: Element) = ?primitives/create_edge
  8. Element function create_value(a: Element) = ?primitives/create_value
  9. Boolean function is_edge(a: Element) = ?primitives/is_edge
  10. Integer function read_nr_out(a: Element) = ?primitives/read_nr_out
  11. Element function read_out(a: Element, b: Integer) = ?primitives/read_out
  12. Integer function read_nr_in(a: Element) = ?primitives/read_nr_in
  13. Element function read_in(a: Element, b: Integer) = ?primitives/read_in
  14. Element function read_edge_src(a: Element) = ?primitives/read_edge_src
  15. Element function read_edge_dst(a: Element) = ?primitives/read_edge_dst
  16. Boolean function delete_element(a: Element) = ?primitives/delete_element
  17. Boolean function element_eq(a: Element, b: Element) = ?primitives/element_eq
  18. Boolean function element_neq(a: Element, b: Element) = ?primitives/element_neq
  19. Float function cast_i2f(a: Integer) = ?primitives/cast_i2f
  20. String function cast_i2s(a: Integer) = ?primitives/cast_i2s
  21. Boolean function cast_i2b(a: Integer) = ?primitives/cast_i2b
  22. Integer function cast_f2i(a: Float) = ?primitives/cast_f2i
  23. Boolean function cast_f2b(a: Float) = ?primitives/cast_f2b
  24. String function cast_f2s(a: Float) = ?primitives/cast_f2s
  25. Integer function cast_s2i(a: String) = ?primitives/cast_s2i
  26. Float function cast_s2f(a: String) = ?primitives/cast_s2f
  27. Boolean function cast_s2b(a: String) = ?primitives/cast_s2b
  28. Integer function cast_b2i(a: Boolean) = ?primitives/cast_b2i
  29. Float function cast_b2f(a: Boolean) = ?primitives/cast_b2f
  30. String function cast_b2s(a: Boolean) = ?primitives/cast_b2s
  31. String function cast_e2s(a: Element) = ?primitives/cast_e2s
  32. String function cast_a2s(a: Action) = ?primitives/cast_a2s
  33. String function cast_v2s(a: Element) = ?primitives/cast_v2s
  34. String function cast_id2s(a: Element) = ?primitives/cast_id2s
  35. Element function dict_add(a: Element, b: Element, c: Element) = ?primitives/dict_add
  36. Element function dict_delete(a: Element, b: Element) = ?primitives/dict_delete
  37. Element function dict_read(a: Element, b: Element) = ?primitives/dict_read
  38. Element function dict_read_edge(a: Element, b: Element) = ?primitives/dict_read_edge
  39. Element function dict_read_node(a: Element, b: Element) = ?primitives/dict_read_node
  40. Integer function dict_len(a: Element) = ?primitives/dict_len
  41. Boolean function dict_in(a: Element, b: Element) = ?primitives/dict_in
  42. Boolean function dict_in_node(a: Element, b: Element) = ?primitives/dict_in_node
  43. Element function dict_keys(a: Element) = ?primitives/dict_keys
  44. Float function float_addition(a: Float, b: Float) = ?primitives/float_addition
  45. Float function float_subtraction(a: Float, b: Float) = ?primitives/float_subtraction
  46. Float function float_multiplication(a: Float, b: Float) = ?primitives/float_multiplication
  47. Float function float_division(a: Float, b: Float) = ?primitives/float_division
  48. Boolean function float_gt(a: Float, b: Float) = ?primitives/float_gt
  49. Boolean function float_lt(a: Float, b: Float) = ?primitives/float_lt
  50. Boolean function float_neg(a: Float) = ?primitives/float_neg
  51. Integer function integer_addition(a: Integer, b: Integer) = ?primitives/integer_addition
  52. Integer function integer_subtraction(a: Integer, b: Integer) = ?primitives/integer_subtraction
  53. Integer function integer_multiplication(a: Integer, b: Integer) = ?primitives/integer_multiplication
  54. Integer function integer_division(a: Integer, b: Integer) = ?primitives/integer_division
  55. Boolean function integer_gt(a: Integer, b: Integer) = ?primitives/integer_gt
  56. Boolean function integer_lt(a: Integer, b: Integer) = ?primitives/integer_lt
  57. Boolean function integer_neg(a: Integer) = ?primitives/integer_neg
  58. Element function list_read(a: Element, b: Integer) = ?primitives/list_read
  59. Element function list_append(a: Element, b: Element) = ?primitives/list_append
  60. Element function list_insert(a: Element, b: Element, c: Integer) = ?primitives/list_insert
  61. Element function list_delete(a: Element, b: Integer) = ?primitives/list_delete
  62. Integer function list_len(a: Element) = ?primitives/list_len
  63. Element function set_add(a: Element, b: Element) = ?primitives/set_add
  64. Element function set_pop(a: Element) = ?primitives/set_pop
  65. Element function set_remove(a: Element, b: Element) = ?primitives/set_remove
  66. Boolean function set_in(a: Element, b: Element) = ?primitives/set_in
  67. Element function set_remove_node(a: Element, b: Element) = ?primitives/set_remove_node
  68. Element function set_in_node(a: Element, b: Element) = ?primitives/set_in_node
  69. String function string_join(a: String, b: String) = ?primitives/string_join
  70. String function string_get(a: String, b: Integer) = ?primitives/string_get
  71. Element function string_split(a: String, b: String) = ?primitives/string_split
  72. Integer function string_len(a: String) = ?primitives/string_len
  73. Element function deserialize(a: String) = ?primitives/deserialize
  74. Element function log(a: String) = ?primitives/log
  75. Element function read_root() = ?primitives/read_root
  76. Boolean function is_physical_int(a: Element) = ?primitives/is_physical_int
  77. Boolean function is_physical_float(a: Element) = ?primitives/is_physical_float
  78. Boolean function is_physical_string(a: Element) = ?primitives/is_physical_string
  79. Boolean function is_physical_boolean(a: Element) = ?primitives/is_physical_boolean
  80. Boolean function is_physical_action(a: Element) = ?primitives/is_physical_action
  81. Element function dict_reverse(a: Element, b: Element) = ?primitives/dict_reverse
  82. Element function exec(first_instr : Element):
  83. // This does very ugly things, so beware!
  84. // Basically, we dynamically construct an if True condition with as body the provided instructions
  85. // after the if conditional, we append a return of an empty element, as we need a return at the end
  86. // returns in the code are therefore allowed (and will be the return value), but not necessarily
  87. Element n
  88. Element exec_if
  89. Element exec_const_true
  90. Element exec_return
  91. n = create_node()
  92. exec_if = create_value(!if)
  93. exec_const_true = create_value(!constant)
  94. exec_return = create_value(!return)
  95. dict_add(n, "params", create_node())
  96. dict_add(exec_const_true, "node", create_value(True))
  97. dict_add(exec_if, "cond", exec_const_true)
  98. dict_add(exec_if, "then", first_instr)
  99. dict_add(n, "body", exec_if)
  100. dict_add(exec_if, "next", exec_return)
  101. return n()
  102. Boolean function string_startswith(a: String, b: String):
  103. Integer i
  104. i = 0
  105. if (string_len(b) > string_len(a)):
  106. return False
  107. while (i < string_len(b)):
  108. if (string_get(a, i) != string_get(b, i)):
  109. return False
  110. i = i + 1
  111. return True
  112. Boolean function has_value(a: Element):
  113. 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)))
  114. Boolean function float_gte(a: Float, b: Float):
  115. return bool_or(float_gt(a, b), value_eq(a, b))
  116. Boolean function float_lte(a: Float, b: Float):
  117. return bool_or(float_lt(a, b), value_eq(a, b))
  118. Boolean function integer_lte(a: Integer, b: Integer):
  119. return bool_or(integer_lt(a, b), value_eq(a, b))
  120. Boolean function integer_gte(a: Integer, b: Integer):
  121. return bool_or(integer_gt(a, b), value_eq(a, b))
  122. // TODO: implement these explicitly and split off this code from the primitives completely
  123. String function string_substr(a: String, b: Integer, c: Integer):
  124. String result
  125. Integer i
  126. // First handle corner cases!
  127. // If the string is too short for b to even start, return empty
  128. if (b > string_len(a)):
  129. return ""
  130. // If the part we want to snip is negative, we return empty
  131. if (b > c):
  132. return ""
  133. i = 0
  134. result = ""
  135. while (i < string_len(a)):
  136. if (bool_and(i > b, i < c)):
  137. result = result + string_get(a, i)
  138. if (i > c):
  139. return result
  140. return result