Skip to content

Latest commit

 

History

History
135 lines (98 loc) · 3.21 KB

README.md

File metadata and controls

135 lines (98 loc) · 3.21 KB

Interpreter for Lox language

TODOS

  • Add tests for the interpreter, parser and lexer
  • Handle case print ;

Instructions

  1. Compile the program
$ rustc src/main.rs -o lox

NOTE: To compile in debug mode, use the following command:

$ rustc src/main -o lox --cfg debug_lox

Conditional Compilation

  1. Execute as REPL
$ ./lox
>

or give a lox script as input

$ ./lox [FILE_NAME]

Syntax Grammar

Version 4 (global variables)

program -> declaration* EOF;

declaration -> varDecl
            | statement;
varDecl -> "var" IDENTIFIER ("=" expression)? ";";
primary -> NUMBER | STRING | "true" | "false" | "nil"
        | "(" expression ")"
        | IDENTIFIER;

expression -> assignment;
assignment -> IDENTIFIER "=" assingment
            | equality;

Version 3 (Statements)

program -> statement* EOF;

statement -> exprStmt
            | printStmt;
exprStmt -> expression ";" ;
printStmt -> "print" expression ";";

Version 2 (Operator Precedence)

expression -> equality ;
equality -> comparison ( ( "!=" | "==" ) comparison )* ;
comparison -> term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
term -> factor ( ( "-" | "+" ) factor )* ;
factor -> unary ( ( "/" | "*" ) unary )* ;
unary -> ( "!" | "-" ) unary
               | primary ;
primary -> NUMBER | STRING | "true" | "false" | "nil"
               | "(" expression ")" ;

Version 1

The bellow grammar would be used to create a syntax tree which is abstract (AST).

expression -> literal
            | grouping
            | unary
            | binary;

literal -> NUMBER
         | STRING
         | "true"
         | "false"
         | "nil";

grouping -> "(" expression ")";

binary -> expression operator expression;

operator -> "*"
          | "/"
          | "-"
          | "+"
          | "=="
          | "!="
          | "<"
          | ">"
          | "<="
          | ">=";

Further Questions

  1. Learn more about error codes while exiting the program.
  2. Difference between expression and statement?
  3. What is dynamic dispatch and static dispatch?

Questions specific to Rust

  1. Generic returns in Rust
  2. Variadic functions

Resources

  1. Crafting Interpretors: Lox
  2. Rolox: SarcasticNastik

Rust Resources

  1. Unofficial Rust Docs
  2. How Rusty is your Rust: Solana Resource

Author