Skip to content

Commit

Permalink
| and ^
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-unc committed Mar 21, 2021
1 parent 31e3af6 commit 82a71c7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Limp has two major constructions. The first is the "atom", which is a singular u
| `*` | Multiplies all rands given. Requires at least two rands (int/float).
| `/` | Divides the first rand from the remaining rands. Requires at least two rands (int/float).
| `&` | Bit ands the first rand with the second. Requires at two rands (int).
| <code>&#124;</code> | Bit ors the first rand with the second. Requires at two rands (int).
| `^` | Bit xors the first rand with the second. Requires at two rands (int).
| `and` | Ands all rands given. Requires at least two rands (boolean).
| `or` | Ors all rands given. Requires at least two rands (boolean).
| `xor` | Xors all rands given. Requires at least two rands (boolean).
Expand Down
30 changes: 30 additions & 0 deletions src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,36 @@ fn eval_invocation(invocation: Pair<Rule>) -> LimpValue {
_ => { panic!("Bad type of {:?} for &!", rands[0])}
}
},
"|" => {
if rands.len() != 2 {
panic!("Rator `|` expects 2 rands!");
}

match rands[0] {
Integer(i1) => {
return match rands[1] {
Integer(i2) => Integer(i1 | i2),
_ => { panic!("Bad type of {:?} for |!", rands[1])}
}
}
_ => { panic!("Bad type of {:?} for |!", rands[0])}
}
},
"^" => {
if rands.len() != 2 {
panic!("Rator `^` expects 2 rands!");
}

match rands[0] {
Integer(i1) => {
return match rands[1] {
Integer(i2) => Integer(i1 ^ i2),
_ => { panic!("Bad type of {:?} for ^!", rands[1])}
}
}
_ => { panic!("Bad type of {:?} for ^!", rands[0])}
}
},
"and" => {
if rands.len() < 2 {
panic!("Rator `and` expects at least 2 rands!");
Expand Down
2 changes: 1 addition & 1 deletion src/limp.pest
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ boolean = { "true" | "false" }
name = { num_procedures | boolean_procedures | void_procedures }

num_procedures = _{ "+" | "-" | "*" | "/" | bitwise_procedures }
bitwise_procedures = _{ "&" }
bitwise_procedures = _{ "&" | "|" | "^" }
boolean_procedures = _{ "and" | "or" | "xor" | "not" | "==" | "!=" }
void_procedures = _{ "print" | "exit" }

Expand Down

0 comments on commit 82a71c7

Please sign in to comment.