Skip to content

Commit

Permalink
Merge pull request rust-lang#19 from 6A/kaleidoscope
Browse files Browse the repository at this point in the history
Added Kaleidoscope example
  • Loading branch information
TheDan64 committed Oct 4, 2017
2 parents cdc1560 + 92ae1da commit fd5199a
Show file tree
Hide file tree
Showing 5 changed files with 1,428 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ enum-methods = "0.0.7"
libc = "*"
llvm-sys = "37.0.5" # TODO: Configure based on feature toggle. GH#1

[[example]]
name = "kaleidoscope"
path = "examples/kaleidoscope/main.rs"

[badges]
travis-ci = { repository = "TheDan64/inkwell" }
codecov = { repository = "TheDan64/inkwell" }
Expand Down
46 changes: 46 additions & 0 deletions examples/kaleidoscope/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Kaleidoscope

This example shows how one can implement the [Kaleidoscope programming language](https://llvm.org/docs/tutorial/index.html) using Inkwell.
It implements every feature up to the [7th chapter](https://llvm.org/docs/tutorial/LangImpl07.html).

When running this example (using the `cargo run --example kaleidoscope` command), a prompt will be displayed; for example:

```
?> 1 + 1
=> 2
?> var a = 5, b = 10 in a * b
=> 50
?> def fib(n) if n < 2 then n else fib(n - 1) + fib(n - 2)
?> fib(40)
=> 102334155
?>
```

Additional arguments can be passed to the produced executable:
- `--dc`: **D**isplay **C**ompiler output
- `--dp`: **D**isplay **P**arser output
- `--dl`: **D**isplay **L**exer output

For example, running with all three switches may lead to the following output:
```
?> 1 + 2 * 2
-> Attempting to parse lexed input:
[Number(1), Op('+'), Number(2), Op('*'), Number(2)]
-> Expression parsed:
Binary { op: '+', left: Number(1), right: Binary { op: '*', left: Number(2), right: Number(2) } }
-> Expression compiled to IR:
define double @anonymous() {
entry:
ret double 5.000000e+00
}
=> 5
```

Finally, the prompt can be exited by entering "exit" or "quit".
Loading

0 comments on commit fd5199a

Please sign in to comment.