Magus Expressions
Today I implemented a first version token parser. I've kept the grammar small for now and have only added support for integer variables. The main parsing function is responsible for variable management and handling keywords. The real work was in the expression function. When a possible expression is encountered in the main parsing loop, it passes the tokens and the memory map to an expression parser.
The expression parser...
The operator precedence expression parser uses two stacks (values and operators) and a precedence lookup table to determine appropriate actions and solve the expression.
"Operator precedence parsing is based on bottom-up shift-reduce parsing. As an expression is parsed, tokens are shifted to a stack. At the appropriate time, the stack is reduced, applying the operator at the top of the stack." - Thomas Niemann
As I already mentioned, I've only implemented an expression parser for integer types. I still need to settle on a final design for the expression parser function. I could make a template out of it but I'll probably end up making a different version of each data type. This may be necessary because of incompatible data types. For example I am including float2, float3, and float4 built in vector data types and while I could add an integer to a float3 I couldn't add a float3 to an integer.
Example code...
It's kind of cool to step through the parsers as they execute a script. This is an example of the kind of code it can handle as of now:
int x = 20;
int y = 4;
int z = (2 * (x - y^2)) / y;
print z;
output: 2
The expression parser...
The operator precedence expression parser uses two stacks (values and operators) and a precedence lookup table to determine appropriate actions and solve the expression.
"Operator precedence parsing is based on bottom-up shift-reduce parsing. As an expression is parsed, tokens are shifted to a stack. At the appropriate time, the stack is reduced, applying the operator at the top of the stack." - Thomas Niemann
As I already mentioned, I've only implemented an expression parser for integer types. I still need to settle on a final design for the expression parser function. I could make a template out of it but I'll probably end up making a different version of each data type. This may be necessary because of incompatible data types. For example I am including float2, float3, and float4 built in vector data types and while I could add an integer to a float3 I couldn't add a float3 to an integer.
Example code...
It's kind of cool to step through the parsers as they execute a script. This is an example of the kind of code it can handle as of now:
int x = 20;
int y = 4;
int z = (2 * (x - y^2)) / y;
print z;
output: 2
0
Sign in to follow this
Followers
0
12 Comments
Recommended Comments
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now