Skip to content

Commit

Permalink
added example from reddit
Browse files Browse the repository at this point in the history
  • Loading branch information
azizghuloum committed Nov 29, 2024
1 parent 617bb41 commit 5647673
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
27 changes: 27 additions & 0 deletions examples/expr-dot-where.ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## `expr-dot-where.ts`

### Status: `DONE`

### Input Program

```typescript
// from https://www.reddit.com/r/ProgrammingLanguages/comments/1gyo9hm/dear_language_designers_please_copy_where_from/
// from https://kiru.io/blog/posts/2024/dear-language-designers-please-copy-where-from-haskell/

using_syntax_rules(
[where, expr.where(a = b, rest), ((a) => expr.where(rest))(b)],
[where, expr.where(a = b), ((a) => expr)(b)],
[where, expr.where(), expr],
).rewrite(

(x + y).where(x = 1, y = x + 2)

);
```

### Output Program

```typescript
((x_6) => ((y_10) => x_6 + y_10)(x_6 + 2))(1);
```

10 changes: 9 additions & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import TS, { SyntaxKind } from "typescript";
import { assert } from "./assert";

const pass_through: { [k in SyntaxKind]?: list_tag } = {
[SyntaxKind.CallExpression]: "call_expression",
[SyntaxKind.ParenthesizedExpression]: "parenthesized_expression",
[SyntaxKind.BinaryExpression]: "binary_expression",
[SyntaxKind.PrefixUnaryExpression]: "unary_expression",
Expand Down Expand Up @@ -102,6 +101,15 @@ function absurdly(node: TS.Node, src: TS.SourceFile): AST {
return { type: "list", tag: "ERROR", content };
}
}
case SyntaxKind.CallExpression: {
assert(ls.length === 4, ls);
assert(ls[2].tag === "syntax_list");
return {
type: "list",
tag: "call_expression",
content: [ls[0], [ls[1], llappend(ls[2].content, [ls[3], null])]],
};
}
case SyntaxKind.ArrowFunction: {
assert(ls.length === 5, ls);
const [lt, fmls, rt, ar, body] = ls;
Expand Down
12 changes: 12 additions & 0 deletions tests/expr-dot-where.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// from https://www.reddit.com/r/ProgrammingLanguages/comments/1gyo9hm/dear_language_designers_please_copy_where_from/
// from https://kiru.io/blog/posts/2024/dear-language-designers-please-copy-where-from-haskell/

using_syntax_rules(
[where, expr.where(a = b, rest), ((a) => expr.where(rest))(b)],
[where, expr.where(a = b), ((a) => expr)(b)],
[where, expr.where(), expr],
).rewrite(

(x + y).where(x = 1, y = x + 2)

);

0 comments on commit 5647673

Please sign in to comment.