Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / Windows Forms

Evaluation Engine 1.3

Rate me:
Please Sign up or sign in to vote.
4.79/5 (13 votes)
15 Apr 2009CPOL2 min read 40.3K   794   53   13
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 !!

Image 1

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

Image 2

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

Image 3

Versioning

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

C#
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)


Written By
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

 
PraiseMy vote of 5 Pin
pauljayhoon9-Oct-18 14:44
pauljayhoon9-Oct-18 14:44 
GeneralMy vote of 5 Pin
Mazen el Senih29-Mar-13 5:41
professionalMazen el Senih29-Mar-13 5:41 
QuestionFantastic job Pin
Babak Deypir29-May-12 6:19
Babak Deypir29-May-12 6:19 
QuestionFantastic job Pin
Babak Deypir29-May-12 6:18
Babak Deypir29-May-12 6:18 
GeneralMy vote of 5 Pin
Member 31095523-Sep-11 3:17
Member 31095523-Sep-11 3:17 
QuestionExcellent Job Pin
Member 31095523-Sep-11 3:15
Member 31095523-Sep-11 3:15 
GeneralMy vote of 5 Pin
Sepehr Mohammad17-Apr-11 20:30
Sepehr Mohammad17-Apr-11 20:30 
GeneralGood Job!! Pin
Jones Chung21-Apr-10 6:00
Jones Chung21-Apr-10 6:00 
GeneralRe: Good Job!! Pin
Arash Javadi5-Oct-10 6:46
Arash Javadi5-Oct-10 6:46 
GeneralMy vote of 1 Pin
zennie24-Apr-09 5:56
zennie24-Apr-09 5:56 
Is it possible for you to explain what it is you have extended on from Donald Snowdy's Evaluation Engine.

Again refer to comments from:
http://www.codeproject.com/KB/recipes/rpn_Compiler_Engine1_4.aspx
GeneralRe: My vote of 1 [modified] Pin
Arash Javadi24-Apr-09 9:48
Arash Javadi24-Apr-09 9:48 
GeneralIrony, NPEG Pin
Leblanc Meneses20-Apr-09 16:34
Leblanc Meneses20-Apr-09 16:34 
GeneralRe: Irony, NPEG [modified] Pin
Arash Javadi21-Apr-09 10:38
Arash Javadi21-Apr-09 10:38 

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

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