-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuutils.pas
107 lines (89 loc) · 2.8 KB
/
uutils.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
unit uutils;
{$mode objfpc}{$H+}
interface
function lpi_is_alpha(c: Char): Boolean;
function lpi_is_numeric(c: Char): Boolean;
function lpi_is_operator(c: Char): Boolean;
function lpi_is_math_operator(c: Char): Boolean;
function lpi_is_whitespace(c: Char): Boolean;
function tokentostr(id: Byte): AnsiString;
function operationtostr(op: Byte): AnsiString;
function padstring(s: AnsiString; l: Integer; ch: Char = #32; reverse: Boolean = false): AnsiString;
implementation
uses SysUtils, uconstants;
function lpi_is_alpha(c: Char): Boolean;
begin
Result := c in ['A' .. 'Z', 'a' .. 'z'];
end;
function lpi_is_numeric(c: Char): Boolean;
begin
Result := c in ['0' .. '9'];
end;
function lpi_is_operator(c: Char): Boolean;
begin
Result := c in [':', '=', '<', '>', '+', '-', '*', '/'];
end;
function lpi_is_math_operator(c: Char): Boolean;
begin
Result := c in ['+', '-', '*', '/'];
end;
function lpi_is_whitespace(c: Char): Boolean;
begin
Result := c in [#9, #10, #13, #32]; // Tab, Line Feed, Carriage Return, Space
end;
function tokentostr(id: Byte): AnsiString;
begin
case id of
CtokenIdentifier: Result := 'Identifier';
CtokenOperator: Result := 'Operator';
CtokenString: Result := 'String';
CtokenNumber: Result := 'Number';
CtokenComment: Result := 'Comment';
CtokenSingle: Result := 'Single';
else Result := '<Unknown Token ID ' + IntToStr(id) + '!>';
end;
end;
function operationtostr(op: Byte): AnsiString;
begin
case op of
CopNOP: Result := 'NOP';
CopAssign: Result := ':=';
CopCommandBlock: Result := 'begin-end';
CopCondition: Result := 'if-then';
CopLoopWhile: Result := 'while-do';
CopLoopRepeatUntil: Result := 'repeat-until';
CopLoopForTo: Result := 'for-to';
CopLogicTrue: Result := 'true';
CopLogicFalse: Result := 'false';
CopLogicOr: Result := 'or';
CopLogicAnd: Result := 'and';
CopLogicNot: Result := 'not';
CopCompareEqual: Result := '=';
CopCompareUnequal: Result := '<>';
CopCompareGreater: Result := '>';
CopCompareSmaller: Result := '<';
CopCompareGreaterOrEqual: Result := '>=';
CopCompareSmallerOrEqual: Result := '<=';
CopMathAdd: Result := '+';
CopMathSub: Result := '-';
CopMathMul: Result := '*';
CopMathDiv: Result := '/';
CopMathDivInt: Result := 'div';
CopMathMod: Result := 'mod';
CopVariable: Result := 'var';
CopString: Result := 'string';
CopNumber: Result := 'number';
CopRound: Result := 'round';
CopWriteln: Result := 'writeln';
else Result := '<Unknown Operation ID ' + IntToStr(op) + '!>';
end;
end;
// String padding...
function padstring(s: AnsiString; l: Integer; ch: Char = #32; reverse: Boolean = false): AnsiString;
begin
while Length(s) < l do
if reverse then s := ch + s
else s := s + ch;
Result := s;
end;
end.