diff --git a/Readme.md b/Readme.md index 03d9ab2..a409f00 100755 --- a/Readme.md +++ b/Readme.md @@ -18,6 +18,44 @@ There are **no** functions in Meow. Instead, we have *macros*. A macro is a piec The only available data type is **String**. (However, you can encode other data types inside String) +## Code Style + +Meow: + +```meow +encode(s) { + var rep = { + "$" = "\$"; + "#" = "\#"; + "\" = "\\"; + s + }; + "#$"+ rep +"$#" +} + +fib(x) { + if( + eq0(x), + 0(), + if( + leq(x, 2()), + 1(), + add( + fib(pred(x)), + fib(pred(pred(x))) + ) + ) + ) +} +``` + +Catlet (Compiled result): + +```catlet +encode s = cat cat "#$" let "$" "\$" let "#" "\#" let "\" "\\" s "$#" +fib x = if eq0 x 0 if leq x 2 1 add fib pred x fib pred pred x +``` + ## Simple Start ```bash diff --git a/docs/Recursion.md b/docs/Recursion.md new file mode 100644 index 0000000..73cf14c --- /dev/null +++ b/docs/Recursion.md @@ -0,0 +1,93 @@ +# Recursion + +It's possible to do recursion **as long as** we use the *by name* catlet interpreter. + +## Lazy (by name) evaluation + +For example, + +```meow +take(x) { + "abc" +} + +inf_loop() { + inf_loop() +} + +test() { + take(inf_loop()) +} +``` + +In the above code, `inf_loop` will be evaluated to itself. However, `take` will only evaluate `inf_loop` when it is used. So `test` will not cause infinite loop. + +Moreover, the strict evaluation will not happen if the replacement is impossible. For example, + +```meow +test() { + "abc" = inf_loop(); + "xyz" +} +``` + +In the above code, `inf_loop` will not be evaluated because the replacement is impossible. + +However, if you write the code like this: + +```meow +test() { + inf_loop() = "abc"; + "xyz" +} +``` + +This will cause infinite loop. + +## Factorial + +Now we can write factorial. + +```meow +factorial(x) { + if( + eq(x, 1()), + 1(), + mul( + x, + factorial(pred(x)) + ) + ) +} +``` + +Compile result: + +```catlet +factorial x = if eq x 1 1 mul x factorial pred x +``` + +## Fibonacci + +```meow +fib(x) { + if( + eq0(x), + 0(), + if( + leq(x, 2()), + 1(), + add( + fib(pred(x)), + fib(pred(pred(x))) + ) + ) + ) +} +``` + +Compile result: + +```catlet +fib x = if eq0 x 0 if leq x 2 1 add fib pred x fib pred pred x +``` diff --git a/docs/Syntax.md b/docs/Syntax.md index b33e08f..fdf7b74 100644 --- a/docs/Syntax.md +++ b/docs/Syntax.md @@ -44,7 +44,7 @@ Different to many languages, `X=Y` requires that both `X` and `Y` are **expressi The replacement **only** takes place in the `in xxx`. Hence, in `let X in (let Y in Z)`, the replacement of `X` only takes place in `Z` rather than `Y` (but there is a way to let it happen in `Y`, see later explanation). -`MacroApplication` is a macro name followed by a list of arguments, like `succ(a,b,c)`. The language is *by value*, so the arguments are evaluated before the macro is applied. +`MacroApplication` is a macro name followed by a list of arguments, like `succ(a,b,c)`. The language is *by name*, so the arguments will be evaluated after the macro is applied. ## Evaluation Mode