Skip to content

Commit

Permalink
assert macro: Allow destructuring calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
FeepingCreature committed May 26, 2023
1 parent ec443dd commit 15adb44
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/neat/base.nt
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,8 @@ abstract class CompilerBase

abstract (string | :none) destructAstStringLiteral(ASTSymbol sym);

abstract (ASTSymbol target, ASTArgument[] args | :none) destructAstCall(ASTSymbol sym);

abstract ASTSymbol astFormatString(ASTSymbol[] parts, LocRange locRange);

ASTSymbol astBoolLiteral(bool value, LocRange locRange) {
Expand Down
7 changes: 7 additions & 0 deletions src/neat/compiler.nt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ class CompilerImpl : CompilerBase
return :none;
}

override (ASTSymbol target, ASTArgument[] args | :none) destructAstCall(ASTSymbol sym) {
if (auto call = sym.instanceOf(ASTCall)) {
return (call.target, call.args);
}
return :none;
}

override ASTSymbol astFormatString(ASTSymbol[] parts, LocRange locRange) {
return new ASTFormatString(parts, locRange);
}
Expand Down
15 changes: 15 additions & 0 deletions src/std/macro/assert.nt
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,24 @@ string repr(ASTSymbol sym, Context context) {
string text: return "\"$text\"";
(:none): {}
}
context.compiler.destructAstCall(sym).case {
(ASTSymbol target, ASTArgument[] args): return repr(target, context) ~ "(" ~ reprArgs(args, context) ~ ")";
(:none): {}
}

return "TODO";
}

string reprArgs(ASTArgument[] args, Context context) {
mut string res;
for (arg in args) {
if (!res.empty) res ~= ", ";
if (arg.name) res ~= "$(arg.name)=";
res ~= repr(arg.sym, context);
}
return res;
}

class ParseAssert : Macro
{
this() { }
Expand Down

0 comments on commit 15adb44

Please sign in to comment.