sample-code/calculator: Bit-shifts and better formatting
This commit is contained in:
parent
02239c5ce4
commit
986bac9e6b
19
sample-code/calculator.cl
Normal file → Executable file
19
sample-code/calculator.cl
Normal file → Executable file
@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/env -S conlang-run
|
#!/usr/bin/env -S conlang -r false
|
||||||
//! A simple five-function pn calculator
|
//! A simple five-function pn calculator
|
||||||
|
|
||||||
enum Expr {
|
enum Expr {
|
||||||
@ -16,6 +16,8 @@ fn execute(expr: Expr) -> f64 {
|
|||||||
Expr::Op('%', [lhs, rhs]) => execute(lhs) % execute(rhs),
|
Expr::Op('%', [lhs, rhs]) => execute(lhs) % execute(rhs),
|
||||||
Expr::Op('+', [lhs, rhs]) => execute(lhs) + execute(rhs),
|
Expr::Op('+', [lhs, rhs]) => execute(lhs) + execute(rhs),
|
||||||
Expr::Op('-', [lhs, rhs]) => execute(lhs) - execute(rhs),
|
Expr::Op('-', [lhs, rhs]) => execute(lhs) - execute(rhs),
|
||||||
|
Expr::Op('>', [lhs, rhs]) => (execute(lhs) as u64 >> execute(rhs) as u64) as f64,
|
||||||
|
Expr::Op('<', [lhs, rhs]) => (execute(lhs) as u64 << execute(rhs) as u64) as f64,
|
||||||
Expr::Op('-', [lhs]) => - execute(lhs),
|
Expr::Op('-', [lhs]) => - execute(lhs),
|
||||||
other => {
|
other => {
|
||||||
panic("Unknown operation: " + fmt(other))
|
panic("Unknown operation: " + fmt(other))
|
||||||
@ -83,6 +85,7 @@ fn space(line: [char]) -> [char] {
|
|||||||
|
|
||||||
enum Power {
|
enum Power {
|
||||||
None,
|
None,
|
||||||
|
Shift,
|
||||||
Factor,
|
Factor,
|
||||||
Term,
|
Term,
|
||||||
Exponent,
|
Exponent,
|
||||||
@ -97,7 +100,9 @@ fn inf_bp(op: char) -> (i32, i32) {
|
|||||||
'%' => Power::Term,
|
'%' => Power::Term,
|
||||||
'+' => Power::Factor,
|
'+' => Power::Factor,
|
||||||
'-' => Power::Factor,
|
'-' => Power::Factor,
|
||||||
_ => panic("Unknown operation: " + op),
|
'>' => Power::Shift,
|
||||||
|
'<' => Power::Shift,
|
||||||
|
_ => Power::None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,9 +114,11 @@ fn pre_bp(op: char) -> i32 {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn my_eval(input: str) {
|
fn main() {
|
||||||
let (expr, rest) = input.chars().parse(0);
|
loop {
|
||||||
println(expr, " ", rest);
|
let line = get_line("calc >");
|
||||||
|
let (expr, rest) = line.chars().parse(0);
|
||||||
|
|
||||||
execute(expr)
|
println(fmt_expr(expr), " -> ", execute(expr));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user