Skip to content

Commit

Permalink
Add voidable to parser
Browse files Browse the repository at this point in the history
  • Loading branch information
HeronErin committed May 7, 2024
1 parent 554ace6 commit 0d4a6c8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
24 changes: 8 additions & 16 deletions source/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,13 @@ void main()
{
size_t index = 0;
// typeFromTokens("".tokenizeText, index);
auto toks = "if ((2..5).contains(4)) 3; else 7;".tokenizeText;
auto x = IfStatementWithoutScope.matchesToken(toks, index);
// toks.writeln;
(x != null).writeln;
x.value[0].tokens.writeln;
toks[index].writeln;

// auto newScope = parseMultilineScope(GLOBAL_SCOPE_PARSE, "
// import std.file;
// int main()
// {
// print(\"Hello World!\");
// __asm(
// ld a, b
// );
// }");
// newScope.tree();
auto newScope = parseMultilineScope(GLOBAL_SCOPE_PARSE, "
import std.os;
int main()
{
int? x = 2;
writeln(x);
}");
newScope.tree();
}
3 changes: 3 additions & 0 deletions source/parsing/tokenizer/tokens.d
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum TokenType
Quotation,
Comment,
ExclamationMark,
QuestionMark,
Period,
Filler
}
Expand Down Expand Up @@ -120,6 +121,8 @@ TokenType getVarietyOfLetter(dchar symbol)
return TokenType.Comma;
case '!':
return TokenType.ExclamationMark;
case '?':
return TokenType.QuestionMark;
default:
break;
}
Expand Down
5 changes: 5 additions & 0 deletions source/parsing/treegen/astTypes.d
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ enum AstAction
TypePointer, // *int
TypeReference, // &int
TypeGeneric, // Result!(int, string)
TypeVoidable

}

Expand Down Expand Up @@ -211,6 +212,7 @@ class AstNode
}

TypeGenericNodeData typeGenericNodeData; // TypeGeneric
AstNode voidableType;
}

void toString(scope void delegate(const(char)[]) sink) const
Expand All @@ -230,6 +232,9 @@ class AstNode
case AstAction.Expression:
sink(expressionNodeData.components.to!string);
break;
case AstAction.TypeVoidable:
sink(voidableType.to!string);
break;
case AstAction.NamedUnit:
sink(namedUnit.names.to!string);
break;
Expand Down
16 changes: 14 additions & 2 deletions source/parsing/treegen/typeParser.d
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,22 @@ private Nullable!AstNode handleNodeTreegen(AstNode node, ref AstNode[] previousl
return nullable!AstNode(null);
switch (node.tokenBeingHeld.tokenVariety)
{
case TokenType.QuestionMark:
if (previouslyParsedNodes.length == 0)
throw new SyntaxError(
"Can't determine voidable and it's connection to a type", node
.tokenBeingHeld);

AstNode newNode = new AstNode;
newNode.action = AstAction.TypeVoidable;
newNode.voidableType = previouslyParsedNodes[$-1];
previouslyParsedNodes[$-1] = newNode;
return nullable!AstNode(null);

case TokenType.ExclamationMark:
if (previouslyParsedNodes.length == 0)
throw new SyntaxError(
"Can't result template and it's connection to a type", node
"Can't determine template and it's connection to a type", node
.tokenBeingHeld);
AstNode newNode = new AstNode;
newNode.action = AstAction.TypeGeneric;
Expand Down Expand Up @@ -168,6 +180,7 @@ size_t prematureTypeLength(Token[] tokens, size_t index)

switch (token.tokenVariety)
{
case TokenType.QuestionMark:
case TokenType.Operator:
case TokenType.Comment:
case TokenType.WhiteSpace:
Expand Down Expand Up @@ -196,7 +209,6 @@ Nullable!AstNode typeFromTokens(Token[] tokens, ref size_t index)
return nullable!AstNode(null);

// Groups parenthesis and brackets into expression groups
Token firstToken = tokens[index];
AstNode[] protoNodes = phaseOne(tokens[index .. index += length]);
Nullable!(AstNode[]) maybeArray = genTypeTree(protoNodes);
if (maybeArray == null)
Expand Down

0 comments on commit 0d4a6c8

Please sign in to comment.