Skip to content

mrjameshamilton/clox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

clox compiler

This is an implementation of the C Lox compiler from Crafting Interpreters. The implementation is mostly the same as in the book but with the following changes:

  • Constants use 8 or 16-bit indices.
  • New instructions:
    • OP_ZERO push 0 onto the stack
    • OP_ONE push 1 onto the stack
    • OP_MINUS_ONE push -1 onto the stack
    • OP_INTEGER pushes an 8-bit integer onto the stack
    • OP_INTEGER_16 pushes a 16-bit integer onto the stack
    • OP_CONSTANT_16 push a 16-bit indexed constant onto the stack
  • New native functions:
    • read() reads a byte from stdin or nil if end of stream
    • utf(byte, byte, byte, byte) converts 1, 2, 3, or 4 bytes into a UTF string
    • printerr(string) prints a string to stderr
    • exit(number) exits with the specific exit code

These additions allow running Lox.lox, an Lox interpreter written in Lox. In particular, Lox.lox introduces more constants than the original limit 255 per-chunk in the original clox implementation and requires the 4 native functions.

Build

$ gcc src/*.src -O3 -o clox

Run

$ ./clox examples/helloworld.lox

Releases

No releases published

Packages

No packages published

Languages