Skip to content

Commit

Permalink
Merge pull request #2 from chenyan2002/dev
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
chenyan2002 authored Apr 25, 2021
2 parents 0735b57 + e8aa1a7 commit 8621031
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Canister REPL

```
ic-repl --replica [local|ic|url]
ic-repl --replica [local|ic|url] --config <dhall config> [script file]
```

## Commands
Expand All @@ -15,9 +15,24 @@ ic-repl --replica [local|ic|url]
| call <name> . <name> ( <val>,* )
| let <id> = <val>
| show <val>
| assert <val> = <val>
| assert <val> <==|~=|!=> <val>
| identity <id>
<var> := <id> | _
<val> := <candid val> | <var> (. <id>)*
```

## 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;
```
36 changes: 22 additions & 14 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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)?,
Expand Down
11 changes: 7 additions & 4 deletions src/grammar.lalrpop
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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};
use candid::{Principal, types::Label};
use super::command::{Command, Commands, Value};
use super::command::{Command, Commands, Value, BinOp};

grammar;

Expand Down Expand Up @@ -41,6 +41,7 @@ extern {
"sign" => Token::Sign(<char>),
"=" => Token::Equals,
"==" => Token::TestEqual,
"~=" => Token::SubEqual,
"!=" => Token::NotEqual,
"(" => Token::LParen,
")" => Token::RParen,
Expand Down Expand Up @@ -72,7 +73,9 @@ pub Command: Command = {
},
"config" <Text> => Command::Config(<>),
"show" <Value> => Command::Show(<>),
"assert" <left:Value> "==" <right:Value> => Command::Assert(left, right),
"assert" <left:Value> "==" <right:Value> => Command::Assert(BinOp::Equal, left, right),
"assert" <left:Value> "~=" <right:Value> => Command::Assert(BinOp::SubEqual, left, right),
"assert" <left:Value> "!=" <right:Value> => Command::Assert(BinOp::NotEqual, left, right),
"let" <id:"id"> "=" <val:Value> => Command::Let(id, val),
"export" <Text> => Command::Export(<>),
"load" <Text> => Command::Load(<>),
Expand Down Expand Up @@ -130,7 +133,7 @@ Arg: IDLValue = {
check_unique(fs.iter().map(|f| &f.id)).map_err(|e| error2(e, span))?;
Ok(IDLValue::Record(fs))
},
"variant" "{" <VariantField> "}" => IDLValue::Variant(Box::new(<>), 0),
"variant" "{" <VariantField> "}" => IDLValue::Variant(VariantValue(Box::new(<>), 0)),
"principal" <Sp<Text>> =>? Ok(IDLValue::Principal(Principal::from_text(&<>.0).map_err(|e| error2(e, <>.1))?)),
"service" <Sp<Text>> =>? Ok(IDLValue::Service(Principal::from_text(&<>.0).map_err(|e| error2(e, <>.1))?)),
"func" <id:Sp<Text>> "." <meth:Name> =>? {
Expand Down
2 changes: 2 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ pub enum Token {
Load,
#[token("==")]
TestEqual,
#[token("~=")]
SubEqual,
#[token("!=")]
NotEqual,
#[token("principal")]
Expand Down

0 comments on commit 8621031

Please sign in to comment.