Commit 7fb71a7f authored by Julien Lopez's avatar Julien Lopez
Browse files

[TESTS][EVAL] Grammar modified: not all expressions can fit in the left part of

	branches in a match now, only variables, pairs and constants
parent 1ca15941
......@@ -5,13 +5,19 @@ expr = id
| expr "." expr
| expr "," expr
| "(" expr ")"
| "match" expr "with" "|" expr ["&" type_id] "->" expr branches
| "match" expr "with" "|" match_value ["&" type_id] "->" expr branches
| "let" id "=" expr (* TODO *)
| "let" id "=" expr "in" expr
| expr ";;" expr (* TODO *)
abstr = "fun" id params "->" expr
match_value = id
| integer
| string
| expr "," expr
| "(" expr ")"
params = (* empty *)
| id params
......
......@@ -31,7 +31,7 @@ let rec print_value v = match v with
let str = if Array.length Sys.argv > 1 then load_file Sys.argv.(1)
else (*"let z = 3 in fun firsts x y -> match x,y with
| (a,_),(b,_) -> a,b (* This (* is (* a nested *) *) comment *)
| _ -> x . z (* That doesn't make any sense *)"*) "(fun f x y -> x, y), (fun f x -> x)" in
| _ -> x . z (* That doesn't make any sense *)"*) "match x with | _ -> x" in
let expr = ExprParser.of_string str in
(*printf "Original: %s\nExpr: " str;
print_expr expr;
......
......@@ -7,9 +7,15 @@ type expr =
| Int of int
| String of string
| Pair of expr * expr
| Match of expr * (expr * string option * expr) list
| Match of expr * (match_value * string option * expr) list
| Let of string * expr * expr
and match_value =
| MVar of string
| MInt of int
| MString of string
| MPair of match_value * match_value
module ExprParser = struct
open Camlp4.PreCast
......@@ -24,10 +30,14 @@ module ExprParser = struct
[
"letexpr"
[ "let"; x = LIDENT; "="; e1 = expression; "in"; e2 = expression ->
Let(x, e1, e2)]
Let(x, e1, e2)
| "let"; x = UIDENT; "="; e1 = expression; "in"; e2 = expression ->
Let(x, e1, e2) ]
| "abstr" RIGHTA
[ "fun"; x = LIDENT; p = LIST0 param; "->"; e = expression ->
Abstract(x, p, e)
| "fun"; x = UIDENT; p = LIST0 param; "->"; e = expression ->
Abstract(x, p, e)
| "match"; e1 = expression; "with"; b = LIST1 branch -> Match(e1, b) ]
| "pair" LEFTA
[ e1 = expression; ","; e2 = expression -> Pair(e1,e2)
......@@ -43,15 +53,32 @@ module ExprParser = struct
[ x = STRING -> String(x) ]
];
param:[[p = LIDENT -> p]];
param:[[p = LIDENT -> p | p = UIDENT -> p]];
branch:
[
"branches" LEFTA
[ "|"; t = expression; "->"; e = expression -> (t, None, e)
| "|"; t = expression; "&"; x = LIDENT; "->"; e = expression ->
[ "|"; t = match_value; "->"; e = expression -> (t, None, e)
| "|"; t = match_value; "&"; x = LIDENT; "->"; e = expression ->
(t, Some x, e)
| "|"; t = match_value; "&"; x = UIDENT; "->"; e = expression ->
(t, Some x, e) ]
];
match_value:
[
"pair" LEFTA
[ e1 = match_value; ","; e2 = match_value -> MPair(e1,e2) ]
| "paren"
[ "("; e = match_value; ")" -> e ]
| "var"
[ x = LIDENT -> MVar(x)
| x = UIDENT -> MVar(x) ]
| "int"
[ x = INT -> MInt(int_of_string x) ]
| "string"
[ x = STRING -> MString(x) ]
];
END;;
let of_string s = Gram.parse_string exp_eoi (Loc.mk "<string>") s
......@@ -79,11 +106,18 @@ and print_params params = match params with
| [] -> ()
and print_branches b = match b with
| (br, None, exp) :: rest -> printf "("; print_expr br; printf ", ";
| (br, None, exp) :: rest -> printf "("; print_mvalues br; printf ", ";
print_expr exp; printf ")"; print_branches rest
| (br, Some x, exp) :: rest -> printf "("; print_expr br; printf " & %s, " x;
print_expr exp; printf ")"; print_branches rest
| [] -> ();;
| (br, Some x, exp) :: rest -> printf "("; print_mvalues br;
printf " & %s, " x; print_expr exp; printf ")"; print_branches rest
| [] -> ()
and print_mvalues mv = match mv with
| MVar(vname) -> printf "Var(%s)" vname
| MInt(i) -> printf "Int(%d)" i
| MString(s) -> printf "String(%s)" s
| MPair(e1, e2) -> printf "Pair("; print_mvalues e1; printf ", ";
print_mvalues e2; printf ")"
let rec expr_to_string expr = match expr with
| Apply(e1, e2) -> (expr_to_string e1) ^ " . " ^ (expr_to_string e2)
......@@ -104,8 +138,15 @@ and params_to_string params = match params with
| [] -> ""
and branches_to_string b = match b with
| (br, None, exp) :: rest -> "\n| " ^ (expr_to_string br) ^ " -> "
| (br, None, exp) :: rest -> "\n| " ^ (mvalues_to_string br) ^ " -> "
^ (expr_to_string exp) ^ (branches_to_string rest)
| (br, Some x, exp) :: rest -> "\n| " ^ (expr_to_string br) ^ " & " ^ x
| (br, Some x, exp) :: rest -> "\n| " ^ (mvalues_to_string br) ^ " & " ^ x
^ " -> " ^ (expr_to_string exp) ^ (branches_to_string rest)
| [] -> "";;
| [] -> ""
and mvalues_to_string mv = match mv with
| MVar(vname) -> vname
| MInt(i) -> string_of_int i
| MString(s) -> "\"" ^ s ^ "\""
| MPair(e1, e2) -> "(" ^ (mvalues_to_string e1) ^ ", "
^ (mvalues_to_string e2) ^ ")"
......@@ -5,9 +5,15 @@ type expr =
| Int of int
| String of string
| Pair of expr * expr
| Match of expr * (expr * string option * expr) list
| Match of expr * (match_value * string option * expr) list
| Let of string * expr * expr
and match_value =
| MVar of string
| MInt of int
| MString of string
| MPair of match_value * match_value
module ExprParser : sig
val of_string : string -> expr
......
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