new language feature: explicit 'import system' statements are allowed
This commit is contained in:
parent
28940ce457
commit
e9eab32e54
5 changed files with 37 additions and 5 deletions
|
|
@ -184,8 +184,8 @@ proc importModule*(s: PSym, fileIdx: int32): PSym {.procvar.} =
|
||||||
# this is called by the semantic checking phase
|
# this is called by the semantic checking phase
|
||||||
result = compileModule(fileIdx, {})
|
result = compileModule(fileIdx, {})
|
||||||
if optCaasEnabled in gGlobalOptions: addDep(s, fileIdx)
|
if optCaasEnabled in gGlobalOptions: addDep(s, fileIdx)
|
||||||
if sfSystemModule in result.flags:
|
#if sfSystemModule in result.flags:
|
||||||
localError(result.info, errAttemptToRedefine, result.name.s)
|
# localError(result.info, errAttemptToRedefine, result.name.s)
|
||||||
# restore the notes for outer module:
|
# restore the notes for outer module:
|
||||||
gNotes = if s.owner.id == gMainPackageId: gMainPackageNotes
|
gNotes = if s.owner.id == gMainPackageId: gMainPackageNotes
|
||||||
else: ForeignPackageNotes
|
else: ForeignPackageNotes
|
||||||
|
|
|
||||||
|
|
@ -418,9 +418,6 @@ proc myOpen(module: PSym): PPassContext =
|
||||||
c.importTable.addSym(module) # a module knows itself
|
c.importTable.addSym(module) # a module knows itself
|
||||||
if sfSystemModule in module.flags:
|
if sfSystemModule in module.flags:
|
||||||
magicsys.systemModule = module # set global variable!
|
magicsys.systemModule = module # set global variable!
|
||||||
else:
|
|
||||||
c.importTable.addSym magicsys.systemModule # import the "System" identifier
|
|
||||||
importAllSymbols(c, magicsys.systemModule)
|
|
||||||
c.topLevelScope = openScope(c)
|
c.topLevelScope = openScope(c)
|
||||||
# don't be verbose unless the module belongs to the main package:
|
# don't be verbose unless the module belongs to the main package:
|
||||||
if module.owner.id == gMainPackageId:
|
if module.owner.id == gMainPackageId:
|
||||||
|
|
@ -434,7 +431,29 @@ proc myOpenCached(module: PSym, rd: PRodReader): PPassContext =
|
||||||
result = myOpen(module)
|
result = myOpen(module)
|
||||||
for m in items(rd.methods): methodDef(m, true)
|
for m in items(rd.methods): methodDef(m, true)
|
||||||
|
|
||||||
|
proc isImportSystemStmt(n: PNode): bool =
|
||||||
|
if magicsys.systemModule == nil: return false
|
||||||
|
case n.kind
|
||||||
|
of nkImportStmt:
|
||||||
|
for x in n:
|
||||||
|
let f = checkModuleName(x)
|
||||||
|
if f == magicsys.systemModule.info.fileIndex:
|
||||||
|
return true
|
||||||
|
of nkImportExceptStmt, nkFromStmt:
|
||||||
|
let f = checkModuleName(n[0])
|
||||||
|
if f == magicsys.systemModule.info.fileIndex:
|
||||||
|
return true
|
||||||
|
else: discard
|
||||||
|
|
||||||
proc semStmtAndGenerateGenerics(c: PContext, n: PNode): PNode =
|
proc semStmtAndGenerateGenerics(c: PContext, n: PNode): PNode =
|
||||||
|
if c.topStmts == 0 and not isImportSystemStmt(n):
|
||||||
|
if sfSystemModule notin c.module.flags and
|
||||||
|
n.kind notin {nkEmpty, nkCommentStmt}:
|
||||||
|
c.importTable.addSym magicsys.systemModule # import the "System" identifier
|
||||||
|
importAllSymbols(c, magicsys.systemModule)
|
||||||
|
inc c.topStmts
|
||||||
|
else:
|
||||||
|
inc c.topStmts
|
||||||
if sfNoForward in c.module.flags:
|
if sfNoForward in c.module.flags:
|
||||||
result = semAllTypeSections(c, n)
|
result = semAllTypeSections(c, n)
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -99,6 +99,7 @@ type
|
||||||
unknownIdents*: IntSet # ids of all unknown identifiers to prevent
|
unknownIdents*: IntSet # ids of all unknown identifiers to prevent
|
||||||
# naming it multiple times
|
# naming it multiple times
|
||||||
generics*: seq[TInstantiationPair] # pending list of instantiated generics to compile
|
generics*: seq[TInstantiationPair] # pending list of instantiated generics to compile
|
||||||
|
topStmts*: int # counts the number of encountered top level statements
|
||||||
lastGenericIdx*: int # used for the generics stack
|
lastGenericIdx*: int # used for the generics stack
|
||||||
hloLoopDetector*: int # used to prevent endless loops in the HLO
|
hloLoopDetector*: int # used to prevent endless loops in the HLO
|
||||||
inParallelStmt*: int
|
inParallelStmt*: int
|
||||||
|
|
|
||||||
9
tests/modules/texplicit_system_import.nim
Normal file
9
tests/modules/texplicit_system_import.nim
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
##.
|
||||||
|
import system except `+`
|
||||||
|
discard """
|
||||||
|
errormsg: "undeclared identifier: '+'"
|
||||||
|
line: 9
|
||||||
|
"""
|
||||||
|
# Testament requires that the initial """ occurs before the 40th byte
|
||||||
|
# in the file. No kidding...
|
||||||
|
echo 4+5
|
||||||
|
|
@ -54,6 +54,9 @@ Language Additions
|
||||||
|
|
||||||
- Added ``{.intdefine.}`` and ``{.strdefine.}`` macros to make use of
|
- Added ``{.intdefine.}`` and ``{.strdefine.}`` macros to make use of
|
||||||
(optional) compile time defines.
|
(optional) compile time defines.
|
||||||
|
- If the first statement is an ``import system`` statement then ``system``
|
||||||
|
is not imported implicitly anymore. This allows for code like
|
||||||
|
``import system except echo`` or ``from system import nil``.
|
||||||
|
|
||||||
Bugfixes
|
Bugfixes
|
||||||
--------
|
--------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue