Skip to content

Commit

Permalink
implement xor operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich committed Apr 10, 2024
1 parent 87b5cd6 commit 51b22d8
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/generator/writers/writeExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ export function writeExpression(f: ASTExpression, ctx: WriterContext): string {
op = "&";
} else if (f.op === "|") {
op = "|";
} else if (f.op === "^") {
op = "^";
} else {
throwError("Unknown binary operator: " + f.op, f.ref);
}
Expand Down
3 changes: 2 additions & 1 deletion src/grammar/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ export type ASTOpBinary = {
| "<<"
| ">>"
| "&"
| "|";
| "|"
| "^";
left: ASTExpression;
right: ASTExpression;
ref: ASTRef;
Expand Down
1 change: 1 addition & 0 deletions src/grammar/grammar.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Tact {
| ExpressionBinary "<<" ExpressionAdd --shl
| ExpressionBinary "&" ExpressionAdd --bin_and
| ExpressionBinary "|" ExpressionAdd --bin_or
| ExpressionBinary "^" ExpressionAdd --bin_xor
| ExpressionAdd
ExpressionAdd = ExpressionAdd "+" ~"+" ExpressionMul --add
| ExpressionAdd "-" ~"-" ExpressionMul --sub
Expand Down
1 change: 1 addition & 0 deletions src/grammar/grammar.ohm-bundle.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export interface TactActionDict<T> extends ActionDict<T> {
ExpressionBinary_shl?: (this: NonterminalNode, arg0: NonterminalNode, arg1: TerminalNode, arg2: NonterminalNode) => T;
ExpressionBinary_bin_and?: (this: NonterminalNode, arg0: NonterminalNode, arg1: TerminalNode, arg2: NonterminalNode) => T;
ExpressionBinary_bin_or?: (this: NonterminalNode, arg0: NonterminalNode, arg1: TerminalNode, arg2: NonterminalNode) => T;
ExpressionBinary_bin_xor?: (this: NonterminalNode, arg0: NonterminalNode, arg1: TerminalNode, arg2: NonterminalNode) => T;
ExpressionBinary?: (this: NonterminalNode, arg0: NonterminalNode) => T;
ExpressionAdd_add?: (this: NonterminalNode, arg0: NonterminalNode, arg1: TerminalNode, arg2: NonterminalNode) => T;
ExpressionAdd_sub?: (this: NonterminalNode, arg0: NonterminalNode, arg1: TerminalNode, arg2: NonterminalNode) => T;
Expand Down
2 changes: 1 addition & 1 deletion src/grammar/grammar.ohm-bundle.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/grammar/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,15 @@ semantics.addOperation<ASTNode>("resolve_expression", {
ref: createRef(this),
});
},
ExpressionBinary_bin_xor(arg0, _arg1, arg2) {
return createNode({
kind: "op_binary",
op: "^",
left: arg0.resolve_expression(),
right: arg2.resolve_expression(),
ref: createRef(this),
});
},

// Unary
ExpressionUnary_add(_arg0, arg1) {
Expand Down
6 changes: 6 additions & 0 deletions src/test/feature-math.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ describe("feature-math", () => {
expect(await contract.getSub(1n, -2n)).toBe(3n);
expect(await contract.getMul(2n, 2n)).toBe(4n);
expect(await contract.getDiv(2n, 2n)).toBe(1n);
expect(await contract.getMod(2n, 2n)).toBe(0n);
expect(await contract.getShr(4n, 1n)).toBe(2n);
expect(await contract.getShl(2n, 1n)).toBe(4n);
expect(await contract.getAnd(2n, 3n)).toBe(2n);
expect(await contract.getOr(2n, 3n)).toBe(3n);
expect(await contract.getXor(2n, 3n)).toBe(1n);

// Augmented Assign
expect(await contract.getAddAug(1n, 2n)).toBe(3n);
Expand Down
4 changes: 4 additions & 0 deletions src/test/features/math.tact
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ contract MathTester with Deployable {
return a | b;
}

get fun xor(a: Int, b: Int): Int {
return a ^ b;
}

//
// Augmented assignment
//
Expand Down
2 changes: 2 additions & 0 deletions src/types/resolveConstantValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ function reduceIntImpl(ast: ASTExpression): bigint {
return l & r;
} else if (ast.op === "|") {
return l | r;
} else if (ast.op === "^") {
return l ^ r;
}
} else if (ast.kind === "op_unary") {
if (ast.op === "-") {
Expand Down
3 changes: 2 additions & 1 deletion src/types/resolveExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ function resolveBinaryOp(
exp.op === ">>" ||
exp.op === "<<" ||
exp.op === "&" ||
exp.op === "|"
exp.op === "|" ||
exp.op === "^"
) {
if (le.kind !== "ref" || le.optional || le.name !== "Int") {
throwError(
Expand Down

0 comments on commit 51b22d8

Please sign in to comment.