Code Monkey home page Code Monkey logo

loxip's Introduction

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 -

$ ./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.
> 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.
> 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.
> 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.
> 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.

> 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.

loxip's People

Contributors

julstrat avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.