mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-29 04:24:43 +00:00
31 lines
853 B
Python
31 lines
853 B
Python
MyBool = Inherit {0, 1}
|
|
MyBool.
|
|
Fls = MyBool::__new__ 0
|
|
Tru = MyBool::__new__ 1
|
|
|
|
Expr = Enum {
|
|
.Lit = Int
|
|
.Add = {lhs = Expr; rhs = Expr}
|
|
.Sub = {lhs = Expr; rhs = Expr}
|
|
.Pos = {expr = Expr}
|
|
.Neg = {expr = Expr}
|
|
# .Failure = ...
|
|
}
|
|
Expr|<: Show|.
|
|
show self = match self:
|
|
Self.Lit(i) -> i.show()
|
|
Self.Add{lhs; rhs} -> "\{lhs}+\{rhs}"
|
|
Self.Sub{lhs; rhs} -> "\{lhs}-\{rhs}"
|
|
Self.Pos{expr;} -> "+\{expr}"
|
|
Self.Neg{expr;} -> "-\{expr}"
|
|
Expr.
|
|
eval self = match self:
|
|
Self.Lit(i) -> i
|
|
Self.Add{lhs; rhs} -> lhs.eval() + rhs.eval()
|
|
Self.Sub{lhs; rhs} -> lhs.eval() - rhs.eval()
|
|
Self.Pos{expr;} -> +expr.eval()
|
|
Self.Neg{expr;} -> -expr.eval()
|
|
|
|
expr = Expr.Add {lhs = Expr.Lit(1); rhs = Expr.Lit(2)}
|
|
print! expr # 1 + 2
|
|
assert expr.eval() == 3
|