Skip to content

Commit 94425f8

Browse files
lean: simplify main
fix running with cli args fix step8 macro
1 parent 25bb6e9 commit 94425f8

File tree

12 files changed

+147
-115
lines changed

12 files changed

+147
-115
lines changed

impls/lean/LeanMal/core.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,12 @@ def countFunc(env : Env) (lst: List Types) : IO (Env × Types) := do
236236
| Types.Nil => return (env, Types.intVal 0)
237237
| _ => throw (IO.userError "count called on non-sequence")
238238

239-
def readString (lst: List Types) (envir: Env) : IO Types := do
239+
def readString (lst: List Types) (_: Env) : IO Types := do
240240
if lst.length < 1 then throw (IO.userError "read-string: 1 arguments required")
241241
else
242242
let first := lst[0]!
243243
match first with
244-
| Types.strVal v => match read_types_with_env v envir.getDict with -- Dict.empty
244+
| Types.strVal v => match read_types_with_env v with -- Dict.empty
245245
| Except.error e => throw (IO.userError e)
246246
| Except.ok res => return res
247247
| x => throw (IO.userError s!"unexpected symbol: {x.toString true}, expected: string")

impls/lean/LeanMal/reader.lean

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ def read_comment : Parsec Unit := do
163163
pure ()
164164

165165
mutual
166-
partial def read_list (envir: Dict := Dict.empty) : Parsec Types := do
166+
partial def read_list : Parsec Types := do
167167
-- ws
168168
let _ ← optional wspace_or_comma_strict
169169
let _ ← pstring "("
170170
let _ ← optional wspace_or_comma_strict
171171
let els ← many (do
172-
let e ← read_types envir
172+
let e ← read_types
173173
let _ ← optional wspace_or_comma_strict
174174
-- let _ ← optional (pchar ',')
175175
return e)
@@ -179,12 +179,12 @@ mutual
179179
let _ ← optional wspace_or_comma_strict
180180
return Types.listVal (els.toList)
181181

182-
partial def read_vector (envir: Dict := Dict.empty) : Parsec Types := do
182+
partial def read_vector : Parsec Types := do
183183
let _ ← optional wspace_or_comma_strict
184184
let _ ← pchar '['
185185
let _ ← optional wspace_or_comma_strict
186186
let els ← many (do
187-
let e ← read_types envir
187+
let e ← read_types
188188
let _ ← optional wspace_or_comma_strict
189189
-- let _ ← optional (pchar ',')
190190
return e)
@@ -195,7 +195,7 @@ mutual
195195
let vec := listToVec vecLst
196196
return Types.vecVal vec
197197

198-
partial def read_hash_map (_: Dict := Dict.empty) : Parsec Types := do
198+
partial def read_hash_map : Parsec Types := do
199199
let _ ← optional wspace_or_comma_strict
200200
let _ ← pchar '{'
201201
let _ ← optional wspace_or_comma_strict
@@ -222,21 +222,21 @@ mutual
222222
| Types.strVal v => return (KeyType.strKey v, value)
223223
| _ => default
224224

225-
partial def read_symbol (chars: String) (name: String) (envir: Dict := Dict.empty) : Parsec Types := do
225+
partial def read_symbol (chars: String) (name: String) : Parsec Types := do
226226
let _ ← optional wspace_or_comma_strict
227227
let _ ← pstring chars
228-
let elem ← read_types envir
228+
let elem ← read_types
229229
let _ ← optional wspace_or_comma_strict
230230

231231
let vecLst := [(Types.symbolVal name), elem]
232232
return Types.listVal vecLst
233233

234-
partial def read_with_meta (envir: Dict := Dict.empty) : Parsec Types := do
234+
partial def read_with_meta : Parsec Types := do
235235
ws
236236
let _ ← pstring "^"
237237

238238
let els ← many (do
239-
let e ← read_types envir
239+
let e ← read_types
240240
ws
241241
let _ ← optional (pchar ',')
242242
return e)
@@ -245,22 +245,22 @@ mutual
245245
let vecLst := (Types.symbolVal "with-meta") :: elsVec
246246
return Types.listVal (List.append vecLst elsVec)
247247

248-
partial def read_atom (_: Dict := Dict.empty) : Parsec Types :=
248+
partial def read_atom : Parsec Types :=
249249
read_operator_or_number <|> read_float_or_int <|> read_str_val <|> read_keyword <|> read_nil_val <|> read_bool_val <|> read_symbol_val
250250

