Rework the example a little bit, add some tokens to match

This commit is contained in:
Emi Simpson 2022-03-10 20:28:29 -05:00
parent 89c5a6985b
commit 74d19864f2
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847
2 changed files with 49 additions and 10 deletions

View file

@ -2,9 +2,9 @@
type SimpleType
= MyVariant1
| MyVariant2 Integer[u32]
| MyVariant3 Integer[1..20]
| MyVariant4 Integer String
| MyVariant2 Int[u32]
| MyVariant3 Int[1..20]
| MyVariant4 Int String
/* Multiline
Comment! */
@ -15,23 +15,24 @@ type Option val
struct MyStruct =
.field1 : SimpleType
.field2 : Integer
.field2 : Int
trait Eq needs
add : Self, Self -> Self
.add : Self, Self -> Self
trait Functor[a, b] on Self _ needs
map : (a -> b), Self a -> Self b
pure : a -> Self a
.map : (a -> b), Self a -> Self b
.pure : a -> Self a
type ComplexType[a, b : Functor]
= Left a
| Right b
impl Functor on Option _:
map mapper union = if union is
.map mapper union = if union is
Some val -> Some (mapper val)
None -> None
.pure elem = Some elem
magnitude : Int, Int -> Int
magnitude x y = sqrt (x * x) (y * y)
@ -43,7 +44,7 @@ soundeffect type =
else
"bop!"
destructure : MyStruct -> Option Integer
destructure : MyStruct -> Option Int
destructure s =
if s is
MyStruct
@ -60,10 +61,23 @@ destructure s =
None
_ -> None
destructure_type : SimpleType -> Integer
destructure_type : SimpleType -> Int
destructure_type t =
if t is
MyVariant1 -> 0
MyVariant2 x -> x * x
MyVariant3 x -> x /2
MyVariant4 x _ -> x
simple_tuple : (Int, Int)
simple_tuple = (1, 2)
magnitude_plus_ten : Int, Int -> Int
magnitude_plus_ten x y =
let magnitude =
let x_squared = x * x
let y_squared = y * y
in sqrt (x + y)
in
let magnitude_plus_5 = magnitude + 5
in magnitude_plus_5 + 5

View file

@ -90,6 +90,19 @@ pub enum Token {
#[token("on")]
On,
/// the `let` keyword
///
/// Allows binding a value to an immutable variable that can be used multiple times
#[token("let")]
Let,
/// the `in` keyword
///
/// Used to seperate a series of `let` bindings from the expression they're being used
/// in.
#[token("in")]
In,
/// An `->` arrow
///
/// Used as part of function type annotations as well as in the cases of If-Is blocks
@ -186,6 +199,18 @@ pub enum Token {
#[token("..")]
RangeOp,
/// A `.` period
///
/// For getting fields of structs
#[token(".")]
Dot,
/// A `,` comma
///
/// The age-old and timeless delineator
#[token(",")]
Comma,
/// A newline NOT followed by whitespace
///
/// This means that the following tokens are at the start of a line. For example