abbreviate binary ops

This commit is contained in:
sfja 2026-04-13 10:42:49 +02:00
parent 086bea3e89
commit c33c7f45a0
4 changed files with 22 additions and 23 deletions

View File

@ -174,7 +174,7 @@ export type IntTy =
export type UnaryOp =
| "Not"
| "Negate"
| "Neg"
| "Ref"
| "RefMut"
| "Deref";
@ -194,10 +194,10 @@ export type BinaryOp =
| "Shl"
| "Shr"
| "Add"
| "Subtract"
| "Multiply"
| "Divide"
| "Remainder";
| "Sub"
| "Mul"
| "Div"
| "Rem";
export type RangeLimit = "Inclusive" | "Exclusive";

View File

@ -409,7 +409,7 @@ class ExprChecker {
private checkUnaryExpr(node: ast.NodeWithKind<"UnaryExpr">): Ty {
const exprTy = this.cx.expr(node.kind.expr);
if (node.kind.op === "Negate" && exprTy.compatibleWith(Ty.I32)) {
if (node.kind.op === "Neg" && exprTy.compatibleWith(Ty.I32)) {
return Ty.I32;
}
if (node.kind.op === "Not" && exprTy.compatibleWith(Ty.Bool)) {
@ -605,7 +605,13 @@ type BinaryOpTest = (op: ast.BinaryOp, left: Ty, right: Ty) => Ty | null;
const binaryOpTests: BinaryOpTest[] = [
(op, left, right) => {
const ops = ["Add", "Subtract", "Multiply", "Divide", "Remainder"];
const ops: ast.BinaryOp[] = [
"Add",
"Sub",
"Mul",
"Div",
"Rem",
];
if (
ops.includes(op) && left.is("Int") && left.compatibleWith(right)
) {
@ -614,7 +620,7 @@ const binaryOpTests: BinaryOpTest[] = [
return null;
},
(op, left, right) => {
const ops = ["Eq", "Ne", "Lt", "Gt", "Lte", "Gte"];
const ops: ast.BinaryOp[] = ["Eq", "Ne", "Lt", "Gt", "Lte", "Gte"];
if (
ops.includes(op) && left.is("Int") && left.compatibleWith(right)
) {

View File

@ -197,10 +197,10 @@ export class Parser {
["<<", "Shl", 3],
[">>", "Shr", 3],
["+", "Add", 2],
["-", "Subtract", 2],
["*", "Multiply", 1],
["/", "Divide", 1],
["%", "Remainder", 1],
["-", "Sub", 2],
["*", "Mul", 1],
["/", "Div", 1],
["%", "Rem", 1],
];
let left = this.parseBinary(prec - 1);
@ -228,7 +228,7 @@ export class Parser {
const loc = this.loc();
const ops: [Tok["type"], ast.UnaryOp][] = [
["not", "Not"],
["-", "Negate"],
["-", "Neg"],
["*", "Deref"],
];
for (const [tok, op] of ops) {

View File

@ -356,7 +356,7 @@ class FnLowerer {
const resultTy = this.tys.expr(expr);
const operandTy = this.tys.expr(expr.kind.expr);
if (
expr.kind.op === "Negate" &&
expr.kind.op === "Neg" &&
operandTy.compatibleWith(Ty.I32) &&
resultTy.compatibleWith(Ty.I32)
) {
@ -435,21 +435,14 @@ type BinaryOpTest = (
const binaryOpTests: BinaryOpTest[] = [
(op, left, right, result) => {
const ops = ["Add", "Subtract", "Multiply", "Divide", "Remainder"];
const tags: Record<string, BinaryOp> = {
"Add": "Add",
"Subtract": "Sub",
"Multiply": "Mul",
"Divide": "Div",
"Remainder": "Rem",
};
const ops: ast.BinaryOp[] = ["Add", "Sub", "Mul", "Div", "Rem"];
if (
ops.includes(op) &&
left.is("Int") &&
left.compatibleWith(right) &&
result.compatibleWith(left)
) {
return tags[op];
return op as BinaryOp;
}
return null;
},