1 exception Exc of string
4 let rec ast_to_string ast = match ast with
6 | Ast.Int i -> string_of_int i
7 | Ast.Node ls -> "[" ^ String.concat " " (List.map ast_to_string ls) ^ "]"
10 (** interpreter for interp_int*)
11 class interp_int x_init =
13 val mutable x = x_init
14 method interp code = match code with
16 | Ast.Node [Ast.Leaf "+"; lhs; rhs] ->
17 let x1 = self#interp lhs in
18 let x2 = self#interp rhs in
20 | Ast.Node [Ast.Leaf "-"; lhs; rhs] ->
21 let x1 = self#interp lhs in
22 let x2 = self#interp rhs in
24 | Ast.Node [Ast.Leaf "-"; x] ->
25 let x1 = self#interp x in
27 | Ast.Node [Ast.Leaf "%apply" ; Ast.Leaf "read"; Ast.Int 0] ->
28 let input = read_int_opt () in
31 | _ -> raise (Exc "input invalid number")
32 | _ -> raise (Exc "unsupported")
41 let lexbuf = Lexing.from_channel stdin in
42 let interp = new interp_int 0 in
44 let result = Parser.main Lexer.token lexbuf in
45 Printf.printf "%d" (interp#interp result); print_newline(); flush stdout