Skip to content

Commit 6facd03

Browse files
committed
Small changes
- Re-order OperationSimulations.ts - Remove GlobalFunction.REQUIRE as it's unused
1 parent 014dded commit 6facd03

4 files changed

Lines changed: 68 additions & 66 deletions

File tree

packages/cashc/src/ast/Globals.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export const NumberUnit: { [index:string] : number } = {
1616

1717

1818
export enum GlobalFunction {
19-
REQUIRE = 'require',
2019
ABS = 'abs',
2120
MIN = 'min',
2221
MAX = 'max',

packages/cashc/src/generation/Script.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export class toOps {
3939
[GlobalFunction.CHECKSIG]: [Op.OP_CHECKSIG],
4040
[GlobalFunction.MAX]: [Op.OP_MAX],
4141
[GlobalFunction.MIN]: [Op.OP_MIN],
42-
[GlobalFunction.REQUIRE]: [Op.OP_VERIFY],
4342
[GlobalFunction.RIPEMD160]: [Op.OP_RIPEMD160],
4443
[GlobalFunction.SHA1]: [Op.OP_SHA1],
4544
[GlobalFunction.SHA256]: [Op.OP_SHA256],
@@ -96,7 +95,6 @@ export function returnType(op: GlobalFunction | BinaryOperator | UnaryOperator):
9695
[GlobalFunction.HASH256]: new BytesType(32),
9796
[GlobalFunction.MAX]: PrimitiveType.INT,
9897
[GlobalFunction.MIN]: PrimitiveType.INT,
99-
[GlobalFunction.REQUIRE]: PrimitiveType.ANY, // TODO: void
10098
[GlobalFunction.RIPEMD160]: new BytesType(20),
10199
[GlobalFunction.SHA1]: new BytesType(32),
102100
[GlobalFunction.SHA256]: new BytesType(32),

packages/cashc/src/optimisations/OperationSimulations.ts

Lines changed: 68 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -41,39 +41,53 @@ export function executeScriptOnVM(script: Script): Uint8Array[] {
4141
return res.stack;
4242
}
4343

44-
export function applyUnaryOperator(
45-
op: UnaryOperator,
46-
expr: LiteralNode,
47-
): LiteralNode {
48-
if (expr instanceof BoolLiteralNode) {
49-
return applyUnaryOperatorToBool(op, expr);
50-
} else if (expr instanceof IntLiteralNode) {
51-
return applyUnaryOperatorToInt(op, expr);
52-
} else {
53-
throw new Error(); // Already checked in typecheck
44+
// TODO: RequireNode
45+
// TODO: BranchNode
46+
// TODO: CastNode
47+
48+
export function applyGlobalFunction(node: FunctionCallNode): Node {
49+
const { parameters } = node;
50+
const fn = node.identifier.name as GlobalFunction;
51+
52+
// TODO: Apply checkMultiSig (requires code generation refactor)
53+
// This is not very important, since checkMultiSig likely requires external args
54+
if (fn === GlobalFunction.CHECKMULTISIG) {
55+
return node;
5456
}
55-
}
5657

57-
function applyUnaryOperatorToBool(
58-
op: UnaryOperator,
59-
expr: BoolLiteralNode,
60-
): BoolLiteralNode {
61-
const script: Script = ([Data.encodeBool(expr.value)] as Script)
62-
.concat(toOps.fromUnaryOp(op));
63-
const res = executeScriptOnVM(script)[0];
64-
return new BoolLiteralNode(Data.decodeBool(Buffer.from(res)));
65-
}
58+
let script: Script = parameters.map((p) => {
59+
if (p instanceof BoolLiteralNode) {
60+
return Data.encodeBool(p.value);
61+
} else if (p instanceof IntLiteralNode) {
62+
return Data.encodeInt(p.value);
63+
} else if (p instanceof StringLiteralNode) {
64+
return Data.encodeString(p.value);
65+
} else if (p instanceof HexLiteralNode) {
66+
return p.value;
67+
} else {
68+
throw new Error(); // Already checked in typecheck
69+
}
70+
});
71+
script = script.concat(toOps.fromFunction(node.identifier.name as GlobalFunction));
6672

67-
function applyUnaryOperatorToInt(
68-
op: UnaryOperator,
69-
expr: IntLiteralNode,
70-
): IntLiteralNode {
71-
const script: Script = ([Data.encodeInt(expr.value)] as Script)
72-
.concat(toOps.fromUnaryOp(op));
7373
const res = executeScriptOnVM(script)[0];
74-
return new IntLiteralNode(Data.decodeInt(Buffer.from(res)));
74+
if (returnType(fn) === PrimitiveType.BOOL) {
75+
return new BoolLiteralNode(Data.decodeBool(Buffer.from(res)));
76+
} else if (returnType(fn) === PrimitiveType.INT) {
77+
return new IntLiteralNode(Data.decodeInt(Buffer.from(res)));
78+
} else if (returnType(fn) === PrimitiveType.STRING) {
79+
return new StringLiteralNode(Data.decodeString(Buffer.from(res)), '"');
80+
} else if (returnType(fn) instanceof BytesType) {
81+
return new HexLiteralNode(Buffer.from(res));
82+
} else {
83+
throw new Error(); // Already checked in typecheck
84+
}
7585
}
7686

87+
// TODO: InstantiationNode
88+
// TODO: TupleIndexOpNode + SplitOpNode
89+
// TODO: SizeOpNode
90+
7791
export function applyBinaryOperator(
7892
left: LiteralNode,
7993
op: BinaryOperator,
@@ -142,40 +156,35 @@ function applyBinaryOperatorToHex(
142156
: new HexLiteralNode(Buffer.from(res));
143157
}
144158

145-
export function applyGlobalFunction(node: FunctionCallNode): Node {
146-
const { parameters } = node;
147-
const fn = node.identifier.name as GlobalFunction;
148-
149-
// Don't apply checkMultiSig or require for now
150-
if (fn === GlobalFunction.CHECKMULTISIG || fn === GlobalFunction.REQUIRE) {
151-
return node;
159+
export function applyUnaryOperator(
160+
op: UnaryOperator,
161+
expr: LiteralNode,
162+
): LiteralNode {
163+
if (expr instanceof BoolLiteralNode) {
164+
return applyUnaryOperatorToBool(op, expr);
165+
} else if (expr instanceof IntLiteralNode) {
166+
return applyUnaryOperatorToInt(op, expr);
167+
} else {
168+
throw new Error(); // Already checked in typecheck
152169
}
170+
}
153171

154-
let script: Script = parameters.map((p) => {
155-
if (p instanceof BoolLiteralNode) {
156-
return Data.encodeBool(p.value);
157-
} else if (p instanceof IntLiteralNode) {
158-
return Data.encodeInt(p.value);
159-
} else if (p instanceof StringLiteralNode) {
160-
return Data.encodeString(p.value);
161-
} else if (p instanceof HexLiteralNode) {
162-
return p.value;
163-
} else {
164-
throw new Error(); // Already checked in typecheck
165-
}
166-
});
167-
script = script.concat(toOps.fromFunction(node.identifier.name as GlobalFunction));
172+
function applyUnaryOperatorToBool(
173+
op: UnaryOperator,
174+
expr: BoolLiteralNode,
175+
): BoolLiteralNode {
176+
const script: Script = ([Data.encodeBool(expr.value)] as Script)
177+
.concat(toOps.fromUnaryOp(op));
178+
const res = executeScriptOnVM(script)[0];
179+
return new BoolLiteralNode(Data.decodeBool(Buffer.from(res)));
180+
}
168181

182+
function applyUnaryOperatorToInt(
183+
op: UnaryOperator,
184+
expr: IntLiteralNode,
185+
): IntLiteralNode {
186+
const script: Script = ([Data.encodeInt(expr.value)] as Script)
187+
.concat(toOps.fromUnaryOp(op));
169188
const res = executeScriptOnVM(script)[0];
170-
if (returnType(fn) === PrimitiveType.BOOL) {
171-
return new BoolLiteralNode(Data.decodeBool(Buffer.from(res)));
172-
} else if (returnType(fn) === PrimitiveType.INT) {
173-
return new IntLiteralNode(Data.decodeInt(Buffer.from(res)));
174-
} else if (returnType(fn) === PrimitiveType.STRING) {
175-
return new StringLiteralNode(Data.decodeString(Buffer.from(res)), '"');
176-
} else if (returnType(fn) instanceof BytesType) {
177-
return new HexLiteralNode(Buffer.from(res));
178-
} else {
179-
throw new Error(); // Already checked in typecheck
180-
}
189+
return new IntLiteralNode(Data.decodeInt(Buffer.from(res)));
181190
}

packages/cashc/test/optimisations/fixtures.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,6 @@ export const fixtures = {
205205
},
206206
applyGlobalFunction: {
207207
success: [
208-
// TODO REQUIRE (require statements are handled differently)
209-
// ['should apply require(true)', GlobalFunction.REQUIRE, [true], undefined],
210208
// ABS
211209
['should apply abs(1)', GlobalFunction.ABS, [1], 1],
212210
['should apply abs(-1)', GlobalFunction.ABS, [-1], 1],
@@ -269,8 +267,6 @@ export const fixtures = {
269267
[`should apply checkDataSig(0x, ${SigCheck.message}, ${SigCheck.publicKey})`, GlobalFunction.CHECKDATASIG, ['0x', SigCheck.message, SigCheck.publicKey], false],
270268
],
271269
fail: [
272-
// REQUIRE
273-
// ['should fail on require(false)', GlobalFunction.REQUIRE, [false], 'TODOERROR'],
274270
// ABS
275271
['should fail on abs(MAXINT + 1)', GlobalFunction.ABS, [MAXINT + 1], Error.INVALID_SCRIPT_NUMBER],
276272
['should fail on abs(-MAXINT - 1)', GlobalFunction.ABS, [-MAXINT - 1], Error.INVALID_SCRIPT_NUMBER],

0 commit comments

Comments
 (0)