Skip to content

Commit

Permalink
Functions work now
Browse files Browse the repository at this point in the history
  • Loading branch information
HeronErin authored May 8, 2024
1 parent b1b4432 commit a935284
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
2 changes: 1 addition & 1 deletion source/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void main()
// newScope.tree();
// ASSIG
import errors;
GLOBAL_ERROR_STATE = "auto x, y = hello(1, 2)(3);";
GLOBAL_ERROR_STATE = "auto x, y = hello(xasd:1, 2)(3, 4);";
auto t = DeclarationAndAssignment.matchesToken(GLOBAL_ERROR_STATE.tokenizeText, index);
// GLOBAL_ERROR_STATE.tokenizeText.writeln;
// (t != null).writeln;
Expand Down
77 changes: 52 additions & 25 deletions source/parsing/treegen/expressionParser.d
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,19 @@ private AstNode[][] splitNodesAtComma(AstNode[] inputNodes)
}
current ~= node;
}
nodes ~= current;
return nodes;
}
// Handle function calls and operators
public void phaseTwo(ref Array!AstNode nodes)
{
AstNode[] nonWhiteStack;
size_t[] nonWhiteIndexStack;

Array!AstNode newNodesArray;


scope (exit)
nodes = newNodesArray;

// Nullable!AstAction lastNonWhite = nullable!AstNode(null);
for (size_t index = 0; index < nodes.length; index++)
{
AstNode node = nodes[index];
Expand All @@ -130,7 +128,7 @@ public void phaseTwo(ref Array!AstNode nodes)
{
AstNode functionCall = new AstNode();
scope (exit)
newNodesArray[$-1] = functionCall;
newNodesArray[$ - 1] = functionCall;
scope (exit)
nonWhiteStack ~= functionCall;

Expand All @@ -142,27 +140,48 @@ public void phaseTwo(ref Array!AstNode nodes)
functionCall.callNodeData = callNodeData;

callNodeData.func = nonWhiteStack[$ - 1];
callNodeData.args = new FunctionCallArgument[0];

Array!AstNode components;
foreach (AstNode[] argumentNodeBatch; splitNodesAtComma(
node.expressionNodeData.components))
{
components.clear();
components ~= argumentNodeBatch;
Token firstToken = components[0].tokenBeingHeld;
phaseTwo(components);
scanAndMergeOperators(components);
components.removeAllWhitespace();



// Array!AstNode components;
// foreach (AstNode[] argumentNodeBatch; splitNodesAtComma(
// node.expressionNodeData.components))
// {
// components.clear();
// components ~= argumentNodeBatch;
// phaseTwo(components);
// scanAndMergeOperators(components);
// }
if (components.length != 1 && components.length != 3)
throw new SyntaxError("Function argument parsing error (node reduction)", firstToken);
FunctionCallArgument component;

scope (exit) callNodeData.args ~= component;

if (components.length == 1)
{
component.source = components[0];
continue;
}
components.writeln;
if (components[1].action != AstAction.TokenHolder)
throw new SyntaxError("Function argument parsing error (Must include colon for named arguments)", firstToken);
if (components[1].tokenBeingHeld.tokenVariety != TokenType.Colon)
throw new SyntaxError("Function argument parsing error (Must include colon for named arguments)", components[1]
.tokenBeingHeld);
if (components[0].action != AstAction.NamedUnit)
throw new SyntaxError("Function argument parsing error (Named argument name can't be determined)", firstToken);
component.specifiedName = Nullable!(dchar[])(components[0].namedUnit.names[0]);
component.source = components[2];
}

}
else
{
newNodesArray ~= node;
nonWhiteStack ~= node;
}
nonWhiteIndexStack ~= newNodesArray.length - 1;

// if (node.action == AstAction.Expression && lastNonWhite != null )
// {
Expand All @@ -171,8 +190,8 @@ public void phaseTwo(ref Array!AstNode nodes)

// Array!AstNode components;
// components ~= args.expressionNodeData.components;
// phaseTwo(components);
// scanAndMergeOperators(components);
// phaseTwo(components);
// scanAndMergeOperators(components);
// args.expressionNodeData.components.length = components.data.length;
// args.expressionNodeData.components[0 .. $] = components.data[0 .. $];

Expand All @@ -196,21 +215,29 @@ public void phaseTwo(ref Array!AstNode nodes)
}
}

void trimAstNodes(Array!AstNode nodes)
void trimAstNodes(ref Array!AstNode nodes)
{
// Remove starting whitespace
while (nodes.length && nodes[0].action == AstAction.TokenHolder &&
(nodes[0].tokenBeingHeld.tokenVariety == TokenType.WhiteSpace
|| nodes[0].tokenBeingHeld.tokenVariety == TokenType.Comment))
while (nodes.length && nodes[0].isWhite)
nodes.linearRemove(nodes[0 .. 1]);

// Remove ending whitespace
while (nodes.length && nodes[$ - 1].action == AstAction.TokenHolder &&
(nodes[$ - 1].tokenBeingHeld.tokenVariety == TokenType.WhiteSpace
|| nodes[$ - 1].tokenBeingHeld.tokenVariety == TokenType.Comment))
while (nodes.length && nodes[$ - 1].isWhite)
nodes.linearRemove(nodes[$ - 1 .. $]);
}

void removeAllWhitespace(ref Array!AstNode nodes)
{
Array!AstNode newNodes;
scope (exit)
nodes = newNodes;
foreach (AstNode node; nodes)
{
if (!node.isWhite)
newNodes ~= node;
}
}

Array!AstNode expressionNodeFromTokens(Token[] tokens)
{
AstNode[] phaseOneNodes = phaseOne(tokens);
Expand Down

0 comments on commit a935284

Please sign in to comment.