spice/lib/spice.ml

30 lines
697 B
OCaml

module Ast = Spice_syntax.Ast
module Code = Spice_runtime.Code
module Value = Spice_runtime.Value
open Spice_syntax
open Spice_runtime
open Spice_compile
exception Error of string
let failf f =
Fmt.kstr (fun s -> raise (Error s)) f
let parse input =
let lexbuf = Lexing.from_string input ~with_positions:true in
try Parser.modl Lexer.read lexbuf
with
| Parser.Error -> failf "syntax error"
| Lexer.Error msg -> failf "syntax error: %s" msg
let compile ast =
try Bcc.compile ast Std.lib
with Bcc.Error msg ->
failf "compile error: %s" msg
let run prog =
try Interp.run prog (Value.native_lib Std.lib)
with Interp.Runtime_error msg ->
failf "runtime error: %s" msg