actionlanguage.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Action Language &#8212; Modelverse 0.4.0 documentation</title>
  7. <link rel="stylesheet" href="_static/classic.css" type="text/css" />
  8. <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  9. <script type="text/javascript">
  10. var DOCUMENTATION_OPTIONS = {
  11. URL_ROOT: './',
  12. VERSION: '0.4.0',
  13. COLLAPSE_INDEX: false,
  14. FILE_SUFFIX: '.html',
  15. HAS_SOURCE: true
  16. };
  17. </script>
  18. <script type="text/javascript" src="_static/jquery.js"></script>
  19. <script type="text/javascript" src="_static/underscore.js"></script>
  20. <script type="text/javascript" src="_static/doctools.js"></script>
  21. <link rel="top" title="Modelverse 0.4.0 documentation" href="index.html" />
  22. <link rel="next" title="Modelling Language" href="modellanguage.html" />
  23. <link rel="prev" title="How to run" href="howto.html" />
  24. </head>
  25. <body role="document">
  26. <div class="related" role="navigation" aria-label="related navigation">
  27. <h3>Navigation</h3>
  28. <ul>
  29. <li class="right" style="margin-right: 10px">
  30. <a href="genindex.html" title="General Index"
  31. accesskey="I">index</a></li>
  32. <li class="right" >
  33. <a href="modellanguage.html" title="Modelling Language"
  34. accesskey="N">next</a> |</li>
  35. <li class="right" >
  36. <a href="howto.html" title="How to run"
  37. accesskey="P">previous</a> |</li>
  38. <li class="nav-item nav-item-0"><a href="index.html">Modelverse 0.4.0 documentation</a> &#187;</li>
  39. </ul>
  40. </div>
  41. <div class="document">
  42. <div class="documentwrapper">
  43. <div class="bodywrapper">
  44. <div class="body" role="main">
  45. <div class="section" id="action-language">
  46. <h1>Action Language<a class="headerlink" href="#action-language" title="Permalink to this headline">¶</a></h1>
  47. <p>The primary language of the Modelverse, is its action language.
  48. This language serves as a nicer representation of the underlying Modelverse Graph Language, which is stored as a graph.
  49. The action language gets parsed by the parser, resulting in a kind of abstract syntax graph.
  50. This abstract syntax graph highly resembles an executable graph in the Modelverse Graph Language.</p>
  51. <p>Users will mainly use this language to write actions and constraints, but maybe also to implement their own interfaces, or override core functionality of the Modelverse.</p>
  52. <div class="admonition warning">
  53. <p class="first admonition-title">Warning</p>
  54. <p class="last">The current parser is unable to handle whitespaces in a general way.
  55. As such, all indentation needs to be done using tabs, and the difference needs to be exactly 1.
  56. This is different from Python, where tabs and whitespaces can be mixed, and the number can even vary.</p>
  57. </div>
  58. <div class="section" id="files">
  59. <h2>Files<a class="headerlink" href="#files" title="Permalink to this headline">¶</a></h2>
  60. <p>There are two kinds of files: code files, and headers.
  61. This distinction is similar to C and related languages.
  62. Code in header files will always be copied throughout all including files.
  63. As such, it is a good idea to have header files which declare the signatures of functions, but leave the actual implementation to the code files.
  64. Code files do not need to import the headers they are implementing: all variable names are automatically shared.</p>
  65. </div>
  66. <div class="section" id="language-description">
  67. <h2>Language description<a class="headerlink" href="#language-description" title="Permalink to this headline">¶</a></h2>
  68. <p>The language is inspired by Python syntax, and highly resembles it.
  69. Nonetheless, it is a lot simpler.
  70. In the remainder of this subsection, we go over the different language constructs.</p>
  71. <div class="section" id="if">
  72. <h3>If<a class="headerlink" href="#if" title="Permalink to this headline">¶</a></h3>
  73. <p>The IF construct is similar to that found in other languages.
  74. Its structure is as follows:</p>
  75. <div class="highlight-default"><div class="highlight"><pre><span class="k">if</span> <span class="n">condition1</span><span class="p">:</span>
  76. <span class="n">do_when_condition1</span>
  77. <span class="k">elif</span> <span class="n">condition2</span><span class="p">:</span>
  78. <span class="n">do_when_condition2</span>
  79. <span class="k">else</span><span class="p">:</span>
  80. <span class="n">do_otherwise</span>
  81. </pre></div>
  82. </div>
  83. <p>Each condition can be an expression.
  84. The code blocks can be any other kind of code.
  85. Just like Python, indentation is the only way to structure code.
  86. Unlike Python, only tabs are allowed (thus no space), and the number of tabs must be exactly one higher for a deeper block.</p>
  87. <p>The presence of an &#8220;elif&#8221; and &#8220;else&#8221; is optional.</p>
  88. </div>
  89. <div class="section" id="while">
  90. <h3>While<a class="headerlink" href="#while" title="Permalink to this headline">¶</a></h3>
  91. <p>The WHILE construct is similar to that found in other languages.
  92. Contrary to Python, there is no support for the &#8220;else&#8221; construct.
  93. Its structure is as follows:</p>
  94. <div class="highlight-default"><div class="highlight"><pre><span class="k">while</span> <span class="n">condition</span><span class="p">:</span>
  95. <span class="n">action</span>
  96. </pre></div>
  97. </div>
  98. <p>Conditions and actions are similar to the If construct.</p>
  99. </div>
  100. <div class="section" id="break">
  101. <h3>Break<a class="headerlink" href="#break" title="Permalink to this headline">¶</a></h3>
  102. <p>Whereas this is supported in the Modelverse, the parser currently does not consider this keyword.</p>
  103. </div>
  104. <div class="section" id="continue">
  105. <h3>Continue<a class="headerlink" href="#continue" title="Permalink to this headline">¶</a></h3>
  106. <p>Whereas this is supported in the Modelverse, the parser currently does not consider this keyword.</p>
  107. </div>
  108. <div class="section" id="return">
  109. <h3>Return<a class="headerlink" href="#return" title="Permalink to this headline">¶</a></h3>
  110. <p>The RETURN construct is again similar to how it is expected.
  111. To prevent ambiguity in the grammar, an exclamation mark should follow after the expression to return.
  112. Its structure is as follows:</p>
  113. <div class="highlight-default"><div class="highlight"><pre>return expression!
  114. </pre></div>
  115. </div>
  116. <p>The expression can be any expression, similar to the condition in an If and While.</p>
  117. </div>
  118. <div class="section" id="function-call">
  119. <h3>Function call<a class="headerlink" href="#function-call" title="Permalink to this headline">¶</a></h3>
  120. <p>Function calls happen like usual, by appending an opening and closing parenthesis at the end.
  121. Its structure is as follows:</p>
  122. <div class="highlight-default"><div class="highlight"><pre><span class="n">my_function</span><span class="p">(</span><span class="n">argument_a</span><span class="p">,</span> <span class="n">argument_b</span><span class="p">)</span>
  123. </pre></div>
  124. </div>
  125. <p>Arguments can again be any kind of expression.
  126. Named parameters are not supported in the grammar, though the Modelverse can internally handle them.</p>
  127. </div>
  128. <div class="section" id="function-definition">
  129. <h3>Function definition<a class="headerlink" href="#function-definition" title="Permalink to this headline">¶</a></h3>
  130. <p>Defining a function makes the function available as a variable in the remainder of the context.
  131. Forward declaration is unnecessary: all function names are retrieved before going over the body of the functions.
  132. This makes mutual recursion easy.</p>
  133. <p>A function needs to define its return type, as well as the type of all its parameters.
  134. In case the type is unknown, or can be anything, <em>Element</em> can be used as a kind of &#8220;void pointer&#8221;.
  135. Its structure is as follows:</p>
  136. <div class="highlight-default"><div class="highlight"><pre><span class="n">Element</span> <span class="n">function</span> <span class="n">function_name</span><span class="p">(</span><span class="n">parameter_a</span> <span class="p">:</span> <span class="n">Integer</span><span class="p">):</span>
  137. <span class="n">body_of_the_function</span>
  138. </pre></div>
  139. </div>
  140. <p>First comes the returntype, followed by the keyword &#8220;function&#8221;.
  141. Again, indentation needs to happen using tabs.</p>
  142. </div>
  143. <div class="section" id="assignment">
  144. <h3>Assignment<a class="headerlink" href="#assignment" title="Permalink to this headline">¶</a></h3>
  145. <p>Assignment is like usual.
  146. Its structure is:</p>
  147. <div class="highlight-default"><div class="highlight"><pre><span class="n">a</span> <span class="o">=</span> <span class="n">expression</span>
  148. </pre></div>
  149. </div>
  150. <p>This assumes that a is already defined.</p>
  151. </div>
  152. <div class="section" id="declaration">
  153. <h3>Declaration<a class="headerlink" href="#declaration" title="Permalink to this headline">¶</a></h3>
  154. <p>All variables used in the Modelverse need to be defined statically.
  155. Defining a variable reserves a part of the Modelverse State to hold the value for this value.
  156. As such, variables cannot be used if they are not defined.
  157. Additionally, a declared variable will have a type defined with it.
  158. Again, if the type can vary or is unknown, this can be <em>Element</em>.</p>
  159. <div class="admonition warning">
  160. <p class="first admonition-title">Warning</p>
  161. <p class="last">Contrary to other languages, declaring a variable does not equal defining the variable.
  162. Therefore, after a variable is declared, it can be used, but its contents will be non-existing.
  163. It is wise to always assign a value to a declared value right after declaration!</p>
  164. </div>
  165. <p>Its structure is as follows:</p>
  166. <div class="highlight-default"><div class="highlight"><pre><span class="n">String</span> <span class="n">abc</span>
  167. </pre></div>
  168. </div>
  169. <p>There are two kinds of declaration: local declaration, and global declaration.
  170. Local declarations happen in the current block of the code, and their references get removed when exiting the block.
  171. Global declarations have identically the same syntax, but are specified at the top level (<em>i.e.</em>, they have no tabs in front of them).
  172. Global declarations do not assign a value and are thus external references, to be defined in the future or by some other module.
  173. Sharing between modules is possible this way, as all global names are linked together when linking the application together.
  174. A global declaration that is never defined is flagged as invalid by the compiler and will prevent compilation.</p>
  175. </div>
  176. <div class="section" id="definition">
  177. <h3>Definition<a class="headerlink" href="#definition" title="Permalink to this headline">¶</a></h3>
  178. <p>Defining a variable is similar to a global declaration, but now there is an immediate assignment.
  179. Immediate assignment is only possible at the top level.
  180. Note that the assignment must be of a constant which requires no execution of functions or such.
  181. Also, it is impossible to assign the value of another variable.</p>
  182. <p>Its structure is as follows:</p>
  183. <div class="highlight-default"><div class="highlight"><pre><span class="n">String</span> <span class="n">abc</span> <span class="o">=</span> <span class="s">&quot;abc&quot;</span>
  184. </pre></div>
  185. </div>
  186. <p>It is possible that a definition does not yet know the value to assign, or is an empty element.
  187. To notify the other files that this is the defining element, use the import syntax to assign <em>?</em> to it:</p>
  188. <div class="highlight-default"><div class="highlight"><pre>Element abc = ?
  189. </pre></div>
  190. </div>
  191. <p>This results in the Modelverse creating an empty node as placeholder for a value.
  192. The value will now be defined and accessible.</p>
  193. </div>
  194. <div class="section" id="imports">
  195. <h3>Imports<a class="headerlink" href="#imports" title="Permalink to this headline">¶</a></h3>
  196. <p>Direct imports in the Modelverse are possible, but are not recommended for ordinary users.
  197. This syntax allows a variable to be bound to an absolute location in the Modelverse, which is resolved at compile time.
  198. The primary use case for this is the creation of bootstrap files, but it is also possible for other purposes.
  199. Use with care!</p>
  200. <p>Its structure is as follows:</p>
  201. <div class="highlight-default"><div class="highlight"><pre>String abc = ?path/in/modelverse
  202. </pre></div>
  203. </div>
  204. <p>A special case is when the path is empty, which indicates that a new (anonymous) node should be created for it:</p>
  205. <div class="highlight-default"><div class="highlight"><pre>Element abc = ?
  206. </pre></div>
  207. </div>
  208. </div>
  209. <div class="section" id="include">
  210. <h3>Include<a class="headerlink" href="#include" title="Permalink to this headline">¶</a></h3>
  211. <p>Other files can easily be included using the include syntax.
  212. This is a raw include: the contents of the file are just copied inside the file at that exact spot.
  213. Generally, you should only include <em>.alh</em> files, unless you know what you are doing.</p>
  214. </div>
  215. <div class="section" id="constants">
  216. <h3>Constants<a class="headerlink" href="#constants" title="Permalink to this headline">¶</a></h3>
  217. <p>The Modelverse supports a set of constants that can be used.
  218. All constants can be assigned in a definition of globals.</p>
  219. <table border="1" class="docutils">
  220. <colgroup>
  221. <col width="14%" />
  222. <col width="79%" />
  223. <col width="7%" />
  224. </colgroup>
  225. <thead valign="bottom">
  226. <tr class="row-odd"><th class="head">Constant type</th>
  227. <th class="head">Values</th>
  228. <th class="head">Example</th>
  229. </tr>
  230. </thead>
  231. <tbody valign="top">
  232. <tr class="row-even"><td>Integer</td>
  233. <td>Any possible integer, either positive (no prefix) or negative (- prefix)</td>
  234. <td>1</td>
  235. </tr>
  236. <tr class="row-odd"><td>Float</td>
  237. <td>Any possible floating point value; scientific notation is not supported</td>
  238. <td>1.0</td>
  239. </tr>
  240. <tr class="row-even"><td>Boolean</td>
  241. <td>Either True or False</td>
  242. <td>True</td>
  243. </tr>
  244. <tr class="row-odd"><td>String</td>
  245. <td>Any possible string, enclosed with either double or single quotes</td>
  246. <td>&#8220;abc&#8221;</td>
  247. </tr>
  248. <tr class="row-even"><td>Action</td>
  249. <td>An action construct from the Modelverse, prefixed with an exclamation mark</td>
  250. <td>!If</td>
  251. </tr>
  252. </tbody>
  253. </table>
  254. </div>
  255. <div class="section" id="operators">
  256. <h3>Operators<a class="headerlink" href="#operators" title="Permalink to this headline">¶</a></h3>
  257. <p>While operators are seemingly supported by the compiler, these are actually expanded to function calls to relevant functions.
  258. For example, <em>1 + 2</em>, is expanded to <em>integer_addition(1, 2)</em></p>
  259. </div>
  260. <div class="section" id="user-i-o">
  261. <h3>User I/O<a class="headerlink" href="#user-i-o" title="Permalink to this headline">¶</a></h3>
  262. <p>User input and output is done through the keyworded operations <em>input()</em> and <em>output(msg)</em>.
  263. <em>input()</em> returns the first message in the input queue of the current user.
  264. <em>output(msg)</em> places the value of the expression in the output queue of the current user.</p>
  265. <p>All I/O is done using queues: the value is only read and written to a specific place in the Modelverse.
  266. To actually read or write it at the client side, these special places should be accessed through dedicated Modelverse operations.</p>
  267. </div>
  268. </div>
  269. <div class="section" id="examples">
  270. <h2>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2>
  271. <p>We now show some simple code fragments written in valid Action Language code.
  272. As this code is directly executable in the Modelverse, the main function is always called &#8220;main&#8221; and all includes are also present.
  273. We do not currently handle how to execute this code: this is explained further on.</p>
  274. <div class="section" id="fibonacci">
  275. <h3>Fibonacci<a class="headerlink" href="#fibonacci" title="Permalink to this headline">¶</a></h3>
  276. <p>The Fibonacci code is fairly simple.
  277. First, the primitives are imported to make sure that we know about all operations on primitives.
  278. Then, a function <em>fib</em> is defined, which will recursively compute the specified Fibonacci number.
  279. This is wrapped by a <em>main</em> function, which takes input from the user and outputs the result of the <em>fib</em> function.</p>
  280. <p>This code looks as follows:</p>
  281. <div class="highlight-default"><div class="highlight"><pre>include &quot;primitives.alh&quot;
  282. Integer function fib(param : Integer):
  283. if (param &lt;= 2):
  284. return 1!
  285. else:
  286. return fib(param - 1) + fib(param - 2)!
  287. Void function main():
  288. while(True):
  289. output(fib(input()))
  290. </pre></div>
  291. </div>
  292. </div>
  293. <div class="section" id="factorial">
  294. <h3>Factorial<a class="headerlink" href="#factorial" title="Permalink to this headline">¶</a></h3>
  295. <p>Similarly, the code for computing a factorial is given below:</p>
  296. <div class="highlight-default"><div class="highlight"><pre>include &quot;primitives.alh&quot;
  297. Integer function factorial(n : Integer):
  298. if(n &lt;= 1):
  299. return 1!
  300. else:
  301. return n * factorial(n - 1)!
  302. Void function main():
  303. while(True):
  304. output(factorial(input()))
  305. </pre></div>
  306. </div>
  307. </div>
  308. <div class="section" id="binary-to-decimal-converter">
  309. <h3>Binary to decimal converter<a class="headerlink" href="#binary-to-decimal-converter" title="Permalink to this headline">¶</a></h3>
  310. <p>A simple binary to decimal converter is given below:</p>
  311. <div class="highlight-default"><div class="highlight"><pre>include &quot;primitives.alh&quot;
  312. Integer function b2d(param : String):
  313. Integer value
  314. value = 0
  315. Integer length
  316. length = string_len(param)
  317. Integer counter
  318. counter = integer_subtraction(length, 1)
  319. Integer accumul
  320. accumul = 1
  321. while (counter &gt;= 0):
  322. if (string_get(param, counter) == &quot;1&quot;):
  323. value = integer_addition(value, accumul)
  324. accumul = integer_multiplication(accumul, 2)
  325. counter = integer_subtraction(counter, 1)
  326. return value!
  327. Void function main():
  328. while(True):
  329. output(b2d(input()))
  330. </pre></div>
  331. </div>
  332. </div>
  333. </div>
  334. </div>
  335. </div>
  336. </div>
  337. </div>
  338. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  339. <div class="sphinxsidebarwrapper">
  340. <h3><a href="index.html">Table Of Contents</a></h3>
  341. <ul>
  342. <li><a class="reference internal" href="#">Action Language</a><ul>
  343. <li><a class="reference internal" href="#files">Files</a></li>
  344. <li><a class="reference internal" href="#language-description">Language description</a><ul>
  345. <li><a class="reference internal" href="#if">If</a></li>
  346. <li><a class="reference internal" href="#while">While</a></li>
  347. <li><a class="reference internal" href="#break">Break</a></li>
  348. <li><a class="reference internal" href="#continue">Continue</a></li>
  349. <li><a class="reference internal" href="#return">Return</a></li>
  350. <li><a class="reference internal" href="#function-call">Function call</a></li>
  351. <li><a class="reference internal" href="#function-definition">Function definition</a></li>
  352. <li><a class="reference internal" href="#assignment">Assignment</a></li>
  353. <li><a class="reference internal" href="#declaration">Declaration</a></li>
  354. <li><a class="reference internal" href="#definition">Definition</a></li>
  355. <li><a class="reference internal" href="#imports">Imports</a></li>
  356. <li><a class="reference internal" href="#include">Include</a></li>
  357. <li><a class="reference internal" href="#constants">Constants</a></li>
  358. <li><a class="reference internal" href="#operators">Operators</a></li>
  359. <li><a class="reference internal" href="#user-i-o">User I/O</a></li>
  360. </ul>
  361. </li>
  362. <li><a class="reference internal" href="#examples">Examples</a><ul>
  363. <li><a class="reference internal" href="#fibonacci">Fibonacci</a></li>
  364. <li><a class="reference internal" href="#factorial">Factorial</a></li>
  365. <li><a class="reference internal" href="#binary-to-decimal-converter">Binary to decimal converter</a></li>
  366. </ul>
  367. </li>
  368. </ul>
  369. </li>
  370. </ul>
  371. <h4>Previous topic</h4>
  372. <p class="topless"><a href="howto.html"
  373. title="previous chapter">How to run</a></p>
  374. <h4>Next topic</h4>
  375. <p class="topless"><a href="modellanguage.html"
  376. title="next chapter">Modelling Language</a></p>
  377. <div role="note" aria-label="source link">
  378. <h3>This Page</h3>
  379. <ul class="this-page-menu">
  380. <li><a href="_sources/actionlanguage.txt"
  381. rel="nofollow">Show Source</a></li>
  382. </ul>
  383. </div>
  384. <div id="searchbox" style="display: none" role="search">
  385. <h3>Quick search</h3>
  386. <form class="search" action="search.html" method="get">
  387. <div><input type="text" name="q" /></div>
  388. <div><input type="submit" value="Go" /></div>
  389. <input type="hidden" name="check_keywords" value="yes" />
  390. <input type="hidden" name="area" value="default" />
  391. </form>
  392. </div>
  393. <script type="text/javascript">$('#searchbox').show(0);</script>
  394. </div>
  395. </div>
  396. <div class="clearer"></div>
  397. </div>
  398. <div class="related" role="navigation" aria-label="related navigation">
  399. <h3>Navigation</h3>
  400. <ul>
  401. <li class="right" style="margin-right: 10px">
  402. <a href="genindex.html" title="General Index"
  403. >index</a></li>
  404. <li class="right" >
  405. <a href="modellanguage.html" title="Modelling Language"
  406. >next</a> |</li>
  407. <li class="right" >
  408. <a href="howto.html" title="How to run"
  409. >previous</a> |</li>
  410. <li class="nav-item nav-item-0"><a href="index.html">Modelverse 0.4.0 documentation</a> &#187;</li>
  411. </ul>
  412. </div>
  413. <div class="footer" role="contentinfo">
  414. &#169; Copyright 2016, Yentl Van Tendeloo.
  415. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.6.
  416. </div>
  417. </body>
  418. </html>