251-
partial def read_types (envir: Dict := Dict.empty) : Parsec Types := do
251+
partial def read_types : Parsec Types := do
252252
let _ ← optional wspace
253253
let _ ← optional (many read_comment)
254254
match ← peek? with
255255
| none => fail "endofinput"
256256
| some _ =>
257-
read_list envir <|> read_vector envir <|> read_hash_map envir <|> read_symbol "'" "quote" envir <|> read_symbol "`" "quasiquote" envir <|> read_symbol "~@" "splice-unquote" envir <|> read_symbol "~" "unquote" envir <|> read_symbol "@" "deref" envir <|> read_with_meta envir <|> read_atom envir
257+
read_list <|> read_vector <|> read_hash_map <|> read_symbol "'" "quote" <|> read_symbol "`" "quasiquote" <|> read_symbol "~@" "splice-unquote" <|> read_symbol "~" "unquote" <|> read_symbol "@" "deref" <|> read_with_meta <|> read_atom
258258
end
259259

260-
def read_types_with_env (input : String) (envir: Dict := Dict.empty) : Except String Types :=
261-
match read_types envir input.trim.iter with
260+
def read_types_with_env (input : String) : Except String Types :=
261+
match read_types input.trim.iter with
262262
| Lean.Parsec.ParseResult.success _ res => Except.ok res
263263
| Lean.Parsec.ParseResult.error _ err => Except.error err
264264

265265
def read_str (input : String) : Except String Types :=
266-
read_types_with_env input (Dict.empty : Dict.{u})
266+
read_types_with_env input

impls/lean/LeanMal/step1_read_print.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import LeanMal.printer
44
universe u
55

66
def READ (input : String) :=
7-
read_str.{u} input
7+
read_str input
88

99
def EVAL (ast : Types) (_: String) := ast
1010

1111
def PRINT (ast : Types): String :=
1212
pr_str true ast
1313

1414
def rep (input : String): String :=
15-
match READ.{u} input with
15+
match READ input with
1616
| Except.ok result =>
1717
PRINT (EVAL result "")
1818
| Except.error err =>
@@ -31,4 +31,4 @@ def main : IO Unit := do
3131
if value.isEmpty then
3232
donext := false
3333
else
34-
IO.println (rep.{u} value)
34+
IO.println (rep value)

impls/lean/LeanMal/step2_eval.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import LeanMal.printer
44
universe u
55

66
def READ (input : String): Except String Types :=
7-
read_str.{u} input
7+
read_str input
88

99
def sum (env : Env) (lst: List Types) : IO (Env × Types) := do
1010
match lst with
@@ -121,7 +121,7 @@ def PRINT (ast : Types): String :=
121121
pr_str true ast
122122

123123
def rep (input : String): IO String := do
124-
match READ.{u} input with
124+
match READ input with
125125
| Except.ok result =>
126126
try
127127
let (_, res) ← evalTypes (Env.data 0 Dict.empty) result
@@ -144,5 +144,5 @@ def main : IO Unit := do
144144
if value.isEmpty then
145145
donext := false
146146
else
147-
let output ← rep.{u} value
147+
let output ← rep value
148148
IO.println output

impls/lean/LeanMal/step3_env.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import LeanMal.types
55
universe u
66

77
def READ (input : String): Except String Types :=
8-
read_str.{u} input
8+
read_str input
99

1010
def sum (env : Env) (lst: List Types) : IO (Env × Types) := do
1111
match lst with
@@ -173,7 +173,7 @@ def PRINT (ast : Types): String :=
173173
pr_str true ast
174174

175175
def rep (env: Env) (input : String): IO (Env × String) := do
176-
match READ.{u} input with
176+
match READ input with
177177
| Except.ok result =>
178178
try
179179
let (newenv, res) ← evalTypes env result
@@ -201,5 +201,5 @@ def main : IO Unit := do
201201
if value.isEmpty then
202202
donext := false
203203
else
204-
let (newenv, value) ← rep.{u} env value
204+
let (newenv, value) ← rep env value
205205
env ← repAndPrint newenv value

impls/lean/LeanMal/step4_if_fn_do.lean

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,13 @@ mutual
194194
end
195195

196196
def READ (input : String): Except String Types :=
197-
read_str.{u} input
197+
read_str input
198198

199199
def PRINT (ast : Types): String :=
200200
pr_str true ast
201201

