@@ -18,6 +18,7 @@ pub enum Token<'src> {
1818 // Keywords
1919 Let ,
2020 In ,
21+ Fn ,
2122}
2223
2324pub type Spanned < T > = ( T , SimpleSpan ) ;
@@ -28,6 +29,7 @@ fn lexer<'src>(
2829 let keyword = text:: ident ( ) . map ( |s| match s {
2930 "let" => Token :: Let ,
3031 "in" => Token :: In ,
32+ "fn" => Token :: Fn ,
3133 s => Token :: Ident ( s) ,
3234 } ) ;
3335
@@ -63,10 +65,11 @@ fn lexer<'src>(
6365pub enum Expr < ' src > {
6466 Local ( & ' src str ) ,
6567 Num ( f64 ) ,
66- Let ( & ' src str , Box < Spanned < Self > > , Box < Spanned < Self > > ) ,
68+ Let ( Spanned < & ' src str > , Box < Spanned < Self > > , Box < Spanned < Self > > ) ,
6769 Add ( Box < Spanned < Self > > , Box < Spanned < Self > > ) ,
6870 Mul ( Box < Spanned < Self > > , Box < Spanned < Self > > ) ,
6971 Call ( Box < Spanned < Self > > , Box < Spanned < Self > > ) ,
72+ Func ( Vec < Spanned < & ' src str > > , Box < Spanned < Self > > ) ,
7073}
7174
7275type ParserInput < ' src > = SpannedInput < Token < ' src > , SimpleSpan , & ' src [ Spanned < Token < ' src > > ] > ;
@@ -81,12 +84,18 @@ fn parser<'src>(
8184 ident. map ( Expr :: Local ) ,
8285 // let x = y in z
8386 just ( Token :: Let )
84- . ignore_then ( ident)
87+ . ignore_then ( ident. map_with ( |x , e| ( x , e . span ( ) ) ) )
8588 . then_ignore ( just ( Token :: Eq ) )
8689 . then ( expr. clone ( ) )
8790 . then_ignore ( just ( Token :: In ) )
8891 . then ( expr. clone ( ) )
8992 . map ( |( ( lhs, rhs) , then) | Expr :: Let ( lhs, Box :: new ( rhs) , Box :: new ( then) ) ) ,
93+ // fn x y = z
94+ just ( Token :: Fn )
95+ . ignore_then ( ident. map_with ( |x, e| ( x, e. span ( ) ) ) . repeated ( ) . collect ( ) )
96+ . then_ignore ( just ( Token :: Eq ) )
97+ . then ( expr. clone ( ) )
98+ . map ( |( args, body) | Expr :: Func ( args, Box :: new ( body) ) ) ,
9099 ) ) ;
91100
92101 atom. map_with ( |expr, e| ( expr, e. span ( ) ) )
@@ -125,7 +134,9 @@ fn parser<'src>(
125134
126135fn main ( ) {
127136 let text = "
128- let x = (5 + 42) * 2 in
137+ let add = fn x y = x + y in
138+ let mul = fn x y = x * y in
139+ let x = mul (add 5 42) 2 in
129140 add x 3.5
130141 " ;
131142
0 commit comments