From de0d7bc009f67716cb133482ebeeaf8ffdffe874 Mon Sep 17 00:00:00 2001 From: Hyeonung Baek Date: Mon, 21 Feb 2022 02:17:29 +0900 Subject: [PATCH] Add print/println primitive --- eval.ml | 20 ++++++++++++++++++++ main.ml | 1 + 2 files changed, 21 insertions(+) diff --git a/eval.ml b/eval.ml index ae42076..7e82ca8 100644 --- a/eval.ml +++ b/eval.ml @@ -204,6 +204,24 @@ module External = struct |> (fun a -> String (Parser.assoc_to_string a)) | _ -> failwith "get_op_assoc" + let print args = + let to_string = function + | Int n -> string_of_int n + | Float n -> string_of_float n + | Bool b -> string_of_bool b + | String s -> s + | Symbol s -> s + | _ -> failwith "print" + in + List.map to_string args + |> List.iter (Printf.printf "%s"); + Nop + + let println args = + ignore @@ print args; + Printf.printf "\n"; + Nop + let apply f args = let f = match f with | "sin" -> floatfun Float.sin @@ -215,6 +233,8 @@ module External = struct | "get_op_pre" -> get_op_pre | "set_op_assoc" -> set_op_assoc | "get_op_assoc" -> get_op_assoc + | "print" -> print + | "println" -> println | _ -> raise @@ Invalid f in f args diff --git a/main.ml b/main.ml index d3ae95d..61744b8 100644 --- a/main.ml +++ b/main.ml @@ -29,6 +29,7 @@ let stdlib = [ "deg"; "rad"; "get_op_pre"; "set_op_pre"; "get_op_assoc"; "set_op_assoc"; + "print"; "println"; ] |> List.to_seq |> Seq.map (fun v -> v, External v)