diff --git a/src/IR/opcodes.ts b/src/IR/opcodes.ts index fe36a855..526cd1fb 100644 --- a/src/IR/opcodes.ts +++ b/src/IR/opcodes.ts @@ -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]; @@ -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; }; diff --git a/src/common/getType.test.ts b/src/common/getType.test.ts index e1e9bfa9..63638f45 100644 --- a/src/common/getType.test.ts +++ b/src/common/getType.test.ts @@ -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"], diff --git a/src/common/getType.ts b/src/common/getType.ts index 128f4933..80a05118 100644 --- a/src/common/getType.ts +++ b/src/common/getType.ts @@ -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": diff --git a/src/plugins/print.ts b/src/plugins/print.ts index 4ec3cd0a..b6419c6e 100644 --- a/src/plugins/print.ts +++ b/src/plugins/print.ts @@ -15,6 +15,7 @@ import { blockOrSingle, type Op, isText, + argsOf, } from "../IR"; import { mapOps } from "./ops"; import type { VisitorContext } from "../common/compile"; @@ -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])), ); } };