gluon_repl 0.5.0

REPL for gluon. A static, type inferred programming language for application embedding
let prelude = import! "std/prelude.glu"
let map = import! "std/map.glu"
let string = import! "std/string.glu"
let { Map } = map
let { pure, (*>) } = prelude.make_Applicative prelude.applicative_IO
let { (>>=), forM_ } = prelude.make_Monad prelude.monad_IO
let { Eq, Result, Monoid } = prelude
let { append = (++) } = string.monoid
let { singleton, find, monoid, to_list } = map.make string.ord
let { (<>), empty } = prelude.make_Monoid monoid


let load_file filename : String -> IO String =
    let last_slash =
        match string.rfind filename "/" with
        | None -> 0
        | Some i -> i + 1
    let modulename = string.slice filename last_slash (string.length filename - 3)
    let read_result =
        io.catch (io.read_file_to_string filename >>= \x -> pure (Ok x)) (\err -> pure (Err err))
    read_result
        >>= \result ->
        match result with
        | Ok expr -> io.load_script modulename expr
        | Err msg -> pure msg

type Cmd = { info : String, action : String -> IO Bool }

let commands : Map String Cmd =
    let print_result result =
        match result with
        | Ok x -> io.println x
        | Err x -> io.println x

    let commands = ref empty
    let cmds =
        singleton "q" { info = "Quit the REPL", action = \_ -> pure False } <> singleton "t" {
                    info = "Prints the type with an expression",
                    action = \arg -> repl_prim.type_of_expr arg >>= print_result *> pure True
                }
            <> singleton "i" {
                    info = "Prints information about the given name",
                    action = \arg -> repl_prim.find_info arg >>= print_result *> pure True
                }
            <> singleton "k" {
                    info = "Prints the kind with the given type",
                    action = \arg -> repl_prim.find_kind arg >>= print_result *> pure True
                }
            <> singleton "l" {
                    info = "Loads the file at \'folder/module.ext\' and stores it at \'module\'",
                    action = \arg -> load_file arg >>= io.println *> pure True
                }
            <> singleton "h" {
                    info = "Print this help",
                    action = \_ ->
                        io.println "Available commands\n"
                            *> forM_ (to_list (load commands)) (\cmd ->
                                let cmd : { key : String, value : Cmd } = cmd
                                io.println ("    :" ++ cmd.key ++ " " ++ cmd.value.info))
                            *> pure True
                }
    commands <- cmds
    load commands

let do_command line : String -> IO Bool =
    if string.length line >= 2 then
        let cmd = string.slice line 1 2
        let arg =
            if string.length line >= 3
            then string.trim (string.slice line 3 (string.length line))
            else ""
        match find cmd commands with
        | Some command -> command.action arg
        | None -> io.println ("Unknown command \'" ++ cmd ++ "\'") *> pure True
    else
        io.println "Expected a command such as `:h`"
            *> pure True

let store line : String -> IO Bool =
    let line = string.trim line
    match string.find line " " with
    | Some bind_end ->
        let binding = string.slice line 0 bind_end
        let expr = string.slice line bind_end (string.length line)
        io.load_script binding expr *> pure True
    | None -> io.println "Expected binding in definition" *> pure True

let loop editor : Editor -> IO () =
    let run_line line =
        if string.is_empty (string.trim line)
        then pure True
        else
            if string.starts_with line ":"
            then do_command line
            else io.catch (repl_prim.eval_line line) pure >>= io.println *> pure True

    rustyline.readline editor "> "
        >>= \line_opt ->
        match line_opt with
        | None -> pure ()
        | Some line -> run_line line >>= \continue -> if continue then loop editor else pure ()

let run x : () -> IO () =
    io.println "gluon (:h for help, :q to quit)"
        *> loop (rustyline.new_editor ())

run