Skip to content

Commit

Permalink
Add Position.bogus() (#527)
Browse files Browse the repository at this point in the history
  • Loading branch information
kengorab authored Dec 25, 2024
1 parent 860fbfe commit c49901f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
8 changes: 4 additions & 4 deletions projects/compiler/src/compiler.abra
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ enum CompileErrorKind {
ResolvedGenericsError(context: String, message: String)
}

func qbeError<V>(message: String): Result<V, CompileError> = Err(CompileError(position: Position(line: 0, col: 0), kind: CompileErrorKind.QbeError(message)))
func qbeError<V>(message: String): Result<V, CompileError> = Err(CompileError(position: Position.bogus(), kind: CompileErrorKind.QbeError(message)))

type ResolvedGenerics {
_layers: (String, Map<String, Type>)[] = []
Expand Down Expand Up @@ -205,7 +205,7 @@ export type Compiler {
val (fnNamesPtr, fnNamesIdx) = builder.addData(QbeData(name: "__fnnames", kind: QbeDataKind.Strings([])))

// Seed the RNG, for any future calls to `libc.rand()`
val timeVal = match mainFn.block.buildCallRaw("time", QbeType.U64, [Value.Int(0)]) { Ok(v) => v, Err(e) => return Err(CompilationError(modulePath: "<entrypoint>", error: CompileError(position: Position(line: 0, col: 0), kind: CompileErrorKind.QbeError(e)))) }
val timeVal = match mainFn.block.buildCallRaw("time", QbeType.U64, [Value.Int(0)]) { Ok(v) => v, Err(e) => return Err(CompilationError(modulePath: "<entrypoint>", error: CompileError(position: Position.bogus(), kind: CompileErrorKind.QbeError(e)))) }
mainFn.block.buildVoidCallRaw("srand", [timeVal])

mainFn.block.buildVoidCallRaw("GC_init", [])
Expand All @@ -223,7 +223,7 @@ export type Compiler {
}
match moduleFn.block.verify() {
Ok(v) => v
Err(e) => return Err(CompilationError(modulePath: mod.name, error: CompileError(position: Position(line: 0, col: 0), kind: CompileErrorKind.QbeError(e))))
Err(e) => return Err(CompilationError(modulePath: mod.name, error: CompileError(position: Position.bogus(), kind: CompileErrorKind.QbeError(e))))
}
mainFn.block.buildVoidCall(Callable.Function(moduleFn), [])
}
Expand All @@ -232,7 +232,7 @@ export type Compiler {

match mainFn.block.verify() {
Ok(v) => v
Err(e) => return Err(CompilationError(modulePath: "<entrypoint>", error: CompileError(position: Position(line: 0, col: 0), kind: CompileErrorKind.QbeError(e))))
Err(e) => return Err(CompilationError(modulePath: "<entrypoint>", error: CompileError(position: Position.bogus(), kind: CompileErrorKind.QbeError(e))))
}

val functionNames = compiler._functionNames.entries().asArray().sortBy(p => p[1])
Expand Down
7 changes: 6 additions & 1 deletion projects/compiler/src/lexer.abra
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import valueIfValidHexDigit from "./utils"

export type Position { line: Int, col: Int }
export type Position {
line: Int
col: Int

func bogus(): Position = Position(line: 0, col: 0)
}

export type Token {
position: Position
Expand Down
3 changes: 1 addition & 2 deletions projects/compiler/src/parser.abra
Original file line number Diff line number Diff line change
Expand Up @@ -1700,11 +1700,10 @@ export type Parser {
_ => false
}
val invokee = if isSome {
val bogusPosition = Position(line: 0, col: 0)
AstNode(
token: token,
kind: AstNodeKind.Accessor(AccessorAstNode(
root: AstNode(token: Token(position: bogusPosition, kind: TokenKind.Ident("Option")), kind: AstNodeKind.Identifier(IdentifierKind.Named("Option"))),
root: AstNode(token: Token(position: Position.bogus(), kind: TokenKind.Ident("Option")), kind: AstNodeKind.Identifier(IdentifierKind.Named("Option"))),
path: [(Token(position: token.position, kind: TokenKind.Dot), Label(name: "Some", position: token.position))]
))
)
Expand Down
37 changes: 18 additions & 19 deletions projects/compiler/src/typechecker.abra
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export type Variable {
isCaptured: Bool = false

func bogus(): Variable {
val label = Label(name: "_bogus", position: Position(line: 0, col: 0))
val label = Label(name: "_bogus", position: Position.bogus())
Variable(label: label, scope: Scope.bogus(), mutable: false, ty: Type(kind: TypeKind.Never))
}

Expand Down Expand Up @@ -232,7 +232,7 @@ export type Struct {
func hash(self): Int = self.moduleId + self.label.hash() * 17

func makeDummy(moduleId: Int, name: String, typeParams: String[] = [], fields: (String, Type)[] = []): Struct {
val bogusPosition = Position(line: 0, col: 0)
val bogusPosition = Position.bogus()
val bogusScope = Scope.bogus(name)

val struct = Struct(
Expand Down Expand Up @@ -296,16 +296,16 @@ enum Instantiatable {
export type Project {
modules: Map<String, TypedModule> = {}
preludeScope: Scope = Scope.bogus()
preludeIntStruct: Struct = Struct(moduleId: 0, label: Label(name: "Int", position: Position(line: 0, col: 0)), scope: Scope(name: "Int"))
preludeFloatStruct: Struct = Struct(moduleId: 0, label: Label(name: "Float", position: Position(line: 0, col: 0)), scope: Scope(name: "Float"))
preludeBoolStruct: Struct = Struct(moduleId: 0, label: Label(name: "Bool", position: Position(line: 0, col: 0)), scope: Scope(name: "Bool"))
preludeCharStruct: Struct = Struct(moduleId: 0, label: Label(name: "Char", position: Position(line: 0, col: 0)), scope: Scope(name: "Char"))
preludeStringStruct: Struct = Struct(moduleId: 0, label: Label(name: "String", position: Position(line: 0, col: 0)), scope: Scope(name: "String"))
preludeArrayStruct: Struct = Struct(moduleId: 0, label: Label(name: "Array", position: Position(line: 0, col: 0)), scope: Scope(name: "Array"), typeParams: ["T"])
preludeMapStruct: Struct = Struct(moduleId: 0, label: Label(name: "Map", position: Position(line: 0, col: 0)), scope: Scope(name: "Array"), typeParams: ["K", "V"])
preludeSetStruct: Struct = Struct(moduleId: 0, label: Label(name: "Set", position: Position(line: 0, col: 0)), scope: Scope(name: "Set"), typeParams: ["T"])
preludeOptionEnum: Enum = Enum(moduleId: 0, label: Label(name: "Option", position: Position(line: 0, col: 0)), scope: Scope(name: "Option"), typeParams: ["T"])
preludeResultEnum: Enum = Enum(moduleId: 0, label: Label(name: "Result", position: Position(line: 0, col: 0)), scope: Scope(name: "Result"), typeParams: ["V", "E"])
preludeIntStruct: Struct = Struct(moduleId: 0, label: Label(name: "Int", position: Position.bogus()), scope: Scope(name: "Int"))
preludeFloatStruct: Struct = Struct(moduleId: 0, label: Label(name: "Float", position: Position.bogus()), scope: Scope(name: "Float"))
preludeBoolStruct: Struct = Struct(moduleId: 0, label: Label(name: "Bool", position: Position.bogus()), scope: Scope(name: "Bool"))
preludeCharStruct: Struct = Struct(moduleId: 0, label: Label(name: "Char", position: Position.bogus()), scope: Scope(name: "Char"))
preludeStringStruct: Struct = Struct(moduleId: 0, label: Label(name: "String", position: Position.bogus()), scope: Scope(name: "String"))
preludeArrayStruct: Struct = Struct(moduleId: 0, label: Label(name: "Array", position: Position.bogus()), scope: Scope(name: "Array"), typeParams: ["T"])
preludeMapStruct: Struct = Struct(moduleId: 0, label: Label(name: "Map", position: Position.bogus()), scope: Scope(name: "Array"), typeParams: ["K", "V"])
preludeSetStruct: Struct = Struct(moduleId: 0, label: Label(name: "Set", position: Position.bogus()), scope: Scope(name: "Set"), typeParams: ["T"])
preludeOptionEnum: Enum = Enum(moduleId: 0, label: Label(name: "Option", position: Position.bogus()), scope: Scope(name: "Option"), typeParams: ["T"])
preludeResultEnum: Enum = Enum(moduleId: 0, label: Label(name: "Result", position: Position.bogus()), scope: Scope(name: "Result"), typeParams: ["V", "E"])

func typesAreEquivalent(self, ty: Type, other: Type): Bool {
match ty.kind {
Expand Down Expand Up @@ -675,7 +675,7 @@ export type Function {
kind: FunctionKind,
typeParams: (Type, Label)[] = [],
): Function {
val bogusPosition = Position(line: 0, col: 0)
val bogusPosition = Position.bogus()

val label = Label(name: name, position: bogusPosition)
val fnScope = scope.makeChild(name, ScopeKind.Func)
Expand All @@ -687,7 +687,7 @@ export type Function {
scope: Scope,
struct: Struct,
): Function {
val bogusPosition = Position(line: 0, col: 0)
val bogusPosition = Position.bogus()

val fnScope = scope.makeChild(struct.label.name, ScopeKind.Func)
val structTypeParams = struct.typeParams.map(t => (Type(kind: TypeKind.Generic(t)), Label(name: t, position: bogusPosition)))
Expand All @@ -704,7 +704,7 @@ export type Function {
variantLabel: Label,
variantFields: Field[],
): Function {
val bogusPosition = Position(line: 0, col: 0)
val bogusPosition = Position.bogus()

val fnScope = scope.makeChild("${enum_.label.name}.${variantLabel.name}", ScopeKind.Func)
val typeParams = enum_.typeParams.map(t => (Type(kind: TypeKind.Generic(t)), Label(name: t, position: bogusPosition)))
Expand Down Expand Up @@ -1529,15 +1529,15 @@ export type Typechecker {
self.project.preludeSetStruct,
]
for struct in preludeStructs {
if struct.label.position == Position(line: 0, col: 0) unreachable("Improperly initialized prelude struct ${struct.label.name}")
if struct.label.position == Position.bogus() unreachable("Improperly initialized prelude struct ${struct.label.name}")
}

val preludeEnums = [
self.project.preludeOptionEnum,
self.project.preludeResultEnum,
]
for enum_ in preludeEnums {
if enum_.label.position == Position(line: 0, col: 0) unreachable("Improperly initialized prelude enum ${enum_.label.name}")
if enum_.label.position == Position.bogus() unreachable("Improperly initialized prelude enum ${enum_.label.name}")
}
}

Expand Down Expand Up @@ -4030,11 +4030,10 @@ export type Typechecker {
}
IdentifierKind.Discard => return Err(TypeError(position: token.position, kind: TypeErrorKind.UnknownName("_", "variable")))
IdentifierKind.None_ => {
val bogusPosition = Position(line: 0, col: 0)
val replacement = AstNode(
token: token,
kind: AstNodeKind.Accessor(AccessorAstNode(
root: AstNode(token: Token(position: bogusPosition, kind: TokenKind.Ident("Option")), kind: AstNodeKind.Identifier(IdentifierKind.Named("Option"))),
root: AstNode(token: Token(position: Position.bogus(), kind: TokenKind.Ident("Option")), kind: AstNodeKind.Identifier(IdentifierKind.Named("Option"))),
path: [(Token(position: token.position, kind: TokenKind.Dot), Label(name: "None", position: token.position))]
))
)
Expand Down
1 change: 0 additions & 1 deletion projects/lsp/src/language_service.abra
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export type AbraLanguageService {
val line = pos.line - 1
val character = pos.col - 1
val range = Range(start: Position(line: line, character: character), end: Position(line: line, character: character))
// val uri = if ident.importedFrom |mod| "file://$mod" else textDocument.uri
val definitionFilePath = match ident.importedFrom {
None => filePath
IdentifierMetaImport.Prelude => self._moduleLoader.stdRoot + "/prelude.abra"
Expand Down

0 comments on commit c49901f

Please sign in to comment.