Skip to content

Commit

Permalink
turn print[Int] & println[Int] into virtual
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalMarsalek committed Mar 12, 2024
1 parent 5c50706 commit c65b1c1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
22 changes: 22 additions & 0 deletions src/IR/opcodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ export const VirtualOpCodes = [
"putc[Ascii]",
"putc[byte]",
"putc[codepoint]",
"print[Int]",
"println[Int]",
] as const satisfies readonly AnyOpCode[];
export type VirtualOpCode = (typeof VirtualOpCodes)[number];

Expand Down Expand Up @@ -750,6 +752,26 @@ export const virtualOpCodeDefinitions = {
}
},
},
"print[Int]": {
construct(a) {
return op["print[Text]"](op.int_to_dec(a));
},
getArgs(node) {
if (isOp["print[Text]"](node) && isOp.int_to_dec(node.args[0])) {
return node.args[0].args;
}
},
},
"println[Int]": {
construct(a) {
return op["println[Text]"](op.int_to_dec(a));
},
getArgs(node) {
if (isOp["println[Text]"](node) && isOp.int_to_dec(node.args[0])) {
return node.args[0].args;
}
},
},
} as const satisfies {
[T in VirtualOpCode]?: VirtualOpCodeDefinition<T>;
};
Expand Down
14 changes: 0 additions & 14 deletions src/common/getType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,20 +349,6 @@ describeOp("println[Text]", [
[[text()], voidType],
]);

describeOp("print[Int]", [
[[text()], "error"],
[[bool], "error"],
[[int(), int()], "error"],
[[int()], voidType],
]);

describeOp("println[Int]", [
[[text()], "error"],
[[bool], "error"],
[[int(), int()], "error"],
[[int()], voidType],
]);

describeOp("or", [
[[bool, int()], "error"],
[[bool], "error"],
Expand Down
3 changes: 0 additions & 3 deletions src/common/getType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,6 @@ function _getOpCodeTypeFromTypes(
case "print[Text]":
case "println[Text]":
return voidType;
case "print[Int]":
case "println[Int]":
return voidType;
case "println_list_joined":
return voidType;
case "println_many_joined":
Expand Down
9 changes: 4 additions & 5 deletions src/plugins/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
blockOrSingle,
type Op,
isText,
argsOf,
} from "../IR";
import { mapOps } from "./ops";
import type { VisitorContext } from "../common/compile";
Expand Down Expand Up @@ -60,12 +61,10 @@ export function golfLastPrintInt(toPrintlnInt = true): PluginVisitor {
const newOp = toPrintlnInt ? "println[Int]" : "print[Int]";
const oldOp = toPrintlnInt ? "print[Int]" : "println[Int]";
const lastStatement = statements[statements.length - 1];
if (isOp(oldOp)(lastStatement)) {
const args = argsOf[oldOp](lastStatement);
if (args !== undefined) {
return blockOrSingle(
statements.with(
statements.length - 1,
op[newOp](lastStatement.args[0]),
),
statements.with(statements.length - 1, op[newOp](args[0])),
);
}
};
Expand Down

0 comments on commit c65b1c1

Please sign in to comment.