2023-12-02 18:50:24 +00:00
|
|
|
module Ast = Spice_syntax.Ast
|
2023-11-29 21:21:35 +00:00
|
|
|
module Code = Spice_runtime.Code
|
2023-11-29 21:48:24 +00:00
|
|
|
module Value = Spice_runtime.Value
|
2023-11-29 18:43:14 +00:00
|
|
|
|
2023-12-07 01:30:42 +00:00
|
|
|
open Spice_syntax
|
|
|
|
open Spice_runtime
|
|
|
|
open Spice_compile
|
|
|
|
|
2023-11-29 18:43:14 +00:00
|
|
|
exception Error of string
|
|
|
|
|
2023-12-02 22:02:40 +00:00
|
|
|
let failf f =
|
|
|
|
Fmt.kstr (fun s -> raise (Error s)) f
|
2023-11-24 04:06:13 +00:00
|
|
|
|
|
|
|
let parse input =
|
|
|
|
let lexbuf = Lexing.from_string input ~with_positions:true in
|
2023-12-07 01:30:42 +00:00
|
|
|
try Parser.modl Lexer.read lexbuf
|
2023-12-02 22:02:40 +00:00
|
|
|
with
|
|
|
|
| Parser.Error -> failf "syntax error"
|
|
|
|
| Lexer.Error msg -> failf "syntax error: %s" msg
|
2023-11-29 18:43:14 +00:00
|
|
|
|
2023-12-13 21:40:44 +00:00
|
|
|
type program =
|
|
|
|
{ main : Code.funct }
|
|
|
|
|
2023-12-07 01:30:42 +00:00
|
|
|
let compile ast =
|
2023-12-13 21:40:44 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
main =
|
|
|
|
Ir.lower ast ~lib:Std.lib
|
|
|
|
|> Bcc.compile_lambda
|
|
|
|
}
|
|
|
|
with Ir.Error msg ->
|
2023-12-07 01:30:42 +00:00
|
|
|
failf "compile error: %s" msg
|
2023-11-29 21:50:26 +00:00
|
|
|
|
2023-12-13 21:40:44 +00:00
|
|
|
let run { main } =
|
|
|
|
try
|
|
|
|
let lib = Value.native_lib Std.lib in
|
|
|
|
let args = [] in
|
|
|
|
Interp.run main lib args
|
2023-12-07 01:30:42 +00:00
|
|
|
with Interp.Runtime_error msg ->
|
|
|
|
failf "runtime error: %s" msg
|