Skip to content

Commit 73d701d

Browse files
2 parents 2d5dfe6 + 20f0a11 commit 73d701d

34 files changed

Lines changed: 9932 additions & 16 deletions

client/src/CashscriptHoverProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class CashscriptHoverProvider implements vscode.HoverProvider{
5757
getVariableTypes(document:vscode.TextDocument, targetWord:string):vscode.MarkdownString[]{
5858
const reg = /([a-zA-Z0-9]+)\s+(pk)[^a-zA-Z0-9]/;
5959
const text = document.getText();
60-
const matches = text.match(new RegExp(`([a-zA-Z0-9]+)\\s+(${targetWord})[^a-zA-Z0-9]`));
60+
const matches = text.match(new RegExp(`([a-zA-Z0-9]+)\\s+(${targetWord})[^a-zA-Z0-9]`)); //regex still incomplete
6161
if(!matches) return null;
6262

6363
return [

client/testFixture/P2PKH.cash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ contract P2PKH(bytes20 pkh) {
77
require(checkSig(s, pk));
88
}
99

10-
}
10+
}

client/testFixture/P2PKH.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
]
2323
}
2424
],
25-
"bytecode": "OP_OVER OP_HASH160 OP_EQUALVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_CHECKSIG",
26-
"source": "pragma cashscript ^0.6.0;\r\n\r\ncontract P2PKH(bytes20 pkh) {\r\n function spend(pubkey pk, sig s) {\r\n require(hash160(pk) == pkh);\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n }\r\n\r\n}",
25+
"bytecode": "OP_OVER OP_HASH160 OP_EQUALVERIFY OP_2DUP OP_CHECKSIGVERIFY OP_CHECKSIG",
26+
"source": "pragma cashscript ^0.6.0;\r\n\r\ncontract P2PKH(bytes20 pkh) {\r\n function spend( pubkey pk , sig s) {\r\n require(hash160(pk) == pkh);\r\n require(checkSig(s, pk));\r\n require(checkSig(s, pk));\r\n }\r\n\r\n}\r\n",
2727
"compiler": {
2828
"name": "cashc",
2929
"version": "0.6.0"
3030
},
31-
"updatedAt": "2021-04-27T15:13:57.815Z"
31+
"updatedAt": "2021-05-01T22:10:47.753Z"
3232
}

client/testFixture/t.cash

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
pragma cashscript ^0.6.0;
21

32
contract ContractName() {
43
function test(pubkey pk) {
54

65
require(hash160(pk) = true);
76
string x = "asdasd";
87
}
9-
}
8+
}

client/testFixture/t.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
"constructorInputs": [],
44
"abi": [
55
{
6-
"name": "test",
6+
"name": "n",
77
"covenant": false,
88
"inputs": []
99
}
1010
],
1111
"bytecode": "OP_1",
12-
"source": "pragma cashscript ^0.6.0;\r\n\r\ncontract ContractName() {\r\n function test() {\r\n require(true);\r\n }\r\n}\r\n",
12+
"source": "\r\ncontract ContractName() {\r\n function n() {\r\n require(true);\r\n }\r\n}",
1313
"compiler": {
1414
"name": "cashc",
1515
"version": "0.6.0"
1616
},
17-
"updatedAt": "2021-04-27T15:14:39.459Z"
17+
"updatedAt": "2021-05-01T22:11:55.032Z"
1818
}

client/testFixture/test.cash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ contract TransferWithTimeout(pubkey sender, pubkey recipient, int timeout) {
1313
bytes34 out1 = new OutputP2PKH(bytes8(10000), pkh);
1414
bytes32 out2 = new OutputP2SH(bytes8(10000), hash160(tx.bytecode));
1515
require(hash256(out1 + out2) == tx.hashOutputs);
16-
require(tx.time >= 1000000)
17-
; require(checkSig(recipientSig, recipient));
16+
require(tx.time >= 1000000);
17+
require(checkSig(recipientSig, recipient));
1818
}
1919

2020

comp/AST.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// export abstract class Node {
2+
// abstract accept<T>(visitor: AstVisitor<T>): T;
3+
// }

comp/AstVisitor.ts

Whitespace-only changes.

comp/compile.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ANTLRInputStream, CommonTokenStream } from 'antlr4ts';
2+
import { CashScriptLexer } from './grammar/CashScriptLexer';
3+
import { CashScriptParser } from './grammar/CashScriptParser';
4+
import { CountFunctionsVisitor } from './visitor';
5+
6+
let code = `
7+
pragma cashscript ^0.6.0;
8+
contract P2PKH(bytes20 pkh) {
9+
function spend( pubkey pk , sig s) {
10+
require(hash160(pk) == pkh);
11+
require(checkSig(s, pk));
12+
require(checkSig(s, pk));
13+
}
14+
15+
}
16+
`
17+
18+
let inputStream = new ANTLRInputStream(code);
19+
let lexer = new CashScriptLexer(inputStream);
20+
let tokenStream = new CommonTokenStream(lexer);
21+
let parser = new CashScriptParser(tokenStream);
22+
23+
let tree = parser.sourceFile();
24+
25+
console.log(tree)
26+
27+
const cf = new CountFunctionsVisitor();
28+
cf.visit(tree);

