Pantograph/Main.lean

59 lines
1.4 KiB
Plaintext
Raw Normal View History

2023-05-09 16:39:24 -07:00
import Lean.Data.Json
2023-05-09 18:01:09 -07:00
import Pantograph.Commands
2023-05-07 15:19:45 -07:00
2023-05-09 16:39:24 -07:00
namespace Pantograph
structure State where
2023-05-09 18:01:09 -07:00
environments: Array String
2023-05-09 16:39:24 -07:00
-- State monad
abbrev T (m: Type → Type) := StateT State m
2023-05-09 18:01:09 -07:00
abbrev Subroutine := T (Except String) Lean.Json
structure Command where
cmd: String
payload: Lean.Json
deriving Lean.FromJson
--def create_environment (args: Create): Subroutine := do
-- let state ← get
2023-05-09 16:39:24 -07:00
end Pantograph
open Pantograph
2023-05-09 18:01:09 -07:00
def execute (command: String): T (ExceptT String IO) Lean.Json := do
2023-05-09 16:39:24 -07:00
let state ← get
let obj ← Lean.Json.parse command
2023-05-09 18:01:09 -07:00
let command: Command ← Lean.fromJson? obj
match command.cmd with
| "create" =>
let create: Commands.Create ← Lean.fromJson? command.payload
return Lean.toJson create.imports
| "catalog" =>
let catalog: Commands.Catalog ← Lean.fromJson? command.payload
return "catalog stub"
| cmd => throw s!"Unknown verb: {cmd}"
2023-05-09 16:39:24 -07:00
-- Main IO functions
unsafe def getLines : IO String := do
match (← (← IO.getStdin).getLine) with
| "" => pure ""
| "\n" => pure "\n"
| line => pure <| line ++ (← getLines)
unsafe def loop : T IO Unit := do
let state ← get
let command ← getLines
if command == "" then return ()
2023-05-09 18:01:09 -07:00
let ret ← execute command
match ret with
2023-05-09 16:39:24 -07:00
| .error e => IO.println s!"Could not parse json: {e}"
| .ok obj => IO.println <| toString <| obj
unsafe def main : IO Unit :=
StateT.run' loop ⟨#[]⟩