| import Lean.Data.Json |
| import Lean.Environment |
|
|
| import Pantograph |
| import Repl |
|
|
| |
| open Pantograph.Repl |
| open Pantograph.Protocol |
|
|
| / |
| def printImmediate (s : String) : IO Unit := do |
| let stdout β IO.getStdout |
| stdout.putStr (s ++ "\n") |
| stdout.flush |
|
|
| / |
| def parseCommand (s: String): Except String Command := do |
| match s.trim.get? 0 with |
| | .some '{' => |
| |
| Lean.fromJson? (β Lean.Json.parse s) |
| | .some _ => |
| |
| let offset := s.posOf ' ' |> s.offsetOfPos |
| if offset = s.length then |
| return { cmd := s.take offset, payload := Lean.Json.null } |
| else |
| let payload β s.drop offset |> Lean.Json.parse |
| return { cmd := s.take offset, payload := payload } |
| | .none => |
| throw "Command is empty" |
|
|
| partial def loop : MainM Unit := do repeat do |
| let state β get |
| let command β (β IO.getStdin).getLine |
| |
| if command.trim.length = 0 then break |
| match parseCommand command with |
| | .error error => |
| let error := Lean.toJson ({ error := "command", desc := error }: InteractionError) |
| |
| printImmediate error.compress |
| | .ok command => |
| try |
| let ret β execute command |
| let str := match state.options.printJsonPretty with |
| | true => ret.pretty |
| | false => ret.compress |
| printImmediate str |
| catch e => |
| let message := e.toString |
| let error := Lean.toJson ({ error := "main", desc := message }: InteractionError) |
| printImmediate error.compress |
|
|
| def main (args: List String): IO Unit := do |
| |
| if args == ["--version"] then do |
| IO.println s!"{Pantograph.version}" |
| return |
|
|
| unsafe do |
| Pantograph.initSearch |
|
|
| |
| let (options, imports) := args.partition (Β·.startsWith "--") |
| let coreContext β options.map (Β·.drop 2) |>.toArray |> Pantograph.createCoreContext |
| let env β Lean.importModules |
| (imports := imports.toArray.map ({ module := Β·.toName })) |
| (opts := {}) |
| (trustLevel := 1) |
| (loadExts := true) |
| try |
| let mainM := loop.run { coreContext } |>.run' { env } |
| printImmediate "ready." |
| mainM |
| catch ex => |
| let message := ex.toString |
| let error := Lean.toJson ({ error := "io", desc := message }: InteractionError) |
| IO.println error.compress |
| |