Implementation a programming language named Tea. Exercise related to this topics:
- Recursive Programming
- C++ (as language)
- 2 Dimensional Arrays
- Struct Templates
- Stack Implementation
- Data Structures
A company's engineers decided to write simple programming language. That language would be easy to write, so has simple rules. Tea programs consist of subsequent lines of statements. Each statement has a simple job. There are 7 statements and 5 variables.
- inc
- Increments arg1 by arg2
- dec
- Decrements arg2 by arg2
- mul
- Multiplies arg1 by arg2
- div
- Divides arg1 by arg2
- function
- Defines a function named arg1
- call
- Calls arg1 with argument arg2
- return
- returns arg1
Engineers of the company all hated local variable declerations. So, Tea does not allow define variables. There are 5 predefined variables: [a,b,c,d,e]. A function can use these variables by accessing and operating on them. They all specific to a function. Every function uses different [a,b,c,d,e] variables. They are all 0 at initialization.
Functions are defined like;
function foo //operations with call, mul, inc, div, dec return a // or any variable
This function call initializes all variables for this call except e. e is taken by parent functions variable. (But it initializes again for new function, changes on it does not change parent's e). Due to return a statement, function finally assigns its a to the parent's a variable. Each Tea program has a main function and starts from this function. It is not necessity to place functions in certain order. A function can be defined and called before or after another functions including main.
function main inc a 2 inc b 3 call foo a call bar b inc d a inc d c return d function foo inc a 4 div a 2 return a function bar inc c 7 dec c b return c
Lines are 0-indexed
Executed Line | Next Line | [a,b,c,d,e] |
---|---|---|
0 | 1 | 0,0,0,0,0 |
1 | 2 | 2,0,0,0,0 |
2 | 3 | 2,3,0,0,0 |
3 | 9 | 2,0,0,0,0 |
9 | 10 | 6,0,0,0,0 |
10 | 11 | 3,0,0,0,0 |
11 | 4 | 3,3,0,0,0 |
4 | 13 | 0,3,0,0,0 |
13 | 14 | 0,3,7,0,0 |
14 | 15 | 0,3,4,0,0 |
15 | 5 | 3,3,4,0,0 |
5 | 6 | 3,3,4,3,0 |
6 | 7 | 3,3,4,7,0 |
7 | 3,3,4,7,0 |
compile:
g++ tea.cpp -o tea.exe
run:
tea.exe example_code
As I code this program, there is a problem with variables. They should initialize at start but every function uses new set of variables. Also functions can call functions, so i cannot use just one set of inputs for main, one for called functions. I needed segmentated memory. But I had limitation which is set by my instructor. In this limits:
I implemented variables as 2D array: T variables[LANGUAGE_DEPTH][5];
. Which means I have LANGUAGE_DEPTH times array of 5 variables. Variables[0] is set of variables of main function. If main calls a function i use Variables[1] for it. If it calls another, program uses Variables[2]. I say this k number in Variables[k] depth or degree of function. I use LANGUAGE_DEPTH property to scale power of Tea programming language. In other words, capability to work with nested functions. You can have more powerfull tea engine with increasing it. (Default: 5).