Minor refactor + fix minor oopsie

This commit is contained in:
Mattia Giambirtone 2023-06-28 17:33:48 +02:00
parent f0ae805773
commit 6979d0316f
Signed by: nocturn9x
GPG Key ID: 8270F9F467971E59
1 changed files with 22 additions and 21 deletions

View File

@ -247,14 +247,14 @@ method unary*(self: Compiler, node: UnaryExpr, compile: bool = true): Type {.dis
method binary*(self: Compiler, node: BinaryExpr, compile: bool = true): Type {.discardable, base.} = nil method binary*(self: Compiler, node: BinaryExpr, compile: bool = true): Type {.discardable, base.} = nil
method lambdaExpr*(self: Compiler, node: LambdaExpr, compile: bool = true): Type {.discardable, base.} = nil method lambdaExpr*(self: Compiler, node: LambdaExpr, compile: bool = true): Type {.discardable, base.} = nil
method literal*(self: Compiler, node: ASTNode, compile: bool = true): Type {.discardable, base.} = nil method literal*(self: Compiler, node: ASTNode, compile: bool = true): Type {.discardable, base.} = nil
method infer*(self: Compiler, node: LiteralExpr): Type proc infer*(self: Compiler, node: LiteralExpr): Type
method infer*(self: Compiler, node: Expression): Type proc infer*(self: Compiler, node: Expression): Type
method inferOrError*(self: Compiler, node: Expression): Type proc inferOrError*(self: Compiler, node: Expression): Type
method findByName*(self: Compiler, name: string): seq[Name] proc findByName*(self: Compiler, name: string): seq[Name]
method findInModule*(self: Compiler, name: string, module: Name): seq[Name] proc findInModule*(self: Compiler, name: string, module: Name): seq[Name]
method findByType*(self: Compiler, name: string, kind: Type): seq[Name] proc findByType*(self: Compiler, name: string, kind: Type): seq[Name]
method compare*(self: Compiler, a, b: Type): bool proc compare*(self: Compiler, a, b: Type): bool
method match*(self: Compiler, name: string, kind: Type, node: ASTNode = nil, allowFwd: bool = true): Name proc match*(self: Compiler, name: string, kind: Type, node: ASTNode = nil, allowFwd: bool = true): Name
method prepareFunction*(self: Compiler, name: Name) {.base.} = discard method prepareFunction*(self: Compiler, name: Name) {.base.} = discard
method dispatchPragmas(self: Compiler, name: Name) {.base.} = discard method dispatchPragmas(self: Compiler, name: Name) {.base.} = discard
method dispatchDelayedPragmas(self: Compiler, name: Name) {.base.} = discard method dispatchDelayedPragmas(self: Compiler, name: Name) {.base.} = discard
@ -417,7 +417,7 @@ proc compareUnions*(self: Compiler, a, b: seq[tuple[match: bool, kind: Type]]):
return i >= short.len() return i >= short.len()
method compare*(self: Compiler, a, b: Type): bool = proc compare*(self: Compiler, a, b: Type): bool =
## Compares two type objects ## Compares two type objects
## for equality ## for equality
result = false result = false
@ -462,14 +462,14 @@ method compare*(self: Compiler, a, b: Type): bool =
return false return false
var i = 0 var i = 0
for (argA, argB) in zip(a.args, b.args): for (argA, argB) in zip(a.args, b.args):
if not self.compare(argA.kind, argB.kind):
return false
if argA.name == "": if argA.name == "":
continue continue
if argB.name == "": if argB.name == "":
continue continue
if argA.name != argB.name: if argA.name != argB.name:
return false return false
if not self.compare(argA.kind, argB.kind):
return false
if a.forwarded or b.forwarded: if a.forwarded or b.forwarded:
# We need to be more strict when checking forward # We need to be more strict when checking forward
# declarations # declarations
@ -569,7 +569,7 @@ proc toIntrinsic*(name: string): Type =
return Type(kind: String) return Type(kind: String)
method infer*(self: Compiler, node: LiteralExpr): Type = proc infer*(self: Compiler, node: LiteralExpr): Type =
## Infers the type of a given literal expression ## Infers the type of a given literal expression
if node.isNil(): if node.isNil():
return nil return nil
@ -602,7 +602,7 @@ method infer*(self: Compiler, node: LiteralExpr): Type =
discard # Unreachable discard # Unreachable
method infer*(self: Compiler, node: Expression): Type = proc infer*(self: Compiler, node: Expression): Type =
## Infers the type of a given expression and ## Infers the type of a given expression and
## returns it ## returns it
if node.isNil(): if node.isNil():
@ -636,7 +636,7 @@ method infer*(self: Compiler, node: Expression): Type =
discard # TODO discard # TODO
method inferOrError*(self: Compiler, node: Expression): Type = proc inferOrError*(self: Compiler, node: Expression): Type =
## Attempts to infer the type of ## Attempts to infer the type of
## the given expression and raises an ## the given expression and raises an
## error if it fails ## error if it fails
@ -645,7 +645,7 @@ method inferOrError*(self: Compiler, node: Expression): Type =
self.error("expression has no type", node) self.error("expression has no type", node)
method stringify*(self: Compiler, typ: Type): string = proc stringify*(self: Compiler, typ: Type): string =
## Returns the string representation of a ## Returns the string representation of a
## type object ## type object
if typ.isNil(): if typ.isNil():
@ -717,7 +717,7 @@ method stringify*(self: Compiler, typ: Type): string =
discard discard
method findByName*(self: Compiler, name: string): seq[Name] = proc findByName*(self: Compiler, name: string): seq[Name] =
## Looks for objects that have been already declared ## Looks for objects that have been already declared
## with the given name. Returns all objects that apply. ## with the given name. Returns all objects that apply.
for obj in reversed(self.names): for obj in reversed(self.names):
@ -728,7 +728,7 @@ method findByName*(self: Compiler, name: string): seq[Name] =
result.add(obj) result.add(obj)
method findInModule*(self: Compiler, name: string, module: Name): seq[Name] = proc findInModule*(self: Compiler, name: string, module: Name): seq[Name] =
## Looks for objects that have been already declared as ## Looks for objects that have been already declared as
## public within the given module with the given name. ## public within the given module with the given name.
## Returns all objects that apply. If the name is an ## Returns all objects that apply. If the name is an
@ -748,7 +748,7 @@ method findInModule*(self: Compiler, name: string, module: Name): seq[Name] =
result.add(obj) result.add(obj)
method findByType*(self: Compiler, name: string, kind: Type): seq[Name] = proc findByType*(self: Compiler, name: string, kind: Type): seq[Name] =
## Looks for objects that have already been declared ## Looks for objects that have already been declared
## with the given name and type. Returns all objects ## with the given name and type. Returns all objects
## that apply ## that apply
@ -757,7 +757,7 @@ method findByType*(self: Compiler, name: string, kind: Type): seq[Name] =
result.add(obj) result.add(obj)
method findAtDepth*(self: Compiler, name: string, depth: int): seq[Name] {.used.} = proc findAtDepth*(self: Compiler, name: string, depth: int): seq[Name] {.used.} =
## Looks for objects that have been already declared ## Looks for objects that have been already declared
## with the given name at the given scope depth. ## with the given name at the given scope depth.
## Returns all objects that apply ## Returns all objects that apply
@ -796,7 +796,7 @@ proc isAny*(typ: Type): bool =
return false return false
method match*(self: Compiler, name: string, kind: Type, node: ASTNode = nil, allowFwd: bool = true): Name = proc match*(self: Compiler, name: string, kind: Type, node: ASTNode = nil, allowFwd: bool = true): Name =
## Tries to find a matching function implementation ## Tries to find a matching function implementation
## compatible with the given type and returns its ## compatible with the given type and returns its
## name object ## name object
@ -829,6 +829,7 @@ method match*(self: Compiler, name: string, kind: Type, node: ASTNode = nil, all
msg = &"call to undefined function '{name}'" msg = &"call to undefined function '{name}'"
self.error(msg, node) self.error(msg, node)
elif impl.len() > 1: elif impl.len() > 1:
echo "AAAAAA\n\n"
# If we happen to find more than one match, we try again # If we happen to find more than one match, we try again
# and ignore forward declarations and automatic functions # and ignore forward declarations and automatic functions
impl = filterIt(impl, not it.valueType.forwarded and not it.valueType.isAuto) impl = filterIt(impl, not it.valueType.forwarded and not it.valueType.isAuto)
@ -859,7 +860,7 @@ proc beginScope*(self: Compiler) =
proc unpackTypes*(self: Compiler, condition: Expression, list: var seq[tuple[match: bool, kind: Type]], accept: bool = true) = proc unpackTypes*(self: Compiler, condition: Expression, list: var seq[tuple[match: bool, kind: Type]], accept: bool = true) =
## Recursively unpacks a type constraint in a generic type ## Recursively unpacks a type constraint
case condition.kind: case condition.kind:
of identExpr: of identExpr:
var typ = self.inferOrError(condition) var typ = self.inferOrError(condition)