Skip to content

Commit

Permalink
improve janet cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalMarsalek committed Mar 11, 2024
1 parent d9d47d5 commit 6c0d222
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/languages/janet/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export class JanetEmitter extends VisitorEmitter {
const prop = spine.pathFragment?.prop;

switch (n.kind) {
case "Cast":
if (n.targetType === "array") {
return ["@[;", "$GLUE$", $.expr, "]"];
}
throw new EmitError(n, "unsuported cast target type");
case "Block": {
return prop === "consequent" || prop === "alternate"
? list("do", $.children.join())
Expand Down
4 changes: 3 additions & 1 deletion src/languages/janet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
isInt,
intToDecOpOrText,
isForEachChar,
cast,
} from "../../IR";
import {
golfLastPrint,
Expand Down Expand Up @@ -220,8 +221,9 @@ const janetLanguage: Language = {
type.kind === "List" &&
["boolean", "integer", "text"].includes(type.member.kind)
) {
return func("array/slice", node, int(0), int(-1));
return cast(node, "array");
}
return func("thaw", node);
}),
),
simplegolf(
Expand Down
8 changes: 7 additions & 1 deletion src/programs/control-flow.test.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ $b <- $a;
_Janet_
```janet nogolf
(var a @[1 2 3])(var b(array/slice a 0 -1))
(var a @[1 2 3])(var b @[;a])
```
_Javascript_
Expand Down Expand Up @@ -403,6 +403,12 @@ $a <- (list (list 1 2 3) (list 4 5 6) (list 7 8 9));
$b <- $a;
```
_Janet_
```janet nogolf
(var a @[@[1 2 3]@[4 5 6]@[7 8 9]])(var b(thaw a))
```
_Javascript_
```js nogolf
Expand Down

0 comments on commit 6c0d222

Please sign in to comment.