Click here to Skip to main content
Click here to Skip to main content

Evaluation Engine 1.3

, 15 Apr 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
The Evaluation Engine is a parser and interpreter that can be used to build a Business Rules Engine. It allows for mathematical and boolean expressions, operand functions, variables, variable assignment, comments, and short-circuit evaluation. A syntax editor is also included.

Introduction

This is an update to a great article Evaluation Engine by Donald Snowdy.

Background

There are some changes that I made to the article that makes it a typical C-Compiler. It is a good starting point for making a compiler. With a few changes, you can complete it as a real C-Compiler !!

RPN-Reverse Polish Notation

The "if" keyword checks the true or false state of a statement and decides where the runtime pointer should jump. This compiler needs specific characters to use them as flags as you see in figure 1. "if" has three set of states:

  1. Token_if_True: Refers to end of "if" statement which is ')' at this point if the statement was True runtime pointer continues on its journey until it meets its End Point and jumps to End Point.
  2. Token_IF_False: If the statement failed, runtime Pointer jumps to Fail Point.
  3. Token_IF_END: If statement finished its True Block or Fail Block, runtime jump to EndPoint

In the RPN tab, you can see all tokens related to the compiled code:

Versioning

Version Changes
1.1 Using parenthesis for functions sin[3*3.14] -- > sin(3*3.14) and free square brackets for further uses Done / Tested OK
1.2 Changing equation from = to == more C# like and changing assignment operator from := to = Done / Tested OK
1.3 Adding if( statement as a function of comparison and related handling Done / Testing
1.4 Adding for( statement as a function and related handling To Do
1.5 Adding Function Declarative and block handling with all parameters push and pops to add for execution To Do

Using the Code

"if" handling:

The code below locates if-related characters that I used as a flag and are located right after making RPN Queue function so that you can make sure that there is no token to be added to the list of token Queue. At this point, all positions of Queue are fixed so you can use the pointer to the queue as a line code. For instance, if you want to jump to a location you can use this number in "if" computer verify the statement and if the statement is True, continue the process and as the True block is finished, the computer must jump to finishing point of if:

if (this.AnyErrors == false) MakeRPNQueue();
Support.ExQueue<int> pos = new Support.ExQueue<int>();
Stack<TokenItem> BlockEnd = new Stack<TokenItem>();
IF = new Stack<TokenItem>();
Stack<TokenItem> Else = new Stack<TokenItem>();
Stack<TokenItem> Element = new Stack<TokenItem>();
Stack<bool> isOpen = new Stack<bool>();
Stack<bool> isTrueBlock = new Stack<bool>();
for (int i = 0; i < rpn_queue.Count; i++)
{
TokenItem tok = rpn_queue[i];
if (tok.TokenName == "if(")
{
//tok.TokenType = TokenType.Token_if;
isTrueBlock.Push(true);
Element.Push(tok);
IF.Push(tok);
}
if (tok.TokenName == "{")
{
Element.Push(tok);
tok.TokenType = TokenType.LeftCurlyBracket;
}
if (tok.IF_FirstLevel != null && tok.TokenName == "]")
{
if (!(isTrueBlock.Peek() == true && tok.TokenName == "{"))
{
if (isTrueBlock.Peek())
{
tok.TokenType = TokenType.Token_if_True;
tok.IF_FirstLevel.TruePosition = i;
Element.Push(tok);
}
}
}
if (tok.TokenName == "else")
{
Element.Push(tok);
Else.Push(tok);
tok.IF_FirstLevel = IF.Peek();
isTrueBlock.Pop();
isTrueBlock.Push(false);
IF.Peek().FalsePosition = i;
tok.TokenType = TokenType.Token_IF_False;
}
 
if (tok.TokenName == "}") //E
{
if (isTrueBlock.Peek() == true)
{
if (i + 1 >= rpn_queue.Count || rpn_queue[i + 1].TokenName != "else")
{///E&F
tok.TokenType = TokenType.Token_IF_FalseAndEnd;
tok.IF_FirstLevel = IF.Peek();
isTrueBlock.Pop();
IF.Peek().EndPosition = i;
IF.Pop();
}
}
else
{ ///E
isTrueBlock.Pop();
tok.IF_FirstLevel = IF.Peek();
tok.IF_FirstLevel.EndPosition = i;
Else.Pop();
tok.TokenType = TokenType.Token_IF_End;
}
Element.Push(tok);
}
 
if (tok.TokenName == ";") //E
{
if(Element.Count>0)
if ( Element.Peek().TokenName == "]" || Element.Peek().TokenName == "else")
{
if (isTrueBlock.Peek() == true)
{
if ((i + 1 >= rpn_queue.Count && Element.Peek().TokenName != "else") || 
	(i + 1 < rpn_queue.Count && rpn_queue[i + 1].TokenName != "else"))
{///E&F
tok.TokenType = TokenType.Token_IF_FalseAndEnd;
isTrueBlock.Pop();
IF.Peek().EndPosition = i;
IF.Peek().FalsePosition = i;
IF.Pop();
}
}
else
{
if (isTrueBlock.Count != 0 && Element.Peek().TokenName == "else")
{ ///E
isTrueBlock.Pop();
Else.Pop();
tok.TokenType = TokenType.Token_IF_End;
IF.Peek().EndPosition = i ;
IF.Pop();
}
}
Element.Push(tok);
}
}
}

Points of Interest

Writing a compiler was a dream for me because I could do anything I wanted when I felt the limitation of standard compiler. If I finish this, I can write a Universal compiler for all sorts of ICs such as Atmel AtMega / PIC families and complete my own PLC.

For more information about me and my project, please visit HexWay.com.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Arash Javadi
Software Developer (Junior)
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
he studied MCSD (C# based 2003) and MCDBA (2005) CWNA, CWNP at Sematech
IC Programming with 8051, AVR , IC desighn with FPGA and board desigh at Contronic Co

He also worked on Wireless Low level TCP/IP Programmable Module and video motion Detection algorithm
he is student of Industrial engineering in University of Payam e noor Tehran learning about PMBOK and management systems.
He has Certificate in Advanced English (CAE) and also he studied German language in ökf österreichisches Kulturforum

Comments and Discussions

 
GeneralIrony, NPEG PinmemberLeblanc Meneses20-Apr-09 17:34 
GeneralRe: Irony, NPEG [modified] PinmemberArash Javadi21-Apr-09 11:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 15 Apr 2009
Article Copyright 2009 by Arash Javadi
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid