From 033f35bbf8295528a70189bb988edd2e57da39c0 Mon Sep 17 00:00:00 2001 From: linsy king Date: Wed, 5 Apr 2023 20:45:40 +0800 Subject: [PATCH] remove iexpr code --- Readme.md | 1 - docs/i-expr.md | 32 ---------- examples/iexpr.meow | 132 +++++++++++++++++++++++++++++++++++++++ examples/syn.cat | 20 ++---- examples/syn.meow | 148 +++----------------------------------------- src/ast.rs | 1 - src/expr.lalrpop | 1 - src/parse.lalrpop | 1 - src/prog.rs | 4 -- 9 files changed, 146 insertions(+), 194 deletions(-) delete mode 100644 docs/i-expr.md create mode 100644 examples/iexpr.meow diff --git a/Readme.md b/Readme.md index 1a24d75..b0bb509 100755 --- a/Readme.md +++ b/Readme.md @@ -42,7 +42,6 @@ hello world - [Syntax](./docs/Syntax.md) - [Encoding](./docs/Encoding.md) - [Double Substitution Lemma](./docs/DSL.md) -- [i Expressions](./docs/i-expr.md) ## Examples diff --git a/docs/i-expr.md b/docs/i-expr.md deleted file mode 100644 index 5119148..0000000 --- a/docs/i-expr.md +++ /dev/null @@ -1,32 +0,0 @@ -# i Expressions - -i expressions allow you to evaluate catlet expressions inside catlet expressions. This gives us the power to do recursion. - -The fibonacci function can be defined as: - -```meow -fib(x) { - if( - eq(x, zero()), - zero(), - !(if( - leq(x, num("2")), - spack(num("1")), - `add fib pred x fib pred pred x` - )) - ) -} -``` - -The `!` operator is used to evaluate the expression inside. - -You can try it in repl: - -``` -> ! `"23"` -23 -> ! `cat "12" "22"` -1222 -``` - -Note that `cat "12" "22"` itself is a string literal rather than many tokens. diff --git a/examples/iexpr.meow b/examples/iexpr.meow new file mode 100644 index 0000000..62963f6 --- /dev/null +++ b/examples/iexpr.meow @@ -0,0 +1,132 @@ + +digit(x) { + "1" = "S0"; + "2" = "SS0"; + "3" = "SSS0"; + "4" = "SSSS0"; + "5" = "SSSSS0"; + "6" = "SSSSSS0"; + "7" = "SSSSSSS0"; + "8" = "SSSSSSSS0"; + "9" = "SSSSSSSSS0"; + x +} + +exp(x,y) { + var sm = { + " 0" = ""; + "S" = `"`+x+`" `; + y + }; + var pre = { + "S" = "mul "; + "S0" = ""; + y + }; + !(pre + sm) +} + +nrep(x,p) { + "0" = p; + "1" = p; + "2" = p; + "3" = p; + "4" = p; + "5" = p; + "6" = p; + "7" = p; + "8" = p; + "9" = p; + x +} + +len(x) { + var s = nrep(x, "S"); + s + "0" +} + +ten() { + succ(digit("9")) +} + +revdigit(x) { + "S0" = "1"; + "SS0" = "2"; + "SSS0" = "3"; + "SSSS0" = "4"; + "SSSSS0" = "5"; + "SSSSSS0" = "6"; + "SSSSSSS0" = "7"; + "SSSSSSSS0" = "8"; + "SSSSSSSSS0" = "9"; + x +} + +head(x) { + var s = { + "$0" = "S"; + "$1" = "SS"; + "$2" = "SSS"; + "$3" = "SSSS"; + "$4" = "SSSSS"; + "$5" = "SSSSSS"; + "$6" = "SSSSSSS"; + "$7" = "SSSSSSSS"; + "$8" = "SSSSSSSSS"; + "$9" = "SSSSSSSSSS"; + "$" + x + }; + var rep = nrep(s, ""); + pred(rep + "0") +} + +num(x) { + var nl = pred(len(x)); + var hd = head(x); + var hdnum = revdigit(hd); + var remain = { + "$" + hdnum = ""; + "$" + x + }; + !(if( + eq(len(x), "S0"), + spack(digit(x)), + `add mul "` + hd + `" exp ten "` + nl + `" ` + `num "` + remain + `"` + )) +} + +spack(x) { + `"` + x + `"` +} + +leq(x,y) { + in(x,y) +} + +show(x) { + var la = { + "SSSSSSSSSS" = ""; + x + }; + var rem = { + "SSSSSSSSSS" = "S"; + sub(x,la) + }; + !(if( + leq(len(x), digit("9")), + spack(revdigit(x)), + `cat show "` + rem + `" revdigit "` + la + `"` + )) +} + +fib(x) { + if( + eq(x, zero()), + zero(), + !(if( + leq(x, num("2")), + spack(num("1")), + `add fib pred x fib pred pred x` + )) + ) +} diff --git a/examples/syn.cat b/examples/syn.cat index dcbf99a..9cab8a4 100644 --- a/examples/syn.cat +++ b/examples/syn.cat @@ -7,22 +7,14 @@ add x y = let "0" y x sub x y = let y "0" x mul x y = let "S" let "0" "" y x dr x y z = let y x let x y z -encode s = cat cat "_^" let "$" "\$" let "^" "\^" let "\" "\\" s "_$" -decode s = let "\\" "\" let "\$" "$" let "\^" "^" let "_^" "" let "_$" "" s +encode s = cat cat "#$" let "$" "\$" let "#" "\#" let "\" "\\" s "$#" +decode s = let "\\" "\" let "\$" "$" let "\#" "#" let "_#" "" let "_$" "" s +encat x y = cat let "#$" "" x let "$#" "" y +enraw s = let "#$" "" let "$#" "" encode s +enrep s x y = let enraw x y s +rep2 s x xr y yr = decode let "x$" enraw xr let "y$" enraw yr enrep enrep encode s x "x$" y "y$" eq x y = let encode x "F" let encode y "T" encode x if c x y = decode let "T$" encode x let "F$" encode y cat c "$" in x y = not eq y let x "" y not x = if x false true ife x = eq x "" -digit x = let "1" "S0" let "2" "SS0" let "3" "SSS0" let "4" "SSSS0" let "5" "SSSSS0" let "6" "SSSSSS0" let "7" "SSSSSSS0" let "8" "SSSSSSSS0" let "9" "SSSSSSSSS0" x -exp x y = ! cat let "S" "mul " let "S0" "" y let " 0" "" let "S" cat cat `"` x `" ` y -nrep x p = let "0" p let "1" p let "2" p let "3" p let "4" p let "5" p let "6" p let "7" p let "8" p let "9" p x -len x = cat nrep x "S" "0" -ten = succ digit "9" -revdigit x = let "S0" "1" let "SS0" "2" let "SSS0" "3" let "SSSS0" "4" let "SSSSS0" "5" let "SSSSSS0" "6" let "SSSSSSS0" "7" let "SSSSSSSS0" "8" let "SSSSSSSSS0" "9" x -head x = pred cat nrep let "$0" "S" let "$1" "SS" let "$2" "SSS" let "$3" "SSSS" let "$4" "SSSSS" let "$5" "SSSSSS" let "$6" "SSSSSSS" let "$7" "SSSSSSSS" let "$8" "SSSSSSSSS" let "$9" "SSSSSSSSSS" cat "$" x "" "0" -num x = ! if eq len x "S0" spack digit x cat cat cat cat cat cat cat `add mul "` head x `" exp ten "` pred len x `" ` `num "` let cat "$" revdigit head x "" cat "$" x `"` -spack x = cat cat `"` x `"` -leq x y = in x y -show x = ! if leq len x digit "9" spack revdigit x cat cat cat cat `cat show "` let "SSSSSSSSSS" "S" sub x let "SSSSSSSSSS" "" x `" revdigit "` let "SSSSSSSSSS" "" x `"` -fib x = if eq x zero zero ! if leq x num "2" spack num "1" "add fib pred x fib pred pred x" diff --git a/examples/syn.meow b/examples/syn.meow index 561660f..b1749de 100644 --- a/examples/syn.meow +++ b/examples/syn.meow @@ -35,32 +35,32 @@ dr(x,y,z) { encode(s) { var rep = { "$" = "\$"; - "^" = "\^"; + "#" = "\#"; "\" = "\\"; s }; - "_^"+ rep +"_$" + "#$"+ rep +"$#" } decode(s) { var rep = { - "_^" = ""; - "_$" = ""; + "$#" = ""; + "#$" = ""; s }; "\\" = "\"; + "\#" = "#"; "\$" = "$"; - "\^" = "^"; rep } encat(x,y) { - {"_$"=""; x} + {"_^"=""; y} + {"#$"=""; x} + {"$#"=""; y} } enraw(s) { - "_^" = ""; - "_$" = ""; + "#$" = ""; + "$#" = ""; encode(s) } @@ -106,135 +106,3 @@ not(x) { ife(x) { eq(x,"") } - -digit(x) { - "1" = "S0"; - "2" = "SS0"; - "3" = "SSS0"; - "4" = "SSSS0"; - "5" = "SSSSS0"; - "6" = "SSSSSS0"; - "7" = "SSSSSSS0"; - "8" = "SSSSSSSS0"; - "9" = "SSSSSSSSS0"; - x -} - -exp(x,y) { - var sm = { - " 0" = ""; - "S" = `"`+x+`" `; - y - }; - var pre = { - "S" = "mul "; - "S0" = ""; - y - }; - !(pre + sm) -} - -nrep(x,p) { - "0" = p; - "1" = p; - "2" = p; - "3" = p; - "4" = p; - "5" = p; - "6" = p; - "7" = p; - "8" = p; - "9" = p; - x -} - -len(x) { - var s = nrep(x, "S"); - s + "0" -} - -ten() { - succ(digit("9")) -} - -revdigit(x) { - "S0" = "1"; - "SS0" = "2"; - "SSS0" = "3"; - "SSSS0" = "4"; - "SSSSS0" = "5"; - "SSSSSS0" = "6"; - "SSSSSSS0" = "7"; - "SSSSSSSS0" = "8"; - "SSSSSSSSS0" = "9"; - x -} - -head(x) { - var s = { - "$0" = "S"; - "$1" = "SS"; - "$2" = "SSS"; - "$3" = "SSSS"; - "$4" = "SSSSS"; - "$5" = "SSSSSS"; - "$6" = "SSSSSSS"; - "$7" = "SSSSSSSS"; - "$8" = "SSSSSSSSS"; - "$9" = "SSSSSSSSSS"; - "$" + x - }; - var rep = nrep(s, ""); - pred(rep + "0") -} - -num(x) { - var nl = pred(len(x)); - var hd = head(x); - var hdnum = revdigit(hd); - var remain = { - "$" + hdnum = ""; - "$" + x - }; - !(if( - eq(len(x), "S0"), - spack(digit(x)), - `add mul "` + hd + `" exp ten "` + nl + `" ` + `num "` + remain + `"` - )) -} - -spack(x) { - `"` + x + `"` -} - -leq(x,y) { - in(x,y) -} - -show(x) { - var la = { - "SSSSSSSSSS" = ""; - x - }; - var rem = { - "SSSSSSSSSS" = "S"; - sub(x,la) - }; - !(if( - leq(len(x), digit("9")), - spack(revdigit(x)), - `cat show "` + rem + `" revdigit "` + la + `"` - )) -} - -fib(x) { - if( - eq(x, zero()), - zero(), - !(if( - leq(x, num("2")), - spack(num("1")), - `add fib pred x fib pred pred x` - )) - ) -} diff --git a/src/ast.rs b/src/ast.rs index 32a78b4..02a8b6a 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -18,7 +18,6 @@ pub enum Expr { MacAp(MacAp), Var(String), Block(Box), - IExpr(Box), } #[derive(Debug, Clone)] diff --git a/src/expr.lalrpop b/src/expr.lalrpop index 4545f00..1463c94 100644 --- a/src/expr.lalrpop +++ b/src/expr.lalrpop @@ -13,7 +13,6 @@ Tokz: Tok = { VarName: String = { r"[a-z]?[a-z0-9_]*" => String::from_str(<>).unwrap(), - "!" => String::from_str("!").unwrap() } Literal: String = { diff --git a/src/parse.lalrpop b/src/parse.lalrpop index 6a9b898..ca54a51 100644 --- a/src/parse.lalrpop +++ b/src/parse.lalrpop @@ -33,7 +33,6 @@ Term: Box = { VarName => Box::new(Expr::Var(<>)), "{" "}" => Box::new(Expr::Block(b)), "(" ")" => e, - "!(" ")" => Box::new(Expr::IExpr(e)), } BlockE: Box = { diff --git a/src/prog.rs b/src/prog.rs index a7a99cd..0e1dc56 100644 --- a/src/prog.rs +++ b/src/prog.rs @@ -94,10 +94,6 @@ fn trans_expr(expr: &Box, context: &Box) -> Vec { } } Expr::Block(x) => tok.append(&mut trans_block(x, &mut context.clone())), - Expr::IExpr(x) => { - tok.push(Tok::Var("!".to_string())); - tok.append(&mut trans_expr(x, context)); - } } tok.to_vec() }