From 208f2e80eae5128e154b0863f526f039e0fd7d0c Mon Sep 17 00:00:00 2001 From: rvcas Date: Wed, 10 Aug 2022 15:50:19 -0400 Subject: [PATCH] feat: more ast elements and some sample syntax --- crates/lang/src/ast.rs | 46 +++++++++++++++++++++++++++-- syntax.txt | 66 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 syntax.txt diff --git a/crates/lang/src/ast.rs b/crates/lang/src/ast.rs index 5541619b..c8f8c0e4 100644 --- a/crates/lang/src/ast.rs +++ b/crates/lang/src/ast.rs @@ -1,5 +1,47 @@ -pub struct Module {} +pub struct Module { + pub name: Vec, + pub docs: Vec, + pub is_script: bool, + pub is_lib: bool, + pub is_policy: bool, +} -pub enum Decl {} +pub enum Definition { + Fn { + arguments: Vec, + body: Expr, + doc: Option, + name: String, + public: bool, + return_annotation: Option<()>, + return_type: (), + }, + + TypeAlias { + alias: String, + annotation: (), + doc: Option, + parameters: Vec, + public: bool, + tipo: (), + }, + + DataType { + constructors: Vec<()>, + doc: Option, + name: String, + opaque: bool, + parameters: Vec, + public: bool, + typed_parameters: Vec<()>, + }, + + Use { + module: Vec, + as_name: Option, + // unqualified: Vec, + // package: PackageName, + }, +} pub enum Expr {} diff --git a/syntax.txt b/syntax.txt new file mode 100644 index 00000000..b71a8d3e --- /dev/null +++ b/syntax.txt @@ -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(), +}