Skip to content

Commit

Permalink
Add todo and unreachable builtins (#488)
Browse files Browse the repository at this point in the history
* Add `todo` and `unreachable` builtins

Prelude: adding `todo(message: String)` and
`unreachable(message: String)` builtin functions. These functions have
the `noreturn` decorator since they call `Process.exit()` after printing
a specific message (along with the optional `message` parameter).

Compiler: Replace usages of ad-hoc todo/unreachable functions with the
new builtin implementations. This requires that the `ABRA_HOME`
environment variable be set to the _current_ `std/` dir during tests, as
opposed to the `std/` dir packaged with the latest published version.

* Minor fix
  • Loading branch information
kengorab authored Nov 6, 2024
1 parent bbb0c7a commit b352861
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 286 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/selfhost.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ jobs:
wget https://github.com/kengorab/abra-lang/releases/latest/download/abra-linux.tar.gz
tar -xzf abra-linux.tar.gz -C abra-linux
echo "PATH=$(pwd)/abra-linux:$PATH" >> $GITHUB_ENV
echo "ABRA_HOME=`realpath $(pwd)/abra-linux/std`" >> $GITHUB_ENV
# echo "ABRA_HOME=`realpath $(pwd)/abra-linux/std`" >> $GITHUB_ENV
echo "ABRA_HOME=`realpath $(pwd)/projects/std/src`" >> $GITHUB_ENV
- name: Run tests
run: |
cd projects/compiler
Expand Down
345 changes: 168 additions & 177 deletions projects/compiler/src/compiler.abra

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion projects/compiler/src/lexer.abra
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ export type LexerError {
}
" | $line\n ${cursor.join()}"
} else {
"unreachable"
// "unreachable"
unreachable()
}
}
}
Expand Down Expand Up @@ -525,6 +526,7 @@ export type Lexer {
continue
} else {
// should be unreachable
unreachable()
}
} else if ch == "\"" {
closed = true
Expand Down
16 changes: 4 additions & 12 deletions projects/compiler/src/parser.abra
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ enum ParseErrorKind {
ExpectedToken(expected: TokenKind[], received: TokenKind)
UnexpectedEof
NotYetImplemented
Unreachable
}

export type ParseError {
Expand Down Expand Up @@ -327,9 +326,6 @@ export type ParseError {
lines.push("Not yet implemented:")
lines.push(self._getCursorLine(self.position, contents))
}
ParseErrorKind.Unreachable => {
lines.push("Critical error: reached unreachable location in code!")
}
}

lines.join("\n")
Expand All @@ -345,7 +341,7 @@ export type ParseError {
}
" | $line\n ${cursor.join()}"
} else {
"unreachable"
unreachable()
}
}
}
Expand Down Expand Up @@ -432,7 +428,7 @@ export type Parser {
TokenKind.StringInterpolation => true
_ => false
}
TokenKind.StringInterpolation => return unreachable() // no reason to ever explicitly expect a StringInterpolation token
TokenKind.StringInterpolation => unreachable("no reason to ever explicitly expect a StringInterpolation token")
TokenKind.Ident => match token.kind { TokenKind.Ident => true, _ => false }
TokenKind.LParen => match token.kind { TokenKind.LParen => true, _ => false }
TokenKind.LBrack=> match token.kind { TokenKind.LBrack => true, _ => false }
Expand Down Expand Up @@ -871,7 +867,7 @@ export type Parser {
AstNodeKind.FunctionDeclaration(node) => methods.push(node)
AstNodeKind.TypeDeclaration(node) => types.push(node)
AstNodeKind.EnumDeclaration(node) => enums.push(node)
_ => return unreachable()
_ => unreachable()
}
}

Expand Down Expand Up @@ -1246,7 +1242,7 @@ export type Parser {
if exprs[0] |inner| {
AstNodeKind.Grouped(inner: inner)
} else {
return unreachable()
unreachable()
}
} else {
AstNodeKind.Tuple(items: exprs)
Expand Down Expand Up @@ -1815,10 +1811,6 @@ export type Parser {
}
}

func unreachable<V>(): Result<V, ParseError> {
Err(ParseError(position: Position(line: 0, col: 0), kind: ParseErrorKind.Unreachable))
}

type Precedence {
// TODO: These are static functions because static constants don't yet exist
func none(): Int = 0
Expand Down
Loading

0 comments on commit b352861

Please sign in to comment.