Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/Solcore/Frontend/Parser/SolcoreParser.y
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ DeclList : Decl DeclList { $1 : $2 }
Decl :: { ContractDecl }
Decl : FieldDef {CFieldDecl $1}
| DataDef {CDataDecl $1}
| Function {CFunDecl $1}
| AnnFunction {CFunDecl $1}
| Constructor {CConstrDecl $1}

-- type synonym
Expand Down Expand Up @@ -223,6 +223,9 @@ Signatures : Signature ';' Signatures {$1 : $3}
Signature :: { Signature }
Signature : SigPrefix 'function' Name '(' ParamList ')' OptRetTy {Signature (fst $1) (snd $1) $3 $5 $7}

AnnSignature :: { Signature }
AnnSignature : SigPrefix 'function' Name '(' AnnParamList ')' OptRetTy {Signature (fst $1) (snd $1) $3 $5 (maybe unitTy Just $7)}

SigPrefix :: {([Ty], [Pred])}
SigPrefix : 'forall' Tyvars '.' ConstraintList '=>' {($2, $4)}
| 'forall' Tyvars '.' {($2, [])}
Expand All @@ -233,10 +236,18 @@ ParamList : Param {[$1]}
| Param ',' ParamList {$1 : $3}
| {- empty -} {[]}

AnnParamList :: { [Param] }
AnnParamList : AnnParam {[$1]}
| AnnParam ',' AnnParamList {$1 : $3}
| {- empty -} {[]}

Param :: { Param }
Param : Name ':' Type {Typed $1 $3}
| Name {Untyped $1}

AnnParam :: { Param }
AnnParam : Name ':' Type {Typed $1 $3}

-- instance declarations

InstDef :: { Instance }
Expand All @@ -259,18 +270,22 @@ Tyvars :: {[Ty]}
Tyvars : Name Tyvars { (TyCon $1 []) : $2}
| {-empty-} {[]}

Functions :: { [FunDef] }
Functions : Function Functions {$1 : $2}
| {- empty -} {[]}
AnnFunctions :: { [FunDef] }
AnnFunctions : AnnFunction AnnFunctions {$1 : $2}
| {- empty -} {[]}


InstBody :: {[FunDef]}
InstBody : '{' Functions '}' {$2}
InstBody : '{' AnnFunctions '}' {$2}

-- Function declaration

Function :: { FunDef }
Function : Signature Body {FunDef $1 $2}

AnnFunction :: { FunDef }
AnnFunction : AnnSignature Body {FunDef $1 $2}

OptRetTy :: { Maybe Ty }
OptRetTy : '->' Type {Just $2}
| {- empty -} {Nothing}
Expand Down Expand Up @@ -567,6 +582,9 @@ tupleExp (t1 : ts) = pairExp t1 (tupleExp ts)
rmquotes :: String -> String
rmquotes = read

unitTy :: Maybe Ty
unitTy = Just (TyCon (Name "()") [])

