Skip to content

Use features to describe LLVM wrapper instead of branch #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Make sure you have
2. Cloned this repository (follow the instructions in each chapter)
3. LLVM installed to run and test locally `cargo test --tests`
* Easiest option is LLVM v10.0 ([Debian/Ubuntu](https://apt.llvm.org/) or [macOS](https://formulae.brew.sh/formula/llvm))
* Otherwise, in `Cargo.toml` you'd need to change the `inkwell = { ..., branch = "your-llvm-version" }` with LLVM version on your system (output of `llvm-config --version`)
* Otherwise, in `Cargo.toml` you'd need to change the `inkwell = { ..., features = ["your-llvm-version"] }` with LLVM version on your system (output of `llvm-config --version`)


To build the book locally, navigate to the `book` subdirectory and follow the instructions in [mdbook](https://github.com/rust-lang/mdBook).
Expand Down
4 changes: 2 additions & 2 deletions book/src/01_calculator/basic_llvm.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

### Setup

The code is available in [`calculator/examples/llvm/src/main.rs`](https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/calculator/examples/llvm/src/main.rs). Because my `llvm-config --version` shows `10.0.0` so I'm using `branch = "llvm=10-0"` in inkwell
The code is available in [`calculator/examples/llvm/src/main.rs`](https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/calculator/examples/llvm/src/main.rs). Because my `llvm-config --version` shows `10.0.0` so I'm using `features = ["llvm10-0"]` in inkwell

```text
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "llvm10-0" }
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["llvm10-0"] }
```

Go to [`calculator/examples/llvm`](https://github.com/ehsanmok/create-your-own-lang-with-rust/blob/master/calculator/examples/llvm/) sub-crate and `cargo run`.
Expand Down
2 changes: 1 addition & 1 deletion calculator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"
pest = "2.1"
pest_derive = "2.1"
anyhow = "1.0"
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "llvm10-0" } # use branch according to your llvm version
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["llvm10-0"] } # use correct feature according to your llvm version
rustyline = "6.2"
cfg-if = "0.1"

Expand Down
3 changes: 2 additions & 1 deletion calculator/src/compiler/jit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use inkwell::{
builder::Builder, context::Context, execution_engine::JitFunction, types::IntType,
values::IntValue, OptimizationLevel,
values::AnyValue, values::IntValue, OptimizationLevel,
};

use crate::{Compile, Node, Operator, Result};
Expand Down Expand Up @@ -94,6 +94,7 @@ mod tests {
assert_eq!(Jit::from_source("2 + (2 - 1)").unwrap(), 3);
assert_eq!(Jit::from_source("(2 + 3) - 1").unwrap(), 4);
assert_eq!(Jit::from_source("1 + ((2 + 3) - (2 + 3))").unwrap(), 1);
assert_eq!(Jit::from_source("(1 + 2)").unwrap(), 3);
// parser fails
// assert_eq!(Jit::from_source("2 + 3 - 1").unwrap(), 4);
}
Expand Down
4 changes: 2 additions & 2 deletions calculator/src/grammar.pest
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Program = _{ SOI ~ Expr ~ EOF }

Expr = { UnaryExpr | BinaryExpr }
Expr = { UnaryExpr | BinaryExpr | Term }

Term = _{Int | "(" ~ Expr ~ ")" }

UnaryExpr = { Operator ~ Term }

BinaryExpr = { Term ~ (Operator ~ Term)* }
BinaryExpr = { Term ~ (Operator ~ Term)+ }

Operator = { "+" | "-" }

Expand Down