Skip to content

Commit

Permalink
Indexing support
Browse files Browse the repository at this point in the history
  • Loading branch information
HeronErin committed Apr 22, 2024
1 parent 33dd850 commit 50d4163
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 201 deletions.
1 change: 0 additions & 1 deletion source/parsing/tokenizer/make_tokens.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import tern.typecons.common : Nullable, nullable;

import parsing.tokenizer.tokens;

import std.stdio;

dchar[] handleMultilineCommentsAtIndex(dchar[] input, ref size_t index)
{
Expand Down
8 changes: 1 addition & 7 deletions source/parsing/tokenizer/tokens.d
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ dchar[] makeUnicodeString(in string input)

const(dchar[]) testMultiLineStyle(dchar first, dchar secound)
{
// validMultiLineCommentStyles.writeln;
foreach (const(dchar[][]) style; validMultiLineCommentStyles)
{
if (style[0][0] == first && style[0][1] == secound)
Expand Down Expand Up @@ -153,13 +152,8 @@ Nullable!Token nextToken(Token[] tokens, ref size_t index)
{
Nullable!Token found = null;
if (tokens.length <= index + 1)
{
import std.stdio;

"boinc".writeln;
found.ptr.writeln;
return found;
}


found = tokens[++index];
return found;
Expand Down
35 changes: 31 additions & 4 deletions source/parsing/treegen/astTypes.d
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum AstAction
DefineFunction,
DefineVariable, // Ex: int x;
AssignVariable, // Ex: x = 5;
IndexInto, // X[...]

SingleArgumentOperation, // Ex: x++, ++x, |x|, ||x||, -8
DoubleArgumentOperation, // Ex: 9+10
Expand All @@ -27,6 +28,12 @@ enum AstAction
TokenHolder // A temporary Node that is yet to be parsed
}

bool isExpressionLike(AstAction action)
{
return action == AstAction.Expression
|| action == AstAction.IndexInto;
}

struct KeywordNodeData
{
dchar[] keywordName;
Expand Down Expand Up @@ -60,8 +67,8 @@ enum OperationVariety
PostIncrement,
PreDecrement,
PostDecrement,
AbsuluteValue,
Magnitude,
// AbsuluteValue,
// Magnitude,

Add,
Substract,
Expand Down Expand Up @@ -93,6 +100,11 @@ enum OperationVariety
BitshiftLeftUnSigned,
BitshiftRightUnSigned,

BitshiftLeftSignedEq,
BitshiftRightSignedEq,
BitshiftLeftUnSignedEq,
BitshiftRightUnSignedEq,

LogicalOr,
LogicalAnd,
LogicalNot,
Expand Down Expand Up @@ -198,8 +210,12 @@ class AstNode
import std.stdio;
import std.conv;

foreach (i; 0 .. tabCount)
write("| ");
if (tabCount != -1)
{
foreach (i; 0 .. tabCount)
write("| ");
write("");
}

switch (action)
{
Expand All @@ -212,6 +228,17 @@ class AstNode
doubleArgumentOperationNodeData.left.tree(tabCount + 1);
doubleArgumentOperationNodeData.right.tree(tabCount + 1);
break;
case AstAction.SingleArgumentOperation:
writeln(singleArgumentOperationNodeData.operationVariety.to!string ~ ":");
singleArgumentOperationNodeData.value.tree(tabCount + 1);
break;
case AstAction.IndexInto:
writeln("Indexing into with result of:");
foreach (subnode; expressionNodeData.components)
{
subnode.tree(tabCount + 1);
}
break;
case AstAction.Expression:
writeln(
"Result of expression with " ~ expressionNodeData.components.length.to!string ~ " components:");
Expand Down
16 changes: 9 additions & 7 deletions source/parsing/treegen/expressionParser.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import std.stdio;
import std.container.array;

// Group letters.letters.letters into NamedUnit s
// Group Parenthesis into AstNode.Expression s to be parsed speratly
// Group Parenthesis and indexing into AstNode.Expression s to be parsed speratly
public AstNode[] phaseOne(Token[] tokens)
{
AstNode[] ret;
Expand All @@ -22,7 +22,10 @@ public AstNode[] phaseOne(Token[] tokens)
if (token.tokenVariety == TokenType.OpenBraces)
{
AstNode newExpression = new AstNode();
newExpression.action = AstAction.Expression;
if (token.value == "(" || token.value == "{")
newExpression.action = AstAction.Expression;
else if (token.value == "[")
newExpression.action = AstAction.IndexInto;
newExpression.expressionNodeData = ExpressionNodeData(
token.value[0],
braceOpenToBraceClose[token.value[0]],
Expand All @@ -35,12 +38,12 @@ public AstNode[] phaseOne(Token[] tokens)
{

if (parenthesisStack.length == 0)
throw new SyntaxError("Parenthesis closed but never opened");
throw new SyntaxError("Group token("~cast(char)token.value[0]~") closed but never opened");

AstNode node = parenthesisStack[$ - 1];

if (node.expressionNodeData.closer != token.value[0])
throw new SyntaxError("Parenthesis not closed with correct token");
throw new SyntaxError("Group token("~cast(char)token.value[0]~") not closed with correct token");

parenthesisStack.length--;

Expand Down Expand Up @@ -89,8 +92,7 @@ public void phaseTwo(Array!AstNode nodes)
for (size_t index = 0; index < nodes.length; index++)
{
AstNode node = nodes[index];
if (node.action == AstAction.NamedUnit && index + 1 < nodes.length && nodes[index + 1].action == AstAction
.Expression)
if (node.action == AstAction.NamedUnit && index + 1 < nodes.length && nodes[index + 1].action.isExpressionLike)
{
AstNode functionCall = new AstNode();
AstNode args = nodes[index + 1];
Expand All @@ -110,7 +112,7 @@ public void phaseTwo(Array!AstNode nodes)
nodes[index] = functionCall;
nodes.linearRemove(nodes[index + 1 .. index + 2]);
}
else if (node.action == AstAction.Expression)
else if (node.action.isExpressionLike)
{
Array!AstNode components;
components ~= node.expressionNodeData.components;
Expand Down
Loading

0 comments on commit 50d4163

Please sign in to comment.