Add print/println primitive

This commit is contained in:
백현웅 2022-02-21 02:17:29 +09:00
parent 1784f83887
commit de0d7bc009
2 changed files with 21 additions and 0 deletions

20
eval.ml
View file

@ -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

View file

@ -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)