comp/grammar/CashScript.g4

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
grammar CashScript;
2+
3+
sourceFile
4+
: pragmaDirective* contractDefinition EOF
5+
;
6+
7+
pragmaDirective
8+
: 'pragma' pragmaName pragmaValue ';'
9+
;
10+
11+
pragmaName
12+
: 'cashscript'
13+
;
14+
15+
pragmaValue
16+
: versionConstraint versionConstraint?
17+
;
18+
19+
versionConstraint
20+
: versionOperator? VersionLiteral
21+
;
22+
23+
versionOperator
24+
: '^' | '~' | '>=' | '>' | '<' | '<=' | '='
25+
;
26+
27+
contractDefinition
28+
: 'contract' Identifier parameterList '{' functionDefinition* '}'
29+
;
30+
31+
functionDefinition
32+
: 'function' Identifier parameterList '{' statement* '}'
33+
;
34+
35+
parameterList
36+
: '(' (parameter (',' parameter)* ','?)? ')'
37+
;
38+
39+
parameter
40+
: typeName Identifier
41+
;
42+
43+
block
44+
: '{' statement* '}'
45+
| statement
46+
;
47+
48+
statement
49+
: variableDefinition
50+
| assignStatement
51+
| timeOpStatement
52+
| requireStatement
53+
| ifStatement
54+
;
55+
56+
variableDefinition
57+
: typeName Identifier '=' expression ';'
58+
;
59+
60+
assignStatement
61+
: Identifier '=' expression ';'
62+
;
63+
64+
timeOpStatement
65+
: 'require' '(' TxVar '>=' expression ')' ';'
66+
;
67+
68+
requireStatement
69+
: 'require' '(' expression ')' ';'
70+
;
71+
72+
ifStatement
73+
: 'if' '(' expression ')' ifBlock=block ('else' elseBlock=block)?
74+
;
75+
76+
functionCall
77+
: Identifier expressionList // Only built-in functions are accepted
78+
;
79+
80+
expressionList
81+
: '(' (expression (',' expression)* ','?)? ')'
82+
;
83+
84+
expression
85+
: '(' expression ')' # Parenthesised
86+
| typeName '(' castable=expression (',' size=expression)? ','? ')' # Cast
87+
| functionCall # FunctionCallExpression
88+
| 'new' Identifier expressionList #Instantiation
89+
| expression '[' index=NumberLiteral ']' # TupleIndexOp
90+
| expression op=('.reverse()' | '.length') # UnaryOp
91+
| op=('!' | '-') expression # UnaryOp
92+
// | expression '**' expression --- OP_POW does not exist in BCH Script
93+
// | expression ('*' | '/' | '%') expression --- OP_MUL is disabled in BCH Script
94+
| left=expression op='.split' '(' right=expression ')' # BinaryOp
95+
| left=expression op=('/' | '%') right=expression # BinaryOp
96+
| left=expression op=('+' | '-') right=expression # BinaryOp
97+
// | expression ('>>' | '<<') expression --- OP_LSHIFT & RSHIFT are disabled in BCH Script
98+
| left=expression op=('<' | '<=' | '>' | '>=') right=expression # BinaryOp
99+
| left=expression op=('==' | '!=') right=expression # BinaryOp
100+
| left=expression op='&' right=expression # BinaryOp
101+
| left=expression op='^' right=expression # BinaryOp
102+
| left=expression op='|' right=expression # BinaryOp
103+
| left=expression op='&&' right=expression # BinaryOp
104+
| left=expression op='||' right=expression # BinaryOp
105+
| '[' (expression (',' expression)* ','?)? ']' # Array
106+
| PreimageField # PreimageField
107+
| Identifier # Identifier
108+
| literal # LiteralExpression
109+
;
110+
111+
literal
112+
: BooleanLiteral
113+
| numberLiteral
114+
| StringLiteral
115+
| DateLiteral
116+
| HexLiteral
117+
;
118+
119+
numberLiteral
120+
: NumberLiteral NumberUnit?
121+
;
122+
123+
typeName
124+
: 'int' | 'bool' | 'string' | 'pubkey' | 'sig' | 'datasig' | Bytes
125+
;
126+
127+
VersionLiteral
128+
: [0-9]+ '.' [0-9]+ '.' [0-9]+
129+
;
130+
131+
BooleanLiteral
132+
: 'true' | 'false'
133+
;
134+
135+
NumberUnit
136+
: 'satoshis' | 'sats' | 'finney' | 'bits' | 'bitcoin'
137+
| 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks'
138+
;
139+
140+
NumberLiteral
141+
: [-]?[0-9]+ ([eE] [0-9]+)?
142+
;
143+
144+
Bytes
145+
: 'bytes' Bound?
146+
;
147+
148+
Bound
149+
: [1-9] [0-9]*
150+
;
151+
152+
StringLiteral
153+
: '"' ('\\"' | ~["\r\n])*? '"'
154+
| '\'' ('\\\'' | ~['\r\n])*? '\''
155+
;
156+
157+
DateLiteral
158+
: 'date(' StringLiteral ')'
159+
;
160+
161+
HexLiteral
162+
: '0' [xX] [0-9A-Fa-f]*
163+
;
164+
165+
TxVar
166+
: 'tx.age'
167+
| 'tx.time'
168+
;
169+
170+
PreimageField
171+
: 'tx.version'
172+
| 'tx.hashPrevouts'
173+
| 'tx.hashSequence'
174+
| 'tx.outpoint'
175+
| 'tx.bytecode'
176+
| 'tx.value'
177+
| 'tx.sequence'
178+
| 'tx.hashOutputs'
179+
| 'tx.locktime'
180+
| 'tx.hashtype'
181+
| 'tx.preimage'
182+
;
183+
184+
Identifier
185+
: [a-zA-Z] [a-zA-Z0-9_]*
186+
;
187+
188+
WHITESPACE
189+
: [ \t\r\n\u000C]+ -> skip
190+
;
191+
192+
COMMENT
193+
: '/*' .*? '*/' -> channel(HIDDEN)
194+
;
195+
196+
LINE_COMMENT
197+
: '//' ~[\r\n]* -> channel(HIDDEN)
198+
;

0 commit comments

Comments
 (0)