doughlang: Expressions in patterns
ast: - Document operators - Parameterize Pat with Annotation - Consolidate Path and Lit into "Value" (Expr) - Consolidate NamedRecord/Namedtuple into PatOp::TypePrefixed - Allow Pat<Pat,*> patterns - Additional helper functions on Expr and Pat lexer: - Support inner-doc comment syntax `//!` - Cleans up `///` or `//!` prefix parser: - Make Parse::Prec `Default` - Allow expression elision after `..`/`..=` - Fix Parser::consume not updating elide_do state - Add token splitting, to make `&&Expr` and `&&Pat` easier - error: Embed Pat precedence in ParseError
This commit is contained in:
@@ -7,15 +7,15 @@ enum Expr {
|
||||
|
||||
fn execute(expr: Expr) -> f64 {
|
||||
match expr {
|
||||
ExprAtom(value) => value,
|
||||
ExprOp('*', [lhs, rhs]) => execute(lhs) * execute(rhs),
|
||||
ExprOp('/', [lhs, rhs]) => execute(lhs) / execute(rhs),
|
||||
ExprOp('%', [lhs, rhs]) => execute(lhs) % execute(rhs),
|
||||
ExprOp('+', [lhs, rhs]) => execute(lhs) + execute(rhs),
|
||||
ExprOp('-', [lhs, rhs]) => execute(lhs) - execute(rhs),
|
||||
// ExprOp('>', [lhs, rhs]) => (execute(lhs) as u64 >> execute(rhs) as u64) as f64,
|
||||
// ExprOp('<', [lhs, rhs]) => (execute(lhs) as u64 << execute(rhs) as u64) as f64,
|
||||
ExprOp('-', [lhs]) => - execute(lhs),
|
||||
Expr::Atom(value) => value,
|
||||
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),
|
||||
other => {
|
||||
panic("Unknown operation: " + fmt(other))
|
||||
}
|
||||
@@ -25,9 +25,9 @@ fn execute(expr: Expr) -> f64 {
|
||||
/// Formats an expression to a string
|
||||
fn fmt_expr(expr: Expr) -> str {
|
||||
match expr {
|
||||
ExprAtom(value) => fmt(value),
|
||||
ExprOp(operator, [lhs, rhs]) => fmt('(', fmt_expr(lhs), ' ', operator, ' ', fmt_expr(rhs), ')'),
|
||||
ExprOp(operator, [rhs]) => fmt(operator, fmt_expr(rhs)),
|
||||
Expr::Atom(value) => fmt(value),
|
||||
Expr::Op(operator, [lhs, rhs]) => fmt('(', fmt_expr(lhs), ' ', operator, ' ', fmt_expr(rhs), ')'),
|
||||
Expr::Op(operator, [rhs]) => fmt(operator, fmt_expr(rhs)),
|
||||
_ => println("Unexpected expr: ", expr),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user