Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works

Random project

loxip

Lox interpreter written in Free Pascal

View on GitHub

Anyone may press Interesting, signed in or not. It helps others find this project.

Member rating

No ratings yet.

Sign in to rate this project.

README

loxip

Lox interpreter written in Free Pascal.

Work in progress.

Build

Under FPC 3.0.4 download (clone) Generics.Collections. Optionally FastMM4-AVX memory manager.

Test

Fibonacci -

pascal
$ ./loxip.exe
Type quit to exit.
> var a=0;var b=1;var c=0;while (c<1000) {if (c>=0) {print c+": "+a;} var temp=a;a=b;b=temp+b;c=c+1;}
0: 0
1: 1
2: 1
3: 2
4: 3
5: 5
6: 8
7: 13
8: 21
9: 34
10: 55
...

Challenges

Chapter 5.Representing Code

  1. Reverse Polish Notation.
pascal
> print (1 + 2) * (4 - 3);
[DEBUG] (TASTPrinter) (* (group (+ 1 2)) (group (- 4 3)))
[DEBUG] (TASTPrinter RPN) 1 2 + 4 3 - *

Chapter 7.Evaluating Expressions

  1. String comparison.
pascal
> print "abd" > "aba";
[DEBUG] (TBinaryExpression.Accept) Accepting binary expression: >
[DEBUG] (TLiteralExpression.Accept) Accepting literal expression: abd
[DEBUG] (TLiteralExpression.Accept) Accepting literal expression: aba
True
Tick count - 0.
> print "abd" < "aba";
[DEBUG] (TBinaryExpression.Accept) Accepting binary expression: <
[DEBUG] (TLiteralExpression.Accept) Accepting literal expression: abd
[DEBUG] (TLiteralExpression.Accept) Accepting literal expression: aba
False
Tick count - 0.
  1. Define + such that if either operand is a string, the other is converted to a string and the results are then concatenated.
pascal
> print "Hello " + 2020 + " " + true;
[DEBUG] (TASTPrinter) (+ (+ (+ "Hello " 2020) " ") True)
[DEBUG] (TASTPrinter RPN) "Hello " 2020 + " " + True +
[DEBUG] (TInterpreter) Evaluating TBinaryExpression.
[DEBUG] (TInterpreter) Evaluating TBinaryExpression.
[DEBUG] (TInterpreter) Evaluating TBinaryExpression.
[DEBUG] (TInterpreter) Evaluating TLiteralExpression.
[DEBUG] (TInterpreter) Evaluating TLiteralExpression.
[DEBUG] (TInterpreter) Evaluating TLiteralExpression.
[DEBUG] (TInterpreter) Evaluating TLiteralExpression.
Hello 2020 True
  1. Division by zero.
pascal
> 8/0;
[DEBUG] (TInterpreter) Evaluating TBinaryExpression.
[DEBUG] (TInterpreter) Evaluating TLiteralExpression.
[DEBUG] (TInterpreter) Evaluating TLiteralExpression.
[ERROR] Division by zero. [line 1].

AST visualization

printdot expression; generates AST in DOT format.

pascal
> printdot 1 + 8 * 89 - - 12;
[DEBUG] (TBinaryExpression) Accepting binary expression - -
[DEBUG] (TBinaryExpression) Accepting binary expression - +
[DEBUG] (TLiteralExpression) Accepting literal expression - 1
[DEBUG] (TBinaryExpression) Accepting binary expression - *
[DEBUG] (TLiteralExpression) Accepting literal expression - 8
[DEBUG] (TLiteralExpression) Accepting literal expression - 89
[DEBUG] (TUnaryExpression) Accepting unary expression - -
[DEBUG] (TLiteralExpression) Accepting literal expression - 12
digraph astgraph {
node [shape=circle, fontsize=10, fontname="Courier"];
rankdir = BT;
0 [label="-"]
1 [label="+"]
2 [label="1", shape=rectangle]
2 -> 1
3 [label="*"]
4 [label="8", shape=rectangle]
4 -> 3
5 [label="89", shape=rectangle]
5 -> 3
3 -> 1
1 -> 0
6 [label="-"]
7 [label="12", shape=rectangle]
7 -> 6
6 -> 0
}

>

AST for expression 1 + 8 * 89 - - 12

Interpreter

Built With

  • Lazarus - The professional Free Pascal RAD IDE.