summaryrefslogtreecommitdiff
path: root/calc1.pas
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2015-12-08 22:03:31 +0200
committerOskari Timperi <oskari.timperi@iki.fi>2015-12-08 22:03:31 +0200
commit668fe7b3ff59e5cb7d3953ca7ed7a82092f91474 (patch)
tree8b8c6cf5a649ad0b609959c6c1522de88929ddf2 /calc1.pas
parent4edafe9b5443dbe4d3b29c2e844775c46f40ad81 (diff)
downloadlbasi-668fe7b3ff59e5cb7d3953ca7ed7a82092f91474.tar.gz
lbasi-668fe7b3ff59e5cb7d3953ca7ed7a82092f91474.zip
add CurChar and GetInteger functions
Diffstat (limited to 'calc1.pas')
-rw-r--r--calc1.pas39
1 files changed, 23 insertions, 16 deletions
diff --git a/calc1.pas b/calc1.pas
index eaad650..69a39c4 100644
--- a/calc1.pas
+++ b/calc1.pas
@@ -42,6 +42,8 @@ type
procedure Error;
procedure SkipWhitespace;
function AtEnd: Boolean;
+ function CurChar: Char;
+ function GetInteger: Integer;
function GetNextToken: Token;
procedure Eat(T: TokenType);
function Expr: Integer;
@@ -101,7 +103,7 @@ end;
procedure Interpreter.SkipWhitespace;
begin
- while (not AtEnd) and IsWhiteSpace(Text[CurPos]) do
+ while (not AtEnd) and IsWhiteSpace(CurChar) do
Inc(CurPos);
end;
@@ -110,12 +112,25 @@ begin
Result := CurPos > Length(Text);
end;
-function Interpreter.GetNextToken: Token;
+function Interpreter.CurChar: Char;
+begin
+ Result := Text[CurPos];
+end;
+
+function Interpreter.GetInteger: Integer;
var
- Text_: String;
- CurrentChar: Char;
Start: Integer;
begin
+ Start := CurPos;
+
+ while (not AtEnd) and IsDigit(CurChar) do
+ Inc(CurPos);
+
+ Result := StrToInt(Copy(Text, Start, CurPos - Start));
+end;
+
+function Interpreter.GetNextToken: Token;
+begin
SkipWhitespace;
if AtEnd then
@@ -124,27 +139,19 @@ begin
Exit;
end;
- Start := CurPos;
-
- while (CurPos <= Length(Text)) and IsDigit(Text[CurPos]) do
- Inc(CurPos);
-
- if CurPos - Start > 0 then
+ if IsDigit(CurChar) then
begin
- Result := TokenInteger.Create(StrToInt(Copy(Text, Start,
- CurPos-Start)));
+ Result := TokenInteger.Create(GetInteger);
Exit;
end;
- CurrentChar := Text[CurPos];
-
- if CurrentChar = '+' then
+ if CurChar = '+' then
begin
Result := TokenPlus.Create;
Inc(CurPos);
Exit;
end
- else if CurrentChar = '-' then
+ else if CurChar = '-' then
begin
Result := TokenMinus.Create;
Inc(CurPos);