Implementation of the Lox Programming language
Source files are not yet supported... will handle this later. Currently the safest way to play with the interpreter is using the REPL.
You have two options to run the REPL, you can either install the latest released binary, or you can clone the repo locally and run the main.go
file.
The command below will download the latest released binary in your system, once downloaded, you can start the REPL by executing the downloaded binary in the directory where you ran the command.
Disclaimer: The command does not work on windows, if you use windows, you can manually open the latest release page and download the
glox-windows-amd64.exe
file.
curl -fsSL https://raw.githubusercontent.com/silverhairs/lox/main/install.sh | sh
About the released binary:
- You need to have cURL installed in your system for the above command to work.
- I don't have a specific release schedule, I just cut a new release whenever a major feature has been implemented.
- Clone the repo
- Navigate to
lox/glox
(command:cd ./glox
) - Run the main.go file (command:
go run main.go
)
Requirements:
- You need to have Go installed in your system
Production rules:
program -> declaration* EOF ;
declaration-> funDecl
| letDecl
| statement ;
funDecl -> "fun" function ;
function -> IDENTIFIER "(" parameters ")" block ;
parameters -> IDENTIFIER ("," IDENTIER)* :
letDecl -> ("var" | "let") IDENTIFIER ("=" expression) ? ";" ;
statement -> exprStmt
| ifStmt
| printStmt
| blockStmt ;
exprStmt -> expression ";" ;
ifStmt -> "if" "(" expression ")" statement
("else" statement)? ;
printStmt -> "print" expression ";" ;
blockStmt -> "{" declaration* "}" ;
expression -> literal
| unary
| binary
| grouping
| ternary ;
literal -> NUMBER | STRING | boolean | "nil" ;
unary -> ( "-" | "!" ) expression ;
grouping -> "(" expression ")" ;
binary -> expression operator expression ;
operator -> "==" | "!=" | "<" | ">" | "<=" | ">="
| "+" | "-" | "*" | "/" ;
ternary -> expression "?" expression ":" expression;
For now the interpreter does not handle source files, so this is to be done in the REPL (line by line).
let age = 15;
let is_adult = age >= 18;
if (is_adult){
print "You are an adult";
} else{
print "You cannot drink";
}
{
let age = age+3;
print age;
let message = age >= 18 ? "is an adult" : "cannot drink";
print message;
}
print age;