From dc03c32c7655a51b18af68ed493bf6a6adc3d9d5 Mon Sep 17 00:00:00 2001 From: chenyan-dfinity Date: Sat, 24 Apr 2021 19:37:56 -0700 Subject: [PATCH 1/3] bump variant index --- Cargo.lock | 4 ++-- src/grammar.lalrpop | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e132b20..75b6c47 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -253,7 +253,7 @@ checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" [[package]] name = "candid" version = "0.7.0-beta.1" -source = "git+https://github.com/dfinity/candid.git?branch=fix-beta#1a8f146d8ba017c8d9849215bf803ecaf47a0907" +source = "git+https://github.com/dfinity/candid.git?branch=fix-beta#c6ec76e3a1c1381ca95a2fe7731739205af990e3" dependencies = [ "anyhow", "arbitrary", @@ -283,7 +283,7 @@ dependencies = [ [[package]] name = "candid_derive" version = "0.4.4" -source = "git+https://github.com/dfinity/candid.git?branch=fix-beta#1a8f146d8ba017c8d9849215bf803ecaf47a0907" +source = "git+https://github.com/dfinity/candid.git?branch=fix-beta#c6ec76e3a1c1381ca95a2fe7731739205af990e3" dependencies = [ "lazy_static", "proc-macro2", diff --git a/src/grammar.lalrpop b/src/grammar.lalrpop index d09ba1c..6888295 100644 --- a/src/grammar.lalrpop +++ b/src/grammar.lalrpop @@ -1,4 +1,4 @@ -use candid::parser::value::{IDLField, IDLValue, IDLArgs}; +use candid::parser::value::{IDLField, IDLValue, IDLArgs, VariantValue}; use candid::parser::types::{IDLType, TypeField, PrimType, FuncType, FuncMode, Binding}; use candid::parser::typing::{TypeEnv, check_unique}; use super::token::{Token, error2, LexicalError, Span, Spanned}; @@ -130,7 +130,7 @@ Arg: IDLValue = { check_unique(fs.iter().map(|f| &f.id)).map_err(|e| error2(e, span))?; Ok(IDLValue::Record(fs)) }, - "variant" "{" "}" => IDLValue::Variant(Box::new(<>), 0), + "variant" "{" "}" => IDLValue::Variant(VariantValue(Box::new(<>), 0)), "principal" > =>? Ok(IDLValue::Principal(Principal::from_text(&<>.0).map_err(|e| error2(e, <>.1))?)), "service" > =>? Ok(IDLValue::Service(Principal::from_text(&<>.0).map_err(|e| error2(e, <>.1))?)), "func" > "." =>? { From 2faac4b2e111e211d2f62ba355058119dc152714 Mon Sep 17 00:00:00 2001 From: chenyan-dfinity Date: Sat, 24 Apr 2021 20:05:23 -0700 Subject: [PATCH 2/3] subequal --- src/command.rs | 36 ++++++++++++++++++++++-------------- src/grammar.lalrpop | 7 +++++-- src/token.rs | 2 ++ 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/command.rs b/src/command.rs index 95eccaa..3ecb1fe 100644 --- a/src/command.rs +++ b/src/command.rs @@ -37,12 +37,18 @@ pub enum Command { Config(String), Show(Value), Let(String, Value), - Assert(Value, Value), + Assert(BinOp, Value, Value), Export(String), Import(String, Principal), Load(String), Identity(String), } +#[derive(Debug, Clone)] +pub enum BinOp { + Equal, + SubEqual, + NotEqual, +} impl Command { pub fn run(&self, helper: &mut MyHelper) -> anyhow::Result<()> { @@ -90,22 +96,24 @@ impl Command { let v = val.get(&helper.env)?.clone(); helper.env.0.insert(id.to_string(), v); } - Command::Assert(left, right) => { + Command::Assert(op, left, right) => { let left = left.get(&helper.env)?; let right = right.get(&helper.env)?; - if left != right { - let l_ty = left.value_ty(); - let r_ty = right.value_ty(); - //println!("{} {}", l_ty, r_ty); - let env = TypeEnv::new(); - //left.annotate_type(false, &env, &r_ty)?; - if let Ok(ref left) = left.annotate_type(false, &env, &r_ty) { - assert_eq!(left, right); - } else if let Ok(ref right) = right.annotate_type(false, &env, &l_ty) { - assert_eq!(left, right); - } else { - assert_eq!(left, right); + 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(ref left) = left.annotate_type(false, &env, &r_ty) { + assert_eq!(left, right); + } else if let Ok(ref right) = right.annotate_type(false, &env, &l_ty) { + assert_eq!(left, right); + } else { + assert_eq!(left, right); + } } + BinOp::NotEqual => assert!(left != right), } } Command::Config(conf) => helper.config = Configs::from_dhall(&conf)?, diff --git a/src/grammar.lalrpop b/src/grammar.lalrpop index 6888295..27a212f 100644 --- a/src/grammar.lalrpop +++ b/src/grammar.lalrpop @@ -3,7 +3,7 @@ use candid::parser::types::{IDLType, TypeField, PrimType, FuncType, FuncMode, Bi use candid::parser::typing::{TypeEnv, check_unique}; use super::token::{Token, error2, LexicalError, Span, Spanned}; use candid::{Principal, types::Label}; -use super::command::{Command, Commands, Value}; +use super::command::{Command, Commands, Value, BinOp}; grammar; @@ -41,6 +41,7 @@ extern { "sign" => Token::Sign(), "=" => Token::Equals, "==" => Token::TestEqual, + "~=" => Token::SubEqual, "!=" => Token::NotEqual, "(" => Token::LParen, ")" => Token::RParen, @@ -72,7 +73,9 @@ pub Command: Command = { }, "config" => Command::Config(<>), "show" => Command::Show(<>), - "assert" "==" => Command::Assert(left, right), + "assert" "==" => Command::Assert(BinOp::Equal, left, right), + "assert" "~=" => Command::Assert(BinOp::SubEqual, left, right), + "assert" "!=" => Command::Assert(BinOp::NotEqual, left, right), "let" "=" => Command::Let(id, val), "export" => Command::Export(<>), "load" => Command::Load(<>), diff --git a/src/token.rs b/src/token.rs index 4739b93..93c3141 100644 --- a/src/token.rs +++ b/src/token.rs @@ -72,6 +72,8 @@ pub enum Token { Load, #[token("==")] TestEqual, + #[token("~=")] + SubEqual, #[token("!=")] NotEqual, #[token("principal")] From e8aa1a7b3b62f98f4ce1e95b535e1e22208e8335 Mon Sep 17 00:00:00 2001 From: chenyan-dfinity Date: Sat, 24 Apr 2021 20:17:06 -0700 Subject: [PATCH 3/3] readme --- README.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ee6af88..fb576ba 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Canister REPL ``` -ic-repl --replica [local|ic|url] +ic-repl --replica [local|ic|url] --config [script file] ``` ## Commands @@ -15,9 +15,24 @@ ic-repl --replica [local|ic|url] | call . ( ,* ) | let = | show - | assert = + | assert <==|~=|!=> | identity := | _ := | (. )* ``` + +## Example + +test.sh +``` +#!/usr/bin/ic-repl -r ic + +import greet = "rrkah-fqaaa-aaaaa-aaaaq-cai"; +call greet.greet("test"); +let result = _; +assert _ == "Hello, test!"; +identity alice; +call "rrkah-fqaaa-aaaaa-aaaaq-cai".greet("test"); +assert _ == result; +```