Skip to content

Commit

Permalink
Merge branch 'fix/eval_in_block' into recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
linsyking committed Apr 4, 2023
2 parents 4e1826a + 79ffae6 commit df1a170
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 15 deletions.
36 changes: 36 additions & 0 deletions examples/syn.cat
Original file line number Diff line number Diff line change
@@ -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"
}

7 changes: 7 additions & 0 deletions examples/syn.meow
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,10 @@ decode_add(s,x) {
ife(x) {
eq(x,"")
}

test2() {
"bb" = "g";
var x = "bc";
var y = { x };
y+x
}
42 changes: 27 additions & 15 deletions src/prog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ fn apply_rule(x: &String, rules: &Vec<Rule>) -> String {
}

pub fn eval_macap(macap: &MacAp, context: &Box<Context>) -> String {
let mut newcontext: Context = *context.clone();
let mut newcontext = *context.clone();
// macro application
newcontext = clear_context(&newcontext);
let mac = context
.symbols
.get(&macap.name)
Expand All @@ -94,6 +95,28 @@ pub fn eval_macap(macap: &MacAp, context: &Box<Context>) -> String {
eval_block(&mac.block, &Box::new(newcontext))
}

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<Expr>, context: &Box<Context>) -> String {
// Evaluate an expression in a given context.
// Will not apply rules
Expand All @@ -110,7 +133,7 @@ pub fn eval_raw(expr: &Box<Expr>, context: &Box<Context>) -> String {
}
}
Expr::MacAp(macap) => eval_macap(macap, context),
Expr::Block(x) => eval_block(x, context),
Expr::Block(x) => eval_block(x, &Box::new(clear_rules(context))),
Expr::IExpr(x) => {
let res = eval_raw(x, context);
let pres = &mut expr::ExpressionParser::new().parse(&res).unwrap();
Expand All @@ -122,19 +145,8 @@ pub fn eval_raw(expr: &Box<Expr>, context: &Box<Context>) -> String {
pub fn eval(expr: &Box<Expr>, context: &Box<Context>) -> 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<Block>, context: &Box<Context>) -> String {
Expand Down

0 comments on commit df1a170

Please sign in to comment.