|
| 1 | +# iox.js |
| 2 | +Flow-based language that compiles into js |
| 3 | +(Ported to js from http://github.com/flipcoder/iox) |
| 4 | + |
| 5 | +## Basics |
| 6 | + |
| 7 | +### Input/Output |
| 8 | + |
| 9 | +``` |
| 10 | +'hello, world' out |
| 11 | +``` |
| 12 | + |
| 13 | +The above takes a string "hello, world" and pipes it to the function "out" |
| 14 | +Note there is no "|" for piping like in bash. It is implied between each token. |
| 15 | + |
| 16 | +iox code reads from left-to-right. |
| 17 | + |
| 18 | +``` |
| 19 | +0 $x |
| 20 | +``` |
| 21 | + |
| 22 | +This pipes a value (0) into $x. |
| 23 | + |
| 24 | +To read a value, you pipe it into something else, in this case, we pipe it to *out*, which outputs it: |
| 25 | + |
| 26 | +``` |
| 27 | +$x out |
| 28 | +``` |
| 29 | + |
| 30 | +To get line-based input, use "in". |
| 31 | + |
| 32 | +``` |
| 33 | +"Enter your name: " in $name |
| 34 | +'Hello, ', $name out |
| 35 | +``` |
| 36 | + |
| 37 | +The message "Enter your name: " is passed to *in*, which is displayed as a prompt. |
| 38 | +*in* will pipe the string it receives from the user on to the next token. |
| 39 | +In this case, it is stored in $name. |
| 40 | + |
| 41 | +The next line sends two strings to *out* which prints the appended greeting. |
| 42 | + |
| 43 | +### Variables |
| 44 | + |
| 45 | +By piping from a value into a named variable, we created a variable of that type |
| 46 | +Variable types (*int*, etc.) are both constructors (pipe from) and casters (pipe to and from). |
| 47 | + |
| 48 | +We can cast values as we're storing them. In this case, it is redundant, since |
| 49 | +0 is already an interger. |
| 50 | + |
| 51 | +``` |
| 52 | +0 int $x |
| 53 | +``` |
| 54 | + |
| 55 | +This pipes 0 into $x, ensuring it is stored as an int. |
| 56 | + |
| 57 | +This is similar to x = int(0) in other languages. |
| 58 | + |
| 59 | +Now, Let's write a basic program addition calculator: |
| 60 | + |
| 61 | +First let's get two numbers from the user: |
| 62 | + |
| 63 | +``` |
| 64 | +'Number: ' in int $num1 |
| 65 | +'Number: ' in int $num2 |
| 66 | +``` |
| 67 | + |
| 68 | +Now let's sum them and print: |
| 69 | + |
| 70 | +``` |
| 71 | +$num1,$num2 + out |
| 72 | +``` |
| 73 | + |
| 74 | +Notice the comma. Commas are used to batch multiple things to send to a pipe. |
| 75 | +The *+* function sums all the parameters together, and returns this number |
| 76 | + |
| 77 | +### Branching |
| 78 | + |
| 79 | +First let's make a boolean called test, and branch based on its value. |
| 80 | + |
| 81 | +Conditions are done with "?" representing the initial test, |
| 82 | +and the code to run in either scenario |
| 83 | + |
| 84 | +``` |
| 85 | +'Enter a string (may be blank): ' in $s |
| 86 | +
|
| 87 | +$s ? |
| 88 | + 'String was not empty' out |
| 89 | +else |
| 90 | + 'String was empty' out |
| 91 | +
|
| 92 | +# or store as bool |
| 93 | +$s bool $was_empty |
| 94 | +
|
| 95 | +``` |
| 96 | + |
| 97 | +The *?* symbol is used for branching based on the contents of a stream. |
| 98 | +The first branch is taken if the stream contains the boolean equivalent of *true*. |
| 99 | +The else clause follows. |
| 100 | + |
| 101 | +Because of the pipe-like order of tokens, |
| 102 | +function parameters are written in suffix notation, meaning, we supply the |
| 103 | +parameters first, separated by commas, then we call the function. |
| 104 | + |
| 105 | +``` |
| 106 | +1,2,3 + out |
| 107 | +``` |
| 108 | + |
| 109 | +This takes the 3 numbers, calls "+", which adds them all, then pipes that to out, which prints them. |
| 110 | + |
| 111 | +### Looping |
| 112 | + |
| 113 | +for each example |
| 114 | + |
| 115 | +``` |
| 116 | +[1,2,3] each |
| 117 | + out |
| 118 | +``` |
| 119 | + |
| 120 | +for loop example |
| 121 | + |
| 122 | +``` |
| 123 | +0..5 each |
| 124 | + out |
| 125 | +``` |
| 126 | + |
| 127 | +turns into: |
| 128 | + |
| 129 | +``` |
| 130 | +for(var i = 0; i < 5; ++i) |
| 131 | +{ |
| 132 | + out(i); |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +also async: |
| 137 | + |
| 138 | +``` |
| 139 | +[1,2,3] async each |
| 140 | + out |
| 141 | +``` |
| 142 | + |
| 143 | +turns into: |
| 144 | + |
| 145 | +``` |
| 146 | +async.each([1,2,3],function(i,cb){ |
| 147 | + out(i); |
| 148 | + return cb(); |
| 149 | +}); |
| 150 | +``` |
| 151 | + |
| 152 | +callbacks trigger at end of scope unless you take control of the callback object using @ |
| 153 | + |
| 154 | +``` |
| 155 | +[1,2,3] async each |
| 156 | + 'blah' @ another_function |
| 157 | +``` |
| 158 | +Using '@' either prefixed or before a function call adds the current callback to the params |
| 159 | + |
| 160 | +in this case: |
| 161 | + |
| 162 | +``` |
| 163 | +async.each([1,2,3],function(i,cb){ |
| 164 | + another_function('blah',cb); |
| 165 | +}); |
| 166 | +
|
| 167 | +``` |
| 168 | + |
| 169 | +### Backcalls |
| 170 | + |
| 171 | +``` |
| 172 | +test then |
| 173 | + blah |
| 174 | +``` |
| 175 | + |
| 176 | +This is equivalent to test(function(){ return blah(); }) |
| 177 | + |
| 178 | +``` |
| 179 | +test ready |
| 180 | +blah |
| 181 | +``` |
| 182 | + |
| 183 | +This is also equivalent to test(function(){ return blah(); }) |
| 184 | + |
| 185 | +### Packing/Unpacking |
| 186 | + |
| 187 | +iox is based around temporary variables being passed down "the stream". Generally these are single values or a list of values. |
| 188 | + |
| 189 | +Variables are composite, meaning they can hold more than one value without being considered a special list type. |
| 190 | +Because of this, they are unpacked consecutively. |
| 191 | + |
| 192 | +For example, |
| 193 | + |
| 194 | +``` |
| 195 | +# unpacking: |
| 196 | +1,2,3 $numbers |
| 197 | +0, $numbers, 4 |
| 198 | +
|
| 199 | +1 type out |
| 200 | +# -> int |
| 201 | +
|
| 202 | +1,2 type out |
| 203 | +# -> int,int |
| 204 | +
|
| 205 | +``` |
| 206 | + |
| 207 | +The underscore (*_*) symbol indicates the insertion point for the pipe contents. |
| 208 | +We can use this for appending and reordering values. |
| 209 | + |
| 210 | +``` |
| 211 | +1,2,3 |
| 212 | +# is equivalent to: |
| 213 | +2,3 1,_ |
| 214 | +
|
| 215 | +#example using string formating |
| 216 | +$name 'Hello ',_,'!' out |
| 217 | +``` |
| 218 | + |
| 219 | +### Functions |
| 220 | + |
| 221 | +Functions in iox take any number of inputs and give any number of outputs. |
| 222 | + |
| 223 | +Here is a basic function declaration: |
| 224 | + |
| 225 | +``` |
| 226 | +message: "Hello there, ", _ |
| 227 | +
|
| 228 | +# Usage: |
| 229 | +"What's your name? " in message out |
| 230 | +``` |
| 231 | + |
| 232 | +Notice the *_* symbol represents the incoming data (unpacked) when piped from |
| 233 | + |
| 234 | +The function automatically returns the content of the pipe on the last |
| 235 | +effective line of the function. |
| 236 | +We can block this behavior with the *;* symbol at the end of the line. |
| 237 | + |
| 238 | +### Coroutines |
| 239 | +Note: this section is being rethought for js await/async |
| 240 | + |
| 241 | +The below features have no not yet been implemented. |
| 242 | + |
| 243 | +The *&* symbol represents an async call, and you can tell a section of code to run in the background |
| 244 | +The *&* symbol tells us to start any following code as a coroutine. |
| 245 | + |
| 246 | +Let's have two threads sleep, then print something |
| 247 | + |
| 248 | +``` |
| 249 | +& 2 sleep "2 second passed" out |
| 250 | +& 1 sleep "1 second passed" out |
| 251 | +``` |
| 252 | + |
| 253 | +The output will be |
| 254 | + |
| 255 | +``` |
| 256 | +1 second passed |
| 257 | +2 second passed |
| 258 | +``` |
| 259 | + |
| 260 | +All threads must finish for a program to complete, or a program must call quit for a program to |
| 261 | +finish. |
| 262 | + |
| 263 | +Contexts are named or numbered. and you can sequence many operations on the same thread. |
| 264 | + |
| 265 | +``` |
| 266 | +0 & "do this first" out |
| 267 | +0 & "do this second" out |
| 268 | +``` |
| 269 | + |
| 270 | +Since we need a handle to access data that becomes available after an async call, |
| 271 | + |
| 272 | +``` |
| 273 | +alarm: & 5 sleep "I just slept!" |
| 274 | +
|
| 275 | +alarm out # wake-up on event (availability of future 'alarm') |
| 276 | +``` |
| 277 | + |
| 278 | +### Events |
| 279 | + |
| 280 | +a keypress |
| 281 | + 'a pressed!' out |
| 282 | + |
| 283 | +### What now? |
| 284 | + |
| 285 | +Work in progress :) |
| 286 | + |
0 commit comments