Skip to content

Commit 7f2be2a

Browse files
committed
typeof builtin
1 parent f8df76d commit 7f2be2a

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

lib/eval.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,19 @@ and map_to_list_builtin (args, state) _ss _loc =
332332
ValList ls, state
333333
| _ -> assert false
334334

335+
and typeof_builtin (args, state) _ss _loc =
336+
match args with
337+
| Tuple [Number _] -> Atom 2, state
338+
| Tuple [Integer _] -> Atom 3, state
339+
| Tuple [Boolean _] -> Atom 4, state
340+
| Tuple [Tuple _] -> Atom 5, state
341+
| Tuple [ValList _] -> Atom 6, state
342+
| Tuple [(Lambda _) | (LambdaCapture _) | (Fn _)] -> Atom 7, state
343+
| Tuple [Dictionary _] -> Atom 8, state
344+
| Tuple [Atom _] -> Atom 9, state
345+
| Tuple [StringVal _] -> Atom 10, state
346+
| _ -> assert false
347+
335348
and eval_pipe ~tc lhs rhs ss loc = fun s ->
336349
let (lhs, s) = (eval_expr lhs ss) s in
337350
let (rhs, s) = (eval_expr rhs ss) s in
@@ -525,6 +538,7 @@ and eval_lambda_call ?tc:(tail_call=false) call ss loc =
525538
| ResolvedIdent 13 -> list_dir_builtin ((eval_expr call.call_args ss) state) ss loc
526539
| ResolvedIdent 14 -> map_keys_builtin ((eval_expr call.call_args ss) state) ss loc
527540
| ResolvedIdent 15 -> map_to_list_builtin ((eval_expr call.call_args ss) state) ss loc
541+
| ResolvedIdent 16 -> typeof_builtin ((eval_expr call.call_args ss) state) ss loc
528542
| UnresolvedIdent s ->
529543
printf "Error: unresolved function %s not found at %s\n" s (location_to_string loc);
530544
print_traceback ss;

lib/run.ml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,19 @@ let run_file filename (ss, state) =
9393
let in_string = In_channel.input_all in_stream in
9494
run_string in_string filename (ss, state)
9595

96-
let base_static_atoms () = [("ok", 0); ("err", 1)]
96+
let base_static_atoms () = [
97+
("ok", 0);
98+
("err", 1);
99+
("number", 2);
100+
("integer", 3);
101+
("boolean", 4);
102+
("tuple", 5);
103+
("list", 6);
104+
("function", 7);
105+
("dictionary", 8);
106+
("atom", 9);
107+
("string", 10);
108+
]
97109

98110
let base_static_idents () =
99111
let builtin_idents = [
@@ -113,6 +125,7 @@ let base_static_idents () =
113125
"list_dir__builtin";
114126
"map_keys__builtin";
115127
"map_to_list__builtin";
128+
"typeof__builtin";
116129
] in
117130
List.zip_exn builtin_idents (List.range 0 (List.length builtin_idents))
118131

lib/stdlib.rsc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,4 @@ let write_file(file, data) = write_file__builtin(file, data)
233233
let list_dir(dir) = list_dir__builtin(dir)
234234
let map_keys(m) = map_keys__builtin(m)
235235
let map_to_list(m) = map_to_list__builtin(m)
236+
let typeof(m) = typeof__builtin(m)

0 commit comments

Comments
 (0)