actionlanguage.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. Action Language
  2. ===============
  3. Apart from simple attributes, such as the number of tokens on a PetriNet Place, code blocks are another frequent requirement.
  4. Current tools often do away with this by typing it as a string, and then passing it on internally to, for example, a Python interpreter.
  5. In this case, the code is nothing else than a string and there is no control whatsoever over the structure and semantics.
  6. Also, requiring an existing interpreter for that language, restricts the set of platforms for which a Modelverse can be implemented.
  7. In the Modelverse, we alleviate this problem by providing a built-in and explicitly modelled action language.
  8. This action language is primitive in the Modelverse, meaning that its abstract syntax tree can be represented, instead of just its textual representation.
  9. The abstract syntax tree is executed on-the-fly in the Modelverse, preventing the mapping to any other internal representation.
  10. The semantics of this language is also explicitly modelled, making it possible to automatically generate interpreters for any desired platform.
  11. In this section, we now focus on how to use this action language.
  12. As PetriNets are a bit too simple to require actions, we will use Finite State Automata as an example.
  13. .. warning::
  14. The current action language compiler is rather basic and contains quite some problems.
  15. Example: Finite State Automata
  16. ------------------------------
  17. In the context of Finite State Automata, it is often necessary to define actions on specific transitions.
  18. Upon execution of the transition, the associated action is executed.
  19. When defining a model, we can also upload code snippets as attribute values::
  20. >>> instantiate_attr_code("models/my_fsa", "transition_1", "action", open("actions/transition_1.alc", "r").read())
  21. This will assign the action stored in the file *actions/transition_1.alc* to the *action* attribute of the transition.
  22. Of course, it is not necessary to open a file for this, as the function itself could just as well be inlined.
  23. The only requirement on the value, is that it is a string that can be parsed as Action Language::
  24. >>> instantiate_attr_code("models/my_fsa", "transition_1", "action", \
  25. ... """
  26. ... include "primitives.alh"
  27. ... Void function action(model : Element):
  28. ... log("Executed action!")
  29. ... return!
  30. ... """
  31. Action language attributes are considered identical to any other attribute.
  32. The exception is of course that they are assigned using the code specific function, as otherwise we would have uploaded a string.
  33. Note the difference between the assignment of a string, and the assignment of action code::
  34. >>> instantiate_attr("models/my_fsa", "transition_1", "action", open("actions/transition_1.alc", "r").read())
  35. >>> instantiate_attr_code("models/my_fsa", "transition_1", "action", open("actions/transition_1.alc", "r").read())
  36. The first line assigns a string to the attribute, and is therefore not executable, nor is it parsed or considered in any way.
  37. The second line assigns code to the attribute, and is therefore executable, and parsed upon assignment.
  38. The signature of each function, in the case of our example this is "Void function action(model : Element)", depends on how the code will be used later on.
  39. Multi-function code
  40. -------------------
  41. Some code is ideally expressed using multiple functions.
  42. As the Modelverse does not support subfunctions, it supports the second best alternative: multiple functions can be defined.
  43. When multiple functions are defined, the *main* function will be executed, which can refer to any of the other functions defined in the same block.
  44. If no *main* function is found, the topmost function is executed.
  45. Language description
  46. --------------------
  47. Next we describe the basic constructs of the language, as offered by our compiler.
  48. The language is inspired by Python syntax, and highly resembles it.
  49. Nonetheless, it is much more minimal.
  50. In the remainder of this subsection, we go over the different language constructs.
  51. If
  52. ^^
  53. The IF construct is similar to that found in other languages.
  54. Its structure is as follows::
  55. if condition1:
  56. do_when_condition1
  57. elif condition2:
  58. do_when_condition2
  59. else:
  60. do_otherwise
  61. Each condition can be an expression.
  62. The code blocks can be any other kind of code.
  63. Just like Python, indentation is the only way to structure code.
  64. Unlike Python, only tabs are allowed (thus no space), and the number of tabs must be exactly one higher for a deeper block.
  65. The presence of an "elif" and "else" is optional.
  66. While
  67. ^^^^^
  68. The WHILE construct is similar to that found in other languages.
  69. Contrary to Python, there is no support for the "else" construct.
  70. Its structure is as follows::
  71. while condition:
  72. action
  73. Conditions and actions are similar to the If construct.
  74. Break
  75. ^^^^^
  76. The BREAK construct is similar to that found in other languages.
  77. Contrary to Python, it is followed by an exclamation mark to differentiate it from the variable *break*.
  78. Its structure is as follows::
  79. while condition:
  80. break!
  81. While the Modelverse supports breaking out of multiple loops simultaneously, this is not currently supported by the HUTN parser.
  82. Continue
  83. ^^^^^^^^
  84. The CONTINUE construct is similar to that found in other languages.
  85. Contrary to Python, it is followed by an exclamation mark to differentiate it from the variable *continue*.
  86. Its structure is as follows::
  87. while condition:
  88. continue!
  89. While the Modelverse supports continuing a higher loop directly, this is not currently supported by the HUTN parser.
  90. Return
  91. ^^^^^^
  92. The RETURN construct is again similar to how it is expected.
  93. To prevent ambiguity in the grammar, an exclamation mark should follow after the expression to return.
  94. Its structure is as follows::
  95. return expression!
  96. The expression can be any expression, similar to the condition in an If and While.
  97. When the function has returntype void, the expression must be empty::
  98. return!
  99. Function call
  100. ^^^^^^^^^^^^^
  101. Function calls happen like usual, by appending an opening and closing parenthesis at the end.
  102. Its structure is as follows::
  103. my_function(argument_a, argument_b)
  104. Arguments can again be any kind of expression.
  105. Named parameters are not supported in the grammar, though the Modelverse can internally handle them.
  106. Function definition
  107. ^^^^^^^^^^^^^^^^^^^
  108. Defining a function makes the function available as a variable in the remainder of the context.
  109. Forward declaration is unnecessary: all function names are retrieved before going over the body of the functions.
  110. This makes mutual recursion easy.
  111. A function needs to define its return type, as well as the type of all its parameters.
  112. In case the type is unknown, or can be anything, *Element* can be used as a kind of "void pointer".
  113. Its structure is as follows::
  114. Element function function_name(parameter_a : Integer):
  115. body_of_the_function
  116. First comes the returntype, followed by the keyword "function".
  117. Again, indentation needs to happen using tabs.
  118. Assignment
  119. ^^^^^^^^^^
  120. Assignment is like usual.
  121. Its structure is::
  122. a = expression
  123. This assumes that a is already defined.
  124. Declaration
  125. ^^^^^^^^^^^
  126. All variables used in the Modelverse need to be defined statically.
  127. Defining a variable reserves a part of the Modelverse State to hold the value for this value.
  128. As such, variables cannot be used if they are not defined.
  129. Additionally, a declared variable will have a type defined with it.
  130. Again, if the type can vary or is unknown, this can be *Element*.
  131. .. warning::
  132. Contrary to other languages, declaring a variable does not equal defining the variable.
  133. Therefore, after a variable is declared, it can be used, but its contents will be non-existing.
  134. It is wise to always assign a value to a declared value right after declaration!
  135. Its structure is as follows::
  136. String abc
  137. There are two kinds of declaration: local declaration, and global declaration.
  138. Local declarations happen in the current block of the code, and their references get removed when exiting the block.
  139. Global declarations have identically the same syntax, but are specified at the top level (*i.e.*, they have no tabs in front of them).
  140. Global declarations do not assign a value and are thus external references, to be defined in the future or by some other module.
  141. Sharing between modules is possible this way, as all global names are linked together when linking the application together.
  142. A global declaration that is never defined is flagged as invalid by the compiler and will prevent compilation.
  143. Definition
  144. ^^^^^^^^^^
  145. Defining a variable is similar to a global declaration, but now there is an immediate assignment.
  146. Immediate assignment is only possible at the top level.
  147. Note that the assignment must be of a constant which requires no execution of functions or such.
  148. Also, it is impossible to assign the value of another variable.
  149. Its structure is as follows::
  150. String abc = "abc"
  151. It is possible that a definition does not yet know the value to assign, or is an empty element.
  152. To notify the other files that this is the defining element, use the import syntax to assign *?* to it::
  153. Element abc = ?
  154. This results in the Modelverse creating an empty node as placeholder for a value.
  155. The value will now be defined and accessible.
  156. Imports
  157. ^^^^^^^
  158. Direct imports in the Modelverse are possible, but are not recommended for ordinary users.
  159. This syntax allows a variable to be bound to an absolute location in the Modelverse, which is resolved at compile time.
  160. The primary use case for this is the creation of bootstrap files, but it is also possible for other purposes.
  161. Use with care!
  162. Its structure is as follows::
  163. String abc = ?path/in/modelverse
  164. A special case is when the path is empty, which indicates that a new (anonymous) node should be created for it::
  165. Element abc = ?
  166. Include
  167. ^^^^^^^
  168. Other files can easily be included using the include syntax.
  169. This is a raw include: the contents of the file are just copied inside the file at that exact spot.
  170. Generally, you should only include *.alh* files, unless you know what you are doing.
  171. There is a set of standard header files included in the Modelverse, which are probably the only ones you will need.
  172. Constants
  173. ^^^^^^^^^
  174. The Modelverse supports a set of constants that can be used.
  175. All constants can be assigned in a definition of globals.
  176. ============= ========================================================================== =======
  177. Constant type Values Example
  178. ============= ========================================================================== =======
  179. Integer Any possible integer, either positive (no prefix) or negative (- prefix) 1
  180. Float Any possible floating point value; scientific notation is not supported 1.0
  181. Boolean Either True or False True
  182. String Any possible string, enclosed with either double or single quotes "abc"
  183. Action An action construct from the Modelverse, prefixed with an exclamation mark !If
  184. ============= ========================================================================== =======
  185. Operators
  186. ^^^^^^^^^
  187. While operators are seemingly supported by the compiler, these are actually expanded to function calls to relevant functions.
  188. For example, *1 + 2*, is expanded to *integer_addition(1, 2)*.
  189. To do this conversion, it is mandatory that the type of both arguments can be determined statically.
  190. User I/O
  191. ^^^^^^^^
  192. User input and output is done through the keyworded operations *input()* and *output(msg)*.
  193. *input()* returns the first message in the input queue of the current user, potentially blocking until there is input.
  194. *output(msg)* places the value of the expression in the output queue of the current user.
  195. All I/O is done using queues: the value is only read and written to a specific place in the Modelverse.
  196. To actually read or write it at the client side, these special places should be accessed through dedicated Modelverse operations.
  197. Primitive operations
  198. ^^^^^^^^^^^^^^^^^^^^
  199. Apart from the minimal syntax, the Modelverse supports a wide library of functions.
  200. These functions are all built on top of the previously defined constructs.
  201. A list of these functions is shown below.
  202. * *Boolean function value_eq(a: Element, b: Element)*
  203. * *Boolean function value_neq(a: Element, b: Element)*
  204. * *Boolean function bool_and(a: Boolean, b: Boolean)*
  205. * *Boolean function bool_or(a: Boolean, b: Boolean)*
  206. * *Boolean function bool_not(a: Boolean)*
  207. * *Element function create_node()*
  208. * *Element function create_edge(a: Element, b: Element)*
  209. * *Element function create_value(a: Element)*
  210. * *Boolean function is_edge(a: Element)*
  211. * *Integer function read_nr_out(a: Element)*
  212. * *Element function read_out(a: Element, b: Integer)*
  213. * *Integer function read_nr_in(a: Element)*
  214. * *Element function read_in(a: Element, b: Integer)*
  215. * *Element function read_edge_src(a: Element)*
  216. * *Element function read_edge_dst(a: Element)*
  217. * *Boolean function delete_element(a: Element)*
  218. * *Boolean function element_eq(a: Element, b: Element)*
  219. * *Boolean function element_neq(a: Element, b: Element)*
  220. * *Float function cast_float(a: Element)*
  221. * *String function cast_string(a: Element)*
  222. * *Boolean function cast_boolean(a: Element)*
  223. * *Integer function cast_integer(a: Element)*
  224. * *String function cast_id(a: Element)*
  225. * *String function cast_value(a: Element)*
  226. * *Element function dict_add(a: Element, b: Element, c: Element)*
  227. * *Element function dict_add_fast(a: Element, b: Element, c: Element)*
  228. * *Element function dict_delete(a: Element, b: Element)*
  229. * *Element function dict_delete_node(a: Element, b: Element)*
  230. * *Element function dict_read(a: Element, b: Element)*
  231. * *Element function dict_read_edge(a: Element, b: Element)*
  232. * *Element function dict_read_node(a: Element, b: Element)*
  233. * *Integer function dict_len(a: Element)*
  234. * *Boolean function dict_in(a: Element, b: Element)*
  235. * *Boolean function dict_in_node(a: Element, b: Element)*
  236. * *Element function dict_keys(a: Element)*
  237. * *Float function float_addition(a: Float, b: Float)*
  238. * *Float function float_subtraction(a: Float, b: Float)*
  239. * *Float function float_multiplication(a: Float, b: Float)*
  240. * *Float function float_division(a: Float, b: Float)*
  241. * *Float function float_neg(a: Float)*
  242. * *Boolean function float_gt(a: Float, b: Float)*
  243. * *Boolean function float_gte(a: Float, b: Float)*
  244. * *Boolean function float_lt(a: Float, b: Float)*
  245. * *Boolean function float_lte(a: Float, b: Float)*
  246. * *Integer function integer_addition(a: Integer, b: Integer)*
  247. * *Integer function integer_subtraction(a: Integer, b: Integer)*
  248. * *Integer function integer_multiplication(a: Integer, b: Integer)*
  249. * *Integer function integer_division(a: Integer, b: Integer)*
  250. * *Integer function integer_modulo(a: Integer, b: Integer)*
  251. * *Boolean function integer_gt(a: Integer, b: Integer)*
  252. * *Boolean function integer_gte(a: Integer, b: Integer)*
  253. * *Boolean function integer_lt(a: Integer, b: Integer)*
  254. * *Boolean function integer_lte(a: Integer, b: Integer)*
  255. * *Boolean function integer_neg(a: Integer)*
  256. * *Element function list_read(a: Element, b: Integer)*
  257. * *Element function list_append(a: Element, b: Element)*
  258. * *Element function list_insert(a: Element, b: Element, c: Integer)*
  259. * *Element function list_delete(a: Element, b: Integer)*
  260. * *Integer function list_len(a: Element)*
  261. * *Boolean function list_in(a : Element, b : Element)*
  262. * *Element function set_add(a: Element, b: Element)*
  263. * *Element function set_add_node(a: Element, b: Element)*
  264. * *Element function set_pop(a: Element)*
  265. * *Element function set_read(a : Element)*
  266. * *Element function set_remove(a: Element, b: Element)*
  267. * *Boolean function set_in(a: Element, b: Element)*
  268. * *Element function set_remove_node(a: Element, b: Element)*
  269. * *Element function set_in_node(a: Element, b: Element)*
  270. * *Integer function set_len(a: Element)*
  271. * *String function string_join(a: String, b: String)*
  272. * *String function string_get(a: String, b: Integer)*
  273. * *String function string_substr(a: String, b: Integer, c: Integer)*
  274. * *Integer function string_len(a: String)*
  275. * *Element function string_split(a: String, b: String)*
  276. * *Boolean function string_startswith(a: String, b: String)*
  277. * *String function log(a: String)*
  278. * *Element function read_root()*
  279. * *Element function read_taskroot()*
  280. * *Element function input()*
  281. * *Element function output(a : Element)*
  282. * *Boolean function is_physical_int(a : Element)*
  283. * *Boolean function is_physical_float(a : Element)*
  284. * *Boolean function is_physical_string(a : Element)*
  285. * *Boolean function is_physical_action(a : Element)*
  286. * *Boolean function is_physical_boolean(a : Element)*
  287. * *Boolean function is_physical_none(a : Element)*
  288. * *Boolean function is_error(a : Element)*
  289. * *Boolean function has_value(a : Element)*
  290. * *Float function time()*
  291. * *String function hash(a : String)*
  292. * *Void function sleep(a : Float)*
  293. * *Void function interruptable_sleep(a : Float)*
  294. * *Element function exec(a : Element)*
  295. * *Element function resolve(var_name : String)*
  296. * *Boolean function has_input()*
  297. * *Element function list_pop(lst : Element, index : Integer)*
  298. * *Element function list_pop_final(lst : Element)*
  299. * *Element function set_copy(set : Element)*
  300. * *String function set_to_string(set : Element)*
  301. * *String function list_to_string(set : Element)*
  302. * *String function dict_to_string(dict : Element)*
  303. * *Element function set_overlap(sa : Element, sb : Element)*
  304. * *Element function set_equality(sa : Element, sb : Element)*
  305. * *Element function dict_eq(da : Element, db : Element)*
  306. * *Element function dict_copy(dict : Element)*
  307. * *Element function set_to_list(s : Element)*
  308. * *Element function create_tuple(a : Element, b : Element)*
  309. * *Void function dict_overwrite(a : Element, b : Element, c : Element)*
  310. * *Void function set_merge(sa : Element, sb : Element)*
  311. * *Element function make_reverse_dictionary(dict : Element)*
  312. * *Element function set_create()*
  313. * *Element function list_create()*
  314. * *Element function dict_create()*
  315. * *String function reverseKeyLookup(a: Element, b: Element)*
  316. * *Element function reverseKeyLookupMulti(a: Element, b: Element)*
  317. * *Element function dict_values(dict : Element)*