Skip to content

Commit

Permalink
added function type parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tk744 committed Dec 19, 2020
1 parent 538fe46 commit 91c4980
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
Binary file modified crispy
Binary file not shown.
20 changes: 15 additions & 5 deletions src/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@
open Printf
open Lexing

let trim_str s =
let s' = String.sub s 1 (String.length s - 2) in
Str(s')
let remove_quotes s =
String.sub s 1 (String.length s - 2)

let t_fun_factory ts =

let rec factory acc ts =
match acc with
| t::[] ->
T_fun(acc, t)
| t::z ->
factory (t::acc) z
in factory [] ts

%}

%token UNIT
Expand Down Expand Up @@ -143,7 +153,7 @@ lit:
| INT { Int($1) }
| SUB INT { Int(-1 * $2) }
| BOOL { Bool($1) }
| STR { trim_str($1) }
| STR { Str(remove_quotes($1)) }
;


Expand All @@ -161,7 +171,7 @@ t_base:
| T_STR { T_str }

t_function:
| typ T_FN typ { $1 }
| typ T_FN typ { T_fun([$1], $3) }
;

typlist:
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions tests/functions/fn-args.crspy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apply = (f: (int >> int), x: int) >> {
f(x)
};

add1 = (x: int) >> {
x + 1
};

assert apply(add1, 10) == 11
7 changes: 7 additions & 0 deletions tests/functions/tuple-args.crspy
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fst = (t: (int, int)) >> { t[0] };
snd = (t: (int, int)) >> { t[1] };

x = (1, 2);

assert fst(x) == 1;
assert snd(x) == 2;

0 comments on commit 91c4980

Please sign in to comment.