tzo 1.0.19 → 1.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (180) hide show
  1. package/README.md +284 -280
  2. package/bin/package.json +2 -2
  3. package/bin/tzo +3 -3
  4. package/bin/tzo.mjs +3 -3
  5. package/dist/browser/cli.d.ts +1 -1
  6. package/dist/browser/cli.js +34 -34
  7. package/dist/browser/concisetext/ConciseTextLexer.d.ts +30 -30
  8. package/dist/browser/concisetext/ConciseTextLexer.js +122 -122
  9. package/dist/browser/concisetext/ConciseTextListener.d.ts +14 -14
  10. package/dist/browser/concisetext/ConciseTextListener.js +35 -35
  11. package/dist/browser/concisetext/ConciseTextParser.d.ts +99 -99
  12. package/dist/browser/concisetext/ConciseTextParser.js +438 -438
  13. package/dist/browser/getStackParams.d.ts +2 -2
  14. package/dist/browser/getStackParams.js +23 -23
  15. package/dist/browser/index.d.ts +5 -5
  16. package/dist/browser/index.js +17 -17
  17. package/dist/browser/interfaces.d.ts +42 -42
  18. package/dist/browser/interfaces.js +2 -2
  19. package/dist/browser/opcodes/and.d.ts +3 -3
  20. package/dist/browser/opcodes/and.js +8 -8
  21. package/dist/browser/opcodes/charCode.d.ts +3 -3
  22. package/dist/browser/opcodes/charCode.js +9 -9
  23. package/dist/browser/opcodes/closeBrace.d.ts +3 -3
  24. package/dist/browser/opcodes/closeBrace.js +6 -6
  25. package/dist/browser/opcodes/concat.d.ts +3 -3
  26. package/dist/browser/opcodes/concat.js +8 -8
  27. package/dist/browser/opcodes/delContext.d.ts +3 -3
  28. package/dist/browser/opcodes/delContext.js +8 -8
  29. package/dist/browser/opcodes/dup.d.ts +6 -6
  30. package/dist/browser/opcodes/dup.js +12 -12
  31. package/dist/browser/opcodes/eq.d.ts +3 -3
  32. package/dist/browser/opcodes/eq.js +9 -9
  33. package/dist/browser/opcodes/exit.d.ts +3 -3
  34. package/dist/browser/opcodes/exit.js +6 -6
  35. package/dist/browser/opcodes/getContext.d.ts +3 -3
  36. package/dist/browser/opcodes/getContext.js +12 -12
  37. package/dist/browser/opcodes/goto.d.ts +3 -3
  38. package/dist/browser/opcodes/goto.js +19 -19
  39. package/dist/browser/opcodes/gt.d.ts +3 -3
  40. package/dist/browser/opcodes/gt.js +9 -9
  41. package/dist/browser/opcodes/hasContext.d.ts +3 -3
  42. package/dist/browser/opcodes/hasContext.js +13 -13
  43. package/dist/browser/opcodes/jgz.d.ts +3 -3
  44. package/dist/browser/opcodes/jgz.js +10 -10
  45. package/dist/browser/opcodes/jz.d.ts +3 -3
  46. package/dist/browser/opcodes/jz.js +10 -10
  47. package/dist/browser/opcodes/lt.d.ts +3 -3
  48. package/dist/browser/opcodes/lt.js +9 -9
  49. package/dist/browser/opcodes/min.d.ts +3 -3
  50. package/dist/browser/opcodes/min.js +10 -10
  51. package/dist/browser/opcodes/mul.d.ts +3 -3
  52. package/dist/browser/opcodes/mul.js +10 -10
  53. package/dist/browser/opcodes/nop.d.ts +6 -6
  54. package/dist/browser/opcodes/nop.js +9 -9
  55. package/dist/browser/opcodes/not.d.ts +6 -6
  56. package/dist/browser/opcodes/not.js +16 -16
  57. package/dist/browser/opcodes/openBrace.d.ts +3 -3
  58. package/dist/browser/opcodes/openBrace.js +20 -20
  59. package/dist/browser/opcodes/or.d.ts +3 -3
  60. package/dist/browser/opcodes/or.js +8 -8
  61. package/dist/browser/opcodes/pause.d.ts +3 -3
  62. package/dist/browser/opcodes/pause.js +6 -6
  63. package/dist/browser/opcodes/plus.d.ts +3 -3
  64. package/dist/browser/opcodes/plus.js +10 -10
  65. package/dist/browser/opcodes/pop.d.ts +3 -3
  66. package/dist/browser/opcodes/pop.js +6 -6
  67. package/dist/browser/opcodes/ppc.d.ts +6 -6
  68. package/dist/browser/opcodes/ppc.js +9 -9
  69. package/dist/browser/opcodes/randInt.d.ts +3 -3
  70. package/dist/browser/opcodes/randInt.js +9 -9
  71. package/dist/browser/opcodes/rconcat.d.ts +3 -3
  72. package/dist/browser/opcodes/rconcat.js +8 -8
  73. package/dist/browser/opcodes/setContext.d.ts +3 -3
  74. package/dist/browser/opcodes/setContext.js +8 -8
  75. package/dist/browser/opcodes/stacksize.d.ts +3 -3
  76. package/dist/browser/opcodes/stacksize.js +6 -6
  77. package/dist/browser/opcodes/stdout.d.ts +3 -3
  78. package/dist/browser/opcodes/stdout.js +6 -6
  79. package/dist/browser/util/builder.d.ts +12 -12
  80. package/dist/browser/util/builder.js +37 -37
  81. package/dist/browser/util/fix_antlr_grammar_ts.d.ts +1 -1
  82. package/dist/browser/util/fix_antlr_grammar_ts.js +7 -7
  83. package/dist/browser/util/tokenizer.d.ts +20 -20
  84. package/dist/browser/util/tokenizer.js +176 -176
  85. package/dist/browser/util/tokenizer.js.map +1 -1
  86. package/dist/browser/vm.d.ts +26 -26
  87. package/dist/browser/vm.js +201 -201
  88. package/dist/browser/vm.test.d.ts +1 -1
  89. package/dist/browser/vm.test.js +398 -355
  90. package/dist/browser/vm.test.js.map +1 -1
  91. package/dist/node/cli.d.ts +1 -1
  92. package/dist/node/cli.js +34 -34
  93. package/dist/node/concisetext/ConciseTextLexer.d.ts +30 -30
  94. package/dist/node/concisetext/ConciseTextLexer.js +122 -122
  95. package/dist/node/concisetext/ConciseTextListener.d.ts +14 -14
  96. package/dist/node/concisetext/ConciseTextListener.js +35 -35
  97. package/dist/node/concisetext/ConciseTextParser.d.ts +99 -99
  98. package/dist/node/concisetext/ConciseTextParser.js +438 -438
  99. package/dist/node/getStackParams.d.ts +2 -2
  100. package/dist/node/getStackParams.js +23 -23
  101. package/dist/node/index.d.ts +5 -5
  102. package/dist/node/index.js +17 -17
  103. package/dist/node/interfaces.d.ts +42 -42
  104. package/dist/node/interfaces.js +2 -2
  105. package/dist/node/opcodes/and.d.ts +3 -3
  106. package/dist/node/opcodes/and.js +8 -8
  107. package/dist/node/opcodes/charCode.d.ts +3 -3
  108. package/dist/node/opcodes/charCode.js +9 -9
  109. package/dist/node/opcodes/closeBrace.d.ts +3 -3
  110. package/dist/node/opcodes/closeBrace.js +6 -6
  111. package/dist/node/opcodes/concat.d.ts +3 -3
  112. package/dist/node/opcodes/concat.js +8 -8
  113. package/dist/node/opcodes/delContext.d.ts +3 -3
  114. package/dist/node/opcodes/delContext.js +8 -8
  115. package/dist/node/opcodes/dup.d.ts +6 -6
  116. package/dist/node/opcodes/dup.js +12 -12
  117. package/dist/node/opcodes/eq.d.ts +3 -3
  118. package/dist/node/opcodes/eq.js +9 -9
  119. package/dist/node/opcodes/exit.d.ts +3 -3
  120. package/dist/node/opcodes/exit.js +6 -6
  121. package/dist/node/opcodes/getContext.d.ts +3 -3
  122. package/dist/node/opcodes/getContext.js +12 -12
  123. package/dist/node/opcodes/goto.d.ts +3 -3
  124. package/dist/node/opcodes/goto.js +19 -19
  125. package/dist/node/opcodes/gt.d.ts +3 -3
  126. package/dist/node/opcodes/gt.js +9 -9
  127. package/dist/node/opcodes/hasContext.d.ts +3 -3
  128. package/dist/node/opcodes/hasContext.js +13 -13
  129. package/dist/node/opcodes/jgz.d.ts +3 -3
  130. package/dist/node/opcodes/jgz.js +10 -10
  131. package/dist/node/opcodes/jz.d.ts +3 -3
  132. package/dist/node/opcodes/jz.js +10 -10
  133. package/dist/node/opcodes/lt.d.ts +3 -3
  134. package/dist/node/opcodes/lt.js +9 -9
  135. package/dist/node/opcodes/min.d.ts +3 -3
  136. package/dist/node/opcodes/min.js +10 -10
  137. package/dist/node/opcodes/mul.d.ts +3 -3
  138. package/dist/node/opcodes/mul.js +10 -10
  139. package/dist/node/opcodes/nop.d.ts +6 -6
  140. package/dist/node/opcodes/nop.js +9 -9
  141. package/dist/node/opcodes/not.d.ts +6 -6
  142. package/dist/node/opcodes/not.js +16 -16
  143. package/dist/node/opcodes/openBrace.d.ts +3 -3
  144. package/dist/node/opcodes/openBrace.js +20 -20
  145. package/dist/node/opcodes/or.d.ts +3 -3
  146. package/dist/node/opcodes/or.js +8 -8
  147. package/dist/node/opcodes/pause.d.ts +3 -3
  148. package/dist/node/opcodes/pause.js +6 -6
  149. package/dist/node/opcodes/plus.d.ts +3 -3
  150. package/dist/node/opcodes/plus.js +10 -10
  151. package/dist/node/opcodes/pop.d.ts +3 -3
  152. package/dist/node/opcodes/pop.js +6 -6
  153. package/dist/node/opcodes/ppc.d.ts +6 -6
  154. package/dist/node/opcodes/ppc.js +9 -9
  155. package/dist/node/opcodes/randInt.d.ts +3 -3
  156. package/dist/node/opcodes/randInt.js +9 -9
  157. package/dist/node/opcodes/rconcat.d.ts +3 -3
  158. package/dist/node/opcodes/rconcat.js +8 -8
  159. package/dist/node/opcodes/setContext.d.ts +3 -3
  160. package/dist/node/opcodes/setContext.js +8 -8
  161. package/dist/node/opcodes/stacksize.d.ts +3 -3
  162. package/dist/node/opcodes/stacksize.js +6 -6
  163. package/dist/node/opcodes/stdout.d.ts +3 -3
  164. package/dist/node/opcodes/stdout.js +6 -6
  165. package/dist/node/util/builder.d.ts +12 -12
  166. package/dist/node/util/builder.js +37 -37
  167. package/dist/node/util/fix_antlr_grammar_ts.d.ts +1 -1
  168. package/dist/node/util/fix_antlr_grammar_ts.js +7 -7
  169. package/dist/node/util/tokenizer.d.ts +20 -20
  170. package/dist/node/util/tokenizer.js +176 -176
  171. package/dist/node/util/tokenizer.js.map +1 -1
  172. package/dist/node/vm.d.ts +26 -26
  173. package/dist/node/vm.js +201 -201
  174. package/dist/node/vm.test.d.ts +1 -1
  175. package/dist/node/vm.test.js +398 -355
  176. package/dist/node/vm.test.js.map +1 -1
  177. package/jest.config.js +4 -4
  178. package/package.json +50 -50
  179. package/tsconfig-browser.json +8 -8
  180. package/tsconfig.json +28 -28
