I solved it myself. I realized that when I called "sub" with no parameters I ran into the "pwr" breakpoint that something was dreadfully wrong. It mixed the stupid if-the-else's up so I had to add some begin/end's to make sure it worked properly.
:roll:
Here's the working code for those interested:
Code:
program tokenizer;
{$APPTYPE CONSOLE}
uses
SysUtils, Math{, Variants};
type TDynStrArray = array of string;
function tokenize(line: string): TDynStrArray;
var temp: string;
l,i: cardinal;
inStr,wasStr: boolean;
begin
l := 0;
temp := line;
instr := false;
while temp <do> length(result)-1 then
SetLength(result, l+8);
wasStr := false;
for i := 1 to Length(temp) do
case temp[i] of
'`': begin inStr := not inStr; wasStr := true; end;
' ',#0: if not inStr then Break;
end;
if not wasStr then
result[l] := copy(temp,1,i-1)
else
result[l] := copy(temp,2,i-3);
Delete(temp,1,i);
inc(l);
end;
setlength(result,l);
end;
var inputtokens: TDynStrArray;
input: string;
i: integer;
exit: boolean = false;
v1,v2: variant;
begin
writeln('Enter tokens seperated by spaces, strings delimited by `''s.');
repeat
v1 := 0;
v2 := 0;
input := '';
setlength(inputtokens,0);
write('> '); readln(input);
writeln;
if input <then>= 3 then
writeln('Sub:'#9,Variant(inputtokens[1]) - Variant(inputtokens[2]));
end else if inputtokens[0] = 'pwr' then
if length(inputtokens) >= 3 then begin
v1 := 0.0 + Math.Power(StrToFloatDef(inputtokens[1],0),StrToFloatDef(inputtokens[2],0.0));
writeln('Power:'#9,v1);
end else writeln('Error, PWR given too few parameters.')
else
writeln('Unknown command "',inputtokens[0],'"');
exit := pos('exit',input) > 0;
end else writeln('Enter something I can process first.');
writeln;
until (exit);
end.
Bookmarks