Skip to content

Commit

Permalink
fix rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanWoollett-Light committed Nov 24, 2023
1 parent 74d5508 commit 4394a4e
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 199 deletions.
40 changes: 22 additions & 18 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,18 @@ impl Type {
I32_MIN..I16_MIN => vec![Self::I64, Self::I32],
I16_MIN..I8_MIN => vec![Self::I64, Self::I32, Self::I16],
I8_MIN..0 => vec![Self::I64, Self::I32, Self::I16, Self::I8],
0..U8_MAX => vec![
Self::I64,
Self::I32,
Self::I16,
Self::I8,
Self::U64,
Self::U32,
Self::U16,
Self::U8,
],
0..U8_MAX => {
vec![
Self::I64,
Self::I32,
Self::I16,
Self::I8,
Self::U64,
Self::U32,
Self::U16,
Self::U8,
]
}
U8_MAX => vec![
Self::I64,
Self::I32,
Expand All @@ -276,14 +278,16 @@ impl Type {
Self::U16,
Self::U8,
],
U16_EDGE..U16_MAX => vec![
Self::I64,
Self::I32,
Self::I16,
Self::U64,
Self::U32,
Self::U16,
],
U16_EDGE..U16_MAX => {
vec![
Self::I64,
Self::I32,
Self::I16,
Self::U64,
Self::U32,
Self::U16,
]
}
U16_MAX => vec![Self::I64, Self::I32, Self::U64, Self::U32, Self::U16],
U32_EDGE..U32_MAX => vec![Self::I64, Self::I32, Self::U64, Self::U32],
U32_MAX => vec![Self::I64, Self::U64, Self::U32],
Expand Down
139 changes: 87 additions & 52 deletions src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,46 @@ pub fn get_type<R: Read>(bytes: &mut Peekable<Bytes<R>>) -> Type {
match bytes.next().map(Result::unwrap) {
Some(b'i') => match bytes.next().map(Result::unwrap) {
Some(b'8') => Type::I8,
Some(b'1') => match bytes.next().map(Result::unwrap) {
Some(b'6') => Type::I16,
_ => panic!(),
},
Some(b'3') => match bytes.next().map(Result::unwrap) {
Some(b'2') => Type::I32,
_ => panic!(),
},
Some(b'6') => match bytes.next().map(Result::unwrap) {
Some(b'4') => Type::I64,
_ => panic!(),
},
Some(b'1') => {
match bytes.next().map(Result::unwrap) {
Some(b'6') => Type::I16,
_ => panic!(),
}
}
Some(b'3') => {
match bytes.next().map(Result::unwrap) {
Some(b'2') => Type::I32,
_ => panic!(),
}
}
Some(b'6') => {
match bytes.next().map(Result::unwrap) {
Some(b'4') => Type::I64,
_ => panic!(),
}
}
_ => panic!(),
},
Some(b'u') => match bytes.next().map(Result::unwrap) {
Some(b'8') => Type::U8,
Some(b'1') => match bytes.next().map(Result::unwrap) {
Some(b'6') => Type::U16,
_ => panic!(),
},
Some(b'3') => match bytes.next().map(Result::unwrap) {
Some(b'2') => Type::U32,
_ => panic!(),
},
Some(b'6') => match bytes.next().map(Result::unwrap) {
Some(b'4') => Type::U64,
_ => panic!(),
},
Some(b'1') => {
match bytes.next().map(Result::unwrap) {
Some(b'6') => Type::U16,
_ => panic!(),
}
}
Some(b'3') => {
match bytes.next().map(Result::unwrap) {
Some(b'2') => Type::U32,
_ => panic!(),
}
}
Some(b'6') => {
match bytes.next().map(Result::unwrap) {
Some(b'4') => Type::U64,
_ => panic!(),
}
}
_ => panic!(),
},
_ => panic!(),
Expand Down Expand Up @@ -242,9 +254,7 @@ pub fn get_value<R: Read>(bytes: &mut Peekable<Bytes<R>>) -> Value {
_ => panic!(),
}
}
Value::Literal(Literal::String(
std::str::from_utf8(&string).unwrap().to_string(),
))
Value::Literal(Literal::String(std::str::from_utf8(&string).unwrap().to_string()))
}
_ => panic!(
"unexpected: {:?}",
Expand Down Expand Up @@ -418,11 +428,13 @@ pub fn get_statement<R: Read>(bytes: &mut Peekable<Bytes<R>>) -> Statement {
arg: get_values(bytes),
},
// Exit
(b"exit", None) => Statement {
comptime,
op: Op::Syscall(Syscall::Exit),
arg: get_values(bytes),
},
(b"exit", None) => {
Statement {
comptime,
op: Op::Syscall(Syscall::Exit),
arg: get_values(bytes),
}
}
// If
(b"if", None) => {
assert_eq!(bytes.next().map(Result::unwrap), Some(b' '));
Expand All @@ -434,11 +446,13 @@ pub fn get_statement<R: Read>(bytes: &mut Peekable<Bytes<R>>) -> Statement {
let arg = vec![lhs, rhs];
Statement { comptime, op, arg }
}
(b"break", None) => Statement {
comptime,
op: Op::Intrinsic(Intrinsic::Break),
arg: Vec::new(),
},
(b"break", None) => {
Statement {
comptime,
op: Op::Intrinsic(Intrinsic::Break),
arg: Vec::new(),
}
}
(b"require", None) => {
assert_eq!(bytes.next().map(Result::unwrap), Some(b' '));
let lhs = get_value(bytes);
Expand All @@ -464,33 +478,44 @@ pub fn get_statement<R: Read>(bytes: &mut Peekable<Bytes<R>>) -> Statement {
assert_eq!(Some(b' '), bytes.next().map(Result::unwrap));
let arg = get_value(bytes);
let arg = vec![lhs, arg];
Statement { comptime, op: Op::Intrinsic(arithmetic), arg, }
Statement {
comptime,
op: Op::Intrinsic(arithmetic),
arg,
}
}
Some(b':') => match bytes.next().map(Result::unwrap) {
Some(b' ') => {
let variable_type = get_type(bytes);
Statement {
comptime,
op: Op::Special(Special::Type),
arg: vec![lhs, Value::Type(variable_type)]
arg: vec![lhs, Value::Type(variable_type)],
}
}
Some(b'=') => {
assert_eq!(Some(b' '), bytes.next().map(Result::unwrap));
let first = get_value(bytes);

let get_statement = |lhs: Value, first: Value, bytes: &mut Peekable<Bytes<R>>| -> Statement {
let get_statement = |lhs: Value,
first: Value,
bytes: &mut Peekable<Bytes<R>>|
-> Statement {
let tail = get_values(bytes);
match (&first, &tail) {
(Value::Variable(Variable { identifier, index: None }), _) if let Ok(syscall) = Syscall::try_from(identifier.as_slice()) => {
(
Value::Variable(Variable {
identifier,
index: None,
}),
_,
) if let Ok(syscall) = Syscall::try_from(identifier.as_slice()) => {
Statement {
comptime,
op: Op::Syscall(syscall),
arg: once(lhs)
.chain(tail.iter().cloned())
.collect(),
arg: once(lhs).chain(tail.iter().cloned()).collect(),
}
},
}
_ => Statement {
comptime,
op: Op::Intrinsic(Intrinsic::Assign),
Expand All @@ -512,17 +537,27 @@ pub fn get_statement<R: Read>(bytes: &mut Peekable<Bytes<R>>) -> Statement {

let second = get_value(bytes);
let arg = vec![lhs, first, second];
Statement { comptime, op: Op::Intrinsic(arithmetic), arg, }
},
Statement {
comptime,
op: Op::Intrinsic(arithmetic),
arg,
}
}
_ => get_statement(lhs, first, bytes),
}
},
}
_ => get_statement(lhs, first, bytes),
}
},
_ => panic!("{:?}", std::str::from_utf8(&bytes.map(Result::unwrap).collect::<Vec<_>>())),
}
_ => panic!("{:?}", std::str::from_utf8(&bytes.map(Result::unwrap).collect::<Vec<_>>())),
}
_ => panic!(
"{:?}",
std::str::from_utf8(&bytes.map(Result::unwrap).collect::<Vec<_>>())
),
},
_ => panic!(
"{:?}",
std::str::from_utf8(&bytes.map(Result::unwrap).collect::<Vec<_>>())
),
}
}
}
Expand Down
Loading

0 comments on commit 4394a4e

Please sign in to comment.