Skip to content

Commit

Permalink
xelim: supports try statements now (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq authored Jul 21, 2024
1 parent 5d349e1 commit 88f3a2c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/xelim/xelim_model.nim
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type
WhileX = "while"
CaseX = "case"
OfX = "of"
TryX = "try"
ExceptX = "except"
FinallyX = "fin"
ProcX = "proc"

StmtsX = "stmts"
Expand Down
39 changes: 39 additions & 0 deletions src/xelim/xelim_transformer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,40 @@ proc trCase(c: var Context; dest: var Tree; t: Tree; n: NodePos; tar: var Target
if tar.m != IsIgnored:
tar.t.addAtom Sym, tmp, info

proc trTry(c: var Context; dest: var Tree; t: Tree; n: NodePos; tar: var Target) =
let info = t[n].info
var tmp = StrId(0)

if tar.m != IsIgnored:
tmp = declareTemp(c, dest, info, AutoX)

copyIntoFrom(dest, t, n):
if tar.m != IsIgnored:
trExprInto c, dest, t, n.firstSon, tmp

for ch in sonsFromX(t, n):
case t[ch].kind
of ExceptX:
copyIntoFrom(dest, t, ch):
for e in sons(t, ch):
if isLastSon(t, ch, e):
if tar.m != IsIgnored:
trExprInto c, dest, t, e, tmp
else:
trStmt c, dest, t, e
else:
copyTree dest, t, e
of FinallyX:
# The `finally` section never produces a value!
copyIntoFrom(dest, t, ch):
let action = ch.firstSon
trStmt c, dest, t, action
else:
# Bug: just copy the thing around
copyTree dest, t, n
if tar.m != IsIgnored:
tar.t.addAtom Sym, tmp, info

proc trStmt(c: var Context; dest: var Tree; t: Tree; n: NodePos) =
case t[n].kind
of Empty, Tag:
Expand All @@ -193,6 +227,9 @@ proc trStmt(c: var Context; dest: var Tree; t: Tree; n: NodePos) =
of CaseX:
var tar = Target(m: IsIgnored)
trCase c, dest, t, n, tar
of TryX:
var tar = Target(m: IsIgnored)
trTry c, dest, t, n, tar

of RetX, DiscardX, RaiseX, YieldX:
var tar = Target(m: IsEmpty)
Expand Down Expand Up @@ -259,6 +296,8 @@ proc trExpr(c: var Context; dest: var Tree; t: Tree; n: NodePos; tar: var Target
trIf c, dest, t, n, tar
of CaseX:
trCase c, dest, t, n, tar
of TryX:
trTry c, dest, t, n, tar
of AndX:
trAnd c, dest, t, n, tar
of OrX:
Expand Down
13 changes: 12 additions & 1 deletion tests/xelim/t2.expected.nif
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,15 @@
(lt 4 5)
(call echo "body"))
(else
(break))))))
(break)))))
(var :tmp.3.01 . .
(auto) .)
(try
(call echo "abc")
(asgn tmp.3.01 3)
(except
(as :e E.t)
(asgn tmp.3.01 90))
(fin
(call echo "90")))
(var :x.3 . . . tmp.3.01))
1 change: 1 addition & 0 deletions tests/xelim/t2.nif
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
(if (elif cond "action") (else "abc"))
)))
(while (expr (call echo "hi\00") (lt 4 5)) (call echo "body"))
(var :x.3 ... (try (expr (call echo "abc") 3) (except (as :e E.t) 90) (fin (call echo "90")) ))
)

0 comments on commit 88f3a2c

Please sign in to comment.