Build an interpreter for "Goggle kids Code"
Python 2, 131 126 123 110 104 bytes
Saved 13 bytes thanks to @dingledooper's approach of flattening nested list using backtick.
Saved 2 bytes thanks to @l4m2
p,t=0,[0]*100
for i in`input()`:
if"/"<i:i=int(i);p+=i/3-1
if i<2:t[p:p+2]=cmp(*t[p:p+2])==i,0
print t
Try it online! or Verify all test cases
Takes input from STDIN
, where /\>=x
are replaced with 6210*
Big idea:
Python 2 input()
implicitly evaluates the string read from STDIN
. Thus, reading modified input like ((6,1)*4,2)*13
returns a nested tuple of integers, aka
((6, 1, 6, 1, 6, 1, 6, 1), 2, (6, 1, 6, 1, 6, 1, 6, 1), 2, ...)
Note that loops containing single instruction must be terminated with comma, e.g (1,)*10
.
Now that all loops have been automatically unrolled for us, we just have to flatten the resulting tuple, and decode the instructions.
To flatten the nested tuple, we simply convert it to string, then takes all numerical characters.
JavaScript (Node.js), 149 bytes
s=>eval(`[${s.replace(/ \d+/g,'.toString().repeat($&)')}]+6`).replace(/\d/g,c=>c>4?p+=c-6:(X[p-1]=X[p-1]-X[p]==c,X[p--]=0),X=Array(100).fill(p=0))&&X
Try it online!
Retina 0.8.2, 135 bytes
,
\d+
$*
+`\([^()]+\)x|(?<=\(([^()]+)\)x1*)1
$1
^
0
{`(\d)/(\D*)((1)|0)?
$1$#4$2
(\d)\\(\D*)
$2$1
(?=(10>|(.)\2=))?.\d[>=](\D*)
$#1$+0
Try it online! Link includes test cases. Explanation:
,
Delete commas.
\d+
$*
Convert the multipliers to unary.
+`\([^()]+\)x|(?<=\(([^()]+)\)x1*)1
$1
Expand all repetitions from the inside out. (The last two stages can be performed by a single stage in Retina 1 at a saving of 19 bytes although you do then have to use ${3}
instead of $+
in the last stage.)
^
0
Start with a tape with a 0
. (The tape is automatically extended to the right as necessary, as this was slightly golfier than predefining the tape..)
{`
Process the instructions until they have all been executed.
(\d)/(\D*)((1)|0)?
$1$#4$2
Move right, extending the tape as necessary.
(\d)\\(\D*)
$2$1
Move left.
(?=(10>|(.)\2=))?.\d[>=](\D*)
$#1$+0
(In)equality. ($+
is used instead of $3
because the next character is a digit.)