Skip to content

Letter is a compiler project built in TypeScript using LLVM node bindings.

Notifications You must be signed in to change notification settings

willothy/Letter

Repository files navigation

Letter

CodeQL DeepSource DeepSource

Letter is a compiler project built in TypeScript using the llvm-bindings module. The parser is based off of lectures by Dmitry Soshnikov. Letter compiles to LLVM IR, which can then be compiled with Clang.

Letter is functional-only at the moment, though the parser supports objects and I plan on implementing them.

Since Letter compiles using LLVM, it is compatible with C and C++. This hasn't been tested much, but there's an example of a Letter function being called from a C program in the /example/c-link/ folder.

Letter requires an installation of LLVM 13. The llvm-bindings readme has good installation instructions.

Features that are working now:

  • Main function as entry point
  • Preprocessor (import, define)
  • Math operations
  • Extern functions
  • Function definitions
    • Void type
    • Overloading
  • If statements
  • Implicit typecasting of numbers
  • Printing values using print.lt (printf wrapper)
  • Strings, Floats, Doubles, Booleans, Integers

Use:
The released version is outdated: I'll update this when there's a release with LLVM working fully. For now, it must be run from node.

Run the compiler using npm run compile <filename>. This will run letter and clang, prividing an executable.
To build an object file, run npm run object <filename>. The below flags don't work with this command - only with npx ts-node ./letter.ts -f <filename> <flags> which will generate llvm IR. You then need to compile this IR using clang.

Current flags available: Tested:

  • -s, --stack: Prints the full JS stack on error. Normally, errors just print the message without stack trace when no debug flags are set.
  • -t, --tokens: Prints a list of all tokens in the program, including from imported files.
  • -a, --ast: Prints the full program's AST representation in JSON.
  • -i, --emit-ir: Emits LLVM IR for compilation.

Untested with new versions:

  • -m, --minify: Combines all code into one file, and turns it into one line.
    • Recommended use: ./bin/letter.rdp -f filename --minify > file.min.lt

Sample programs and are included in the /example/ folder.