(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:
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.Code:Bool (uses Boolean type) Int (uses Int32 type) Float (uses Double type) Byte (uses Byte type) Text (uses String type)
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:
This class also have a virtual functions table which you can extend to support additional ones. Right now it supports the following functions: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
Download link: PARSER.vbCode: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




Reply With Quote