feat: expand term try, for, unless, and return
This commit is contained in:
parent
a042212909
commit
87bf97bdc1
2 changed files with 45 additions and 0 deletions
|
|
@ -1514,6 +1514,24 @@ end Do
|
|||
|
||||
builtin_initialize registerTraceClass `Elab.do
|
||||
|
||||
private def toDoElem (newKind : SyntaxNodeKind) : Macro := fun stx => do
|
||||
let stx := stx.setKind newKind
|
||||
let stxNew ← `(do $stx:doElem)
|
||||
return stxNew.copyInfo stx
|
||||
|
||||
@[builtinMacro Lean.Parser.Term.termFor]
|
||||
def expandTermFor : Macro := toDoElem `Lean.Parser.Term.doFor
|
||||
|
||||
@[builtinMacro Lean.Parser.Term.termTry]
|
||||
def expandTermTry : Macro := toDoElem `Lean.Parser.Term.doTry
|
||||
|
||||
@[builtinMacro Lean.Parser.Term.termUnless]
|
||||
def expandTermUnless : Macro := toDoElem `Lean.Parser.Term.doUnless
|
||||
|
||||
@[builtinMacro Lean.Parser.Term.termReturn]
|
||||
def expandTermReturn : Macro := toDoElem `Lean.Parser.Term.doReturn
|
||||
|
||||
|
||||
end Term
|
||||
end Elab
|
||||
end Lean
|
||||
|
|
|
|||
27
tests/lean/doElemAsTermNotation.lean
Normal file
27
tests/lean/doElemAsTermNotation.lean
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
def f1 (x : Nat) : IO Unit :=
|
||||
unless x > 10 do
|
||||
IO.println s!"x: {x}"
|
||||
|
||||
#eval f1 0
|
||||
#eval f1 100
|
||||
|
||||
def f2 (x : Nat) (ref : IO.Ref Nat) : IO Nat :=
|
||||
return x + (←ref.get)
|
||||
|
||||
#eval id (α := IO Nat) do f2 5 (← IO.mkRef 10)
|
||||
|
||||
def f3 (x : Nat) : IO Nat :=
|
||||
try
|
||||
IO.println x
|
||||
throw $ IO.userError "failed"
|
||||
catch
|
||||
| IO.Error.userError msg => IO.println s!"at catch: {msg}"; pure 0
|
||||
| ex => throw ex
|
||||
|
||||
#eval f3 5
|
||||
|
||||
def f4 (xs : List Nat) : IO Unit :=
|
||||
for x in xs do
|
||||
IO.println x
|
||||
|
||||
#eval f4 [1,2,3]
|
||||
Loading…
Add table
Reference in a new issue