Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: more ast elements and some sample syntax
- Loading branch information
Showing
2 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,47 @@ | ||
pub struct Module {} | ||
pub struct Module { | ||
pub name: Vec<String>, | ||
pub docs: Vec<String>, | ||
pub is_script: bool, | ||
pub is_lib: bool, | ||
pub is_policy: bool, | ||
} | ||
|
||
pub enum Decl {} | ||
pub enum Definition { | ||
Fn { | ||
arguments: Vec<String>, | ||
body: Expr, | ||
doc: Option<String>, | ||
name: String, | ||
public: bool, | ||
return_annotation: Option<()>, | ||
return_type: (), | ||
}, | ||
|
||
TypeAlias { | ||
alias: String, | ||
annotation: (), | ||
doc: Option<String>, | ||
parameters: Vec<String>, | ||
public: bool, | ||
tipo: (), | ||
}, | ||
|
||
DataType { | ||
constructors: Vec<()>, | ||
doc: Option<String>, | ||
name: String, | ||
opaque: bool, | ||
parameters: Vec<String>, | ||
public: bool, | ||
typed_parameters: Vec<()>, | ||
}, | ||
|
||
Use { | ||
module: Vec<String>, | ||
as_name: Option<String>, | ||
// unqualified: Vec<UnqualifiedImport>, | ||
// package: PackageName, | ||
}, | ||
} | ||
|
||
pub enum Expr {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// imports | ||
use std/address.Address as A // alias | ||
|
||
use std/list.{map, foldl} // access module members in one import | ||
|
||
// `///` used for document generation | ||
|
||
// Records (single constructor `data` type) | ||
pub type Datum { | ||
signer: Address, | ||
} | ||
|
||
// above is suger for | ||
pub type Datum { | ||
Datum { signer: Address }, | ||
} | ||
|
||
// type aliases | ||
type A = Address | ||
|
||
// multiple constructors and a `generic` | ||
pub type Redeemer(a) { | ||
// records wrapped in parens | ||
Buy(Address, a), | ||
// records wrapped in curlies | ||
Sell { address: Address, some_thing: a }, | ||
} | ||
|
||
pub fn main(datum: Datum, redeemer: Redeemer, ctx: ScriptContext) { | ||
[1, 2, 3] | ||
|> list.map(fn(x) -> x + 1) | ||
} | ||
|
||
// named and anonymous functions | ||
fn(x) -> x + 1 | ||
|
||
fn add_one(x) -> x + 1 | ||
|
||
fn(x: Int) -> x + 1 | ||
|
||
fn add_one(label x: Int) -> x + 1 | ||
|
||
fn(x: Int) { | ||
x + 1 | ||
} | ||
|
||
fn(x: Int) -> Int { | ||
x + 1 | ||
} | ||
|
||
fn add_one(x: Int) -> Int { | ||
x + 1 | ||
} | ||
|
||
// can be curried | ||
fn add(a, b) { | ||
a + 1 | ||
} | ||
|
||
let add_one = add(1) | ||
|
||
// matching | ||
when redeemer is { | ||
Buyer(address, thing) -> do_stuff(), | ||
Seller { address, some_thing } -> do_seller_stuff(), | ||
} |