202202
def rep (env: Env) (input : String): IO (Env × String) := do
203-
match READ.{u} input with
203+
match READ input with
204204
| Except.ok result =>
205205
try
206206
let (newenv, res) ← evalTypes env result
@@ -212,7 +212,7 @@ def rep (env: Env) (input : String): IO (Env × String) := do
212212
def loadMalFns (env: Env) (fndefs: List String): IO (Env × String) := do
213213
fndefs.foldlM (fun (res : Env × String) fndef => do
214214
let (ref, msg) := res
215-
let (newref, newmsg) ← rep.{u} ref fndef
215+
let (newref, newmsg) ← rep ref fndef
216216
return (newref, s!"{msg}¬{newmsg}")
217217
) (env, "")
218218

@@ -226,7 +226,7 @@ def repAndPrint (env: Env) (output : String): IO Env := do
226226
return env
227227

228228
def main : IO Unit := do
229-
let (env0, _) ← loadMalFns.{u} (loadFnNativeAll (Env.data 0 Dict.empty)) fnDefs
229+
let (env0, _) ← loadMalFns (loadFnNativeAll (Env.data 0 Dict.empty)) fnDefs
230230
let mut env := env0
231231
let mut donext := true
232232
while donext do
@@ -240,5 +240,5 @@ def main : IO Unit := do
240240
if value.isEmpty then
241241
donext := false
242242
else
243-
let (newenv, value) ← rep.{u} env value
243+
let (newenv, value) ← rep env value
244244
env ← repAndPrint newenv value

impls/lean/LeanMal/step5_tco.lean

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,13 @@ mutual
193193
end
194194

195195
def READ (input : String): Except String Types :=
196-
read_str.{u} input
196+
read_str input
197197

198198
def PRINT (ast : Types): String :=
199199
pr_str true ast
200200

201201
def rep (env: Env) (input : String): IO (Env × String) := do
202-
match READ.{u} input with
202+
match READ input with
203203
| Except.ok result =>
204204
try
205205
let (newenv, res) ← evalTypes env result
@@ -211,7 +211,7 @@ def rep (env: Env) (input : String): IO (Env × String) := do
211211
def loadMalFns (env: Env) (fndefs: List String): IO (Env × String) := do
212212
fndefs.foldlM (fun (res : Env × String) fndef => do
213213
let (ref, msg) := res
214-
let (newref, newmsg) ← rep.{u} ref fndef
214+
let (newref, newmsg) ← rep ref fndef
215215
return (newref, s!"{msg}¬{newmsg}")
216216
) (env, "")
217217

@@ -225,7 +225,7 @@ def repAndPrint (env: Env) (output : String): IO Env := do
225225
return env
226226

227227
def main : IO Unit := do
228-
let (env0, _) ← loadMalFns.{u} (loadFnNativeAll (Env.data 0 Dict.empty)) fnDefs
228+
let (env0, _) ← loadMalFns (loadFnNativeAll (Env.data 0 Dict.empty)) fnDefs
229229
let mut env := env0
230230
let mut donext := true
231231
while donext do
@@ -239,5 +239,5 @@ def main : IO Unit := do
239239
if value.isEmpty then
240240
donext := false
241241
else
242-
let (newenv, value) ← rep.{u} env value
242+
let (newenv, value) ← rep env value
243243
env ← repAndPrint newenv value

impls/lean/LeanMal/step6_file.lean

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,13 @@ mutual
238238
end
239239

240240
def READ (input : String): Except String Types :=
241-
read_str.{u} input
241+
read_str input
242242

243243
def PRINT (ast : Types): String :=
244244
pr_str true ast
245245

246246
def rep (env: Env) (input : String): IO (Env × String) := do
247-
match READ.{u} input with
247+
match READ input with
248248
| Except.ok result =>
249249
try
250250
let (newenv, res) ← evalTypes env result
@@ -256,7 +256,7 @@ def rep (env: Env) (input : String): IO (Env × String) := do
256256
def loadMalFns (env: Env) (fndefs: List String): IO (Env × String) := do
257257
fndefs.foldlM (fun (res : Env × String) fndef => do
258258
let (ref, msg) := res
259-
let (newref, newmsg) ← rep.{u} ref fndef
259+
let (newref, newmsg) ← rep ref fndef
260260
return (newref, s!"{msg}¬{newmsg}")
261261
) (env, "")
262262

@@ -270,16 +270,10 @@ def repAndPrint (env: Env) (output : String): IO Env := do
270270
else IO.println output
271271
return env
272272

