Skip to content

Commit

Permalink
Merge branch 'master' of github.com:kai-language/kai
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettRToomey committed Feb 24, 2018
2 parents da366a0 + 3df891f commit abda4da
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 19 deletions.
44 changes: 27 additions & 17 deletions Sources/Core/Checker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct Checker {

func declare(_ entity: Entity, scopeOwnsEntity: Bool = true) {
let previous = context.scope.insert(entity, scopeOwnsEntity: scopeOwnsEntity)
if let previous = previous, entity.file === previous.file {
if let previous = previous, entity.file !== previous.file {
reportError("Invalid redeclaration of '\(previous.name)'", at: entity.ident.start)
file.attachNote("Previous declaration here: \(file.position(for: previous.ident.start).description)")
}
Expand All @@ -88,6 +88,22 @@ struct Checker {
extension Checker {

mutating func checkFile() {
// Before we begin checking this file we import the entities from our imports where applicable
// We can't guarentee collecting happens in any sort of order because that is where we establish an ordering
// because of this importing entities into our scope is only possible now that all of the entities for
// the imported files exist
for i in file.imports {
if i.importSymbolsIntoScope {
for member in i.scope.members.values {
guard !member.isFile && !member.isLibrary else {
continue
}

declare(member, scopeOwnsEntity: i.exportSymbolsOutOfScope)
}
}
}

for node in file.nodes {
check(topLevelStmt: node)
}
Expand Down Expand Up @@ -134,32 +150,26 @@ extension Checker {
// below
return assert(file.errors.count > 0)
}
var entity: Entity?
var fileEntity: Entity?
if let alias = i.alias {
entity = newEntity(ident: alias, flags: .file)
fileEntity = newEntity(ident: alias, flags: .file)
} else if !i.importSymbolsIntoScope {
guard let name = i.resolvedName else {
reportError("Cannot infer an import name for '\(i.path)'", at: i.path.start)
file.attachNote("You will need to manually specify one")
return
}
let ident = Ident(start: noPos, name: name)
entity = newEntity(ident: ident, flags: .file)
fileEntity = newEntity(ident: ident, flags: .file)
}

// TODO: Ensure the import has been fully checked
if i.importSymbolsIntoScope {
for member in i.scope.members.values {
guard !member.isFile && !member.isLibrary else {
continue
}

declare(member, scopeOwnsEntity: i.exportSymbolsOutOfScope)
}
} else if let entity = entity {
entity.memberScope = i.scope
entity.type = ty.File(memberScope: i.scope)
declare(entity)
// NOTE: If i.importSymbolsIntoScope we leave the import of entities until the file containing the import
// statement is starting to be checked. Only then do we know that all of the entities for the imported file
// have been created.
if let fileEntity = fileEntity {
fileEntity.memberScope = i.scope
fileEntity.type = ty.File(memberScope: i.scope)
declare(fileEntity)
}
}

Expand Down
9 changes: 7 additions & 2 deletions Sources/Core/Type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,14 @@ func convert(_ type: Type, to target: Type, at expr: Expr) -> Bool {
case (is ty.Boolean, is ty.Boolean):
allowed = true

case (let exprType as ty.Enum, let targetType as ty.Integer):
case (let enumType as ty.Enum, is ty.Integer),
(is ty.Integer, let enumType as ty.Enum):
// FIXME: Decide the rules for casting to and from integers
allowed = canCast(exprType.backingType, to: targetType)
allowed = canCast(enumType.backingType, to: target)

case (is ty.Enum, is ty.UntypedInteger),
(is ty.UntypedInteger, is ty.Enum):
allowed = true

case (_, is ty.Anyy):
allowed = true
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions code/bugs/open/issue122/a.kai
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

#import "b.kai" _
#import "c.kai" _
main :: fn() -> void {
z := ent
}

1 change: 1 addition & 0 deletions code/bugs/open/issue122/b.kai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ent :: 1
1 change: 1 addition & 0 deletions code/bugs/open/issue122/c.kai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ent :: 2

0 comments on commit abda4da

Please sign in to comment.