-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(gen): handling of comptime pseudo vars (#156)
- remove from Prefs and set them during codegen - set only on usage - properly print them in failed asserts
- Loading branch information
Showing
14 changed files
with
120 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
<!-- | ||
Copyright (c) 2023-present Lukas Neubert. | ||
This Documentation is subject to the terms of the Mozilla Public License 2.0. | ||
SPDX-FileCopyrightText: 2023-present Lukas Neubert <[email protected]> | ||
SPDX-License-Identifier: MPL-2.0 | ||
--> | ||
|
||
# Bait Documentation | ||
> Documentation of the standard library is :construction: | ||
> Documentation of the standard library is work in progress :construction: | ||
## Entry Point | ||
The entry point of any program is the `main` function. It is automatically called when the program starts. | ||
|
@@ -424,28 +425,30 @@ struct FooBar { | |
| ----------- | ------------------------------------------- | ------ | | ||
| `@required` | The field must be initialized with a value. | _none_ | | ||
|
||
|
||
## Conditional Compilation | ||
### Compile Time Pseudo Variables | ||
Bait supports a few pseudo variables of `string` type. | ||
They are replaced with the actual value during compilation. | ||
|
||
| Variable | Description | Example | | ||
| ------------ | ----------------------------------- | ----------------------- | | ||
| `$PKG` | Name of the current package. | `main` | | ||
| `$FILE` | Relative path of the source file. | `lib/builtin/file.bt` | | ||
| `$ABS_FILE` | Absolute path of the source file. | `/path/to/file.bt` | | ||
| `$LINE` | Line number of it's appearance. | `123` | | ||
| `$FILE_LINE` | Relative path followed by the line. | `tests/my_test.bt:27` | | ||
| `$FUN` | Name of the current function. | `test_read_line` | | ||
| `$BAITEXE` | Absolut path to the Bait compiler. | `/path/to/bait/bait.js` | | ||
| `$BAITDIR` | Directory where the compiler is in. | `/path/to/bait` | | ||
| `$BAITHASH` | Short commit hash of the compiler. | `5e7fd6e` | | ||
Bait supports a few pseudo variables, which are replaced by their actual values during compilation. | ||
They are all of the type `string`. | ||
|
||
| Variable | Description | Example | | ||
| ------------ | --------------------------------- | ----------------------- | | ||
| `$PKG` | Current package name | `main` | | ||
| `$ABS_FILE` | Absolute source file path | `/path/to/file.bt` | | ||
| `$FILE` | Relative source file path | `lib/builtin/file.bt` | | ||
| `$LINE` | Line number where it is used | `123` | | ||
| `$FILE_LINE` | Relative path and the line | `tests/my_test.bt:27` | | ||
| `$FUN` | Current function name | `test_read_line` | | ||
| `$BAITEXE` | Absolut path to the Bait compiler | `/path/to/bait/bait.js` | | ||
| `$BAITDIR` | Directory of the compiler | `/path/to/bait` | | ||
| `$BAITHASH` | The compiler's short commit hash | `5e7fd6e` | | ||
|
||
They are useful for running external tools or debugging. For example: | ||
```bait | ||
eprintln('error in file ${$FILE}, line ${$LINE}, function ${$PKG}.${$FUN}') | ||
``` | ||
|
||
|
||
## Global Variables | ||
While the use of global variables is discouraged, they are important in some cases. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-FileCopyrightText: 2023-present Lukas Neubert <[email protected]> | ||
// SPDX-License-Identifier: MPL-2.0 | ||
package js | ||
|
||
import os | ||
import bait.ast | ||
import bait.token | ||
|
||
fun (mut g Gen) comptime_var(node ast.ComptimeVar){ | ||
g.write('from_js_string("') | ||
g.write(g.get_comptime_val(node.name, node.pos)) | ||
g.write('")') | ||
} | ||
|
||
fun (mut g Gen) get_comptime_val(name string, pos token.Pos) string { | ||
return match name { | ||
// Dynamic | ||
'PKG' { g.pkg } | ||
'ABS_FILE' { os.abs_path(g.path).replace('\\', '\\\\') } | ||
'FILE' { g.path.replace('\\', '\\\\') } | ||
'LINE' { pos.line.str() } | ||
'FILE_LINE' { | ||
file := g.get_comptime_val('FILE', pos) | ||
line := g.get_comptime_val('LINE', pos) | ||
'${file}:${line}' | ||
} | ||
'FUN' { g.cur_fun.name } | ||
// Cached | ||
'BAITEXE' { g.comptime_baitexe() } | ||
'BAITDIR' { g.comptime_baitdir() } | ||
'BAITHASH' { g.comptime_baithash() } | ||
} | ||
} | ||
|
||
fun (mut g Gen) comptime_baitexe() string { | ||
if g.baitexe.length == 0 { | ||
g.baitexe = os.executable().replace('\\', '\\\\') | ||
} | ||
return g.baitexe | ||
} | ||
|
||
fun (mut g Gen) comptime_baitdir() string { | ||
if g.baitdir.length == 0 { | ||
g.baitdir = os.dir(g.comptime_baitexe()).trim_right('\\') | ||
} | ||
return g.baitdir | ||
} | ||
|
||
fun (mut g Gen) comptime_baithash() string { | ||
if g.baithash.length == 0 { | ||
bd := g.comptime_baitdir() | ||
g.baithash = os.exec('git -C ${bd} rev-parse --short HEAD').stdout.trim_space() | ||
} | ||
return g.baithash | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// SPDX-FileCopyrightText: 2023-present Lukas Neubert <[email protected]> | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
import os | ||
|
||
fun test_comptime_vars() { | ||
assert $PKG == 'main' | ||
assert $FILE == 'tests/other/comptime_vars_test.bt'.replace('/', os.PATH_SEP) | ||
assert $LINE == '9' | ||
assert $FILE_LINE == 'tests/other/comptime_vars_test.bt:10'.replace('/', os.PATH_SEP) | ||
assert $FUN == 'test_comptime_vars' | ||
|
||
// The following vars are hard to test | ||
|
||
assert $ABS_FILE.length > $FILE.length | ||
|
||
assert $BAITEXE.length > 0 | ||
|
||
assert $BAITDIR.length < $BAITEXE.length | ||
|
||
assert $BAITHASH.length == 7 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// Copyright (c) 2023-present Lukas Neubert. | ||
// This Source Codeubject to the terms of the Mozilla Public License 2.0. | ||
// SPDX-FileCopyrightText: 2023-present Lukas Neubert <[email protected]> | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
fun test_floats() { | ||
f := 1.23 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// Copyright (c) 2023-present Lukas Neubert. | ||
// This Source Codeubject to the terms of the Mozilla Public License 2.0. | ||
// SPDX-FileCopyrightText: 2023-present Lukas Neubert <[email protected]> | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
fun test_pointer_deref() { | ||
n := 42 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// Copyright (c) 2023-present Lukas Neubert. | ||
// This Source Codeubject to the terms of the Mozilla Public License 2.0. | ||
// SPDX-FileCopyrightText: 2023-present Lukas Neubert <[email protected]> | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
fun test_u8_operations() { | ||
a := `a` | ||
|