package/README.md CHANGED
@@ -1,280 +1,284 @@
1
- # Tzo 💎 - a simple Virtual Stack Machine
2
-
3
- Tzo is a simple Virtual Stack Machine. Its primary use case is to be implemented and embedded in games for a potentially wide variety of use cases. It is the default runtime engine for [QuestMark](https://github.com/jorisvddonk/questmark).
4
-
5
- This repository defines:
6
-
7
- * The Tzo Virtual Machine and its capabilities and featureset.
8
- * The Tzo Standard Runtime - opcodes that VM implementations are _expected_ to impleemnt.
9
- * One (or more) _representations_ of Tzo VM programs (instruction lists) and Tzo VM State.
10
-
11
- This repository furthermore contains:
12
-
13
- * A Reference Implementation, in TypeScript.
14
- * Testcases and other reference material.
15
-
16
- Note that Tzo is a _hobby project_ that is primarily focused around a single (set of) use case(s). Several language features _may or may not_ be added later on in the future.
17
-
18
- # Design goals
19
-
20
- * Easy to implement.
21
- * Machine-readable code (JSON) that is somewhat human-readable, and can easily be parsed and minimized to other usecase specific representations when needed (e.g. if a game MUST be super small and compressed, a game programmer can spend some time on a custom bytecode representation of a program listing).
22
- * Easy to compile to / generate code for.
23
- * Easy to extend with extra, use-case-specific functionality (Virtual Opcodes).
24
-
25
- # VM
26
-
27
- The Tzo virtual machine is at its core a simple stack machine. All _standard operations_ operate on the stack, not on registers. That said, there _is_ a "Context" available, which acts as a place to store and retrieve keyed values. This is particularly useful for game scripting, as this is where you can store part of your game's state.
28
-
29
- Each item on the stack can either be a _number_ (floating point or integer), or a _string_. There are no booleans inherently. The size and precision of numbers is left as an implementation detail. The reference implementation in this repository uses JavaScript's basic number type (64 bit floating point) for all numbers.
30
-
31
- Each instruction in the program list (_either_ an opcode invocation, a string literal to be pushed onto the stack, or a number literal to be pushed onto the stack) takes up 1 space in the program list. The program list starts at 0.
32
-
33
- # Opcodes
34
-
35
- The following section describes all opcodes from the standard runtime, which implementations are expected to implement. Note that it is also possible to push string or number literals onto the stack; this is not documented here as there is no associated opcode for that.
36
-
37
- *NOTE*: Even if not explicitly mentioned, all arguments are _always_ popped off the stack, and not pushed back onto it unless otherwise explicitly mentioned.
38
-
39
- | opcode(s) | arguments (rightmost item: top of stack) | argument types | description |
40
- |--------------|------------------------------------------|------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
41
- | `nop` | | | does nothing |
42
- | `pop` | A | string/number | Pops a value off of the stack, disarding it in the process |
43
- | `+` / `plus` | B, A | number, number | Adds A to B and pushes the result onto the stack |
44
- | `-` / `min` | B, A | number, number | Subtracts B from A and pushes the result onto the stack |
45
- | `*` / `mul` | B, A | number, number | Multiplies B and A and pushes the result onto the stack |
46
- | `concat` | B, A | string/number, string/number | Concatenate A, B and push the result onto the stack. |
47
- | `rconcat` | B, A | string/number, string/number | Concatenate B, A and push the result onto the stack. |
48
- | `randInt` | A | number | Generate a random number between 0 (inclusive) and A and push it onto the stack |
49
- | `charCode` | A | number | Pops A off the stack, gets the corresponding character code, creates a string using said character code and pushes the resulting string onto the stack. The exact definition of how this works are admittedly a weakness in the spec; it is recommended to not using this opcode until this has been clarified or improved. |
50
- | `ppc` | | | Push the current program counter onto the stack |
51
- | `not` | A | number | Pops A off the stack. If A is 1, push 0 onto the stack. Otherwise, push 1 onto the stack. |
52
- | `or` | B, A | number, number | If both A and B are 0, push 0 onto the stack. Otherwise, if either is nonzero, push 1 onto the stack. |
53
- | `and` | B, A | number, number | If either A or B is 0, push 0 onto the stack. Otherwise, if both are nonzero, push 1 onto the stack. |
54
- | `jgz` | A | number | If A is greater than zero, skip ("hop over") the next instruction. |
55
- | `jz` | A | number | If A is zero, skip ("hop over") the next instruction. |
56
- | `gt` | B, A | number, number | If A is greater than B, push 1 onto the stack. Otherwise, push 0. |
57
- | `lt` | B, A | number, number | If A is less than B, push 1 onto the stack. Otherwise, push 0. |
58
- | `eq` | B, A | string/number, string/number | If A and B are equal (equal value if number, byte-for-byte equal if string), push 1 onto the stack. Otherwise, push 0. |
59
- | `dup` | A | string/number | Pop A off the stack, then push it onto the stack twice. |
60
- | `pause` | | | Pause the virtual machine. It will not resume execution until some other process makes it resume execution. |
61
- | `exit` | | | Exit (quit) the virtual machine. It will not be allowed to resume execution again. |
62
- | `{` | | | Find the next matching close brace ( `}` ) in the program list, then set the program counter to that close brace's position in the program list plus one. The matching semantics are similar to most programming languages, e.g. if the program counter is 2 and finds a `{` there, there is a `{` at 4, a `}` at 7 and another `}` at 9, the program counter will be set to 10. |
63
- | `}` | | | Does nothing by itself, and acts as a `nop`. |
64
- | `getContext` | A | string | Pops A off the stack, then gets the context value pointed to by A and pushes it onto the stack |
65
- | `hasContext` | A | string | Pops A off the stack, then checks if the context has any existing value for A; if so, pushes 1 to the stack, 0 otherwise |
66
- | `setContext` | B, A | string/number, string | Pops A and B off the stack. The context value pointed to by A is set to B |
67
- | `delContext` | A | string | Pops A off the stack, then deletes the value (and key / pointer, depending on implementation) pointed to by A completely, effectively freeing it. |
68
- | `goto` | A | string/number | Pops A off the stack. If it is a number, sets the program counter to that value. If it is a string, looks up the string in the labelmap and sets the program counter to the value found in the label map |
69
- | `stacksize` | | | Push the _current_ length of the stack to the stack itself. NOTE: the total length of the stack after this operation will be 1 _larger_ than the actual value! |
70
- | `stdout` | A | string/number | Pops A off the stack. If number, converts it to string first. Then writes it on standard output. |
71
-
72
- ## Custom (Foreign) Opcodes
73
-
74
- As you can see above, the default set of opcodes is pretty barebones, and does not support a lot of functionality. This is by design!
75
-
76
- The Tzo VM is _designed to be extended_. Define your own opcodes, their behaviour, and then use these custom opcodes in your programs! Programs that target your custom opcodes will obviously not be executable outside of your VM or a VM implementing the same specification, but that doesn't matter!
77
-
78
- The [QuestMark](https://github.com/jorisvddonk/questmark) [QuestVM](https://github.com/jorisvddonk/questmark/blob/fb37a58dd38c9dda38d8c9b6dd3fc07daaa8ec05/src/QuestVM.ts) is an example of a customized Tzo VM.
79
-
80
- # Standard Representation
81
-
82
- The Standard Representation of Tzo VM code is a JSON array:
83
-
84
- ```json
85
- [
86
- {
87
- "type": "push-number-instruction",
88
- "value": 42
89
- },
90
- {
91
- "type": "push-string-instruction",
92
- "value": "Foo"
93
- },
94
- {
95
- "type": "invoke-function-instruction",
96
- "functionName": "setContext"
97
- }
98
- ]
99
- ```
100
-
101
- Though not space-efficient, this is *very* easy to parse in a wide variety of languages, and well-specified. If more space-efficient representations are preferred, custom integrations can be made to allow this.
102
-
103
- Comments can be added to add clarification, where needed:
104
-
105
- ```json
106
- [
107
- {
108
- "type": "push-number-instruction",
109
- "value": 0
110
- },
111
- {
112
- "type": "invoke-function-instruction",
113
- "functionName": "jgz"
114
- },
115
- {
116
- "type": "invoke-function-instruction",
117
- "functionName": "{"
118
- },
119
- {
120
- "type": "push-string-instruction",
121
- "functionName": "hi",
122
- "comment": "This instruction will never be invoked!"
123
- },
124
- {
125
- "type": "invoke-function-instruction",
126
- "functionName": "}"
127
- }
128
- ]
129
- ```
130
-
131
- Labels can be placed (to serve as jump targets for the `goto` opcode) on any instruction as well:
132
-
133
- ```json
134
- [
135
- {
136
- "type": "push-number-instruction",
137
- "value": 1
138
- },
139
- {
140
- "type": "push-string-instruction",
141
- "value": "Awesome",
142
- "comment": "This will set the program counter to whatever value is pointed to by the \"Awesome\" label"
143
- },
144
- {
145
- "type": "invoke-function-instruction",
146
- "functionName": "goto"
147
- },
148
- {
149
- "type": "push-number-instruction",
150
- "value": 2,
151
- "comment": "Due to the goto above, this instruction will be skipped, and so 2 won't be pushed to the stack"
152
- },
153
- {
154
- "type": "push-number-instruction",
155
- "value": 3,
156
- "label": "Awesome"
157
- },
158
- {
159
- "type": "invoke-function-instruction",
160
- "functionName": "nop"
161
- }
162
- ]
163
- ```
164
-
165
- (the above program, when run, will halt with [`1`, `3`] on the stack)
166
-
167
- # ConciseText Representation
168
-
169
- The ConciseText representation, is - as its name suggests - a lot more concise than the standard representation, but also slightly more difficult to parse. See [`src/grammars/ConciseText.g4`](src/grammars/ConciseText.g4) for an [ANTLR4](https://www.antlr.org/) grammar, and [`/reference/ConciseText.rrd.pdf`](/reference/ConciseText.rrd.pdf) for a railroad diagram of this grammar.
170
-
171
- Examples:
172
-
173
- ```text
174
- 1 "Awesome" goto 2 #Awesome nop
175
- ```
176
- (encodes the same example as above)
177
-
178
- ```text
179
- 0 jgz { "hi" }
180
- ```
181
-
182
- ```text
183
- "Hello" "," " world" rconcat rconcat
184
- ```
185
- (will halt with `"Hello, world"` on the stack)
186
-
187
- # Techniques
188
-
189
- This section lists some common techniques. For conciseness, all examples will be given in the ConciseText representation.
190
-
191
- ## Commenting out code
192
-
193
- Want to "comment out" some code? Encase it within `{` and `}`!
194
-
195
- ```text
196
- 1 2 3 { "do" "not" "push" "these" "strings" } 4 5 6
197
- ```
198
-
199
- ## If
200
-
201
- Simple if statements:
202
-
203
- ```text
204
- 1 1 + 2 eq jgz {
205
- "1 + 1 = 2!"
206
- }
207
- ```
208
-
209
- ## If/else
210
-
211
- duplicate the evaluation result via `dup` to create an if/else block.
212
- NOTE: you need to ensure that within the `if` block, you do NOT push to the stack!
213
-
214
- ```text
215
- 1 1 + 2 eq dup jgz {
216
- "1 + 1 = 2!" pop
217
- } jz {
218
- "1 + 1 is not 2!?"
219
- }
220
- ```
221
- (if the `pop` statement here was omitted, `jz` would be erroneously evaluated against `"1 + 1 = 2!"`)
222
-
223
- if you need to ensure that within the `if` block you _can_ push to the stack (note: conditionally pushing to the stack is dangerous! ), one technique you can use is pushing the condition expression to the context:
224
-
225
- ```text
226
- 1 1 + 2 eq "conditionResult" setContext
227
- "conditionResult" getContext jgz {
228
- "1 + 1 = 2!"
229
- }
230
- "conditionResult" getContext jz {
231
- "1 + 1 is not 2!?"
232
- }
233
- ```
234
-
235
- ## Cleaning the stack
236
-
237
- Got some stuff on your stack and want to clean it completely? Here is a simple list of instructions to reset the stack to length 0:
238
-
239
- ```text
240
- stacksize jgz { pop } stacksize jgz { 9 ppc - goto }
241
- ```
242
-
243
- ## Poor man's functions
244
-
245
- Tzo does not support functions by default, but you can recreate them using `goto` and `ppc`.
246
-
247
- The following bit of code defines a "mul3" function, which pops an item off the stack, multiplies that by 3, and pushes the result to the stack.
248
-
249
- ```text
250
- {
251
- nop #mul3
252
- "_mul3_return_pc" setContext
253
- 3 *
254
- "_mul3_return_pc" getContext 3 + "_mul3_return_pc" delContext goto
255
- }
256
- ```
257
-
258
- Note a few things:
259
-
260
- 1. A temporary context value is used to store the program counter to return to. This means that _it is not possible to call these "functions" recursively_!
261
- 2. `{` and `}` are used to ensure that the function is not accidentally called, allowing it to be placed at the top of a program listing without issue.
262
- 3. The code can be shortened by 1 operation by removing the `nop` instruction and moving the `mul3` label one forwards.
263
-
264
- To invoke this "function", push its argument on the stack and then call `ppc "mul3" goto` :
265
-
266
- ```text
267
- 1 ppc "mul3" goto
268
- 2 ppc "mul3" goto
269
- 3 ppc "mul3" goto
270
- 4 ppc "mul3" goto
271
- ```
272
-
273
- # CLI
274
-
275
- There is a CLI available for Tzo, which is capable of interpreting either Standard Representation or ConciseText Tzo vm code. To invoke it:
276
-
277
- ```bash
278
- npx tzo -i <path to .txt or .json file>
279
- ```
280
-
1
+ # Tzo 💎 - a simple Virtual Stack Machine
2
+
3
+ Tzo is a simple Virtual Stack Machine. Its primary use case is to be implemented and embedded in games for a potentially wide variety of use cases. It is the default runtime engine for [QuestMark](https://github.com/jorisvddonk/questmark).
4
+
5
+ This repository defines:
6
+
7
+ * The Tzo Virtual Machine and its capabilities and featureset.
8
+ * The Tzo Standard Runtime - opcodes that VM implementations are _expected_ to impleemnt.
9
+ * One (or more) _representations_ of Tzo VM programs (instruction lists) and Tzo VM State.
10
+
11
+ This repository furthermore contains:
12
+
13
+ * A Reference Implementation, in TypeScript.
14
+ * Testcases and other reference material.
15
+
16
+ Note that Tzo is a _hobby project_ that is primarily focused around a single (set of) use case(s). Several language features _may or may not_ be added later on in the future.
17
+
18
+ # Design goals
19
+
20
+ * Easy to implement.
21
+ * Machine-readable code (JSON) that is somewhat human-readable, and can easily be parsed and minimized to other usecase specific representations when needed (e.g. if a game MUST be super small and compressed, a game programmer can spend some time on a custom bytecode representation of a program listing).
22
+ * Easy to compile to / generate code for.
23
+ * Easy to extend with extra, use-case-specific functionality (Virtual Opcodes).
24
+
25
+ # VM
26
+
27
+ The Tzo virtual machine is at its core a simple stack machine. All _standard operations_ operate on the stack, not on registers. That said, there _is_ a "Context" available, which acts as a place to store and retrieve keyed values. This is particularly useful for game scripting, as this is where you can store part of your game's state.
28
+
29
+ Each item on the stack can either be a _number_ (floating point or integer), or a _string_. There are no booleans inherently. The size and precision of numbers is left as an implementation detail. The reference implementation in this repository uses JavaScript's basic number type (64 bit floating point) for all numbers.
30
+
31
+ Each instruction in the program list (_either_ an opcode invocation, a string literal to be pushed onto the stack, or a number literal to be pushed onto the stack) takes up 1 space in the program list. The program list starts at 0.
32
+
33
+ # Opcodes
34
+
35
+ The following section describes all opcodes from the standard runtime, which implementations are expected to implement. Note that it is also possible to push string or number literals onto the stack; this is not documented here as there is no associated opcode for that.
36
+
37
+ *NOTE*: Even if not explicitly mentioned, all arguments are _always_ popped off the stack, and not pushed back onto it unless otherwise explicitly mentioned.
38
+
39
+ *NOTE*: A _number_ is a real (floating-point) value: arithmetic and comparisons use real-number (not integer) semantics. For example, `0.9 > 0.1` is true, and `jgz` with `0.5` skips the next instruction. Unless stated otherwise, supplying an invalid argument type (e.g. a non-string context key, or a non-number where a number is required) results in an error.
40
+
41
+ | opcode(s) | arguments (rightmost item: top of stack) | argument types | description |
42
+ |--------------|------------------------------------------|------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
43
+ | `nop` | | | does nothing |
44
+ | `pop` | A | string/number | Pops a value off of the stack, disarding it in the process |
45
+ | `+` / `plus` | B, A | number, number | Adds A to B and pushes the result onto the stack. Both operands must be numbers; otherwise an error is raised. |
46
+ | `-` / `min` | B, A | number, number | Subtracts B from A and pushes the result onto the stack. Both operands must be numbers; otherwise an error is raised. |
47
+ | `*` / `mul` | B, A | number, number | Multiplies B and A and pushes the result onto the stack. Both operands must be numbers; otherwise an error is raised. |
48
+ | `concat` | B, A | string/number, string/number | Concatenate A, B and push the result onto the stack. |
49
+ | `rconcat` | B, A | string/number, string/number | Concatenate B, A and push the result onto the stack. |
50
+ | `randInt` | A | number | Generate a random integer `N = floor(random() * A)` and push it. For `A > 0` this yields `0 <= N < A`; for `A = 0` the result is `0`. `A` must be a number. |
51
+ | `charCode` | A | number | Pops A (a number) and pushes the string consisting of the single Unicode character whose code point is `A mod 65536` (equivalent to JavaScript's `String.fromCharCode(A)`). A must be a number. |
52
+ | `ppc` | | | Push the current program counter onto the stack |
53
+ | `not` | A | number | Pops A off the stack. If A is 0, push 1 onto the stack. Otherwise, push 0 onto the stack. A must be a number. |
54
+ | `or` | B, A | number, number | If both A and B are 0, push 0 onto the stack. Otherwise, if either is nonzero, push 1 onto the stack. A and B must be numbers. |
55
+ | `and` | B, A | number, number | If either A or B is 0, push 0 onto the stack. Otherwise, if both are nonzero, push 1 onto the stack. A and B must be numbers. |
56
+ | `jgz` | A | number | If A (a number) is greater than zero, skip ("hop over") the next instruction. Fractional values are compared as real numbers, so e.g. `0.5` skips. |
57
+ | `jz` | A | number | If A (a number) is zero, skip ("hop over") the next instruction. Values are compared as real numbers. |
58
+ | `gt` | B, A | number, number | If A is greater than B, push 1 onto the stack. Otherwise, push 0. A and B must be numbers; compared as real numbers. |
59
+ | `lt` | B, A | number, number | If A is less than B, push 1 onto the stack. Otherwise, push 0. A and B must be numbers; compared as real numbers. |
60
+ | `eq` | B, A | string/number, string/number | If A and B are equal (equal value if number, byte-for-byte equal if string), push 1 onto the stack. Otherwise, push 0. Numbers are compared by value, so `1` and `1.0` are equal; a number and a string are never equal. |
61
+ | `dup` | A | string/number | Pop A off the stack, then push it onto the stack twice. |
62
+ | `pause` | | | Pause the virtual machine. It will not resume execution until some other process makes it resume execution. |
63
+ | `exit` | | | Exit (quit) the virtual machine. It will not be allowed to resume execution again. |
64
+ | `{` | | | Find the next matching close brace ( `}` ) in the program list, then set the program counter to that close brace's position in the program list plus one. The matching semantics are similar to most programming languages, e.g. if the program counter is 2 and finds a `{` there, there is a `{` at 4, a `}` at 7 and another `}` at 9, the program counter will be set to 10. |
65
+ | `}` | | | Does nothing by itself, and acts as a `nop`. |
66
+ | `getContext` | A | string | Pops A off the stack, then gets the context value pointed to by A and pushes it onto the stack. A must be a string; an error is raised if A is not a string or if no value is set for A. |
67
+ | `hasContext` | A | string | Pops A off the stack, then checks if the context has any existing value for A; if so, pushes 1 to the stack, 0 otherwise. A must be a string; an error is raised if A is not a string. |
68
+ | `setContext` | B, A | string/number, string | Pops A and B off the stack. The context value pointed to by A (the key, which must be a string) is set to B (a string or number). An error is raised if A is not a string. |
69
+ | `delContext` | A | string | Pops A off the stack, then deletes the value (and key / pointer, depending on implementation) pointed to by A completely, effectively freeing it. A must be a string; an error is raised if A is not a string. |
70
+ | `goto` | A | string/number | Pops A off the stack. If it is a number, sets the program counter to that value. If it is a string, looks up the string in the labelmap and sets the program counter to the value found in the label map |
71
+ | `stacksize` | | | Push the _current_ length of the stack to the stack itself. NOTE: the total length of the stack after this operation will be 1 _larger_ than the actual value! |
72
+ | `stdout` | A | string/number | Pops A off the stack. If A is a string, it is written; if A is a number, it is coerced to a string exactly as JavaScript does (`"" + A`): whole numbers print without a decimal part (e.g. `1`, not `1.000000`), `-0` prints as `0`, and `Infinity`/`NaN` print as such. Popping from an empty stack writes `undefined`. |
73
+
74
+ ## Custom (Foreign) Opcodes
75
+
76
+ As you can see above, the default set of opcodes is pretty barebones, and does not support a lot of functionality. This is by design!
77
+
78
+ The Tzo VM is _designed to be extended_. Define your own opcodes, their behaviour, and then use these custom opcodes in your programs! Programs that target your custom opcodes will obviously not be executable outside of your VM or a VM implementing the same specification, but that doesn't matter!
79
+
80
+ The [QuestMark](https://github.com/jorisvddonk/questmark) [QuestVM](https://github.com/jorisvddonk/questmark/blob/fb37a58dd38c9dda38d8c9b6dd3fc07daaa8ec05/src/QuestVM.ts) is an example of a customized Tzo VM.
81
+
82
+ # Standard Representation
83
+
84
+ The Standard Representation of Tzo VM code is a JSON array:
85
+
86
+ ```json
87
+ [
88
+ {
89
+ "type": "push-number-instruction",
90
+ "value": 42
91
+ },
92
+ {
93
+ "type": "push-string-instruction",
94
+ "value": "Foo"
95
+ },
96
+ {
97
+ "type": "invoke-function-instruction",
98
+ "functionName": "setContext"
99
+ }
100
+ ]
101
+ ```
102
+
103
+ Though not space-efficient, this is *very* easy to parse in a wide variety of languages, and well-specified. If more space-efficient representations are preferred, custom integrations can be made to allow this.
104
+
105
+ Comments can be added to add clarification, where needed:
106
+
107
+ ```json
108
+ [
109
+ {
110
+ "type": "push-number-instruction",
111
+ "value": 0
112
+ },
113
+ {
114
+ "type": "invoke-function-instruction",
115
+ "functionName": "jgz"
116
+ },
117
+ {
118
+ "type": "invoke-function-instruction",
119
+ "functionName": "{"
120
+ },
121
+ {
122
+ "type": "push-string-instruction",
123
+ "functionName": "hi",
124
+ "comment": "This instruction will never be invoked!"
125
+ },
126
+ {
127
+ "type": "invoke-function-instruction",
128
+ "functionName": "}"
129
+ }
130
+ ]
131
+ ```
132
+
133
+ Labels can be placed (to serve as jump targets for the `goto` opcode) on any instruction as well:
134
+
135
+ ```json
136
+ [
137
+ {
138
+ "type": "push-number-instruction",
139
+ "value": 1
140
+ },
141
+ {
142
+ "type": "push-string-instruction",
143
+ "value": "Awesome",
144
+ "comment": "This will set the program counter to whatever value is pointed to by the \"Awesome\" label"
145
+ },
146
+ {
147
+ "type": "invoke-function-instruction",
148
+ "functionName": "goto"
149
+ },
150
+ {
151
+ "type": "push-number-instruction",
152
+ "value": 2,
153
+ "comment": "Due to the goto above, this instruction will be skipped, and so 2 won't be pushed to the stack"
154
+ },
155
+ {
156
+ "type": "push-number-instruction",
157
+ "value": 3,
158
+ "label": "Awesome"
159
+ },
160
+ {
161
+ "type": "invoke-function-instruction",
162
+ "functionName": "nop"
163
+ }
164
+ ]
165
+ ```
166
+
167
+ (the above program, when run, will halt with [`1`, `3`] on the stack)
168
+
169
+ # ConciseText Representation
170
+
171
+ The ConciseText representation, is - as its name suggests - a lot more concise than the standard representation, but also slightly more difficult to parse. See [`src/grammars/ConciseText.g4`](src/grammars/ConciseText.g4) for an [ANTLR4](https://www.antlr.org/) grammar, and [`/reference/ConciseText.rrd.pdf`](/reference/ConciseText.rrd.pdf) for a railroad diagram of this grammar.
172
+
173
+ Number literals may be integers _or_ decimals, e.g. `1`, `-5` and `0.5` are all valid; they are parsed as real (floating-point) numbers.
174
+
175
+ Examples:
176
+
177
+ ```text
178
+ 1 "Awesome" goto 2 #Awesome nop
179
+ ```
180
+ (encodes the same example as above)
181
+
182
+ ```text
183
+ 0 jgz { "hi" }
184
+ ```
185
+
186
+ ```text
187
+ "Hello" "," " world" rconcat rconcat
188
+ ```
189
+ (will halt with `"Hello, world"` on the stack)
190
+
191
+ # Techniques
192
+
193
+ This section lists some common techniques. For conciseness, all examples will be given in the ConciseText representation.
194
+
195
+ ## Commenting out code
196
+
197
+ Want to "comment out" some code? Encase it within `{` and `}`!
198
+
199
+ ```text
200
+ 1 2 3 { "do" "not" "push" "these" "strings" } 4 5 6
201
+ ```
202
+
203
+ ## If
204
+
205
+ Simple if statements:
206
+
207
+ ```text
208
+ 1 1 + 2 eq jgz {
209
+ "1 + 1 = 2!"
210
+ }
211
+ ```
212
+
213
+ ## If/else
214
+
215
+ duplicate the evaluation result via `dup` to create an if/else block.
216
+ NOTE: you need to ensure that within the `if` block, you do NOT push to the stack!
217
+
218
+ ```text
219
+ 1 1 + 2 eq dup jgz {
220
+ "1 + 1 = 2!" pop
221
+ } jz {
222
+ "1 + 1 is not 2!?"
223
+ }
224
+ ```
225
+ (if the `pop` statement here was omitted, `jz` would be erroneously evaluated against `"1 + 1 = 2!"`)
226
+
227
+ if you need to ensure that within the `if` block you _can_ push to the stack (note: conditionally pushing to the stack is dangerous! ), one technique you can use is pushing the condition expression to the context:
228
+
229
+ ```text
230
+ 1 1 + 2 eq "conditionResult" setContext
231
+ "conditionResult" getContext jgz {
232
+ "1 + 1 = 2!"
233
+ }
234
+ "conditionResult" getContext jz {
235
+ "1 + 1 is not 2!?"
236
+ }
237
+ ```
238
+
239
+ ## Cleaning the stack
240
+
241
+ Got some stuff on your stack and want to clean it completely? Here is a simple list of instructions to reset the stack to length 0:
242
+
243
+ ```text
244
+ stacksize jgz { pop } stacksize jgz { 9 ppc - goto }
245
+ ```
246
+
247
+ ## Poor man's functions
248
+
249
+ Tzo does not support functions by default, but you can recreate them using `goto` and `ppc`.
250
+
251
+ The following bit of code defines a "mul3" function, which pops an item off the stack, multiplies that by 3, and pushes the result to the stack.
252
+
253
+ ```text
254
+ {
255
+ nop #mul3
256
+ "_mul3_return_pc" setContext
257
+ 3 *
258
+ "_mul3_return_pc" getContext 3 + "_mul3_return_pc" delContext goto
259
+ }
260
+ ```
261
+
262
+ Note a few things:
263
+
264
+ 1. A temporary context value is used to store the program counter to return to. This means that _it is not possible to call these "functions" recursively_!
265
+ 2. `{` and `}` are used to ensure that the function is not accidentally called, allowing it to be placed at the top of a program listing without issue.
266
+ 3. The code can be shortened by 1 operation by removing the `nop` instruction and moving the `mul3` label one forwards.
267
+
268
+ To invoke this "function", push its argument on the stack and then call `ppc "mul3" goto` :
269
+
270
+ ```text
271
+ 1 ppc "mul3" goto
272
+ 2 ppc "mul3" goto
273
+ 3 ppc "mul3" goto
274
+ 4 ppc "mul3" goto
275
+ ```
276
+
277
+ # CLI
278
+
279
+ There is a CLI available for Tzo, which is capable of interpreting either Standard Representation or ConciseText Tzo vm code. To invoke it:
280
+
281
+ ```bash
282
+ npx tzo -i <path to .txt or .json file>
283
+ ```
284
+