Execute Brain****/J: Difference between revisions

removed some unneeded code from the implementation
No edit summary
(removed some unneeded code from the implementation)
Line 47:
right=: [:/:~/ [:;"1 (#:1 2) <@I.@E."1/ nesting
next=: (1 + 1 { ])`1:`]}
traceNext=: (1 + 1 { ])`1:`]} ([ smoutput) NB. version of next for debugging crashes
 
NB. interpreter
OPCODES=: '><+-.,[]'
OPS=: inc`dec`INC`DEC`OUT`ACC`FIXME`FIXME`NOP
PASS1=: OPS {~ OPCODES i. ([-.-.)&OPCODES
compile=:3 :0
src=. ([-.-.)&OPCODES y
Line 64 ⟶ 62:
step=: [ next ] evoke~ pc { [
 
trace=: [: flush step^:(pc < #@[)^:a:
run=: [: flush step^:(pc < #@[)^:_
 
Line 119 ⟶ 116:
Note this implementation encodes branch targets into the compiled code, and that <code>compile</code> assumes that <code>[</code> and <code>]</code> are balanced. Unbalanced brackets are not supported (and will cause the first instruction of the program to be replaced with an unspecified branch instruction).
 
You can also use <code>trace=: [: flush step^:(pc < #@[)^:a:</code>, instead of <code>run</code>. <code>trace</code> takes the same arguments as run, but will return every state the tape went through, instead of the final state. This is mostly interested for understanding the behavior of small programs.
 
When investigating a bug, you can use:
 
<lang J>next=: traceNext(1 + 1 { ])`1:`]} ([ smoutput)</lang>
 
This means that programs when run (or traced) will display each tape state after it has been generated, which means that when the program crashes you will have displayed a tape state shortly before the crash.
6,951

edits