Skip to content

Commit

Permalink
Runtime error as a special exception
Browse files Browse the repository at this point in the history
  • Loading branch information
ppolesiuk committed Nov 25, 2024
1 parent f43a86e commit 46453dc
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/Base/Assert.fram
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub let runtimeError {type T} =
(extern dbl_runtimeError : String ->[] T)

(** Explicitly assert, that this case is impossible *)
pub let assertImpossible {?msg : String} () =
pub let impossible {?msg : String} () =
runtimeError
match msg with
| None => "Assertion failed"
Expand All @@ -25,4 +25,4 @@ pub let assertImpossible {?msg : String} () =
pub let assert {?msg} b =
if b then ()
else
assertImpossible {?msg} ()
impossible {?msg} ()
4 changes: 3 additions & 1 deletion src/Eval/Eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

open Value

exception Runtime_error = External.Runtime_error

(** Evaluator *)
(* ========================================================================= *)

Expand Down Expand Up @@ -90,7 +92,7 @@ let rec eval_expr env (e : Lang.Untyped.expr) cont =
end
| ERepl func -> eval_repl env func cont
| EReplExpr(e1, tp, e2) ->
Printf.printf ": %s\n" tp;
Printf.printf ": %s\n%!" tp;
eval_expr env e1 (fun v1 ->
Printf.printf "= %s\n" (to_string v1);
eval_expr env e2 cont)
Expand Down
2 changes: 2 additions & 0 deletions src/Eval/Eval.mli
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

(** Evaluator *)

exception Runtime_error

val eval_program : Lang.Untyped.program -> unit
17 changes: 10 additions & 7 deletions src/Eval/External.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,32 @@

open Value

exception Runtime_error

(** External functions *)
(* ========================================================================= *)

let runtime_error msg =
Printf.eprintf "Runtime error: %s\n%!" msg;
raise Runtime_error

let unit_fun f = VFn (fun v cont -> cont (f ()))

let int_fun f = VFn (fun v cont ->
match v with
| VNum n -> cont (f n)
| _ -> failwith "Runtime error!")
| _ -> runtime_error "Not an integer")

let str_fun f = VFn (fun v cont ->
match v with
| VStr s -> cont (f s)
| _ -> failwith "Runtime error!")
| _ -> runtime_error "Not a string")

let list_chr_fun f = VFn (fun v cont ->
let rec parse_list = function
| VCtor(0, []) -> []
| VCtor(1, [VNum x; xs]) -> Char.chr x :: parse_list xs
| _ -> failwith "Runtime error!" in
| _ -> runtime_error "Not a list" in
cont (f @@ parse_list v))

let v_unit = VCtor(0, [])
Expand All @@ -44,7 +50,7 @@ let int_cmpop op = int_fun2 (fun x y -> of_bool (op x y))
let int64_fun f = VFn (fun v cont ->
match v with
| VNum64 n -> cont (f n)
| _ -> failwith "Runtime error!")
| _ -> failwith "Not a 64-bit integer")

let int64_fun2 f = int64_fun (fun x -> int64_fun (f x))

Expand All @@ -55,9 +61,6 @@ let int64_cmpop op = int64_fun2 (fun x y -> of_bool (op x y))

let str_cmpop op = str_fun (fun s1 -> str_fun (fun s2 -> of_bool (op s1 s2)))

let runtime_error msg =
failwith ("Runtime error: " ^ msg)

let extern_map =
[ "dbl_runtimeError", str_fun runtime_error;
"dbl_negInt", int_unop ( ~- );
Expand Down
1 change: 1 addition & 0 deletions src/dbl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ let _ =
| Some fname -> Pipeline.run_file fname
with
| InterpLib.Error.Fatal_error -> exit 1
| Eval.Runtime_error -> exit 2

0 comments on commit 46453dc

Please sign in to comment.