A simple Pascal interpreter written in rust.
Download the latest rascal executable from the release page.
Run the executable.
rascal.exe <name-of-pascal-file>.pas
program exampleVariables;
var
intOne, intTwo: integer;
realOne, realTwo, realThree: real;
stringOne, stringTwo: string;
boolOne: boolean;
begin
intOne := 5;
realOne := 5.5;
stringOne := 'foobar';
boolOne := true;
end.program exampleProcedure;
procedureprintSum(a, b: integer);
var
sum: integer;
begin
sum := a + b;
writeln(IntToString(a) + ' + ' + IntToString(b) + ' = ' + IntToString(sum));
endbegin
printSum(5, 10);
end.program exampleFunction;
var
mySum: integer;
functionsum(a, b: integer): integer;
var
sum: integer;
begin
sum := a + b;
endbegin
mySum := sum(5, 10);
end.program exampleControlFlow;
beginif20 = 5thenbegin
writeln('unreachable');
endelseif5 + 7 < 30thenbegin
writeln('this will print');
endelseifnot true thenbegin
writeln('this will not print');
endelseif20 <> 5thenbegin
writeln('<> means not equal');
endelsebegin
writeln('this will not print');
endend.program exampleExpressions;
var
foo: integer;
bar: real;
baz: boolean;
begin
foo := 5 * ( 7 - -2) div5;
bar := 5.5 * (7.0 - -2.5) / 10.0;
baz := true and (true or false) and (10 < foo or9 = foo); end.program exampleBuiltIns;
var
my_int: integer;
my_real: real;
my_string: string;
begin
my_int := 5;
my_string := IntToString(my_int);
my_real := 5.5;
my_string := RealToString(my_real);
my_string := '5';
my_int := StringToInt(my_string);
my_string := '5.5';
my_real := StringToReal(my_string);
write('print without a newline');
writeln('print with a newline');
my_string := readln();
end.program helloworld;
begin
writeln('hello world!');
end.note: This program is not very efficient. You should probably stick to integers less than 20.
program fibonacci;
var
input: integer;
functionfib(n:integer): integer;
var
val: integer;
return: integer;
beginif (n <= 2) thenbegin
val := 1;
endelsebegin
val := fib(n-1) + fib(n-2);
end
return := val;
endbegin
writeln('Welcome to fibonacci!');
write('Please enter an integer: ');
input := StringToInt(readln());
writeln('fib of ' + IntToString(input) + ' is ' + IntToString(fib(input)));
end.Pascal basic syntax can be read about here.
Bear in mind that this interpreter does not implement every feature of pascal.