I've taken a straightforward approach using locale name space to track
which nouns are known, and to process the instructions for which the
names are known, meanwhile gained experience with stack errors and f. ,
vectors and scalars and {:: .
Note 'solve with'
LOCALE =: f'instruction.file'
a__LOCALE
)
BITS16 =:16bffff
NOT=:BITS16 AND 0&(2b11010 b.)
RSHIFT =: BITS16 AND (33 b.~ -)
LSHIFT=: BITS16 AND 33 b.~
OR=:2b10111 b.
AND=:2b10001 b.
Note 'unused'
uc=: ((26{.Alpha_j_),a.) {~ ((26}.Alpha_j_),a.)&i. NB. capitalize
While=: 2 :'u^:(0-.@-:v)^:_'
Until=: 2 :'u^:(0-:v)^:_'
)
f=: 3 :0 NB. f 'instruction.file'
L0=. 18!:3''
smoutput L0;(datatype L0);(coname'')
isnoun__L0 =: isnoun f.
verbg__L0 =: verbg f.
BITS16__L0 =: BITS16
NOT__L0=: NOT f.
RSHIFT__L0=: RSHIFT f.
LSHIFT__L0=: LSHIFT f.
OR__L0=: OR f.
AND__L0=: AND f.
NB. make a boxed list of words for each line of the file
NB. but first convert x and y to X and Y to avoid x:= 'domain error'
NB. and LF or CR already known nouns.
C=. 1!:1@<y
B=. (('XY',a.) {~ ('xy',a.)&i.)C
A=. (<@;:;._2) B
NB. repeat processing the instructions for which the inputs are known
NB. until finished.
while. #A do.
N=.verbg__L0&> A NB. N masks the instructions that occurred
assert 0 < +/ N NB. Are we making progress?
A =. (-.N)#A
end.
L0
)
isnoun =: ((_ ~: _ ". 0 {:: ,) +. (0 -: 4!:0))"0
verbg=:3 :0 NB. return 1 on success, 0 otherwise
0
if. ((4=#) *. ([:isnoun 0&{)) y do.
S =. ((_1&{::),' =: ',0&{::) y
smoutput S NB. display the instructions for fun
".S
1
elseif. (('NOT' -: 0&{::) *. ([: isnoun 1&{)) y do.
S =. ((_1&{::) , ' =: NOT ' , (1&{::)) y
smoutput S
".S
1
elseif. ([: *./ [: isnoun 0 2&{) y do.
S =. ((_1&{::) , ' =: ' , [: ;:inv 3&{.) y
smoutput S
".S
1
end.
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm