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 type SimpleType
= MyVariant1 = MyVariant1
| MyVariant2 Integer[u32] | MyVariant2 Int[u32]
| MyVariant3 Integer[1..20] | MyVariant3 Int[1..20]
| MyVariant4 Integer String | MyVariant4 Int String
/* Multiline /* Multiline
Comment! */ Comment! */
@ -15,23 +15,24 @@ type Option val
struct MyStruct = struct MyStruct =
.field1 : SimpleType .field1 : SimpleType
.field2 : Integer .field2 : Int
trait Eq needs trait Eq needs
add : Self, Self -> Self .add : Self, Self -> Self
trait Functor[a, b] on Self _ needs trait Functor[a, b] on Self _ needs
map : (a -> b), Self a -> Self b .map : (a -> b), Self a -> Self b
pure : a -> Self a .pure : a -> Self a
type ComplexType[a, b : Functor] type ComplexType[a, b : Functor]
= Left a = Left a
| Right b | Right b
impl Functor on Option _: impl Functor on Option _:
map mapper union = if union is .map mapper union = if union is
Some val -> Some (mapper val) Some val -> Some (mapper val)
None -> None None -> None
.pure elem = Some elem
magnitude : Int, Int -> Int magnitude : Int, Int -> Int
magnitude x y = sqrt (x * x) (y * y) magnitude x y = sqrt (x * x) (y * y)
@ -43,7 +44,7 @@ soundeffect type =
else else
"bop!" "bop!"
destructure : MyStruct -> Option Integer destructure : MyStruct -> Option Int
destructure s = destructure s =
if s is if s is
MyStruct MyStruct
@ -60,10 +61,23 @@ destructure s =
None None
_ -> None _ -> None
destructure_type : SimpleType -> Integer destructure_type : SimpleType -> Int
destructure_type t = destructure_type t =
if t is if t is
MyVariant1 -> 0 MyVariant1 -> 0
MyVariant2 x -> x * x MyVariant2 x -> x * x
MyVariant3 x -> x /2 MyVariant3 x -> x /2
MyVariant4 x _ -> x 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")] #[token("on")]
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 /// An `->` arrow
/// ///
/// Used as part of function type annotations as well as in the cases of If-Is blocks /// 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("..")] #[token("..")]
RangeOp, 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 /// A newline NOT followed by whitespace
/// ///
/// This means that the following tokens are at the start of a line. For example /// This means that the following tokens are at the start of a line. For example