Readmi
Readmi
Description
-------------------
- Expressions syntax:
- Statements supported:
BEGIN... END
IF... THEN... ELSE
CASE
FOR... TO/DOWNTO... DO
WHILE... DO
REPEAT... UNTIL
BREAK
CONTINUE
GOTO
EXIT
USES
INCLUDE
2. License
----------
If you want get source code, you should register PASCALC and get registration
code.
Use your registration code as password for source archive (Source\pascsrc.zip).
You can modify PASCALC source code as you need, distribute executable files
compiled with PASCALC source. But you can't distribute original or modified
PASCALC source code, .DCU units or libraries based on PASCALC source code.
3. PASCALC inteface
-------------------
unit pascalc;
{$F+,B-,R-}
interface
uses
Windows, Messages, SysUtils, Classes, Math;
type TToken =
(tEMPTY, tVR, tCON, tTRUE, tFALSE,
tEQU, tOR, tAND, tNOT, tXOR,
tCOMMA, tLBL, tNEQ, tGT, tLS,
tGTE, tLSE, tADD, tSUB, tMUL,
tDIV, tPWR, tLBR, tRBR, tLARR,
tRARR, tSEMI, tREM, tREMB, tREME,
tASSIGN, tBEGIN, tEND, tIF, tTHEN,
tELSE, tFOR, tTO, tDOWNTO, tDO,
tWHILE, tREPEAT, tUNTIL, tBREAK, tCONTINUE,
tEXIT, tGOTO, tSHL, tSHR, tPROC,
tFUNCT, tUSES, tINCLUDE, tCASE, tOF,
tCOMMA2);
const
ResWords : array[TToken] of string[10] =
('', '', '', 'TRUE', 'FALSE',
'=', 'OR', 'AND', 'NOT', 'XOR',
',', ':', '<>', '>', '<',
'>=', '<=', '+', '-', '*',
'/', '^', '(', ')', '[',
']', ';', '//', '{', '}',
':=', 'BEGIN', 'END', 'IF', 'THEN',
'ELSE', 'FOR', 'TO', 'DOWNTO', 'DO',
'WHILE', 'REPEAT', 'UNTIL', 'BREAK', 'CONTINUE',
'EXIT', 'GOTO', 'SHL', 'SHR', 'PROCEDURE',
'FUNCTION', 'USES', 'INCLUDE', 'CASE', 'OF',
'..');
const
Alpha : set of char =
['_','0'..'9','a'..'z','A'..'Z','�'..'�','�','�'..'�','�'];
StrDelimiter : char = '''';
DecimalPoint : char = '.';
TokenDelimiter : char = #127;
BlockLevel : integer;
BlockCmd : TToken;
GotoLabel : string;
VarList : TVarList;
FuncList : TFuncList;
ProcList : TProcList;
ProcessProc : PProcessProc;
LastString : string;
LastParsed : string;
procedure Clear;
procedure Process;
procedure Error(Msg,Line:string; Code:integer);
procedure Level1(var R:TVar);
procedure Level2(var R:TVar);
procedure Level3(var R:TVar);
procedure Level4(var R:TVar);
procedure Level5(var R:TVar);
procedure Level6(var R:TVar);
procedure Level7(var R:TVar);
procedure Level8(var R:TVar);
procedure Arith(o : TToken; var R,H:TVar);
procedure Unary(o : TToken; var R:TVar);
function GetIndex(S:string; var Index:integer; var T:TToken) : string;
function GetFuncParams(S:string; var Index:integer) : string;
function FindFunc(N:string) : integer;
function FindArray(N:string) : boolean;
procedure SetVarDirect(var R:TVar);
function CallFunc(N:string; A:string; var V:TVar) : boolean;
function CallProc(N:string; A:string; var V:TVar) : boolean;
function GetTextToken(S: string; var Index : integer; var Code : TToken) :
string;
function TokenStr(T:TToken;S:string) : string;
function GetToken(S:string; var Index : integer; var Code : TToken) : string;
function GetTokenCode(S: string; var Index:integer; var Code:TToken) :
integer;
function GetTokenLine(S:string; var Index:integer; var Code:TToken;
StopToken:TTokenSet) : string;
function NextToken(S:string; Index:integer) : TToken;
function GetOperator(Txt:string; var Index : integer; EndToken:TTokenSet) :
string;
function ParseOperator(Txt:string; var Cmd,Line,Lbl : string) : TToken;
function DelRemarks(S:string) : string;
function UnParse(S:string; Show:boolean) : string;
function PreProcess(Txt:string):string;
function Calc(S:string; var R:TVar) : boolean;
procedure Exec(Txt:string);
procedure DoSet(CmdLine,Cmd,Line:string);
procedure DoIf(CmdLine,Line:string);
procedure DoBegin(CmdLine,Line:string);
procedure DoFor(CmdLine,Line:string);
procedure DoBreak(CmdLine,Line:string);
procedure DoContinue(CmdLine,Line:string);
procedure DoExit(CmdLine,Line:string);
procedure DoWhile(CmdLine,Line:string);
procedure DoRepeat(CmdLine,Line:string);
procedure DoGoto(CmdLine,Line:string);
procedure DoCase(CmdLine,Line:string);
public
Stop : boolean;
ErrCode : integer;
ErrMsg : string;
ErrLine : string;
end;
// String functions
Val
IntToStr
StrToInt
FloatToStr
StrToFloat
Copy
Pos
Length
Insert
Delete
Trim
TrimLeft
TrimRight
UpperCase
LowerCase
Format
// DateTime functions
Now
Date
Time
DateToStr
StrToDate
TimeToStr
StrToTime
FormatDateTime
DayOfWeek
IncMonth
DecodeDate
DecodeTime
EncodeDate
EncodeTime
YearDays
YearFrac
// Math functions
Abs
Int
Frac
Round
Ceil
Floor
Trunc
Sin
Cos
Tan
ArcSin
ArcCos
ArcTan
Exp
Ln
IntPower
Sqr
Sqrt
Min
Max
Inc
Dec
SetVar
GetVar
// Misc functions
5. Interpreter usage
--------------------
See usage example in demo program (demo.dpr). At first you should write
user-defined functions you want to use in scripts and expressions. Function
should set result value (field Value of variable R:TVar) and return true.
On errors (wrong parameters, etc) function should return false. In your
program you should create an instance of TPasCalc class.
Then associate functions with names (TPasCalc.SetFunction).
To register pasfunc.pas function library, call SetFunctions.
You can set predefined variables (TPasCalc.SetVarNum or TPasCalc.SetVarStr).
Method TPasCalc.Calculate evaluates expression and returns result in variable
R:TVar. Method TPasCalc.Execute executes script and fills variable list.
You can access variables using TPasCalc.VarCount, TPasCalc.VarByIndex or
TPasCalc.VarByName methods. TPasCalc.ErrCode, TPasCalc.ErrMsg and
TPasCalc.ErrLine indicates last error code, error message and line that
raises error. Variable list not cleared in Calculate and Execute methods.
You can reset all variables using TPasCalc.ClearVars. TPasCalc.ClearFuncs
resets function list. Method TPasCalc.SetProcessProc sets pointer to
user-defined callback procedure. Interpreter calls this procedure in internal
cycles. In this procedure you can check some condition (timeout for example)
and break execution by assigning TPasCalc.Stop:=TRUE. It is useful to call
Application.ProcessMessages in this procedure. If callback procedure not
assigned, interpreter uses Process method instead. Process method performs
only messages processing.
To improve perfomance you can prepare script using Parse method, and then
execute previously prepared script.
6. Error codes
--------------
0 - O.K.
1 - Error in expression
2 - Unpaired parentheses
3 - Variable not found
4 - Invalid variable/function/procedure name
5 - Invalid typecast
6 - Invalid�string constant
7 - Incorrect function call
8 - Function not found
9 - Invalid operator
10 - END expected
11 - Too many END
12 - TO or DOWNTO expected
13 - FOR-loop variable expected
14 - DO expected
15 - BREAK outside a loop
16 - UNTIL expected
17 - Too many UNTIL
18 - Label not found
19 - Index out of range
20 - Value out of range
21 - ']' expected
22 - Too many '['
23 - Division by zero
24 - Variable or array name duplicated
25 - File �pen error
26 - Function must return result
27 - CASE without OF
28 - Too many ELSE in CASE statement
29 - Case range expected
7. Author
---------
Alex Boiko
[email protected]
http://alexboiko.da.ru
http://alexboiko.chat.ru