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 branch 'master' of github.com:sampsyo/bril
- Loading branch information
Showing
70 changed files
with
1,269 additions
and
57 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
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,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
Oops, something went wrong.