Skip to content

Commit

Permalink
fix(compiler): while nodes should have 3 children
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperFola committed Jun 7, 2024
1 parent f7d10e1 commit f6eb48e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
- fixed a bug in the compiler allowing the use of operators without any argument: `(+)`
- fixed a bug in the vm during error reporting when a non-function was used as a function
- refactored code inside the bytecode reader to promote code reuse
- fixed a bug in the compiler generating invalid `while` nodes

### Removed
- removed unused `NodeType::Closure`
Expand Down
8 changes: 5 additions & 3 deletions src/arkreactor/Compiler/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,15 +475,14 @@ namespace Ark
{
if (const auto sym = x.constList()[1]; sym.nodeType() != NodeType::Symbol)
throwCompilerError(fmt::format("Expected a symbol, got a {}", typeToString(sym)), sym);
if (x.constList().size() != 3)
throwCompilerError("Invalid node ; if it was computed by a macro, check that a node is returned", x);

const std::string name = x.constList()[1].string();
uint16_t i = addSymbol(x.constList()[1]);
if (n != Keyword::Set)
addDefinedSymbol(name);

if (x.constList().size() != 3)
throwCompilerError("Invalid node ; if it was computed by a macro, check that a node is returned", x);

// put value before symbol id
// starting at index = 2 because x is a (let|mut|set variable ...) node
for (std::size_t idx = 2, end = x.constList().size(); idx < end; ++idx)
Expand All @@ -499,6 +498,9 @@ namespace Ark

void Compiler::compileWhile(const Node& x, const int p)
{
if (x.constList().size() != 3)
throwCompilerError("Invalid node ; if it was computed by a macro, check that a node is returned", x);

// save current position to jump there at the end of the loop
std::size_t current = page(p).size();
// push condition
Expand Down
3 changes: 3 additions & 0 deletions tests/errors/compiler/invalid_while.ark
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(while ($ a b) {
(set acc (+ acc (@ src a)))
(set a (+ 1 a))})
7 changes: 7 additions & 0 deletions tests/errors/compiler/invalid_while.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
At (while (begin (set acc (+ acc (@ src b))) (set b (+ 1 b)))) @ 1:1
1 | (while ($ a b) {
| ^~~~~~~~~~~~~~~~
2 | (set acc (+ acc (@ src a)))
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 | (set a (+ 1 a))})
Invalid node ; if it was computed by a macro, check that a node is returned

0 comments on commit f6eb48e

Please sign in to comment.