|
-
Aug 20th, 2011, 06:33 PM
#1
Typized Expression evaluator
(The download link is at the bottom)
I was a bit doubtful when posting this class, after all I invested a lot of efforts in it, but I think this would be to a benefit of the community if I share it.
Basically, this class is an expression evaluator. It parses a given command and evaluates the result. It's syntax is a mix of C (mostly for operators) and VB (for functions).
So what? Well, this is a typized evaluator. It uses built in data types (you can extend them if you like. Right now, it supports these data types:
Code:
Bool (uses Boolean type)
Int (uses Int32 type)
Float (uses Double type)
Byte (uses Byte type)
Text (uses String type)
I had plans to add Pointer type (it is present in the enum) but it is currently unsupported with the only exception of AddressOf function.
Evaluation guesses the most suitable data type for the operands and uses them. It also supports variables and strings. Yes, it's almost a compiler, well interpreter actually. In order to use a vairable just use any literal with an assignment operator (not being a keyword).
Usage: use ParseCommand method in order to evaluate the string.
Note: The class WILL throw exceptions if incorrect syntax or any other error is occured (division by zero, type mismatch, etc) so use exception handlers.
Try it. Any feedback will be greatly appreciated.
P.S. I've been writing it with several months breaks so there might be inconsistences in the code, but it's working. Also I plan to re-factor it to get rid of VALs and InStr functions, but all due checks are done, so there won't be any unexpected behavior.
The precedence of the operators is given below:
Code:
Syntax Description Priority Associativity Unary or Binary
x++ ' Postfix increment 1 Left Unary
x-- ' Postfix decrement 2 Left Unary
--x ' Prefix decrement 10 Right Unary
++x ' Prefix increment 11 Right Unary
-x ' Unary minus 21 Right Unary
+x ' Unary plus 22 Right Unary
x**y ' PowerOf 25 Right Binary
!x ' Bitwise NOT 31 Right Unary
NOT x ' Bitwise NOT 31 Right Unary
&x ' AddressOf 33 Right Unary
x*y ' Multiplication 41 Left Binary
x/y ' Division 42 Left Binary
x\y ' Integer division 43 Left Binary
x%y ' Modulus 44 Left Binary
x+y ' Addition (Sum) 51 Left Binary
x-y ' Subtraction 52 Left Binary
x<<y ' Left shift 61 Left Binary
x>>y ' Right shift 62 Left Binary
x<y ' Less than 70 Left Binary
x<=y ' Less than or equal to 71 Left Binary
x>y ' More than 72 Left Binary
x>=y ' More than or equal to 73 Left Binary
x==y ' Equal to 80 Left Binary
x!=y ' Not equal to 81 Left Binary
x<>y ' Not equal to 81 Left Binary
x&y ' Bitwise AND 90 Left Binary
x^y ' Bitwise XOR 91 Left Binary
x|y ' Bitwise OR 92 Left Binary
x&&y ' Logical AND 93 Left Binary
x|y ' Logical OR 94 Left Binary
x=y ' Direct assignment 100 Right Binary
x-=y ' Assignment by difference 101 Right Binary
x+=y ' Assignment by sum 102 Right Binary
x*=y ' Assignment by product 103 Right Binary
x/=y ' Assignment by quotient 104 Right Binary
x\=y ' Assignment by int. quot. 105 Right Binary
x%=y ' Assignment by remainder 102 Right Binary
This class also have a virtual functions table which you can extend to support additional ones. Right now it supports the following functions:
Code:
Name No. of args Description
================================================
byte 1 Converts the argument into the Byte data type
int 1 Converts the argument into the Int data type
bool 1 Converts the argument into the Bool data type
text 1 Converts the argument into the Text data type
float 1 Converts the argument into the Float data type
format 2 Prints the argument to the output
sin 1 Returns SIN(x)
cos 1 Returns COS(x)
tan 1 Returns TAN(x)
sqrt 1 Returns SQRT(x)
abs 1 Returns ABS(x)
acos 1 Returns ACOS(x)
asin 1 Returns ASIN(x)
atan 1 Returns ATAN(x)
exp 1 Returns EXP(x)
ln 1 Returns LN(x) (natural)
log 2 Returns LOG(x, base)
lg 1 Returns LOG(x, 10)
max 2 Returns the greatest of two arguments
min 2 Returns the smallest of two arguments
round 2 Rounds the given argument to the desired precision
sign 1 Returns -1 if the argument is negative, 1 if positive and 0 if 0
fix 1 Returns an integer part of a fractional number
left 2 Returns the left part of the string
right 2 Returns the right part of the string
len 1 Returns the length of the text
substr 3 Returns the substring of the text
seek 2 Searches through the string and returns the first pos found
ucase 1 Returns the given string in uppercase
lcase 1 Returns the given string in lowercase
trim 1 Trims leading and trailing spaces
chr 1 Returns a character with the given charcode
chrw 1 Returns a wide character with the given charcode
asc 1 Returns an ascii code of the given character
ascw 1 Returns a long ascii code of the given character
replace 5 Replaces a substring of the text with a given sample
Download link: PARSER.vb
Last edited by cicatrix; Aug 20th, 2011 at 06:40 PM.
-
Sep 12th, 2011, 01:32 PM
#2
Re: Typized Expression evaluator
Very nice! I have ALWAYS wanted to make my own scripting/programming language. I hope I am going to learn from this!
EDIT: I have yet to read the code, but I was wondering if you could maybe post a small example program/script so that we can see the syntax of your language.
Last edited by BlindSniper; Sep 12th, 2011 at 01:58 PM.
-
Sep 13th, 2011, 03:00 AM
#3
Re: Typized Expression evaluator
It's not a script parser. It only parses expressions. In order to make it a script parser you will need to 'teach' it how to recognize code blocks. Also no I/O operations here, you have to implement them yourself.
As for the syntax - I thought it would be obvious from the description above.
(2+5) / 7 + SQRT(4)
will evaluate to 3
-
Sep 13th, 2011, 09:01 AM
#4
Re: Typized Expression evaluator
Thanks, the reason I was asking was because nothing I did returned a result. But now I've realized that I made a small mistake in my coding and I was passing empty strings. It works as intended now.
I have one more question.
How would you make use of variables ?
I tried something like this
X=5
Y=2
X+Y
But I received an error.
-
Sep 14th, 2011, 03:03 AM
#5
Re: Typized Expression evaluator
I was a bit wrong when said that is actually supports variables.
Not in this verion
as you can see in the code of this function:
Code:
Private Shared Function EvaluatePostFix(ByVal Tokens() As String) As ValueClass
you should look for the comment:
' it's not a function
' TODO: Qualify identifier
Variables can be added simply by adding a dictionary collection (Of String, ValueClass)
Here you can check if the collection contains a symbol (variable name) and if it is, just push it into ValueStack.
Of course you should decide which keyword you would use for variable definitions. Keep in mind, that right now it's a case sensitive parser Function FUNCTION and function are different identifiers.
-
Sep 14th, 2011, 01:07 PM
#6
Re: Typized Expression evaluator
Nevertheless, this is still the single most awesome snippet I've ever seen.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|