Skip to content

Commit

Permalink
fail command and text containment
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed Jun 17, 2021
1 parent 79003a1 commit f0c4dea
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ic-repl --replica [local|ic|url] --config <dhall config> [script file]
| <candid val> // any candid value
| <var> <selector>* // variable with optional selectors
| file <text> // load external file as a blob value
| fail <exp> // convert error message as text
| call (as <name>)? <name> . <name> ( <exp>,* ) // call a canister method, and store the result as a single value
| encode (<name> . <name>)? ( <exp>,* ) // encode candid arguments as a blob value
| decode (as <name> . <name>)? <exp> // decode blob as candid values
Expand All @@ -32,7 +33,7 @@ ic-repl --replica [local|ic|url] --config <dhall config> [script file]
| [ <nat> ] // select index from vec, record, or variant value
<binop> :=
| == // structural equality
| ~= // equal under candid subtyping
| ~= // equal under candid subtyping; for text value, we check if the right side is contained in the left side
| != // not equal
```

Expand Down
20 changes: 12 additions & 8 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,19 @@ impl Command {
match op {
BinOp::Equal => assert_eq!(left, right),
BinOp::SubEqual => {
let l_ty = left.value_ty();
let r_ty = right.value_ty();
let env = TypeEnv::new();
if let Ok(left) = left.annotate_type(false, &env, &r_ty) {
assert_eq!(left, right);
} else if let Ok(right) = right.annotate_type(false, &env, &l_ty) {
assert_eq!(left, right);
if let (IDLValue::Text(left), IDLValue::Text(right)) = (&left, &right) {
assert!(left.contains(right));
} else {
assert_eq!(left, right);
let l_ty = left.value_ty();
let r_ty = right.value_ty();
let env = TypeEnv::new();
if let Ok(left) = left.annotate_type(false, &env, &r_ty) {
assert_eq!(left, right);
} else if let Ok(right) = right.annotate_type(false, &env, &l_ty) {
assert_eq!(left, right);
} else {
assert_eq!(left, right);
}
}
}
BinOp::NotEqual => assert_ne!(left, right),
Expand Down
5 changes: 5 additions & 0 deletions src/exp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum Exp {
method: Option<Method>,
blob: Box<Exp>,
},
Fail(Box<Exp>),
// from IDLValue without the infered types + Nat8
Bool(bool),
Null,
Expand Down Expand Up @@ -92,6 +93,10 @@ impl Exp {
let env = TypeEnv::new();
arg.annotate_type(true, &env, &ty)?
}
Exp::Fail(v) => match v.eval(helper) {
Err(e) => IDLValue::Text(e.to_string()),
Ok(_) => return Err(anyhow!("Expect an error state")),
},
Exp::Decode { method, blob } => {
let blob = blob.eval(helper)?;
if blob.value_ty() != Type::Vec(Box::new(Type::Nat8)) {
Expand Down
2 changes: 2 additions & 0 deletions src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ extern {
"config" => Token::Config,
"assert" => Token::Assert,
"let" => Token::Let,
"fail" => Token::Fail,
"file" => Token::File,
"identity" => Token::Identity,
"sign" => Token::Sign(<char>),
Expand Down Expand Up @@ -82,6 +83,7 @@ pub Exp: Exp = {
Arg => <>,
Variable => <>,
"file" <Text> => Exp::Blob(<>),
"fail" <Exp> => Exp::Fail(Box::new(<>)),
"call" <method:Method> <args:Exps> => Exp::Call{method:Some(method), args, mode: CallMode::Call},
"call" "as" <proxy:Name> <method:Method> <args:Exps> => Exp::Call{method:Some(method), args, mode: CallMode::Proxy(proxy)},
"encode" <method:Method?> <args:Exps> => Exp::Call{method, args, mode: CallMode::Encode},
Expand Down
2 changes: 2 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub enum Token {
Import,
#[token("opt")]
Opt,
#[token("fail")]
Fail,
#[token("call")]
Call,
#[token("encode")]
Expand Down

0 comments on commit f0c4dea

Please sign in to comment.