Commit 52f533a0 authored by Julien Lopez's avatar Julien Lopez
Browse files

[TESTS][EVAL] Add constant string values; add pretty printer for eval

parent b0741904
expr = id
| integer
| string
| abstr
| expr "." expr
| expr "," expr
......
......@@ -2,6 +2,7 @@ open Printf
open Parse
open Lambda
open Auto_pat
open Value
let rec parse_to_lambda expr = (* TODO: Replace dummy_branches *)
let dummy_state = { uid=0; arity=[||]; actions=AIgnore(0,[||],0);
......@@ -23,6 +24,11 @@ let rec parse_to_lambda expr = (* TODO: Replace dummy_branches *)
Abstraction(params, [], dummy_branches, nbrparams, true, List([[]]))
| Var(vname) -> Var(Local(0)) (* TODO: Not supposed to be 0 *)
| Int(i) -> Const(Value.Integer(Intervals.V.from_int i))
| String(s) ->
let s = Ident.U.mk s in
let nil_atom = Atoms.V.mk_ascii "nil" in
String(Ident.U.start_index s, Ident.U.end_index s, s,
Const(Atom(nil_atom)))
| Pair(e1, e2) -> Pair(parse_to_lambda e1, parse_to_lambda e2)
| Match(e, branches) ->
Match(parse_to_lambda e, dummy_branches)
......@@ -35,14 +41,33 @@ let load_file f =
let s = String.create n in
really_input ic s 0 n;
close_in ic;
s;;
s
let rec print_value v = match v with
| Value.Pair(v1, v2) -> printf "("; print_value v1; printf ", ";
print_value v2; printf ")"
| Xml(_,_,_) -> printf "Xml"
| XmlNs(_,_,_,_) -> printf "XmlNs"
| Record(_) -> printf "Record"
| Atom(_) -> printf "Atom"
| Integer(i) -> printf "%d" (Big_int.int_of_big_int i)
| Char(i) -> printf "Char(%d)" i
| Abstraction(_, _) -> printf "Abstraction()"
| Abstract((name, _)) -> printf "Abstract(%s)" name
| String_latin1(i1, i2, s, v) -> printf "String_latin1(%d, %d, %s)" i1 i2 s;
print_value v
| String_utf8(_,_,s,_) -> printf "String(%s)" s
| Concat(v1, v2) -> printf "Concat("; print_value v1; printf ", ";
print_value v2; printf ")"
| Absent -> printf "Absent"
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 x -> x" in
| _ -> x . z (* That doesn't make any sense *)"*) "\"The cake is a lie\"" in
let expr = ExprParser.of_string str in
(*printf "Original: %s\nExpr: " str;
print_expr expr;
printf "\nResult: %s\n" (expr_to_string expr);*)
Eval.expr (parse_to_lambda expr) 100
let evalexpr = Eval.expr (parse_to_lambda expr) 100 in
print_value evalexpr; printf "\n"
......@@ -5,6 +5,7 @@ type expr =
| Abstract of string * string list * expr
| Var of string
| Int of int
| String of string
| Pair of expr * expr
| Match of expr * (expr * string option * expr) list
| Let of string * expr * expr;;
......@@ -34,9 +35,12 @@ module ExprParser = struct
| "paren"
[ "("; e = expression; ")" -> e ]
| "var"
[ x = LIDENT -> Var(x) ]
[ x = LIDENT -> Var(x)
| x = UIDENT -> Var(x) ]
| "int"
[ x = INT -> Int(int_of_string x) ]
| "string"
[ x = STRING -> String(x) ]
];
param:[[p = LIDENT -> p]];
......@@ -64,6 +68,7 @@ let rec print_expr expr = match expr with
print_params params; printf ", "; print_expr e; printf ")"
| Var(vname) -> printf "Var(%s)" vname
| Int(i) -> printf "Int(%d)" i
| String(s) -> printf "String(%s)" s
| Pair(e1, e2) -> printf "Pair("; print_expr e1; printf ", "; print_expr e2;
printf ")"
| Match(e, b) -> printf "Match("; print_expr e; printf ", ";
......@@ -88,6 +93,7 @@ let rec expr_to_string expr = match expr with
^ " -> " ^ (expr_to_string e)
| Var(vname) -> vname
| Int(i) -> string_of_int i
| String(s) -> "\"" ^ s ^ "\""
| Pair(e1, e2) -> "(" ^ (expr_to_string e1) ^ ", " ^ (expr_to_string e2)
^ ")"
| Match(e, b) -> "match " ^ (expr_to_string e) ^ " with"
......
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