273-
def main (args : List String) : IO Unit := do
274-
let (env0, _) ← loadMalFns.{u} (loadFnNativeAll (Env.data 0 Dict.empty)) fnDefs
275-
let astArgs := ((args.drop 1).map (fun arg => Types.strVal arg))
276-
let mut env := setSymbol env0 "*ARGV*" (Types.listVal astArgs)
277-
if args.length > 0 then
278-
let (_, val) ← rep.{u} env s!"(load-file \"{args[0]!}\")"
279-
IO.println val
280-
else
281-
282-
let mut donext := true
273+
def reploop (inienv: Env) : IO Unit := do
274+
let mut donext := false
275+
let mut env := inienv
276+
donext := true
283277
while donext do
284278
IO.print "user> "
285279
let stdin ← IO.getStdin
@@ -291,5 +285,16 @@ def main (args : List String) : IO Unit := do
291285
if value.isEmpty then
292286
donext := false
293287
else
294-
let (newenv, value) ← rep.{u} env value
288+
let (newenv, value) ← rep env value
295289
env ← repAndPrint newenv value
290+
291+
def main (args : List String) : IO Unit := do
292+
let (env0, _) ← loadMalFns (loadFnNativeAll (Env.data 0 Dict.empty)) fnDefs
293+
let env := setSymbol env0 "*ARGV*" (Types.listVal [])
294+
295+
if args.length > 0 then do
296+
let astArgs := ((args.drop 1).map (fun arg => Types.strVal arg))
297+
let newenv := setSymbol env0 "*ARGV*" (Types.listVal astArgs)
298+
let (_, _) ← rep newenv s!"(load-file \"{args[0]!}\")"
299+
IO.Process.exit 0
300+
else reploop env

impls/lean/LeanMal/step7_quote.lean

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,13 @@ mutual
278278
end
279279

280280
def READ (input : String): Except String Types :=
281-
read_str.{u} input
281+
read_str input
282282

283283
def PRINT (ast : Types): String :=
284284
pr_str true ast
285285

286286
def rep (env: Env) (input : String): IO (Env × String) := do
287-
match READ.{u} input with
287+
match READ input with
288288
| Except.ok result =>
289289
try
290290
let (newenv, res) ← evalTypes env result
@@ -296,7 +296,7 @@ def rep (env: Env) (input : String): IO (Env × String) := do
296296
def loadMalFns (env: Env) (fndefs: List String): IO (Env × String) := do
297297
fndefs.foldlM (fun (res : Env × String) fndef => do
298298
let (ref, msg) := res
299-
let (newref, newmsg) ← rep.{u} ref fndef
299+
let (newref, newmsg) ← rep ref fndef
300300
return (newref, s!"{msg}¬{newmsg}")
301301
) (env, "")
302302

@@ -310,17 +310,10 @@ def repAndPrint (env: Env) (output : String): IO Env := do
310310
else IO.println output
311311
return env
312312

313-
def main (args : List String) : IO Unit := do
314-
let (env0, _) ← loadMalFns.{u} (loadFnNativeAll (Env.data 0 Dict.empty)) fnDefs
315-
let astArgs := ((args.drop 1).map (fun arg => Types.strVal arg))
316-
let mut env := setSymbol env0 "*ARGV*" (Types.listVal astArgs)
317-
318-
if args.length > 0 then
319-
let (_, val) ← rep.{u} env s!"(load-file \"{args[0]!}\")"
320-
IO.println val
321-
else
322-
323-
let mut donext := true
313+
def reploop (inienv: Env) : IO Unit := do
314+
let mut donext := false
315+
let mut env := inienv
316+
donext := true
324317
while donext do
325318
IO.print "user> "
326319
let stdin ← IO.getStdin
@@ -332,5 +325,16 @@ def main (args : List String) : IO Unit := do
332325
if value.isEmpty then
333326
donext := false
334327
else
335-
let (newenv, value) ← rep.{u} env value
328+
let (newenv, value) ← rep env value
336329
env ← repAndPrint newenv value
330+
331+
def main (args : List String) : IO Unit := do
332+
let (env0, _) ← loadMalFns (loadFnNativeAll (Env.data 0 Dict.empty)) fnDefs
333+
let env := setSymbol env0 "*ARGV*" (Types.listVal [])
334+
335+
if args.length > 0 then do
336+
let astArgs := ((args.drop 1).map (fun arg => Types.strVal arg))
337+
let newenv := setSymbol env0 "*ARGV*" (Types.listVal astArgs)
338+
let (_, _) ← rep newenv s!"(load-file \"{args[0]!}\")"
339+
IO.Process.exit 0
340+
else reploop env

0 commit comments

Comments
 (0)