Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/riwanou/simple-compiler
Compiler project from Tuebingen University.
https://github.com/riwanou/simple-compiler
Last synced: 16 days ago
JSON representation
Compiler project from Tuebingen University.
- Host: GitHub
- URL: https://github.com/riwanou/simple-compiler
- Owner: riwanou
- License: mit
- Created: 2024-04-26T14:28:58.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-04-26T15:09:21.000Z (9 months ago)
- Last Synced: 2024-11-11T19:43:12.166Z (3 months ago)
- Language: Rust
- Size: 47.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple compiler in Rust
Simple rust compiler with a backend targeting [WebAssembly](https://webassembly.org/).
## Overview
- `scanner`: Converts source code into tokens.
- `syntax`: Contains language constructs.
- `parser`: Builds an abstract syntax tree (AST) from tokens.
- `typecheck`: Ensures type correctness of the AST.
- `codegen`: Generates WebAssembly code from on the AST.## Libraries used
- [wasm-tools](https://github.com/bytecodealliance/wasm-tools) (by Bytecode Alliance)
- wasm-encoder for code generation.
- wasmi for the interpreter.
- helper functions.
- [pretty_assertions](https://github.com/rust-pretty-assertions/rust-pretty-assertions) (for colored test diffs)## Commands
- Compile and run a `.lg` file:
```bash
cargo run -- filename.lg
```- Run all the tests (nocapture is used for scanner test):
```bash
cargo test -- --nocapture
```## Basics
- Every programs must have a `main` entry point function, returning an `int`.
- Numeric types are 32-bit integers, booleans are compiled as `0` or `1`.
- Types: `int` and `bool`.
- `let` and `=` body are defined by the next underneath line, *at least one line break*.
- Everything except functions are expressions.## Example
- Returns `3` if `is_null` is true, otherwise returns `3`:
```
int main()
let is_null = true
let var = (foo(is_null, 5*2, 0-10) + 1) * 2
var = var + 2 / 2
var
endint foo(bool null, int res1, int res2)
if null then
0
else
let boolean = if fee() then res1 else res2 end
boolean
end
endbool fee()
(1 < 2 & 4-2 > 3) == false & true
end
```