From 79ffae6c11811f206c5b9aa3b127b0a5eadc7189 Mon Sep 17 00:00:00 2001 From: linsy king Date: Tue, 4 Apr 2023 10:13:27 +0800 Subject: [PATCH] fix --- examples/syn.cat | 36 ++++++++++++++++++++++++++++++++++++ examples/syn.meow | 7 +++++++ src/prog.rs | 42 +++++++++++++++++++++++++++--------------- 3 files changed, 70 insertions(+), 15 deletions(-) create mode 100644 examples/syn.cat diff --git a/examples/syn.cat b/examples/syn.cat new file mode 100644 index 0000000..10e9ed9 --- /dev/null +++ b/examples/syn.cat @@ -0,0 +1,36 @@ +true() {"T"} + +false() {"F"} + +zero() {"0"} + +succ(x) {cat "S" x} + +pred(x) { + let "S0" "0" x +} + +dr(x,y,z) { + let y x let x y z +} + +encode(s) { + cat cat "_^" let "$" "\$" let "^" "\^" let "\" "\\" s "_$" +} + +eq(x,y) { + let encode x "F" encode y "T" encode "x" +} + +if(c,x,y) { + decode let "T$" encode x let "F$" encode y cat x "$" +} + +in(x,y) { + eq y let x "" y +} + +test2() { + let "bb" "g" cat "bc" "bc" +} + diff --git a/examples/syn.meow b/examples/syn.meow index 87fa290..6af0f8e 100644 --- a/examples/syn.meow +++ b/examples/syn.meow @@ -72,3 +72,10 @@ decode_add(s,x) { ife(x) { eq(x,"") } + +test2() { + "bb" = "g"; + var x = "bc"; + var y = { x }; + y+x +} diff --git a/src/prog.rs b/src/prog.rs index f1fd1df..aa9dd8a 100644 --- a/src/prog.rs +++ b/src/prog.rs @@ -67,6 +67,28 @@ fn apply_rule(x: &String, rules: &Vec) -> String { result } +fn clear_context(context: &Context) -> Context { + // clear the context + let mut newcontext = Context::new(); + newcontext.symbols = context + .symbols + .iter() + .filter(|(_, v)| match v { + Symbol::Variable(_) => false, + Symbol::Macro(_) => true, + }) + .map(|(k, v)| (k.clone(), v.clone())) + .collect(); + newcontext +} + +fn clear_rules(context: &Context) -> Context { + // clear the context + let mut newcontext = Context::new(); + newcontext.symbols = context.symbols.clone(); + newcontext +} + pub fn eval_raw(expr: &Box, context: &Box) -> String { // Evaluate an expression in a given context. // Will not apply rules @@ -83,8 +105,9 @@ pub fn eval_raw(expr: &Box, context: &Box) -> String { } } Expr::MacAp(macap) => { - let mut newcontext: Context = *context.clone(); + let mut newcontext = *context.clone(); // macro application + newcontext = clear_context(&newcontext); let mac = context .symbols .get(&macap.name) @@ -107,26 +130,15 @@ pub fn eval_raw(expr: &Box, context: &Box) -> String { } eval_block(&mac.block, &Box::new(newcontext)) } - Expr::Block(x) => eval_block(x, context), + Expr::Block(x) => eval_block(x, &Box::new(clear_rules(context))), } } pub fn eval(expr: &Box, context: &Box) -> String { // Evaluate an expression in a given context. // Not allow to change context - match &**expr { - Expr::Literal(x) => apply_rule(x, &context.rules), - Expr::Cat(x, y) => { - let cat = eval_raw(x, context) + eval_raw(y, context).as_str(); - apply_rule(&cat, &context.rules) - } - _ => - // other cases - { - let res = eval_raw(expr, context); - apply_rule(&res, &context.rules) - } - } + let res = eval_raw(expr, context); + apply_rule(&res, &context.rules) } pub fn eval_block(block: &Box, context: &Box) -> String {