(self, helper: &MyHelper)
| 82 | ) |
| 83 | } |
| 84 | pub fn eval(self, helper: &MyHelper) -> Result<IDLValue> { |
| 85 | Ok(match self { |
| 86 | Exp::Path(id, path) => { |
| 87 | let v = helper |
| 88 | .env |
| 89 | .0 |
| 90 | .get(&id) |
| 91 | .ok_or_else(|| anyhow!("Undefined variable {}", id))? |
| 92 | .clone(); |
| 93 | project(helper, v, path)? |
| 94 | } |
| 95 | Exp::AnnVal(v, ty) => { |
| 96 | let arg = v.eval(helper)?; |
| 97 | cast_type(arg, &ty).with_context(|| format!("casting to type {ty} fails"))? |
| 98 | } |
| 99 | Exp::Fail(v) => match v.eval(helper) { |
| 100 | Err(e) => IDLValue::Text(e.to_string()), |
| 101 | Ok(_) => return Err(anyhow!("Expects an error state")), |
| 102 | }, |
| 103 | Exp::Apply(func, exps) => { |
| 104 | use crate::account_identifier::*; |
| 105 | |
| 106 | // functions that cannot evaluate arguments first |
| 107 | match func.as_str() { |
| 108 | "ite" => { |
| 109 | if exps.len() != 3 { |
| 110 | return Err(anyhow!( |
| 111 | "ite expects a bool, true branch and false branch" |
| 112 | )); |
| 113 | } |
| 114 | return Ok(match exps[0].clone().eval(helper)? { |
| 115 | IDLValue::Bool(true) => exps[1].clone().eval(helper)?, |
| 116 | IDLValue::Bool(false) => exps[2].clone().eval(helper)?, |
| 117 | _ => { |
| 118 | return Err(anyhow!( |
| 119 | "ite expects the first argument to be a boolean expression" |
| 120 | )); |
| 121 | } |
| 122 | }); |
| 123 | } |
| 124 | "exist" => { |
| 125 | if exps.len() != 1 { |
| 126 | return Err(anyhow!("exist expects an expression")); |
| 127 | } |
| 128 | return Ok(match exps[0].clone().eval(helper) { |
| 129 | Ok(_) => IDLValue::Bool(true), |
| 130 | Err(_) => IDLValue::Bool(false), |
| 131 | }); |
| 132 | } |
| 133 | "export" => { |
| 134 | use std::io::{BufWriter, Write}; |
| 135 | if exps.len() <= 1 { |
| 136 | return Err(anyhow!("export expects at least two arguments")); |
| 137 | } |
| 138 | let path = exps[0].clone().eval(helper)?; |
| 139 | let IDLValue::Text(path) = path else { |
| 140 | return Err(anyhow!("export expects first argument to be a file path")); |
| 141 | }; |
no test coverage detected