Pantograph/Main.lean

64 lines
2.0 KiB
Plaintext
Raw Normal View History

2023-05-09 16:39:24 -07:00
import Lean.Data.Json
2023-05-09 22:51:19 -07:00
import Lean.Environment
2023-05-07 15:19:45 -07:00
2023-08-13 21:19:06 -07:00
import Pantograph.Version
import Pantograph
2023-05-09 22:51:19 -07:00
2023-05-09 16:39:24 -07:00
-- Main IO functions
2023-05-21 17:41:39 -07:00
open Pantograph
2023-05-09 16:39:24 -07:00
unsafe def loop : Subroutine Unit := do
let command ← (← IO.getStdin).getLine
if command.trim.length = 0 then return ()
match parse_command command with
| .error error =>
let error := Lean.toJson ({ error := "json", desc := error }: Commands.InteractionError)
IO.println (toString error)
| .ok command =>
let ret ← execute command
IO.println <| toString <| ret
2023-05-09 22:51:19 -07:00
loop
2023-05-09 16:39:24 -07:00
unsafe def main (args: List String): IO Unit := do
2023-08-13 21:19:06 -07:00
-- NOTE: A more sophisticated scheme of command line argument handling is needed.
-- Separate imports and options
if args == ["--version"] then do
println! s!"{version}"
return
2023-05-12 16:12:21 -07:00
Lean.enableInitializersExecution
2023-05-12 01:08:36 -07:00
Lean.initSearchPath (← Lean.findSysroot)
2023-08-13 21:19:06 -07:00
let options? ← args.filterMap (λ s => if s.startsWith "--" then .some <| s.drop 2 else .none)
|>.foldlM Lean.setOptionFromString'' Lean.Options.empty
|>.run
let options ← match options? with
| .ok options => pure options
| .error e => throw $ IO.userError s!"Options cannot be parsed: {e}"
let imports:= args.filter (λ s => ¬ (s.startsWith "--"))
let env ← Lean.importModules
(imports := imports.map (λ str => { module := str_to_name str, runtimeOnly := false }))
(opts := {})
(trustLevel := 1)
let context: Context := {
}
let coreContext: Lean.Core.Context := {
2023-08-13 21:19:06 -07:00
currNamespace := Lean.Name.str .anonymous "Aniva"
openDecls := [], -- No 'open' directives needed
fileName := "<Pantograph>",
fileMap := { source := "", positions := #[0], lines := #[1] },
2023-08-13 21:19:06 -07:00
options := options
}
2023-05-21 17:41:39 -07:00
try
let termElabM := loop.run context |>.run' {}
let metaM := termElabM.run' (ctx := {
declName? := some "_pantograph",
errToSorry := false
})
let coreM := metaM.run'
discard <| coreM.toIO coreContext { env := env }
2023-05-21 17:41:39 -07:00
catch ex =>
IO.println "Uncaught IO exception"
IO.println ex.toString