MCPcopy Create free account
hub / github.com/dfinity/ic-repl / eval

Method eval

src/exp.rs:84–795  ·  view source on GitHub ↗
(self, helper: &MyHelper)

Source from the content-addressed store, hash-verified

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 };

Callers 7

runMethod · 0.80
to_labelMethod · 0.80
projectFunction · 0.80
mapFunction · 0.80
filterFunction · 0.80
foldFunction · 0.80
parse_valueFunction · 0.80

Calls 15

projectFunction · 0.85
cast_typeFunction · 0.85
TextEnum · 0.85
resolve_pathFunction · 0.85
from_textFunction · 0.85
get_neuron_subaccountFunction · 0.85
parse_state_pathFunction · 0.85
fetch_state_pathFunction · 0.85
get_fieldFunction · 0.85
sendFunction · 0.85
send_messagesFunction · 0.85
args_to_valueFunction · 0.85

Tested by

no test coverage detected