Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
linsyking committed Apr 5, 2023
1 parent aeaf73b commit db854fb
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
38 changes: 38 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
93 changes: 93 additions & 0 deletions docs/Recursion.md
Original file line number Diff line number Diff line change
@@ -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
```
2 changes: 1 addition & 1 deletion docs/Syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit db854fb

Please sign in to comment.