Forte is a very peculiar esoteric language based on the concept of modifying the values of numbers. In Forte numbers are not constants but variables, you can use the
LET instruction to assign new values to them.
For example, after executing
LET 2=4-1 from now on
2 assumes the value of
3, which means that whenever the value
2 comes up in an expression it is instead "replaced" by
3. The expression
(1+1)*2 would now evaluate to
This instruction in Forte is used both for storing information and for flow control (lines are numbered and by changing the value of their numbers you can determine the order of their execution). In this challenge we will not deal with this second aspect.
You are required to write an interpreter for a simplified subset of Forte's
You will receive as input a series of lines following this grammar:
<line>::= <number>=<expression> <expression>::= <number>|<expression>+<number>
Note: this grammar is not valid Forte because it lacks line numbers, LET, and parentheses (which are always mandatory)
That is, you will only need to deal with computing summations and assigning values to numbers. Parentheses won't be present in the input, and each expression will need to be evaluated from left to right: beware that partial results are affected by redefinitions!
Numbers will always be non-negative integers, up to the limit of your language's native integer type (or 2^32, whichever is higher).
For each line you should output the result of the expression and assign this result to the (possibly reassigned) value of the first number, which will affect how the following lines will be interpreted.
This is code-golf, the shortest code (in bytes) wins!
- The input format is flexible, you can for example take a single string with newlines, a list of strings, a list of lists of numbers... The same goes for the output, as long as it's clear what's the result of each expression in the input.
- You may submit either a function, a full program, or a solution to be run in a REPL environment calling it once for each line.
- Standard loopholes are forbidden, in particular you can't call an external Forte interpreter in your code.
These are all part of the same input. After each line the expected output relative to that line is shown, sometimes with a comment indicating relevant reassignments (not part of the required output).
5=4 4 6=5 4 # 5 -> 4 7=1+2+5 7 7=5+2+1 4 # Order of operations matters! 5+2 -> 4+2 -> 6 -> 4 18=5+6+7 12 5=3 3 # Remember: 5 -> 4 10=6+4 3 # 6 -> 4 -> 3, 3+3 = 6 -> 3