Skip to content

Commit

Permalink
adds (discard Expr) to NIFC (nim-lang#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored Nov 14, 2024
1 parent a21a7ec commit 2044223
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ nimcache/
nimblecache/
htmldocs/
*.exe
nifcache/
nifcache/
src/nifc/nifc_grammar.nim
5 changes: 3 additions & 2 deletions doc/nifc-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ Stmt ::= Call |
(case Expr (of BranchRanges StmtList)* (else StmtList)?) |
(lab SymbolDef) |
(jmp Symbol) |
(scope StmtList)
(ret [Empty | Expr]) # return statement
(scope StmtList) |
(ret [Empty | Expr]) | # return statement
(discard Expr)
StmtList ::= (stmts SCOPE Stmt*)
Expand Down
1 change: 1 addition & 0 deletions src/nifc/codegen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type
TypedefKeyword = "typedef "
IncludeKeyword = "#include "
LineDirKeyword = "#line "
DiscardToken = "(void) "

proc fillTokenTable(tab: var BiTable[Token, string]) =
for e in EmptyToken..high(PredefinedToken):
Expand Down
4 changes: 4 additions & 0 deletions src/nifc/genstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,9 @@ proc genStmt(c: var GeneratedCode; t: Tree; n: NodePos) =
c.add Space
c.genx t, n.firstSon
c.add Semicolon
of DiscardC:
c.add DiscardToken
c.genx t, n.firstSon
c.add Semicolon
else:
error c.m, "expected statement but got: ", t, n
4 changes: 3 additions & 1 deletion src/nifc/nifc_grammar.nif
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@
(lab SYMBOLDEF)
(jmp SYMBOL)
(scope StmtList)
(ret (OR . Expr))))
(ret (OR . Expr))
(discard Expr)
))

(RULE :StmtList (SCOPE (stmts (ZERO_OR_MANY Stmt))))

Expand Down
1 change: 1 addition & 0 deletions src/nifc/nifc_model.nim
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type
InfC = "inf"
NegInfC = "neginf"
NanC = "nan"
DiscardC = "discard"


const
Expand Down
2 changes: 1 addition & 1 deletion src/nifc/typenav.nim
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,5 @@ proc getType*(m: var Module; t: Tree; n: NodePos): TypeDesc =
VoidC, PtrC, ArrayC, FlexarrayC, APtrC, TypeC, CdeclC,
StdcallC, SafecallC, SyscallC, FastcallC, ThiscallC, NoconvC, MemberC,
AttrC, InlineC, NoinlineC, VarargsC, WasC, SelectanyC,
PragmasC, AlignC, BitsC, VectorC, ImpC, NodeclC, InclC, ScopeC:
PragmasC, AlignC, BitsC, VectorC, ImpC, NodeclC, InclC, ScopeC, DiscardC:
result = errorType()
9 changes: 9 additions & 0 deletions tests/data/nifc_grammar.nim
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,15 @@ proc matchStmt(c: var Context; it: var Item): bool =
if kw18:
or2 = true
break or3
var kw21 = false
if isTag(c, it, DiscardT):
if not matchExpr(c, it):
error(c, it, "Expr expected")
break or3
kw21 = matchParRi(c, it)
if kw21:
or2 = true
break or3
if not or2: return false
when declared(handleStmt):
handleStmt(c, it, before1)
Expand Down
2 changes: 2 additions & 0 deletions tests/nifc/issues.nif
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
(call foo.intminmax)

(call foo.inline)
(discard (call foo.inline))
(call foo.noinline)
(call foo.cdecl)
(call foo.stdcall)
Expand All @@ -212,6 +213,7 @@
(call foo.member)

(var :x.c . (i +8) (suf +12 "i8"))
(discard x.c)
(var :x.m . (i +16) (suf -12 "i16"))
(var :x1 . (i +32) (suf +12 "i32"))
(var :y1 . (u +32) (suf +12 "u32"))
Expand Down

0 comments on commit 2044223

Please sign in to comment.