Skip to content

justahero/interpreter-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

323 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Interpreter

This repository contains the language lexer, parser and interpreter for Robert Nystrom's lox language from the Crafting Interpreters book. The virtual machine will be added later.

Goals

This is to show case modern Rust, using a few selected libraries. It's a good opportunity to get experience with the following crates.

  • logos - a fast / simple lexer
  • chumsky - a parser library & parser combinator
  • ariadne - a compiler diagnostics crate for error reporting

The idea is not to translate the content to code directly, but to learn how to use the existing libraries to provide the same functionality.

Grammar

The following is the current list of expression and statement grammar as implemented. This is based on Crafting Interpreter's Syntax Grammar.

Expressions:

expression     → assignment ;
assignment     → ( call "." )? IDENTIFIER "=" assignment | logic_or ;

logic_or       → logic_and ( "or" logic_and )* ;
logic_and      → equality ( "and" equality )* ;
equality       → comparison ( ( "!=" | "==" ) comparison )* ;
comparison     → term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
term           → factor ( ( "-" | "+" ) factor )* ;
factor         → unary ( ( "/" | "*" ) )* unary ;

unary          → ( "!" | "-" ) unary | call ;
call           → primary ( "(" arguments? ")" | "." IDENTIFIER )* ;
primary        → literal | "(" expression ")" | IDENTIFIER ;
literal        → NUMBER | STRING | "true" | "false" | "nil" | "this" ;
grouping       → "(" expression ")" ;
arguments      → expression "(" "," expression )* ;

Statements:

program        → statement* EOF ;

declaration    → classDecl
               | funDecl
               | varDecl
               | statement ;

classDecl      → "class" IDENTIFIER ( "<" IDENTIFIER )? "{" function* "}" ;
funDecl        → fun function ;
function       → IDENTIFIER "(" parameters? ")" block ;
parameters     → IDENTIFIER ( "," IDENTIFIER )* ;
varDecl        → "var" IDENTIFIER ( "=" expression )? ";" ;
statement      → exprStmt
               | forStmt
               | ifStmt
               | printStmt
               | returnStmt
               | whileStmt
               | block ;

exprStmt       → expression ";" ;
forStmt        → "for" "(" ( varDecl | exprStmt | ";" ) expression? ";" expression? ")" statement ;
ifStmt         → "if" "(" expression ")" statement ( "else" statement )? ;
printStmt      → "print" expression ";" ;
returnStmt     → "return" expression? ";" ;
whileStmt      → "while" "(" expression ")" statement ;
block          → "{" declaration* "}" ;

Lexical Grammar

The fundamental rules used by the scanner.

NUMBER         → DIGIT+ ( "." DIGIT+ )? ;
STRING         → "\"" <any char except "\"">* "\"" ;
IDENTIFIER     → ALPHA ( ALPHA | DIGIT )* ;
ALPHA          → "a" ... "z" | "A" ... "Z" | "_" ;
DIGIT          → "0" ... "9" ;

About

Language interpreter for Lox, based on the Crafting Interpreters book.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages