Kaleidoscope in Rust but somewhat modified.
Main differences from original Kaleidoscope:
- Function definition with
fn
instead ofdef
- Comment with
//
or/* */
, not with#
- Loop statement with genarator-like syntax (
for i <- 1..10, 1 { /* .. */ }
) - Pipe operator
/>
- LLVM version 15.x
You can open REPL with:
cargo build --release
./target/release/karreidos -i
then, you can write some codes like:
?>> extern fn cos(x); // from standard C library
?>> fn add(x, y) { x + y; }
?>> fn double(x) { x * 2; }
?>> fn func(arg) {
... if arg < 10 {
... let mut i = 10;
... for mut v <- arg .. 100, 1 {
... i = i + v;
... v = v + 1; // number of repetition is not affected by this assignment
... };
... i /> add(3);
... } else {
... cos(arg);
... }
... /> double();
... }
...
?>> func(9);
[INFO] 9854 // (10 + sum(9..=99) + 3) * 2
?>> func(31.415926535);
[INFO] 2 // cos(10pi) * 2
Or you can generate LLVM-IR with source files:
./target/release/karreidos /path/to/src.krr -o /path/to/output.ll
with debug info (but incomplete feature):
./target/release/karreidos /path/to/src.krr --debug-info -o /path/to/output.ll
Also, you can emit object file or assembly:
./target/release/karreidos /path/to/src.krr --emit obj -o /path/to/output.out
./target/release/karreidos /path/to/src.krr --emit asm -o /path/to/output.s
specify target:
./target/release/karreidos /path/to/src.krr --emit asm --target=x86_64-unknown-linux-gnu /path/to/output.s
Grammer (BNF form) is available here. Note that redefining variable in the same scope (block) is prohibited.
?>> fn func() {
... let i = 10;
... let i = 10;
... }
...
[ERROR] Redefined("i", Variable)
- Kaleidoscope itself (except chapter 6).
- Language server protocol
- Port them using tree-sitter