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 =
|
export type UnaryOp =
|
||||||
| "Not"
|
| "Not"
|
||||||
| "Negate"
|
| "Neg"
|
||||||
| "Ref"
|
| "Ref"
|
||||||
| "RefMut"
|
| "RefMut"
|
||||||
| "Deref";
|
| "Deref";
|
||||||
@ -194,10 +194,10 @@ export type BinaryOp =
|
|||||||
| "Shl"
|
| "Shl"
|
||||||
| "Shr"
|
| "Shr"
|
||||||
| "Add"
|
| "Add"
|
||||||
| "Subtract"
|
| "Sub"
|
||||||
| "Multiply"
|
| "Mul"
|
||||||
| "Divide"
|
| "Div"
|
||||||
| "Remainder";
|
| "Rem";
|
||||||
|
|
||||||
export type RangeLimit = "Inclusive" | "Exclusive";
|
export type RangeLimit = "Inclusive" | "Exclusive";
|
||||||
|
|
||||||
|
|||||||
@ -409,7 +409,7 @@ class ExprChecker {
|
|||||||
|
|
||||||
private checkUnaryExpr(node: ast.NodeWithKind<"UnaryExpr">): Ty {
|
private checkUnaryExpr(node: ast.NodeWithKind<"UnaryExpr">): Ty {
|
||||||
const exprTy = this.cx.expr(node.kind.expr);
|
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;
|
return Ty.I32;
|
||||||
}
|
}
|
||||||
if (node.kind.op === "Not" && exprTy.compatibleWith(Ty.Bool)) {
|
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[] = [
|
const binaryOpTests: BinaryOpTest[] = [
|
||||||
(op, left, right) => {
|
(op, left, right) => {
|
||||||
const ops = ["Add", "Subtract", "Multiply", "Divide", "Remainder"];
|
const ops: ast.BinaryOp[] = [
|
||||||
|
"Add",
|
||||||
|
"Sub",
|
||||||
|
"Mul",
|
||||||
|
"Div",
|
||||||
|
"Rem",
|
||||||
|
];
|
||||||
if (
|
if (
|
||||||
ops.includes(op) && left.is("Int") && left.compatibleWith(right)
|
ops.includes(op) && left.is("Int") && left.compatibleWith(right)
|
||||||
) {
|
) {
|
||||||
@ -614,7 +620,7 @@ const binaryOpTests: BinaryOpTest[] = [
|
|||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
(op, left, right) => {
|
(op, left, right) => {
|
||||||
const ops = ["Eq", "Ne", "Lt", "Gt", "Lte", "Gte"];
|
const ops: ast.BinaryOp[] = ["Eq", "Ne", "Lt", "Gt", "Lte", "Gte"];
|
||||||
if (
|
if (
|
||||||
ops.includes(op) && left.is("Int") && left.compatibleWith(right)
|
ops.includes(op) && left.is("Int") && left.compatibleWith(right)
|
||||||
) {
|
) {
|
||||||
|
|||||||
@ -197,10 +197,10 @@ export class Parser {
|
|||||||
["<<", "Shl", 3],
|
["<<", "Shl", 3],
|
||||||
[">>", "Shr", 3],
|
[">>", "Shr", 3],
|
||||||
["+", "Add", 2],
|
["+", "Add", 2],
|
||||||
["-", "Subtract", 2],
|
["-", "Sub", 2],
|
||||||
["*", "Multiply", 1],
|
["*", "Mul", 1],
|
||||||
["/", "Divide", 1],
|
["/", "Div", 1],
|
||||||
["%", "Remainder", 1],
|
["%", "Rem", 1],
|
||||||
];
|
];
|
||||||
|
|
||||||
let left = this.parseBinary(prec - 1);
|
let left = this.parseBinary(prec - 1);
|
||||||
@ -228,7 +228,7 @@ export class Parser {
|
|||||||
const loc = this.loc();
|
const loc = this.loc();
|
||||||
const ops: [Tok["type"], ast.UnaryOp][] = [
|
const ops: [Tok["type"], ast.UnaryOp][] = [
|
||||||
["not", "Not"],
|
["not", "Not"],
|
||||||
["-", "Negate"],
|
["-", "Neg"],
|
||||||
["*", "Deref"],
|
["*", "Deref"],
|
||||||
];
|
];
|
||||||
for (const [tok, op] of ops) {
|
for (const [tok, op] of ops) {
|
||||||
|
|||||||
@ -356,7 +356,7 @@ class FnLowerer {
|
|||||||
const resultTy = this.tys.expr(expr);
|
const resultTy = this.tys.expr(expr);
|
||||||
const operandTy = this.tys.expr(expr.kind.expr);
|
const operandTy = this.tys.expr(expr.kind.expr);
|
||||||
if (
|
if (
|
||||||
expr.kind.op === "Negate" &&
|
expr.kind.op === "Neg" &&
|
||||||
operandTy.compatibleWith(Ty.I32) &&
|
operandTy.compatibleWith(Ty.I32) &&
|
||||||
resultTy.compatibleWith(Ty.I32)
|
resultTy.compatibleWith(Ty.I32)
|
||||||
) {
|
) {
|
||||||
@ -435,21 +435,14 @@ type BinaryOpTest = (
|
|||||||
|
|
||||||
const binaryOpTests: BinaryOpTest[] = [
|
const binaryOpTests: BinaryOpTest[] = [
|
||||||
(op, left, right, result) => {
|
(op, left, right, result) => {
|
||||||
const ops = ["Add", "Subtract", "Multiply", "Divide", "Remainder"];
|
const ops: ast.BinaryOp[] = ["Add", "Sub", "Mul", "Div", "Rem"];
|
||||||
const tags: Record<string, BinaryOp> = {
|
|
||||||
"Add": "Add",
|
|
||||||
"Subtract": "Sub",
|
|
||||||
"Multiply": "Mul",
|
|
||||||
"Divide": "Div",
|
|
||||||
"Remainder": "Rem",
|
|
||||||
};
|
|
||||||
if (
|
if (
|
||||||
ops.includes(op) &&
|
ops.includes(op) &&
|
||||||
left.is("Int") &&
|
left.is("Int") &&
|
||||||
left.compatibleWith(right) &&
|
left.compatibleWith(right) &&
|
||||||
result.compatibleWith(left)
|
result.compatibleWith(left)
|
||||||
) {
|
) {
|
||||||
return tags[op];
|
return op as BinaryOp;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user