Back
Close

Complicated interpreter

Statement

 Goal

Your task is to create an interpreter for a language that has 12 instructions (excluding comments). At the end, you have to output all the variable values separated by a space.

This language doesn't care about syntax, so for an example: "x = 2 n function x add 1" is legal, you could define a function and a variable in the same line, and this applies for everything else.

When defining functions/if statements/loops, you need to know that all the code that goes after it in the same line is its body. There's nothing that indicates their body end other than the end of the line.

When referencing variables, a dollar sign should be at the start, so: $x would represent the value of variable x, and x add $y would mean add y to x, when passing variables to an instruction that'll modify the given variable, you need to give the variable itself but not it's value. Variables that aren't valid should return an error, and in that case, print ERROR and stop the execution.

Comments are represented by a //, comments can be anywhere in the line, everything in the line after the comment should be ignored, while the rest before it executed.

Cases of errors: Invalid variables and invalid functions.
Ex: Referencing variable x which does not exist, or calling function x which also does not exist.
In case of an error: print ERROR and stop the execution

Variables are ordered by the order at which they were first created, so the following example would output 5 1:
x = 5
y = 3
z = 1
delete y
print

Instructions

Format: Instruction || Description || Usage

print || Stop execution and print the variables || print
= || Assigning variables, variables will always be integers || identifier = value
add || Add value to variable || variable add value
sub || Subtract value from variable || variable sub value
mult || Multiply variable by value || variable mult value
delete || Delete variable from memory || delete variable
loop || Executes body for x amount of times || loop x do body
function || Create function name, everything after it is the function body, for this puzzle, this will not take arguments, nor return a value, but do changes || name function body

Logical instructions

Everything here is only used in if blocks.

if || If the condition given is true, execute the body, otherwise skip it || if condition then body
== || Equality operator, If x is equal to y then it returns true|| value == value
!= || Not equal operator, if x is not equal to y then it returns true || value != value
or || Logical or operator, If at least one of the conditions are true it returns true || cond1 or cond2
and || Logical and operator, If both of the conditions are true it returns true || cond1 and cond2

Logical instructions accept literal integers or variable values, so variables are referenced when given.

A function call is a function call if it ends with (), functions and variables can have the same name. All names consist of letters only.
Input
Line 1: An integer n for the number of lines
Next n lines: A string consisting of one or more instructions
Output
Line 1: All variable values separated by a space
Constraints
2 <= n <= 8
Example
Input
2
x = 50
print
Output
50

Tags
InterpretersParsingLogic

Difficulty
Medium

Test cases
One variable Test
Input
2 x = 50 print
Output
50

Validator 1 Validator
Input
4 x = 2 y = 4 z = 6 print
Output
2 4 6

Two variables and addition Test
Input
4 var = 50 y = 50 var add 32 print
Output
82 50

Validator 2 Validator
Input
4 x = 12 y = 32 x add 50 print
Output
62 32

Subtraction and referencing Test
Input
6 x = 1000 y = 2000 x add $y x sub 500 y sub 500 print
Output
2500 1500

Validator 3 Validator
Input
4 x = 50 y = 50 x sub $y print
Output
0 50

Invalid Variable Test
Input
4 x = 50 x sub $y x add $y print
Output
ERROR

Validator 4 Validator
Input
3 x = 50 x add $y print
Output
ERROR

Multiplication Test
Input
6 x = 50 y = 90 x sub $y x mult $y x add $x print
Output
-7200 90

Validator 5 Validator
Input
4 x = 1000 y = 10 x mult $y print
Output
10000 10

Comments Test
Input
6 x = 50 //y = 5 //This won't do anything x add $x //x add $x print x add $x
Output
100

Validator 6 Validator
Input
3 x = 1 //y = 10 print
Output
1

Delete Test
Input
6 x = 50 y = $x x add $y x mult $y delete y print
Output
5000

Validator 7 Validator
Input
5 x = 90 y = 20 y add $x delete x print
Output
110

Loops Test
Input
7 x = 50 y = $x loop 50 do x add $y x sub $y print delete y z add $x
Output
2500 50

Validator 9 Validator
Input
3 x = 50 loop 5 do x add $x print
Output
1600

Long code Test
Input
8 foo = 500 //y = 10 z = 50 foo sub 50 foo mult $foo foo add 50000 loop 50 do loop 50 do loop 50 do foo add $y print
Output
ERROR

Validator 10 Validator
Input
8 x = 12 y = 20 //z = -22 x sub $y x add $y x mult $y loop 50 do x add $z print
Output
ERROR

Functions Test
Input
7 x = 500 y = 600 y add 600 //z = 0 firstfunc function x add $y firstfunc() delete y print
Output
1700

Validator 11 Validator
Input
8 x = 25 //y = 3 func function x add 8 func() func() func() func() print
Output
57

If statements and equality operators Test
Input
7 x = 5 y = 5 x function if $x != 8 then x mult 3 if 3 == 3 then x add 3 x() delete y print
Output
8

Validator 12 Validator
Input
5 x = 5 y = 1 if $y == 1 then x add $y if $x != 6 then x add 1 print
Output
6 1

or operator Test
Input
4 foo = 5 bar = 5 if $foo != 5 or $bar == 5 then foo add 5 print
Output
10 5

Validator 13 Validator
Input
5 x = 6 y = $x if $x != 6 or $y == 6 then x add $y if 6 != 6 then x add 2 print
Output
12 6

and operator Test
Input
4 x = 2 y = 0 if $y == 0 and $x == 2 then x add 4 print
Output
6 0

Validator 14 Validator
Input
4 x = 6 y = 5 if $x == $x and $y == 3 then x add 5 print
Output
6 5

Error if statement Test
Input
4 x = 5 y = 6 if $z == $x and $y == $y then x add 6 print
Output
ERROR

Validator 15 Validator
Input
4 z = 5 y = 6 if $x == $z and y != $z then x add 0 print
Output
ERROR

Function reassignment and one line calls Test
Input
5 x = 6 x function x add 5 x function x add 6 x() x() x() print
Output
24

Validator 16 Validator
Input
7 x = 5 y = $x x = $y n function x add 5 n function x add 6 n() print n()
Output
11 5

Invalid function Test
Input
4 x = 5 n function x add 5 x() print
Output
ERROR

Validator 17 Validator
Input
4 z = 5 x function z add $x n() print
Output
ERROR

Nested loops Test
Input
5 x = 0 x function loop 5 do loop 5 do x add 1 x() x() print
Output
50

Validator 18 Validator
Input
5 x = 0 n function loop 50 do loop 50 do loop 50 do x add 1 n() n() print
Output
250000

Solution language

Solution

Stub generator input