ergotree_interpreter/eval/
expr.rs

1//! Evaluation of ErgoTree expressions
2
3use ergotree_ir::mir::expr::Expr;
4use ergotree_ir::mir::value::Value;
5use ergotree_ir::source_span::Spanned;
6
7use super::error::ExtResultEvalError;
8use super::Context;
9use super::Env;
10use super::EvalError;
11use super::Evaluable;
12
13impl Evaluable for Expr {
14    fn eval<'ctx>(
15        &self,
16        env: &mut Env<'ctx>,
17        ctx: &Context<'ctx>,
18    ) -> Result<Value<'ctx>, EvalError> {
19        //ctx.cost_accum.add_cost_of(self)?;
20        let res = match self {
21            Expr::Const(c) => Ok(Value::from(c.v.clone())),
22            Expr::SubstConstants(op) => op.expr().eval(env, ctx),
23            Expr::ByteArrayToLong(op) => op.expr().eval(env, ctx),
24            Expr::ByteArrayToBigInt(op) => op.expr().eval(env, ctx),
25            Expr::LongToByteArray(op) => op.eval(env, ctx),
26            Expr::CalcBlake2b256(op) => op.eval(env, ctx),
27            Expr::CalcSha256(op) => op.eval(env, ctx),
28            Expr::Fold(op) => op.eval(env, ctx),
29            Expr::ExtractRegisterAs(op) => op.expr().eval(env, ctx),
30            Expr::GlobalVars(op) => op.eval(env, ctx),
31            Expr::MethodCall(op) => op.expr().eval(env, ctx),
32            Expr::PropertyCall(op) => op.expr().eval(env, ctx),
33            Expr::BinOp(op) => op.expr().eval(env, ctx),
34            Expr::Global => Ok(Value::Global),
35            Expr::Context => Ok(Value::Context),
36            Expr::OptionGet(v) => v.expr().eval(env, ctx),
37            Expr::Apply(op) => op.eval(env, ctx),
38            Expr::FuncValue(op) => op.eval(env, ctx),
39            Expr::ValUse(op) => op.eval(env, ctx),
40            Expr::BlockValue(op) => op.expr().eval(env, ctx),
41            Expr::SelectField(op) => op.eval(env, ctx),
42            Expr::ExtractAmount(op) => op.eval(env, ctx),
43            Expr::ConstPlaceholder(_) => Err(EvalError::UnexpectedExpr(
44                ("ConstPlaceholder is not supported").to_string(),
45            )),
46            Expr::Collection(op) => op.eval(env, ctx),
47            Expr::ValDef(_) => Err(EvalError::UnexpectedExpr(
48                ("ValDef should be evaluated in BlockValue").to_string(),
49            )),
50            Expr::And(op) => op.eval(env, ctx),
51            Expr::Or(op) => op.eval(env, ctx),
52            Expr::Xor(op) => op.eval(env, ctx),
53            Expr::Atleast(op) => op.eval(env, ctx),
54            Expr::LogicalNot(op) => op.eval(env, ctx),
55            Expr::Map(op) => op.eval(env, ctx),
56            Expr::Filter(op) => op.eval(env, ctx),
57            Expr::BoolToSigmaProp(op) => op.eval(env, ctx),
58            Expr::Upcast(op) => op.eval(env, ctx),
59            Expr::Downcast(op) => op.eval(env, ctx),
60            Expr::If(op) => op.eval(env, ctx),
61            Expr::Append(op) => op.expr().eval(env, ctx),
62            Expr::ByIndex(op) => op.expr().eval(env, ctx),
63            Expr::ExtractScriptBytes(op) => op.eval(env, ctx),
64            Expr::SizeOf(op) => op.eval(env, ctx),
65            Expr::Slice(op) => op.eval(env, ctx),
66            Expr::CreateProveDlog(op) => op.eval(env, ctx),
67            Expr::CreateProveDhTuple(op) => op.eval(env, ctx),
68            Expr::ExtractCreationInfo(op) => op.eval(env, ctx),
69            Expr::Exists(op) => op.eval(env, ctx),
70            Expr::ExtractId(op) => op.eval(env, ctx),
71            Expr::SigmaPropBytes(op) => op.eval(env, ctx),
72            Expr::OptionIsDefined(op) => op.expr().eval(env, ctx),
73            Expr::OptionGetOrElse(op) => op.expr().eval(env, ctx),
74            Expr::Negation(op) => op.expr().eval(env, ctx),
75            Expr::BitInversion(op) => op.eval(env, ctx),
76            Expr::ForAll(op) => op.eval(env, ctx),
77            Expr::Tuple(op) => op.eval(env, ctx),
78            Expr::DecodePoint(op) => op.eval(env, ctx),
79            Expr::SigmaAnd(op) => op.eval(env, ctx),
80            Expr::SigmaOr(op) => op.eval(env, ctx),
81            Expr::DeserializeRegister(op) => op.eval(env, ctx),
82            Expr::DeserializeContext(op) => op.eval(env, ctx),
83            Expr::GetVar(op) => op.eval(env, ctx),
84            Expr::MultiplyGroup(op) => op.eval(env, ctx),
85            Expr::Exponentiate(op) => op.eval(env, ctx),
86            Expr::XorOf(op) => op.eval(env, ctx),
87            Expr::ExtractBytes(op) => op.eval(env, ctx),
88            Expr::ExtractBytesWithNoRef(op) => op.eval(env, ctx),
89            Expr::TreeLookup(op) => op.eval(env, ctx),
90            Expr::CreateAvlTree(op) => op.eval(env, ctx),
91        };
92        res.enrich_err(self.span(), env)
93    }
94}
95
96impl<T: Evaluable> Evaluable for Spanned<T> {
97    fn eval<'ctx>(
98        &self,
99        env: &mut Env<'ctx>,
100        ctx: &Context<'ctx>,
101    ) -> Result<Value<'ctx>, EvalError> {
102        self.expr.eval(env, ctx)
103    }
104}