|
1 | 1 | #iox.js
|
2 | 2 | Flow-based language that compiles into js
|
| 3 | +(Ported to js fromhttp://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 | +###Packing/Unpacking |
| 112 | + |
| 113 | +iox is based around temporary variables being passed down "the stream". Generally these are single values or a list of values. |
| 114 | + |
| 115 | +Variables are composite, meaning they can hold more than one value without being considered a special list type. |
| 116 | +Because of this, they are unpacked consecutively. |
| 117 | + |
| 118 | +For example, |
| 119 | + |
| 120 | +``` |
| 121 | +# unpacking: |
| 122 | +1,2,3 $numbers |
| 123 | +0, $numbers, 4 |
| 124 | +
|
| 125 | +1 type out |
| 126 | +# -> int |
| 127 | +
|
| 128 | +1,2 type out |
| 129 | +# -> int,int |
| 130 | +
|
| 131 | +``` |
| 132 | + |
| 133 | +The underscore (*_*) symbol indicates the insertion point for the pipe contents. |
| 134 | +We can use this for appending and reordering values. |
| 135 | + |
| 136 | +``` |
| 137 | +1,2,3 |
| 138 | +# is equivalent to: |
| 139 | +2,3 1,_ |
| 140 | +
|
| 141 | +#example using string formating |
| 142 | +$name 'Hello ',_,'!' out |
| 143 | +``` |
| 144 | + |
| 145 | +###Functions |
| 146 | + |
| 147 | +Functions in iox take any number of inputs and give any number of outputs. |
| 148 | + |
| 149 | +Here is a basic function declaration: |
| 150 | + |
| 151 | +``` |
| 152 | +message: |
| 153 | + "Hello there, ", _ |
| 154 | +
|
| 155 | +# Usage: |
| 156 | +"What's your name? " in message out |
| 157 | +``` |
| 158 | + |
| 159 | +Notice the*_* symbol represents the incoming data (unpacked) when piped from |
| 160 | + |
| 161 | +The function automatically returns the content of the pipe on the last |
| 162 | +effective line of the function. |
| 163 | +We can block this behavior with the*;* symbol at the end of the line. |
| 164 | + |
| 165 | +###Coroutines |
| 166 | +Note: this section is being rethought for js await/async |
| 167 | + |
| 168 | +The below features have no not yet been implemented. |
| 169 | + |
| 170 | +The*&* symbol represents an async call, and you can tell a section of code to run in the background |
| 171 | +The*&* symbol tells us to start any following code as a coroutine. |
| 172 | + |
| 173 | +Let's have two threads sleep, then print something |
| 174 | + |
| 175 | +``` |
| 176 | +& 2 sleep "2 second passed" out |
| 177 | +& 1 sleep "1 second passed" out |
| 178 | +``` |
| 179 | + |
| 180 | +The output will be |
| 181 | + |
| 182 | +``` |
| 183 | +1 second passed |
| 184 | +2 second passed |
| 185 | +``` |
| 186 | + |
| 187 | +All threads must finish for a program to complete, or a program must call quit for a program to |
| 188 | +finish. |
| 189 | + |
| 190 | +Contexts are named or numbered. and you can sequence many operations on the same thread. |
| 191 | + |
| 192 | +``` |
| 193 | +0 & "do this first" out |
| 194 | +0 & "do this second" out |
| 195 | +``` |
| 196 | + |
| 197 | +Since we need a handle to access data that becomes available after an async call, |
| 198 | + |
| 199 | +``` |
| 200 | +alarm: & 5 sleep "I just slept!" |
| 201 | +
|
| 202 | +alarm out # wake-up on event (availability of future 'alarm') |
| 203 | +``` |
| 204 | + |
| 205 | +###Events |
| 206 | + |
| 207 | +a keypress |
| 208 | +'a pressed!' out |
| 209 | + |
| 210 | +###Callbacks/Backcalls |
| 211 | + |
| 212 | +``` |
| 213 | +test |
| 214 | + blah |
| 215 | +``` |
| 216 | + |
| 217 | +This is equivalent to test(function(){ return blah(); }) |
| 218 | + |
| 219 | +``` |
| 220 | +test ^ |
| 221 | +blah |
| 222 | +``` |
| 223 | + |
| 224 | +This is also equivalent to test(function(){ return blah(); }) |
| 225 | + |
| 226 | +###What now? |
| 227 | + |
| 228 | +Work in progress :) |
| 229 | + |