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

Content added Content deleted
m (update for j804)
m (Fixed syntax highlighting.)
 
Line 1: Line 1:
<lang J>require'general/misc/prompt' NB. was require'misc' in j602
<syntaxhighlight lang="j">require'general/misc/prompt' NB. was require'misc' in j602


NB. operations
NB. operations
Line 63: Line 63:
run=: [: flush step^:(pc < #@[)^:_
run=: [: flush step^:(pc < #@[)^:_


execute=: 2 }. compile@] run 0 0,0: :[</lang>
execute=: 2 }. compile@] run 0 0,0: :[</syntaxhighlight>


Here is hello world:
Here is hello world:


<lang J>hello=: 0 :0
<syntaxhighlight lang="j">hello=: 0 :0
+++++ +++++ initialize counter (cell #0) to 10
+++++ +++++ initialize counter (cell #0) to 10
[ use loop to set the next four cells to 70/100/30/10
[ use loop to set the next four cells to 70/100/30/10
Line 89: Line 89:
> + . print '!'
> + . print '!'
> . print '\n'
> . print '\n'
)</lang>
)</syntaxhighlight>


Example use:
Example use:
<lang J> execute hello
<syntaxhighlight lang="j"> execute hello
Hello World!
Hello World!


0 87 100 33 10</lang>
0 87 100 33 10</syntaxhighlight>


execute compiles the program, generates an initial state of a blank tape with the data and instruction pointers both being zero, runs the program and then returns as its result the final state of the tape.
execute compiles the program, generates an initial state of a blank tape with the data and instruction pointers both being zero, runs the program and then returns as its result the final state of the tape.
Line 103: Line 103:
Or, here is the addDigit program from wikipedia, with explicit compilation and run as separate operations:
Or, here is the addDigit program from wikipedia, with explicit compilation and run as separate operations:


<lang J> (compile ',>++++++[<-------->-],[<+>-]<.') run 0 0
<syntaxhighlight lang="j"> (compile ',>++++++[<-------->-],[<+>-]<.') run 0 0


23
23
5
5
0 30 53 0</lang>
0 30 53 0</syntaxhighlight>


Here, 2 and 3 were provided as input, and 5 was displayed as the result.
Here, 2 and 3 were provided as input, and 5 was displayed as the result.
Line 119: Line 119:
When investigating a bug, you can use:
When investigating a bug, you can use:


<lang J>next=: (1 + 1 { ])`1:`]} ([ smoutput)</lang>
<syntaxhighlight lang="j">next=: (1 + 1 { ])`1:`]} ([ smoutput)</syntaxhighlight>


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.