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

Content added Content deleted
No edit summary
(removed some unneeded code from the implementation)
Line 47: Line 47:
right=: [:/:~/ [:;"1 (#:1 2) <@I.@E."1/ nesting
right=: [:/:~/ [:;"1 (#:1 2) <@I.@E."1/ nesting
next=: (1 + 1 { ])`1:`]}
next=: (1 + 1 { ])`1:`]}
traceNext=: (1 + 1 { ])`1:`]} ([ smoutput) NB. version of next for debugging crashes


NB. interpreter
NB. interpreter
OPCODES=: '><+-.,[]'
OPCODES=: '><+-.,[]'
OPS=: inc`dec`INC`DEC`OUT`ACC`FIXME`FIXME`NOP
OPS=: inc`dec`INC`DEC`OUT`ACC`FIXME`FIXME`NOP
PASS1=: OPS {~ OPCODES i. ([-.-.)&OPCODES
compile=:3 :0
compile=:3 :0
src=. ([-.-.)&OPCODES y
src=. ([-.-.)&OPCODES y
Line 64: Line 62:
step=: [ next ] evoke~ pc { [
step=: [ next ] evoke~ pc { [


trace=: [: flush step^:(pc < #@[)^:a:
run=: [: flush step^:(pc < #@[)^:_
run=: [: flush step^:(pc < #@[)^:_


Line 119: Line 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).
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</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.
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:
When investigating a bug, you can use:


<lang J>next=: traceNext</lang>
<lang J>next=: (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.
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.