abbreviate binary ops
This commit is contained in:
parent
086bea3e89
commit
c33c7f45a0
10
src/ast.ts
10
src/ast.ts
@ -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";
|
||||
|
||||
|
||||
@ -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)
|
||||
) {
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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;
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user