Skip to content

Commit

Permalink
improve gear3 and testing (#115)
Browse files Browse the repository at this point in the history
* imrpove gear3 and testing

* fixes calls

* fixes

* fixes basic ifs
  • Loading branch information
ringabout authored Nov 13, 2024
1 parent 756518a commit 9e35e2e
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 18 deletions.
61 changes: 45 additions & 16 deletions src/gear3/expander.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ proc newNifModule(infile: string): NifModule =
discard processDirectives(result.stream.r)

result.buf = fromStream(result.stream)
# fromStream only parses the topLevel 'stmts'
let eof = next(result.stream)
result.buf.add eof
let indexName = infile.changeFileExt".idx.nif"
if not fileExists(indexName) or getLastModificationTime(indexName) < getLastModificationTime(infile):
createIndex infile
Expand Down Expand Up @@ -219,7 +222,6 @@ proc traverseType(e: var EContext; c: var Cursor; flags: set[TypeFlag] = {}) =
error e, "type expected but got: ", c

proc traverseParams(e: var EContext; c: var Cursor) =
traverseType e, c
if c.kind == DotToken:
e.dest.add c
inc c
Expand All @@ -230,6 +232,8 @@ proc traverseParams(e: var EContext; c: var Cursor) =
if c.substructureKind != ParamS:
error e, "expected (param) but got: ", c
traverseLocal(e, c, "param", TraverseSig)
# the result type
traverseType e, c

type
CollectedPragmas = object
Expand Down Expand Up @@ -322,12 +326,14 @@ proc closeGenPragmas(e: var EContext; g: GenPragmas) =
e.dest.addDotToken()

proc traverseProc(e: var EContext; c: var Cursor; mode: TraverseMode) =
# namePos* = 0
# patternPos* = 1 # empty except for term rewriting macros
# genericParamsPos* = 2
# paramsPos* = 3
# pragmasPos* = 4
# miscPos* = 5 # used for undocumented and hacky stuff
# bodyPos* = 6 # position of body; use rodread.getBody() instead!
# resultPos* = 4
# pragmasPos* = 5
# miscPos* = 6 # used for undocumented and hacky stuff
# bodyPos* = 7 # position of body; use rodread.getBody() instead!
var dst = createTokenBuf(50)
swap e.dest, dst
#let toPatch = e.dest.len
Expand All @@ -337,17 +343,26 @@ proc traverseProc(e: var EContext; c: var Cursor; mode: TraverseMode) =
expectSymdef(e, c)
let s = c.symId
let sinfo = c.info

# namePos
e.dest.add toToken(SymbolDef, s, sinfo)
e.offer s

inc c

skipExportMarker e, c

skip c # patterns

let isGeneric = c.kind != DotToken

skip c # generic parameters

traverseParams e, c

let pinfo = c.info
let prag = parsePragmas(e, c)

e.dest.add toToken(SymbolDef, s, sinfo)
e.offer s
let oldOwner = setOwner(e, s)

var genPragmas = openGenPragmas()
Expand All @@ -358,6 +373,8 @@ proc traverseProc(e: var EContext; c: var Cursor; mode: TraverseMode) =
e.addKey genPragmas, "selectany", pinfo
closeGenPragmas e, genPragmas

skip c # miscPos

# body:
if mode != TraverseSig or prag.callConv == InlineC:
traverseStmt e, c, TraverseAll
Expand Down Expand Up @@ -410,11 +427,14 @@ proc traverseExpr(e: var EContext; c: var Cursor) =
of ParLe:
e.dest.add c
inc nested
of ParRi:
if nested <= 0:
error e, "unmached ')': ", c
of ParRi: # TODO: refactoring: take the whole statement into consideration
if nested == 0:
break
e.dest.add c
dec nested
if nested == 0:
inc c
break
of SymbolDef:
e.dest.add c
e.offer c.symId
Expand Down Expand Up @@ -524,9 +544,17 @@ proc traverseIf(e: var EContext; c: var Cursor) =
# (if cond (.. then ..) (.. else ..))
e.dest.add c
inc c
traverseExpr e, c
traverseStmt e, c
traverseStmt e, c
while c.kind == ParLe and pool.tags[c.tag] == $ElifS:
e.dest.add c
inc c # skips '(elif'
traverseExpr e, c
traverseStmt e, c
wantParRi e, c
if c.kind == ParLe and pool.tags[c.tag] == $ElseS:
e.dest.add c
inc c
traverseStmt e, c
wantParRi e, c
wantParRi e, c

proc traverseCase(e: var EContext; c: var Cursor) =
Expand Down Expand Up @@ -572,15 +600,14 @@ proc traverseStmt(e: var EContext; c: var Cursor; mode = TraverseAll) =
e.loop c:
traverseStmt e, c, mode
of VarS, LetS, CursorS, ResultS:
traverseLocal e, c, (if e.nestedIn[^1][0] == StmtsS: "gvar" else: "var"), mode
traverseLocal e, c, (if e.nestedIn[^1][0] == StmtsS and mode == TraverseTopLevel: "gvar" else: "var"), mode
of ConstS:
traverseLocal e, c, "const", mode
of EmitS, AsgnS, RetS:
of EmitS, AsgnS, RetS, CallS:
e.dest.add c
inc c
e.loop c:
traverseExpr e, c

of BreakS: traverseBreak e, c
of WhileS: traverseWhile e, c
of BlockS: traverseBlock e, c
Expand Down Expand Up @@ -719,7 +746,9 @@ proc expand*(infile: string) =
e.mods[e.main] = m

traverseStmt e, c, TraverseTopLevel
if c.kind != EofToken:
if c.kind == ParRi:
error e, "unmached ')'"
elif c.kind != EofToken:
quit "Internal error: file not processed completely"
# fix point expansion:
var i = 0
Expand Down
1 change: 1 addition & 0 deletions src/nimony/nimony_model.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type
MacroS = "macro"
TemplateS = "template"
TypeS = "type"
CallS = "call"

SymKind* = enum
NoSym
Expand Down
60 changes: 60 additions & 0 deletions tests/gear3/gear3_helloworld.nif
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
(.nif24)

(stmts
(var :x . . (i +32) +12)
(var :y . . (i +32) +12)


(proc :foo.c . . .
(params
(param :x . . (i +32) .)
(param :y . . (i +32) .)) . . .
(stmts
(var :x.m . . (i +32) +2)


(if
(elif (true)
(stmts
)
)

(elif (false)
(stmts
)
)
)

))

(proc :main.c . . . . (i +32) . .
(stmts
(var :x.c . . (i +32) +12)

(asgn x.c +6)


(call foo.c x.c +5)


(if
(elif (true)
(stmts
(call foo.c x.c +1))
)

(elif (false)
(stmts
(call foo.c x.c +2))
)

(else
(stmts
(call foo.c x.c +2))
)

)

))

)
17 changes: 15 additions & 2 deletions tests/tester.nim
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ proc testNifGram(overwrite: bool) =

testNifGram(overwrite)

proc testNifc(overwrite: bool) =
proc testNifc() =
exec "nim c src/nifc/nifc"
let t1 = "tests/nifc/selectany/t1.nif"
let t2 = "tests/nifc/selectany/t2.nif"
Expand All @@ -237,4 +237,17 @@ proc testNifc(overwrite: bool) =
let issues = "tests/nifc/issues.nif"
exec ("src" / "nifc" / "nifc").addFileExt(ExeExt) & " c -r --linedir:on " & issues
exec ("src" / "nifc" / "nifc").addFileExt(ExeExt) & " cpp -r --linedir:off " & issues
testNifc(overwrite)
testNifc()


proc testGear3() =
exec "nim c src/gear3/gear3"

let helloworld = "tests/gear3/gear3_helloworld"


exec ("src" / "gear3" / "gear3").addFileExt(ExeExt) & " " & helloworld & ".nif"
exec ("src" / "nifc" / "nifc").addFileExt(ExeExt) & " c -r " & helloworld & ".c.nif"


testGear3()

0 comments on commit 9e35e2e

Please sign in to comment.