chore: Cleanup REPL loop

This commit is contained in:
Leni Aniva 2025-01-24 19:22:58 -08:00
parent 976646fb67
commit 787c9e606d
Signed by: aniva
GPG Key ID: 4D9B1C8D10EA4C50
3 changed files with 13 additions and 16 deletions

View File

@ -10,22 +10,25 @@ open Pantograph.Protocol
/-- Parse a command either in `{ "cmd": ..., "payload": ... }` form or `cmd { ... }` form. -/ /-- Parse a command either in `{ "cmd": ..., "payload": ... }` form or `cmd { ... }` form. -/
def parseCommand (s: String): Except String Command := do def parseCommand (s: String): Except String Command := do
let s := s.trim match s.trim.get? 0 with
match s.get? 0 with | .some '{' =>
| .some '{' => -- Parse in Json mode -- Parse in Json mode
Lean.fromJson? (← Lean.Json.parse s) Lean.fromJson? (← Lean.Json.parse s)
| .some _ => -- Parse in line mode | .some _ =>
-- Parse in line mode
let offset := s.posOf ' ' |> s.offsetOfPos let offset := s.posOf ' ' |> s.offsetOfPos
if offset = s.length then if offset = s.length then
return { cmd := s.take offset, payload := Lean.Json.null } return { cmd := s.take offset, payload := Lean.Json.null }
else else
let payload ← s.drop offset |> Lean.Json.parse let payload ← s.drop offset |> Lean.Json.parse
return { cmd := s.take offset, payload := payload } return { cmd := s.take offset, payload := payload }
| .none => throw "Command is empty" | .none =>
throw "Command is empty"
partial def loop : MainM Unit := do repeat do partial def loop : MainM Unit := do repeat do
let state ← get let state ← get
let command ← (← IO.getStdin).getLine let command ← (← IO.getStdin).getLine
-- Halt the program if empty line is given
if command.trim.length = 0 then break if command.trim.length = 0 then break
match parseCommand command with match parseCommand command with
| .error error => | .error error =>
@ -47,20 +50,17 @@ partial def loop : MainM Unit := do repeat do
unsafe def main (args: List String): IO Unit := do unsafe def main (args: List String): IO Unit := do
-- NOTE: A more sophisticated scheme of command line argument handling is needed. -- NOTE: A more sophisticated scheme of command line argument handling is needed.
-- Separate imports and options
if args == ["--version"] then do if args == ["--version"] then do
IO.println s!"{Pantograph.version}" IO.println s!"{Pantograph.version}"
return return
Pantograph.initSearch "" Pantograph.initSearch ""
let coreContext ← args.filterMap (λ s => if s.startsWith "--" then .some <| s.drop 2 else .none) -- Separate imports and options
|>.toArray |> Pantograph.createCoreContext let (options, imports) := args.partition (·.startsWith "--")
let imports:= args.filter (λ s => ¬ (s.startsWith "--")) let coreContext ← options.map (·.drop 2) |>.toArray |> Pantograph.createCoreContext
let coreState ← Pantograph.createCoreState imports.toArray let coreState ← Pantograph.createCoreState imports.toArray
let context: Context := { let context: Context := {}
imports
}
try try
let coreM := loop.run context |>.run' {} let coreM := loop.run context |>.run' {}
IO.println "ready." IO.println "ready."

View File

@ -4,7 +4,6 @@ import Pantograph
namespace Pantograph.Repl namespace Pantograph.Repl
structure Context where structure Context where
imports: List String
/-- Stores state of the REPL -/ /-- Stores state of the REPL -/
structure State where structure State where

View File

@ -235,9 +235,7 @@ def test_frontend_process_sorry : Test :=
def runTest (env: Lean.Environment) (steps: Test): IO LSpec.TestSeq := do def runTest (env: Lean.Environment) (steps: Test): IO LSpec.TestSeq := do
-- Setup the environment for execution -- Setup the environment for execution
let context: Context := { let context: Context := {}
imports := ["Init"]
}
let commands: MainM LSpec.TestSeq := let commands: MainM LSpec.TestSeq :=
steps.foldlM (λ suite step => do steps.foldlM (λ suite step => do
let result ← step let result ← step