parseError (Token (line, col) lexeme)
= alexError $ "Parse error while processing lexeme: " ++ show lexeme
++ "\n at line " ++ show line ++ ", column " ++ show col
Expand Down
1 change: 1 addition & 0 deletions test/Cases.hs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ cases =
, runTestForFile "yul-function-typing.solc" caseFolder
, runTestForFile "yul-return.solc" caseFolder
, runTestExpectingFailure "unbound-instance-var.solc" caseFolder
, runTestForFile "optional-return-function-contract.solc" caseFolder
]
where
caseFolder = "./test/examples/cases"
Expand Down
2 changes: 1 addition & 1 deletion test/examples/cases/Add1.solc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
contract Add1 {
function main() {
function main() -> word {
let res: word;
assembly {
res := add(40, 2)
Expand Down
8 changes: 4 additions & 4 deletions test/examples/cases/Compose2.solc
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
contract Compose {
function compose(f,g) {
forall a b c. function compose(f :(b) -> c, g :((a) -> b)) -> ((a) -> c) {
return lam (x) {
return f(g(x));
} ;
}

function id(x) { return x; }
forall a . function id(x : a) -> a { return x; }

function idid() { return compose(id,id); }
forall a . function idid() -> ((a) -> a) { return compose(id,id); }

// function main() { return idid(42); }

function main() {
function main() -> word {
let f = compose(id,id);
return f(42);
}
Expand Down
10 changes: 5 additions & 5 deletions test/examples/cases/Compose3.solc
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
contract Compose {
function compose(f,g) {
forall a b c . function compose(f : (b) -> c,g : (a) -> b) -> ((a) -> c){
return lam (x) {
return f(g(x));
} ;
}

function id(x) { return x; }
forall a . function id(x : a) -> a { return x; }

function idid() { return compose(id,id); }
forall a . function idid() -> ((a) -> a) { return compose(id,id); }

function apply1(f, a) { return f(a); }
forall a b . function apply1(f : (a) -> b , a : a) -> b { return f(a); }

function main() {
function main() -> word {
return apply1(compose(id, id), 42);
}
}
4 changes: 2 additions & 2 deletions test/examples/cases/CondExp.solc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
contract CondExp {
function main() {
function main() -> word {
return
if if true then false else true
then if false then 1 else 2
else if true then 42 else 56;
}
}
}
2 changes: 1 addition & 1 deletion test/examples/cases/EitherModule.solc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ contract EitherModule {
data Either(a,b) = Left(a) | Right(b);
data List(a) = Nil | Cons(a,List(a));

function lefts(xs) {
forall a b . function lefts(xs : List(Either(a,b))) -> List(a) {
match xs {
| Nil => return Nil ;
| Cons(y,ys) =>
Expand Down
4 changes: 2 additions & 2 deletions test/examples/cases/EvenOdd.solc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ contract EvenOdd {
data Nat = Zero | Succ(Nat);
data Bool = False | True;

function even (n) {
function even (n : Nat) -> Bool {
match n {
| Zero => return True;
| Succ(m) => return odd(m);
}
}

function odd(n) {
function odd(n : Nat) -> Bool {
match n {
| Zero => return False;
| Succ(m) => return even(m);
Expand Down
4 changes: 2 additions & 2 deletions test/examples/cases/Id.solc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function id() {
forall a . function id() -> ((a) -> a) {
return lam (x) { return x; } ;
}

contract Id {
function main () {
function main () -> word {
let f = id();
return f(0);
}
Expand Down
4 changes: 2 additions & 2 deletions test/examples/cases/ListModule.solc
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ contract ListModule {
data Bool = True | False;


function zipWith (f,xs,ys) {
forall a b c . function zipWith (f : (a,b) -> c,xs : List(a),ys : List(b)) -> List(c) {
match xs, ys {
| Nil, Nil => return Nil ;
| Cons(x1,xs1), Cons(y1,ys1) =>
return Cons(f(x1,y1), zipWith(f,xs1,ys1)) ;
}
}

function foldr(f, v, xs) {
forall a b . function foldr(f : (a,b) -> b, v : b, xs : List(a)) -> b {
match xs {
| Nil => return v;
| Cons(y,ys) =>
Expand Down
8 changes: 4 additions & 4 deletions test/examples/cases/Logic.solc
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
contract Logic {
data Bool = True | False;

function not (x) {
function not (x : Bool) -> Bool {
match x {
| True => return False ;
| False => return True ;
}
}

function and(x, y) {
function and(x : Bool, y : Bool) -> Bool {
match x, y {
| False, _ => return False ;
| True , _ => return y ;
}
}

function and1 (x, y) {
function and1 (x : Bool, y : Bool) -> Bool{
match x, y {
| False, False => return False ;
| True , False => return False;
Expand All @@ -24,7 +24,7 @@ contract Logic {
}
}

function elim (f, g, x) {
forall a . function elim (f : a, g : a, x : Bool) -> a {
match x {
| True => return f;
| False => return g;
Expand Down
4 changes: 2 additions & 2 deletions test/examples/cases/MatchCall.solc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
data Bool = False | True;

contract MatchCall {
function f() {
function f() -> Bool {
return True;
}

function main() {
function main() -> word {
match f() {
| True => return 42;
}
Expand Down
4 changes: 2 additions & 2 deletions test/examples/cases/Mutuals.solc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
contract Mutual {
function main () {
function main () -> word {
return f();
}
function f () {
function f() -> word {
return 42;
}
}
6 changes: 3 additions & 3 deletions test/examples/cases/NegPair.solc
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ forall a b . a : Neg, b : Neg => instance (a,b):Neg {

contract NegPair {

function bnot(x) {
function bnot(x : B) -> B {
match x {
| T => return F;
| F => return T;
}
}

function fromB(b) {
function fromB(b : B) -> word {
match b {
| F => return 0;
| T => return 1;
}
}

function main() { return fromB(fst(Neg.neg((F,T)))); }
function main() -> word { return fromB(fst(Neg.neg((F,T)))); }
}
2 changes: 1 addition & 1 deletion test/examples/cases/Option.solc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
contract Option {
data Option(a) = None | Some(a);

function join(mmx) {
forall a . function join(mmx : Option(Option(a))) -> Option(a) {
match mmx {
| None => return None;
| Some(Some(x)) => return Some(x);
Expand Down
4 changes: 2 additions & 2 deletions test/examples/cases/SimpleLambda.solc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function addWord(x : word, y : word) -> word {
}

contract SimpleLambda{
function f (z) {
function f (z : word) -> word {
let n = lam (x,y) {
return addWord(x,addWord(y,1));
} ;
Expand All @@ -16,7 +16,7 @@ contract SimpleLambda{
} ;
return m(n(1,0));
}
function main() {
function main() -> word {
return f(40);
}
}
2 changes: 1 addition & 1 deletion test/examples/cases/array.solc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ forall size elem . size : ToWord, elem:MemoryType => instance memory(array(size,

contract Array {

function main() {
function main() -> word {
let arr : memory(array(Succ(Succ(Succ(Succ(Zero)))), word)) = memory(42); // = (1,2,3,4,5,6,7,8,9,10);
IndexAccessible.set(arr, 3, 33);

Expand Down
2 changes: 1 addition & 1 deletion test/examples/cases/const.solc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function const() {
}

contract Foo {
function main () {
function main () -> word {
let f = const();
return f(0,1);
}
Expand Down
4 changes: 2 additions & 2 deletions test/examples/cases/import-std.solc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import std;

contract Test {
function main() {
function main() -> word {
return Add.add(21, 21);
}
}
}
12 changes: 6 additions & 6 deletions test/examples/cases/join.solc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ contract Option {
data Option(a) = None | Some(a);
data Bool = False | True;

function maybe(n, o) {
match o {
| None => return n;
forall a . function maybe(n : Option(a), o : a) -> a {
match n {
| None => return o;
| Some(x) => return x;
}
}

function join(mmx) {
forall a . function join(mmx : Option(Option(a))) -> Option(a) {
let result = None;
match mmx {
| Some(Some(x)) => result = Some(x);
Expand All @@ -18,7 +18,7 @@ contract Option {
return result;
}

function main() {
return maybe(0, join(Some(Some(0))));
function main() -> word {
return maybe(join(Some(Some(0))), 0);
}
}
4 changes: 2 additions & 2 deletions test/examples/cases/modifier.solc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
contract C {
function modifier(f) {
forall a b c. function modifier(f : (a,b) -> c) -> ((a,b) -> c){
return lam (x, y) {
// before solidity placeholder
let result = f(x,y); // Solidity's placeholder: _;
Expand All @@ -16,7 +16,7 @@ contract C {
return r;
}

function main() {
function main() -> word {
//function g(x, y) modifier(x,y) {
// return add(x,y);
//}
Expand Down
Loading