LoginLogin

Math Compiler

Root / Submissions / [.]

ninjagnuCreated:
Download:8RNQ23CJ
Version:Size:
Compiles simple semicolon separated math expressions like this one,
1+1;
into stack based SmileBASIC code like these:
DIM S[32]
PUSH S,1
PUSH S,1
R=POP(S)
L=POP(S)
PUSH S,L+R
PRINT POP(S)
I plan to expand this into a much larger fully featured compiler for a programming language.

Instructions:

Change your active project to MATHC go to create programs with SmileBASIC in the main menu. Run this command in direct mode:
EXEC "PRG1:COMPILER"
The compiler will compile the code in IN and output SmileBASIC to OUT. If you wish to change the input to compiler you can edit the file named IN.

interesting

This is the project I need a tree data structure for. The parser would return a populated tree data structure instead of emitting SmileBASIC. The parser would pass this tree to the type checker and the type checker would type check everything recursively with a DFS algorithm if it encounters a type error it will alert the user and stop. If no errors are encountered the type checker will pass the type checked tree on to the code generator etc. etc.

Right. If you do get that malloc/tree implementation, it would be neat if you could publish it as a separate library so other people can find it easily, too.

Will do, I think I've got an idea on how things should work. Just need to get a working flow chart done on paper so I can visualize things.

You may be interested in the shunting yard algorithm (if you aren't using it already.)

Replying to:snail_
You may be interested in the shunting yard algorithm (if you aren't using it already.)
But if you use the shunting yard algorithm BE SURE that you mark a way for the output to know where a parentheses end. Otherwise you'll have a bug where a function's parameters fade into another function and it becomes a big mess trying to estimate how many arguments each function has But yes, use the Shunting Yard Algorithm, as it is much higher performance compared to an abstract syntax tree. ASTs should be used for Computer Algebra Systems only, cause they allow moving around operators and arguments to simplify If you have any questions message me

Replying to:snail_
You may be interested in the shunting yard algorithm (if you aren't using it already.)
I used shunting yard to do something similar in JS, but I added functions by just bodging together prefix operators. There really isn't much difference between sin x and sin(x) so it worked okay. The consequence is that they only took one variable.