forked from sampsyo/bril
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sampsyo#89 from Pat-Lafon/bril-rust-extension
Bril<->Rust extension
- Loading branch information
Showing
5 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
**/target | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "bril-rs" | ||
version = "0.1.0" | ||
authors = ["Patrick LaFontaine <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
serde_json = "1.0" | ||
serde = { version = "1.0", features = ["derive"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::io::{self, Read}; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct Program { | ||
pub functions: Vec<Function>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct Function { | ||
pub name: String, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub args: Option<Vec<Argument>>, | ||
#[serde(rename = "type")] | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub return_type: Option<Type>, | ||
pub instrs: Vec<Code>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct Argument { | ||
pub name: String, | ||
#[serde(rename = "type")] | ||
pub arg_type: Type, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] | ||
#[serde(untagged)] | ||
pub enum Code { | ||
Label { label: String }, | ||
Instruction(Instruction), | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] | ||
#[serde(untagged)] | ||
pub enum Instruction { | ||
Constant { | ||
op: ConstOps, | ||
dest: String, | ||
#[serde(rename = "type")] | ||
const_type: Type, | ||
value: Literal, | ||
}, | ||
Value { | ||
op: ValueOps, | ||
dest: String, | ||
#[serde(rename = "type")] | ||
op_type: Type, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
args: Option<Vec<String>>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
funcs: Option<Vec<String>>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
labels: Option<Vec<String>>, | ||
}, | ||
Effect { | ||
op: EffectOps, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
args: Option<Vec<String>>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
funcs: Option<Vec<String>>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
labels: Option<Vec<String>>, | ||
}, | ||
} | ||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] | ||
pub enum ConstOps { | ||
#[serde(rename = "const")] | ||
Const, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] | ||
#[serde(rename_all = "lowercase")] | ||
pub enum EffectOps { | ||
#[serde(rename = "jmp")] | ||
Jump, | ||
#[serde(rename = "br")] | ||
Branch, | ||
Call, | ||
#[serde(rename = "ret")] | ||
Return, | ||
Print, | ||
Nop, | ||
Store, | ||
Free, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] | ||
#[serde(rename_all = "lowercase")] | ||
pub enum ValueOps { | ||
Add, | ||
Sub, | ||
Mul, | ||
Div, | ||
Eq, | ||
Lt, | ||
Gt, | ||
Le, | ||
Ge, | ||
Not, | ||
And, | ||
Or, | ||
Call, | ||
Id, | ||
Phi, | ||
Fadd, | ||
Fsub, | ||
Fmul, | ||
Fdiv, | ||
Feq, | ||
Flt, | ||
Fgt, | ||
Fle, | ||
Fge, | ||
Alloc, | ||
Load, | ||
PtrAdd, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] | ||
#[serde(rename_all = "lowercase")] | ||
pub enum Type { | ||
Int, | ||
Bool, | ||
Float, | ||
#[serde(rename = "ptr")] | ||
Pointer(Box<Type>), | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] | ||
#[serde(untagged)] | ||
pub enum Literal { | ||
Int(i64), | ||
Bool(bool), | ||
Float(f64), | ||
} | ||
|
||
pub fn load_program() -> Program { | ||
let mut buffer = String::new(); | ||
io::stdin().read_to_string(&mut buffer).unwrap(); | ||
serde_json::from_str(&buffer).unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Rust Library | ||
============ | ||
|
||
This is a no-frills interface between bril's json and your rust code. It supports the bril core syntax along with SSA, Memory, and Floating Point extensions. | ||
|
||
Use | ||
--- | ||
|
||
Include this by adding the following to your Cargo.toml | ||
|
||
```toml | ||
[dependencies.bril-rs] | ||
version = "0.1.0" | ||
path = "../bril-rs" | ||
``` | ||
|
||
There is one helper function ```load_program``` which will read a valid bril program from stdin and return it as a Rust struct. Otherwise, this library can be treated like any other [serde](https://github.com/serde-rs/serde) json representation. | ||
|
||
Development | ||
----------- | ||
|
||
To maintain consistency and cleanliness, run: | ||
|
||
```bash | ||
cargo fmt | ||
cargo clippy | ||
``` |