Making a Programming Language From Scratch/Complex Expressions

Parenthesizes edit

The previous chapter deals with the processing of expressions devoid of parenthesizes. However in real-world model, usually more than one level of expressions are used, and that obviously means the usage of parenthesizes.

Syntax

...Expression...(...Expression...More parenthesizes...)...

Algorithm for dealing with Parenthesis.

1.Go through the entire line. Increment index of array until element is ';' or ')'.
(This assumes that the expression has gone through function call processing, which is dealt with later.)
 1.1 If ';' terminate process
 1.2 If ')' continue.
2. While element not '(' decrement index.
3. Remove segment thus isolated by '(' and ')'. Store in separate array. Replace by 'pa(pnum)'.
4. Replace ')' by ';' and append to start 'pa(pnum)=' (replace pnum by actual value of variable)
5.Send to previous algorithm.
Repeat step 1.


 

To do:
Add more content


Sample Expression Conversion edit

i p=(p*r*t)/100; 

the i signifies that it is going to be an integer expression.

Result:

Mov eax,p
IMUL r,eax
MOV eax,eax
MOV eax,eax
IMUL t,eax
MOV eax,eax
MOV va1,eax
MOV eax,va1
MOV pa1,eax
MOV eax,pa1
CDQ
IDIV pa1/100
MOV p,eax

Now, this isnt the best of results, but this result works. However, there are huge opportunities for optimization. As you can see, nearly a third of this code is garbage with meaningless instructions like:

MOV eax,eax

Other times there are cases that the compiler loads the value onto eax and then dumps it onto another variable. (On my system it was intentional as the instructions did not work with two memory operands.

Try to optimize this algorithm by yourself before moving forward.

Finally Some Advantages!! edit

Here we have an advantage over general market languages. Because they are made for portability , they usually use what is known as a 'Emulator' which does NOT use the math co-processor but uses the CPU to integer emulate float calculations. In our case we use direct float commands, which reduces portability but increases execution speed by almost 100 times!!