Skip to content

Commit fd5199a

Browse files
authored
Merge pull request rust-lang#19 from 6A/kaleidoscope
Added Kaleidoscope example
2 parents cdc1560 + 92ae1da commit fd5199a

File tree

5 files changed

+1428
-4
lines changed

5 files changed

+1428
-4
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ enum-methods = "0.0.7"
1616
libc = "*"
1717
llvm-sys = "37.0.5" # TODO: Configure based on feature toggle. GH#1
1818

19+
[[example]]
20+
name = "kaleidoscope"
21+
path = "examples/kaleidoscope/main.rs"
22+
1923
[badges]
2024
travis-ci = { repository = "TheDan64/inkwell" }
2125
codecov = { repository = "TheDan64/inkwell" }

examples/kaleidoscope/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Kaleidoscope
2+
3+
This example shows how one can implement the [Kaleidoscope programming language](https://llvm.org/docs/tutorial/index.html) using Inkwell.
4+
It implements every feature up to the [7th chapter](https://llvm.org/docs/tutorial/LangImpl07.html).
5+
6+
When running this example (using the `cargo run --example kaleidoscope` command), a prompt will be displayed; for example:
7+
8+
```
9+
?> 1 + 1
10+
=> 2
11+
12+
?> var a = 5, b = 10 in a * b
13+
=> 50
14+
15+
?> def fib(n) if n < 2 then n else fib(n - 1) + fib(n - 2)
16+
17+
?> fib(40)
18+
=> 102334155
19+
20+
?>
21+
```
22+
23+
Additional arguments can be passed to the produced executable:
24+
- `--dc`: **D**isplay **C**ompiler output
25+
- `--dp`: **D**isplay **P**arser output
26+
- `--dl`: **D**isplay **L**exer output
27+
28+
For example, running with all three switches may lead to the following output:
29+
```
30+
?> 1 + 2 * 2
31+
-> Attempting to parse lexed input:
32+
[Number(1), Op('+'), Number(2), Op('*'), Number(2)]
33+
34+
-> Expression parsed:
35+
Binary { op: '+', left: Number(1), right: Binary { op: '*', left: Number(2), right: Number(2) } }
36+
37+
-> Expression compiled to IR:
38+
define double @anonymous() {
39+
entry:
40+
ret double 5.000000e+00
41+
}
42+
43+
=> 5
44+
```
45+
46+
Finally, the prompt can be exited by entering "exit" or "quit".

0 commit comments

Comments
 (0)