Commit a74737ff authored by Pietro Abate's avatar Pietro Abate
Browse files

Minor code refactory for lambda test parser

parent 344ce554
......@@ -13,7 +13,7 @@ let type_of_string s = match s with
| "Char" -> Builtin_defs.char
| "Bool" -> Builtin_defs.bool
| "Any" -> Builtin_defs.any
| _ -> Types.empty
| _ -> failwith (Printf.sprintf "Type %s not recognized" s)
let rec _to_typed env l expr =
let open Typed in
......@@ -253,17 +253,7 @@ and parse_match_value env l list toptype = function
(type_of_ptype toptype) in
(t, Patterns.Constr(t), list, l, is_subtype)
| MBool (origloc, b) ->
let t = match b with
| "true" -> Builtin_defs.true_type
| "false" -> Builtin_defs.false_type
| _ ->
let line = Loc.start_line origloc in
let cbegin = Loc.start_off origloc - Loc.start_bol origloc in
let cend = Loc.stop_off origloc - Loc.start_bol origloc in
Printf.eprintf
"File %s, line %d, characters %d-%d:\nError: Unknown special term %s\n"
(Loc.file_name origloc) line cbegin cend b;
raise Error in
let t = if b then Builtin_defs.true_type else Builtin_defs.false_type in
let is_subtype = Types.subtype t (type_of_ptype toptype) in
(t, Patterns.Constr(t), list, l, is_subtype)
......
......@@ -20,7 +20,7 @@ and match_value =
| MVar of Loc.t * string * ptype
| MInt of Loc.t * int
| MString of Loc.t * string
| MBool of Loc.t * string
| MBool of Loc.t * bool
and ptype =
| Type of string
| PType of string * (string * ptype) list list
......@@ -63,12 +63,12 @@ module ExprParser = struct
| "let"; x = LIDENT; ":"; t = type_id; "="; v = SELF; "in"; e = SELF;
":"; te = type_id -> Match(_loc, v, t, [_loc, MVar(_loc, x, t), e])
| "if"; e1 = SELF; "then"; e2 = SELF ->
let b = [(_loc, MBool(_loc, "true"), e2);
(_loc, MBool(_loc, "false"), Var(_loc, "`nil"))] in
let b = [(_loc, MBool(_loc, true), e2);
(_loc, MBool(_loc, false), Var(_loc, "`nil"))] in
Match(_loc, e1, Type("Bool"), b)
| "if"; e1 = SELF; "then"; e2 = SELF; "else"; e3 = SELF ->
let b = [(_loc, MBool(_loc, "true"), e2);
(_loc, MBool(_loc, "false"), e3)] in
let b = [(_loc, MBool(_loc, true), e2);
(_loc, MBool(_loc, false), e3)] in
Match(_loc, e1, Type("Bool"), b)
| "match"; e = SELF; ":"; t = type_id; "with"; b = LIST1 branch ->
Match(_loc, e, t, b) ]
......@@ -118,7 +118,20 @@ module ExprParser = struct
| "var" [ x = LIDENT; ":"; t = type_id -> MVar(_loc, x, t) ]
| "int" [ x = INT -> MInt(_loc, int_of_string x) ]
| "string" [ x = STRING -> MString(_loc, x) ]
| "bool" [ "`"; x = LIDENT -> MBool(_loc, x) ]
| "bool" [ "`"; x = LIDENT ->
try MBool(_loc, bool_of_string x)
with Invalid_argument "bool_of_string" -> begin
let line = Loc.start_line _loc in
let cbegin = Loc.start_off _loc - Loc.start_bol _loc in
let cend = Loc.stop_off _loc - Loc.start_bol _loc in
let err =
Printf.sprintf
"File %s, line %d, characters %d-%d:\nError: Unknown special term %s\n"
(Loc.file_name _loc) line cbegin cend x
in
raise (Stream.Error err)
end
]
| "empty" [ "["; "]" -> MVar(_loc, "`nil", Type("Any")) ]
];
......
......@@ -19,7 +19,7 @@ and match_value =
| MVar of Loc.t * string * ptype
| MInt of Loc.t * int
| MString of Loc.t * string
| MBool of Loc.t * string
| MBool of Loc.t * bool
and ptype =
| Type of string
| PType of string * (string * ptype) list list
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment