electricwah zrewidował ten Gist 3 months ago. Przejdź do rewizji
1 file changed, 441 insertions
combined_compiler.nv(stworzono plik)
| @@ -0,0 +1,441 @@ | |||
| 1 | + | | :: get cause 1 | | |
| 2 | + | :stack: A | |
| 3 | + | :pattern: . a | |
| 4 | + | . b | |
| 5 | + | . variable c | |
| 6 | + | . d | |
| 7 | + | . variable e | |
| 8 | + | :index: ( 0 1 2 3 4 ) | |
| 9 | + | | :: get cause 2 | | |
| 10 | + | :stack: A | |
| 11 | + | :pattern: . stride | |
| 12 | + | . test | |
| 13 | + | :index: ( 0 1 ) | |
| 14 | + | | :: get cause 3 | | |
| 15 | + | :stack: B | |
| 16 | + | :pattern: . other | |
| 17 | + | . cause | |
| 18 | + | . match | |
| 19 | + | . variable c | |
| 20 | + | :index: ( 0 1 2 3 ) | |
| 21 | + | | :: get effect 1 | | |
| 22 | + | :stack: out_A | |
| 23 | + | :items: . output | |
| 24 | + | . one | |
| 25 | + | . variable c | |
| 26 | + | :index: ( 0 1 2 ) | |
| 27 | + | | :: get effect 2 | | |
| 28 | + | :stack: out_B | |
| 29 | + | :items: . output | |
| 30 | + | . 2 | |
| 31 | + | . variable e | |
| 32 | + | :index: ( 0 1 2 ) | |
| 33 | + | ||
| 34 | + | | :: get rule 1 | | |
| 35 | + | :causes: ( 1 2 3 ) | |
| 36 | + | :effects: ( 1 2 ) | |
| 37 | + | ||
| 38 | + | | :: get cause 4 | | |
| 39 | + | :stack: ohnoes | |
| 40 | + | :pattern: . a | |
| 41 | + | . second | |
| 42 | + | . rule | |
| 43 | + | :index: ( 0 1 2 ) | |
| 44 | + | | :: get effect 3 | | |
| 45 | + | :stack: x | |
| 46 | + | :items: . what | |
| 47 | + | . shall | |
| 48 | + | . we | |
| 49 | + | . do | |
| 50 | + | :index: ( 0 1 2 3 ) | |
| 51 | + | ||
| 52 | + | | :: get rule 2 | | |
| 53 | + | :causes: ( 4 ) | |
| 54 | + | :effects: ( 3 ) | |
| 55 | + | ||
| 56 | + | || :rules: ( 1 2 ) | |
| 57 | + | ||
| 58 | + | |------------------------------| | |
| 59 | + | |- handle incrementing -| | |
| 60 | + | |------------------------------| | |
| 61 | + | | :@math: add 0 1 | :@math: 1 | |
| 62 | + | | :@math: add 1 1 | :@math: 2 | |
| 63 | + | | :@math: add 2 1 | :@math: 3 | |
| 64 | + | | :@math: add 3 1 | :@math: 4 | |
| 65 | + | | :@math: add 4 1 | :@math: 5 | |
| 66 | + | | :@math: add 5 1 | :@math: 6 | |
| 67 | + | | :@math: add 6 1 | :@math: 7 | |
| 68 | + | | :@math: add 7 1 | :@math: 8 | |
| 69 | + | | :@math: add 8 1 | :@math: 9 | |
| 70 | + | | :@math: add 9 1 | :@math: 10 | |
| 71 | + | | :@math: add 10 1 | :@math: 11 | |
| 72 | + | | :@math: add 11 1 | :@math: 12 | |
| 73 | + | | :@math: add 12 1 | :@math: 13 | |
| 74 | + | | :@math: add 13 1 | :@math: 14 | |
| 75 | + | | :@math: add 14 1 | :@math: 15 | |
| 76 | + | | :@math: add 1 $x | :@math: add $x 1 | |
| 77 | + | |------------------------------| | |
| 78 | + | ||
| 79 | + | ||
| 80 | + | ||
| 81 | + | |# run with > nova -d mathpolyfill.nv data.nv emitter.nv < #| | |
| 82 | + | ||
| 83 | + | ||
| 84 | + | | :: process rule :rules: $ruleid | | |
| 85 | + | :: get rule $ruleid :current rule id: $ruleid | |
| 86 | + | :: process causes | |
| 87 | + | :: clear causes temp state | |
| 88 | + | :: check stack depths | |
| 89 | + | :: process effects | |
| 90 | + | :: clear rule temp state | |
| 91 | + | ||
| 92 | + | | :: clear rule temp state? :stack: $stack | | |
| 93 | + | | :: clear rule temp state? :vars: variable $x = index $y of stack $z at depth $q | | |
| 94 | + | ||
| 95 | + | ||
| 96 | + | | :: clear rule temp state | | |
| 97 | + | ||
| 98 | + | ||
| 99 | + | | :: process causes? :causes: $causeid | | |
| 100 | + | :: get cause $causeid :length: 0 | |
| 101 | + | :: check stack | |
| 102 | + | :: process pattern | |
| 103 | + | :: check fact lengths | |
| 104 | + | :: pop cause | |
| 105 | + | ||
| 106 | + | ||
| 107 | + | | :: process causes | | |
| 108 | + | ||
| 109 | + | ||
| 110 | + | |:: check stack depths| | |
| 111 | + | ||
| 112 | + | | :: check fact lengths | |
| 113 | + | :stack: $stack? | |
| 114 | + | :current stride depth: $depth ? | |
| 115 | + | :length: $length | | |
| 116 | + | :check fact lengths: check stack $stack depth $depth has length $length | |
| 117 | + | ||
| 118 | + | | :: clear causes temp state :current stride depth: $n | | |
| 119 | + | ||
| 120 | + | | :: check stack :stack: $stack? | |
| 121 | + | | :: add stack $stack | |
| 122 | + | ||
| 123 | + | | :: pop cause | |
| 124 | + | :stack: $stack | | |
| 125 | + | :pop causes: pop $stack | |
| 126 | + | ||
| 127 | + | |------------------------------| | |
| 128 | + | ||
| 129 | + | | :: add stack $stack? | |
| 130 | + | :current stride depth: $n | | |
| 131 | + | ||
| 132 | + | | :: add stack $stack :@math: $depth | | |
| 133 | + | :current stride depth: $depth | |
| 134 | + | :strides: check stack $stack has at least depth $depth | |
| 135 | + | :: strides temp -> strides | |
| 136 | + | ||
| 137 | + | | :: add stack $stack? :strides: check stack $stack has at least depth $depth | | |
| 138 | + | :@math: add $depth 1 | |
| 139 | + | ||
| 140 | + | ||
| 141 | + | | :: add stack $stack? :strides: check stack $different has at least depth $depth | | |
| 142 | + | :strides temp: check stack $different has at least depth $depth | |
| 143 | + | ||
| 144 | + | | :: add stack $stack | | |
| 145 | + | :current stride depth: 1 | |
| 146 | + | :strides temp: check stack $stack has at least depth 1 | |
| 147 | + | :: strides temp -> strides | |
| 148 | + | ||
| 149 | + | | :: strides temp -> strides? | |
| 150 | + | :strides temp: check stack $stack has at least depth $depth | | |
| 151 | + | :strides: check stack $stack has at least depth $depth | |
| 152 | + | ||
| 153 | + | | :: strides temp -> strides | | |
| 154 | + | ||
| 155 | + | |------------------------------| | |
| 156 | + | ||
| 157 | + | | :: increment length :length: $old :@math: $n | :length: $n | |
| 158 | + | | :: increment length? :length: $n? | :@math: add $n 1 | |
| 159 | + | ||
| 160 | + | ||
| 161 | + | | | |
| 162 | + | :: process pattern? | |
| 163 | + | :stack: $stack? | |
| 164 | + | :pattern: variable $name | |
| 165 | + | :index: $i | |
| 166 | + | :current stride depth: $depth? | |
| 167 | + | | | |
| 168 | + | :: do variable $name at index $i of stack $stack at depth $depth | |
| 169 | + | :: increment length | |
| 170 | + | ||
| 171 | + | |- var is already declared -| | |
| 172 | + | | :: do variable $name at index $i of stack $stack at depth $depth | |
| 173 | + | :vars: variable $name = index $j of stack $stack2 at depth $depth2? | |
| 174 | + | | | |
| 175 | + | :check vars: check index $i of top of stack $stack at depth $depth equals value of var $name | |
| 176 | + | :: vars temp -> vars | |
| 177 | + | ||
| 178 | + | |# can also do this to give more information #| | |
| 179 | + | :check vars: check index $i of top of stack $stack at depth $depth equals | |
| 180 | + | var $name stack $stack2 depth $depth2 index $index2 | |
| 181 | + | ||
| 182 | + | |- not the right var, check next -| | |
| 183 | + | | :: do variable $name at index $i of stack $stack at depth $depth? | |
| 184 | + | :vars: variable $different = index $j of stack $stack2 at depth $depth2 | |
| 185 | + | | | |
| 186 | + | :vars temp: variable $different = index $j of stack $stack2 at depth $depth2 | |
| 187 | + | ||
| 188 | + | ||
| 189 | + | |- variable isn't declared yet, so declare it -| | |
| 190 | + | | :: do variable $name at index $i of stack $stack at depth $depth | | |
| 191 | + | :set vars: declare variable $name = index $i of stack $stack at depth $depth | |
| 192 | + | :vars: variable $name = index $i of stack $stack at depth $depth | |
| 193 | + | :: vars temp -> vars | |
| 194 | + | ||
| 195 | + | ||
| 196 | + | ||
| 197 | + | | :: vars temp -> vars | |
| 198 | + | :vars temp: variable $name = index $i of stack $stack at depth $depth | |
| 199 | + | | | |
| 200 | + | :vars: variable $name = index $i of stack $stack at depth $depth | |
| 201 | + | ||
| 202 | + | | :: vars temp -> vars | | |
| 203 | + | ||
| 204 | + | | | |
| 205 | + | :: process pattern? | |
| 206 | + | :stack: $stack? | |
| 207 | + | :pattern: $symbol | |
| 208 | + | :index: $i | |
| 209 | + | :current stride depth: $depth? | |
| 210 | + | | | |
| 211 | + | :check facts: check stack $stack at depth $depth matches symbol $symbol at index $i | |
| 212 | + | :: increment length | |
| 213 | + | ||
| 214 | + | | :: process pattern | | |
| 215 | + | ||
| 216 | + | |--------------------| | |
| 217 | + | ||
| 218 | + | ||
| 219 | + | | :: process effects? :effects: $effectid | | |
| 220 | + | :: get effect $effectid | |
| 221 | + | :: push effects start | |
| 222 | + | :: process items | |
| 223 | + | :: push effects end | |
| 224 | + | ||
| 225 | + | | :: process effects | | |
| 226 | + | ||
| 227 | + | | :: push effects start | :push effects: start fact to push | |
| 228 | + | | :: push effects end | :push effects: end fact to push | |
| 229 | + | ||
| 230 | + | ||
| 231 | + | | | |
| 232 | + | :: process items? | |
| 233 | + | :stack: $stack? | |
| 234 | + | :items: $symbol | |
| 235 | + | :index: $i | |
| 236 | + | | | |
| 237 | + | :push effects: symbol $symbol | |
| 238 | + | ||
| 239 | + | | | |
| 240 | + | :: process items? | |
| 241 | + | :stack: $stack? | |
| 242 | + | :items: variable $name | |
| 243 | + | :index: $i | |
| 244 | + | | | |
| 245 | + | :push effects: variable $name | |
| 246 | + | ||
| 247 | + | | :: process items | | |
| 248 | + | ||
| 249 | + | ||
| 250 | + | | :emit: rule header $ruleid | |
| 251 | + | | :out: rule header $ruleid | |
| 252 | + | ||
| 253 | + | | :emit: check stack heights? | |
| 254 | + | :strides: check stack $stack has at least depth $depth | |
| 255 | + | | :out: check stack $stack has at least depth $depth | |
| 256 | + | | :emit: check stack heights | | |
| 257 | + | ||
| 258 | + | | :emit: check facts lengths? | |
| 259 | + | :check fact lengths: check stack $stack depth $depth has length $length | |
| 260 | + | | :out: check stack $stack depth $depth has length $length | |
| 261 | + | | :emit: check facts lengths | | |
| 262 | + | ||
| 263 | + | | :emit: check facts constant symbol matches? | |
| 264 | + | :check facts: check stack $stack at depth $depth matches symbol $symbol at index $index | |
| 265 | + | | :out: check stack $stack at depth $depth matches symbol $symbol at index $index | |
| 266 | + | | :emit: check facts constant symbol matches | | |
| 267 | + | ||
| 268 | + | | :emit: set variables? | |
| 269 | + | :set vars: declare variable $name = index $i of stack $stack at depth $depth | |
| 270 | + | | :out: declare variable $name = index $i of stack $stack at depth $depth | |
| 271 | + | | :emit: set variables | | |
| 272 | + | ||
| 273 | + | | :emit: check variables equality? | |
| 274 | + | :check vars: check index $i of top of stack $stack at depth $depth equals value of var $name | |
| 275 | + | | :out: check index $i of top of stack $stack at depth $depth equals value of var $name | |
| 276 | + | | :emit: check variables equality | | |
| 277 | + | ||
| 278 | + | | :emit: pop causes ? | |
| 279 | + | :pop causes: $a $b | |
| 280 | + | | :out: $a $b | |
| 281 | + | ||
| 282 | + | | :emit: pop causes | | |
| 283 | + | ||
| 284 | + | | :emit: push effects -> push effects reversed? | |
| 285 | + | :push effects: $a $b | |
| 286 | + | | :push effects reversed: $a $b | |
| 287 | + | ||
| 288 | + | | :emit: push effects -> push effects reversed? | |
| 289 | + | :push effects: $a $b $c $d | |
| 290 | + | | :push effects reversed: $a $b $c $d | |
| 291 | + | | :emit: push effects -> push effects reversed | | |
| 292 | + | ||
| 293 | + | | :emit: push effects reversed -> out? | |
| 294 | + | :push effects reversed: $a $b $c $d | |
| 295 | + | | :out: $a $b $c $d | |
| 296 | + | ||
| 297 | + | | :emit: push effects reversed -> out? | |
| 298 | + | :push effects reversed: $a $b | |
| 299 | + | | :out: $a $b | |
| 300 | + | | :emit: push effects reversed -> out | | |
| 301 | + | ||
| 302 | + | | :emit: push effects | | |
| 303 | + | :emit: push effects -> push effects reversed | |
| 304 | + | :emit: push effects reversed -> out | |
| 305 | + | ||
| 306 | + | | :emit: rule footer | |
| 307 | + | | :out: rule footer | |
| 308 | + | ||
| 309 | + | | :: emit rule | |
| 310 | + | :current rule id: $ruleid | |
| 311 | + | | :: emitting rule | |
| 312 | + | :emit: | |
| 313 | + | . rule header $ruleid | |
| 314 | + | . check stack heights | |
| 315 | + | . check facts lengths | |
| 316 | + | . check facts constant symbol matches | |
| 317 | + | . set variables | |
| 318 | + | . check variables equality | |
| 319 | + | . pop causes | |
| 320 | + | . push effects | |
| 321 | + | . rule footer | |
| 322 | + | ||
| 323 | + | | :: emitting rule | | |
| 324 | + | ||
| 325 | + | |- :vim:sw=3:ts=3:et: -| | |
| 326 | + | ||
| 327 | + | | :: translate? :out: rule header $ruleid | | |
| 328 | + | :@js: f('final out', `function rule${$ruleid} () {`); | |
| 329 | + | f('rule functions', `rule${$ruleid}`); | |
| 330 | + | ||
| 331 | + | | :: translate? :out: check stack $stack has at least depth $depth | | |
| 332 | + | :@js: f('final out', `if ( stack["${$stack}"].length < ${$depth} ) return false;`) | |
| 333 | + | ||
| 334 | + | | :: translate? :out: check stack $stack depth $depth has length $length | | |
| 335 | + | :@js: f('final out', `if ( stack["${$stack}"][${$depth}].length != ${$length} ) return false;`) | |
| 336 | + | ||
| 337 | + | | :: translate? :out: check stack $stack at depth $depth matches symbol $symbol at index $index | | |
| 338 | + | :@js: f('final out', `if ( stack["${$stack}"][${$depth}][${$index}] != "${$symbol}" ) return false;`) | |
| 339 | + | ||
| 340 | + | | :: translate? :out: declare variable $name = index $index of stack $stack at depth $depth | | |
| 341 | + | :@js: f('final out', `let ${$name} = stack["${$stack}"][${$depth}][${$index}];`) | |
| 342 | + | ||
| 343 | + | | :: translate? :out: check index $i of top of stack $stack at depth $depth equals value of var $name | | |
| 344 | + | :@js: f('final out', `if ( stack["${$stack}"][${$depth}][${$i}] != ${$name} ) return false;`) | |
| 345 | + | ||
| 346 | + | | :: translate? :out: start fact to push | | |
| 347 | + | :final out: f( | |
| 348 | + | ||
| 349 | + | | :: translate? :out: symbol $symbol | | |
| 350 | + | :@js: f('final out',`"${$symbol}",`) | |
| 351 | + | ||
| 352 | + | | :: translate? :out: variable $varname | | |
| 353 | + | :@js: f('final out',`${$varname},`) | |
| 354 | + | ||
| 355 | + | | :: translate? :out: pop $stack | | |
| 356 | + | :@js: f('final out',`stack["${$stack}"].pop();`) | |
| 357 | + | ||
| 358 | + | | :: translate? :out: end fact to push | | |
| 359 | + | :final out: ); | |
| 360 | + | ||
| 361 | + | | :: translate? :out: rule footer | | |
| 362 | + | :final out: } :final out: [] | |
| 363 | + | ||
| 364 | + | | :: emit file header | | |
| 365 | + | :final out: [/* stub */] :final out: [] | |
| 366 | + | ||
| 367 | + | | :: translate | | |
| 368 | + | ||
| 369 | + | |#========#| | |
| 370 | + | ||
| 371 | + | | :: breakpoint | | |
| 372 | + | | :: breakpoint | :@js: me.gas = 0; | |
| 373 | + | ||
| 374 | + | || :: emit file header :: output | |
| 375 | + | :: process, emit, and output rules | |
| 376 | + | :: emit step function ::output | |
| 377 | + | :: output main loop | |
| 378 | + | ||
| 379 | + | | :: process, emit, and output rules ? :rules: $n?| | |
| 380 | + | :: breakpoint | |
| 381 | + | :: process rule | |
| 382 | + | :: breakpoint | |
| 383 | + | :: emit rule | |
| 384 | + | :: breakpoint | |
| 385 | + | :: translate | |
| 386 | + | :: breakpoint | |
| 387 | + | :: output | |
| 388 | + | :: breakpoint | |
| 389 | + | | :: process, emit, and output rules | | |
| 390 | + | ||
| 391 | + | ||
| 392 | + | |# in reverse order because we are pushing directly to the final out stack #| | |
| 393 | + | | :: emit step function | | |
| 394 | + | :: emit step function footer | |
| 395 | + | :: emit rule function names | |
| 396 | + | :: emit step function header | |
| 397 | + | ||
| 398 | + | | :: emit step function header | | |
| 399 | + | :final out: [function step () {] | |
| 400 | + | ||
| 401 | + | | :: emit rule function names? :rule functions: $fname | | |
| 402 | + | :@js: f('final out',`if (${$fname}()) return true;`) | |
| 403 | + | | :: emit rule function names | | |
| 404 | + | ||
| 405 | + | | :: emit step function footer | | |
| 406 | + | :final out: } :final out: [] | |
| 407 | + | ||
| 408 | + | | :: output main loop | | |
| 409 | + | :: output | |
| 410 | + | :final out: . [function run() {] | |
| 411 | + | . [ while (step()) {] | |
| 412 | + | . [ // nop] | |
| 413 | + | . [ }] | |
| 414 | + | . [}] | |
| 415 | + | ||
| 416 | + | ||
| 417 | + | |#===============#| | |
| 418 | + | || '@include' lib/platforms/browser_dom.nv | |
| 419 | + | ||
| 420 | + | || :DOM: ( <query #code-actions <@after #out or textarea id = out value = [] ) | |
| 421 | + | ||
| 422 | + | ||
| 423 | + | ||
| 424 | + | | :: output reversed? :first: :reversed: $x | :append: $x | |
| 425 | + | | :append: \n :current dom element: $o?| :@js: $o.value += '\n'; | |
| 426 | + | | :append: \t :current dom element: $o?| :@js: $o.value += '\t'; | |
| 427 | + | | :append: $x :current dom element: $o?| :@js: $o.value += $x; | |
| 428 | + | ||
| 429 | + | ||
| 430 | + | | :: output reversed? :reversed: label :in label: | :append: ( label \n \t \t ) | |
| 431 | + | | :: output reversed? :reversed: label | :append: ( \n \t label [ ] ) :in label: | |
| 432 | + | | :: output reversed? :reversed: rule | :append: ( \n rule [ ] ) | |
| 433 | + | | :: output reversed? :reversed: end | :append: ( \n end ) | |
| 434 | + | | :: output reversed? :reversed: $x | :append: ( $x [ ] ) | |
| 435 | + | ||
| 436 | + | | :: output reversed | :DOM: ( > > ) | |
| 437 | + | ||
| 438 | + | |#===============#| | |
| 439 | + | ||
| 440 | + | | :: output :current dom element: $out? | | |
| 441 | + | :@js: $out.value += (stacks['final out'].map(x=>x.join('')).toReversed().join('\n')+'\n'); delete stacks['final out']; | |
Nowsze
Starsze