Skip to content
/ loxcc Public

Lox to C compiler, with corresponding runtime implementation in C.

License

Notifications You must be signed in to change notification settings

cylixlee/loxcc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LoxCC

Lox to C compiler, with corresponding runtime implementation in C.

Motivation

Lox, introduced in the book Crafting Interpreters, is a language that a lot of PL enthusiasts implement compilers and interpreters for. However:

  1. My (now deleted) implementation is not as fast as clox
  2. An AOT language (with or without additional runtime), instead of JIT/Interpreted language, is what I've been seeking for.

Thus, the LoxCC project is spawned. Similar to older version of Go, there will be a C implementation of Lox C Runtime, which is compiled together with Lox programs.

Implemented Features

Lox is a dynamically typed, GC managed language. However, making it AOT-compatible is not so easy than running it in VM. Currently, we support these features:

  • Expressions
  • Value types (numbers, booleans, nil)
  • Strings
  • String interning
  • Global variables
  • Local variables
  • Control flow
  • Functions and invocations
  • Closure (Not supported)
  • Garbage Collection
  • Classes and instances
  • Methods and initializers
  • Superclasses

All of which are the corresponding concepts from the book.

NOTE: In this version, closures will NOT be supported because of implementation difficulties. It may be added in future major or minor versions.

Architecture

For now, the LoxCC is directly targeting C. The compiler direcly generates corresponding C code, copies the LOXCRT to the output directory, and (optionally) calls the system C compiler.

For more compiler behavior, see build configs.

graph LR

L[Lox Program]
A[AST]
O[Generated C code]
R["Lox C Runtime (LOXCRT)"]
C[System C Compiler]
E[Executable Binaries]

L --analyzer--> A
A --codegen--> O
O --> C
R --> C
C --> E
Loading

As the graph describes, the pipeline of compiling a Lox program using LoxCC is as below:

  1. Parse the source of Lox Program, and creates corresponding AST (Abstract Syntax Tree).
  2. Generate the AST into C codes.
  3. Copy the handwritten LOXCRT to the output directory.
  4. (optional) Call the system C compiler.

Build Configs

The build configs are written in YAML, a very expressive serialization language, and placed at the root of LoxCC.

For detailed behavior, see build-config.yaml

About

Lox to C compiler, with corresponding runtime implementation in C.

Resources

License

Stars

Watchers

Forks

Packages

No packages published