Skip to content

dragon540/LoxLang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LoxLang using LLVM

Implements a Lexer, a LL(1) Recursive Descent Parser, a rudimentary Pretty Printer (to visualize AST nodes) and a module to generate LLVM Intermediate Representation (IR). The language definitions are based on Robert Nystrom's book Crafting Interpreters.

Build:

$ mdkir build

$ cmake -DLLVM_DIR=path_to_llvm_build/lib/cmake/llvm -B build/

$ cd build

$ cmake --build .

Example:

A factorial program written in Lox:

var input = 5;
var result = 1;

while(input > 1) {
   result = result * input;
   input = input - 1;
}

Excerpt of LLVM IR generated by using LoxLang compiler:

define void @main() {
entry:
  %input = alloca i32, align 4
  store i32 5, ptr %input, align 4
  %res = alloca i32, align 4
  store i32 1, ptr %res, align 4
  %tmp = alloca i32, align 4
  store i32 1, ptr %tmp, align 4
  br label %loop_holder

loop_exit:                                        ; preds = %loop_holder
  ret void

block:                                            ; preds = %loop_holder
  %0 = load i32, ptr %res, align 4
  %1 = load i32, ptr %input, align 4
  %2 = mul i32 %0, %1
  store i32 %2, ptr %res, align 4
  %3 = load i32, ptr %input, align 4
  %4 = sub i32 %3, 1
  store i32 %4, ptr %input, align 4
  ret void

loop_holder:                                      ; preds = %entry
  %5 = load i32, ptr %input, align 4
  %6 = icmp ugt i32 %5, 1
  br i1 %6, label %block, label %loop_exit
}

The LLVM IR generated above can be used to generate assembly/object files using LLVM's llc for a number of target architectures supported by the LLVM (eg. x86, RISC-V, Arm, etc).

Language Grammar:

  1. Program -> Declaration* EOF

  2. Declaration -> VarDecl | FuncDecl | ClassDecl | Statement

  3. Statement -> ExprStmt | AssignStmt | ForStmt | IfStmt | PrintStmt | ReturnStmt | WhileStmtl | Block

  4. ExprStmt -> Literal | Identifier | Unary | Binary | Grouping

  5. AssignStmt -> Identifier EQUAL ExprStmt SEMICOLON

  6. ForStmt -> FOR OPEN_PAREN VarDecl SEMICOLON ExprStmt SEMICOLON ExprStmt CLOSE_PAREN Block

  7. IfStmt -> IF OPEN_PAREN ExprStmt CLOSE_PAREN Block | IF OPEN_PAREN ExprStmt CLOSE_PAREN Block ELSE Block

  8. PrintStmt -> PRINT OPEN_PAREN ExprStmt CLOSE_PAREN SEMICOLON

  9. ReturnStmt -> RETURN SEMICOLON | RETURN ExprStmt SEMICOLON

  10. WhileStmt -> WHILE OPEN_PAREN ExprStmt CLOSE_PAREN Block

  11. Block -> OPEN_CURLY Statement* CLOSE_CURLY

  12. Literal -> NUMBER | STRING | "true" | "false" | "nil" ;

  13. Grouping -> "(" ExprStmt ")" ;

  14. Unary -> ( "-" | "!" ) ExprStmt ;

  15. Binary -> ExprStmt Operator ExprStmt ;

  16. Operator -> "==" | "!=" | "<" | "<=" | ">" | ">=" | "+" | "-" | "*" | "/" ;

  17. VarDecl -> VAR Identifier SEMICOLON | VAR Identifier EQUAL ExprStmt SEMICOLON

  18. FuncDecl -> FUN Function

  19. ClassDecl -> CLASS Identifier OPEN_CURLY Function* CLOSE_CURLY SEMICOLON

  20. Function -> Identifier ( ) Block; | Identifier ( Parameters ) Block

  21. Parameters -> Identifier | Identifier , Identifier+

  22. VAR -> "var"

  23. PRINT -> "print"

  24. CLASS -> "class"

  25. RETURN -> "return"

About

My attempt to implement the Lox Language by utilizing LLVM. The language definitions are based on Robert Nystrom's book Crafting Interpreters.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors