Skip to content

Commit

Permalink
compiling more infix exprs
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Aug 18, 2024
1 parent 3dd2d86 commit 64a5615
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/compiler/compiler-babel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,39 @@ describe("intrinsics", () => {
expect(out).toMatchInlineSnapshot(`"const Main$x = 1 * 2;"`);
});

test("strings concat", () => {
const out = compileSrc(`let x = "a" ++ "b"`);
expect(out).toMatchInlineSnapshot(`
"const Main$x = "a" + "b";"
`);
});

test("boolean negation", () => {
const out = compileSrc(`let x = fn b { !b }`);

expect(out).toMatchInlineSnapshot(`"const Main$x = b => !b;"`);
});

test("infix &&", () => {
const out = compileSrc(`let x = fn a, b { a && b }`);

expect(out).toMatchInlineSnapshot(`"const Main$x = (a, b) => a && b;"`);
});

test("infix ||", () => {
const out = compileSrc(`let x = fn a, b { a || b }`);

expect(out).toMatchInlineSnapshot(`"const Main$x = (a, b) => a || b;"`);
});

test("boolean negation and && prec", () => {
const out = compileSrc(`let x = fn t, f { !(t && f)}`);

expect(out).toMatchInlineSnapshot(`
"const Main$x = (t, f) => !(t && f);"
`);
});

test("compile == of ints", () => {
const out = compileSrc(`pub let x = 1 == 2`);
expect(out).toMatchInlineSnapshot(`"const Main$x = 1 === 2;"`);
Expand Down
48 changes: 48 additions & 0 deletions src/compiler/compiler-babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ class Compiler {
right: this.compileExpr(src.args[1]!),
};
}

const infixLogicalName = toJsInfixLogical(src.caller.name);
if (infixLogicalName !== undefined) {
return {
type: "LogicalExpression",
operator: infixLogicalName,
left: this.compileExpr(src.args[0]!),
right: this.compileExpr(src.args[1]!),
};
}

const prefixName = toJsPrefix(src.caller.name);
if (prefixName !== undefined) {
return {
type: "UnaryExpression",
operator: prefixName,
argument: this.compileExpr(src.args[0]!),
prefix: true,
};
}
}

return {
Expand Down Expand Up @@ -334,6 +354,18 @@ function compileConst(ast: ConstLiteral): t.Expression {
}
}

function toJsPrefix(
kestrelCaller: string,
): t.UnaryExpression["operator"] | undefined {
switch (kestrelCaller) {
case "!":
return kestrelCaller;

default:
return undefined;
}
}

function toJsInfix(
kestrelCaller: string,
): BinaryExpression["operator"] | undefined {
Expand All @@ -342,6 +374,9 @@ function toJsInfix(
case "*":
return kestrelCaller;

case "++":
return "+";

case "==":
return "===";

Expand All @@ -350,6 +385,19 @@ function toJsInfix(
}
}

function toJsInfixLogical(
kestrelCaller: string,
): t.LogicalExpression["operator"] | undefined {
switch (kestrelCaller) {
case "&&":
case "||":
return kestrelCaller;

default:
return undefined;
}
}

function sanitizeNamespace(ns: string): string {
return ns?.replace(/\//g, "$");
}
Expand Down

0 comments on commit 64a5615

Please sign in to comment.