Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge parserDev into main #6

Merged
merged 7 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions source/fnc/errors.d
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,27 @@ class SyntaxError : Error
string errorString = msg;

import tern.string : AnsiColor;
import core.exception;
try{
errorString ~= "\n Line: " ~ errData.lineCount.to!string;
errorString ~= "\n Col: " ~ errData.afterLineStart.to!string;
errorString ~= "\n\t";

errorString ~= "\n Line: " ~ errData.lineCount.to!string;
errorString ~= "\n Col: " ~ errData.afterLineStart.to!string;
errorString ~= "\n\t";

errorString ~= GLOBAL_ERROR_STATE[errData.startOfLine .. start];

errorString ~= GLOBAL_ERROR_STATE[errData.startOfLine .. start];
errorString ~= AnsiColor.BackgroundRed;

errorString ~= AnsiColor.BackgroundRed;
errorString ~= GLOBAL_ERROR_STATE[start .. end];
errorString ~= AnsiColor.Reset;
import std.algorithm : max;
// TODO: Make this better.
errorString ~= GLOBAL_ERROR_STATE[end .. max(errData.endOfProblemLine, end)];

errorString ~= GLOBAL_ERROR_STATE[start .. end];
errorString ~= AnsiColor.Reset;
import std.algorithm : max;
// TODO: Make this better.
errorString ~= GLOBAL_ERROR_STATE[end .. max(errData.endOfProblemLine, end)];

return errorString;
return errorString;
}catch(ArraySliceError e){
return msg ~ " (line and col can't be resolved. Please report this as a github issue with code samples)";
}
}

protected this(string msg, string file, size_t line, Throwable next = null) @nogc nothrow pure @safe
Expand Down
185 changes: 78 additions & 107 deletions source/fnc/treegen/ast_types.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ module fnc.treegen.ast_types;
import fnc.tokenizer.tokens : Token;
import tern.typecons.common : Nullable, nullable;

struct NameValuePair
{
Nullable!AstNode name;
AstNode value;
}

struct NamedUnit
{
dchar[][] names;
Expand All @@ -23,12 +29,14 @@ enum AstAction
WhileLoop,

AssignVariable, // Ex: x = 5;
ArrayGrouping, // [...]
ArrayGrouping, // [...] THIS IS A TEMPORATRY NODE THAT IS STILL BEING PARSED AND SHOULD BE RESOLVED TO SOMETHING ELSE
Array, // [1, 2, 3]
IndexInto, // X[N]

SingleArgumentOperation, // Ex: x++, ++x
DoubleArgumentOperation, // Ex: 9+10

GenericOf, // Not to be confused with TypeGeneric, ex: foo!bar(), foo!(bar)(), or "auto x classFoo!bar";
Call, // Ex: foo(bar);

// Misc tokens:
Expand All @@ -39,14 +47,7 @@ enum AstAction

TokenHolder, // A temporary Node that is yet to be parsed

// Type tokens
TypeTuple, // [int, float]
TypeArray, // int[3] OR int[]
TypeCall, // const(int) Note: const is ALSO a keyword
TypePointer, // *int
TypeReference, // &int
TypeGeneric, // Result!(int, string)
TypeVoidable
Voidable
}

bool isExpressionLike(AstAction action)
Expand All @@ -61,6 +62,7 @@ bool isCallable(AstAction action)
|| action == AstAction.SingleArgumentOperation
|| action == AstAction.Call
|| action == AstAction.Expression
|| action == AstAction.GenericOf
|| action == AstAction.LiteralUnit
|| action == AstAction.NamedUnit;
}
Expand Down Expand Up @@ -134,7 +136,9 @@ enum OperationVariety
NotEqualTo,

Period, // foo.bar
Arrow, // foo->bar
Range, // x..y OR 0..99
Voidable, // int?
}

import fnc.treegen.scope_parser : ScopeData;
Expand Down Expand Up @@ -182,29 +186,23 @@ struct ExpressionNodeData
AstNode[] components;
}

struct IndexIntoNodeData
struct ArrayOrIndexingNodeData
{
AstNode indexInto;
AstNode index;
}
/+++ These are the act of calling a function ++++/
struct FunctionCallArgument
{
Nullable!(dchar[]) specifiedName = Nullable!(dchar[])(null);
AstNode source;
AstNode index; // MIGHT BE NULL!
}

struct CallNodeData
{
AstNode func;
FunctionCallArgument[] args;
NameValuePair[] args;
}
/+++++++/

struct TypeGenericNodeData
struct GenericNodeData // Generic used in code. Ex: foo.bar!baz
{
AstNode left;
AstNode right;
AstNode symbolUsedAsGeneric;
AstNode genericData;
}

class AstNode
Expand All @@ -227,25 +225,23 @@ class AstNode
Token tokenBeingHeld; // TokenHolder

AstNode nodeToReturn; // ReturnStatement
IndexIntoNodeData indexIntoNodeData; // IndexInto
ArrayOrIndexingNodeData arrayOrIndexingNodeData; // IndexInto

struct
{ // TypeArray
AstNode firstNodeOperand; // This might be the thing being indexed
bool isIntegerLiteral;
AstNode[][] commaSeperatedNodes; // Declaring arrays, array types, typles, etc
}

TypeGenericNodeData typeGenericNodeData; // TypeGeneric
AstNode voidableType;
GenericNodeData genericNodeData; // GenericOf
NameValuePair[] arrayNodeData; // Array
AstNode voidableType; // Voidable
}
static AstNode VOID_NAMED_UNIT(){

static AstNode VOID_NAMED_UNIT()
{
AstNode voidNamedUnit = new AstNode();
voidNamedUnit.action = AstAction.NamedUnit;
import fnc.tokenizer.tokens : makeUnicodeString;

voidNamedUnit.namedUnit = NamedUnit(["void".makeUnicodeString]);
return voidNamedUnit;
}

void toString(scope void delegate(const(char)[]) sink) const
{
import std.conv;
Expand All @@ -260,13 +256,9 @@ class AstNode
case AstAction.TokenHolder:
sink(tokenBeingHeld.to!string);
break;
case AstAction.TypePointer:
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 All @@ -285,27 +277,6 @@ class AstNode
sink(doubleArgumentOperationNodeData.left.to!string);
sink(", ");
sink(doubleArgumentOperationNodeData.right.to!string);
break;
case AstAction.TypeArray:
bool hasFirstOperand = (cast(void*) firstNodeOperand) != null;
if (hasFirstOperand)
{
sink("Array of: ");
sink(firstNodeOperand.to!string);
sink(" ");
}
if (isIntegerLiteral)
{
sink("with ");
sink(commaSeperatedNodes[0][0].to!string);
sink(" elements");
}
else
foreach (const(AstNode[]) containingReductions; commaSeperatedNodes)
{
sink(commaSeperatedNodes.to!string);
}

break;
default:
break;
Expand All @@ -332,46 +303,13 @@ class AstNode

switch (action)
{
case AstAction.TypeGeneric:
case AstAction.GenericOf:
write(action);
writeln(":");
typeGenericNodeData.left.tree(tabCount + 1);
typeGenericNodeData.right.tree(tabCount + 1);
break;
case AstAction.TypePointer:
case AstAction.TypeReference:
write(action);
writeln(":");
foreach (subnode; expressionNodeData.components)
{
subnode.tree(tabCount + 1);
}
genericNodeData.symbolUsedAsGeneric.tree(tabCount + 1);
genericNodeData.genericData.tree(tabCount + 1);
break;
case AstAction.TypeArray:
bool hasFirstOperand = (cast(void*) firstNodeOperand) != null;
if (hasFirstOperand && commaSeperatedNodes.length)
writeln("List of N indexed with X");
else
writeln("List of X");
if (firstNodeOperand)
firstNodeOperand.tree(tabCount + 1);
foreach (AstNode[] possibleReducedNodes; commaSeperatedNodes)
{
if (possibleReducedNodes.length > 0)
possibleReducedNodes[0].tree(tabCount + 1);

}
break;
case AstAction.TypeTuple:
write(action);
writeln(":");
foreach (AstNode[] possibleReducedNodes; commaSeperatedNodes)
{
if (possibleReducedNodes.length > 0)
possibleReducedNodes[0].tree(tabCount + 1);

}
break;
case AstAction.Call:
writeln("Calling function resolved from:");
callNodeData.func.tree(tabCount + 1);
Expand All @@ -381,15 +319,15 @@ class AstNode
writeln(")");
foreach (arg; callNodeData.args)
{
if (arg.specifiedName != null)
if (arg.name != null)
{
printTabs();
arg.specifiedName.value.write();
arg.name.value.write();
": ".writeln;
arg.source.tree(tabCount + 2);
arg.value.tree(tabCount + 2);
}
else
arg.source.tree(tabCount + 1);
arg.value.tree(tabCount + 1);

}

Expand All @@ -406,14 +344,18 @@ class AstNode
singleArgumentOperationNodeData.value.tree(tabCount + 1);
break;
case AstAction.IndexInto:
writeln("Index into:");
writeln("Index or Array type:");
tabCount++;
printTabs();
writeln("This:");
indexIntoNodeData.indexInto.tree(tabCount + 1);
printTabs();
writeln("With this:");
indexIntoNodeData.index.tree(tabCount + 1);
writeln("With:");
arrayOrIndexingNodeData.indexInto.tree(tabCount + 1);
if (false != (arrayOrIndexingNodeData.index !is(null)))
{
printTabs();
writeln("Index value of:");

arrayOrIndexingNodeData.index.tree(tabCount + 2);
}
tabCount--;
break;

Expand Down Expand Up @@ -465,6 +407,26 @@ class AstNode
else
elseNodeData.elseResultNode.tree(tabCount + 1);
break;
case AstAction.Array:
write(action);
write(" of ");
write(arrayNodeData.length);
writeln(" items:");
tabCount++;
foreach (NameValuePair pair; arrayNodeData){
if (pair.name != null){
printTabs;
"Name:".writeln;
pair.name.value.tree(tabCount+1);
printTabs;
"Value:".writeln;
pair.value.tree(tabCount+1);
}else{
pair.value.tree(tabCount);
}
}
tabCount--;
break;
default:
writeln(this.to!string);
break;
Expand All @@ -485,10 +447,6 @@ void getRelatedTokens(AstNode node, ref Token[] output)
switch (node.action)
{
// TODO: Improve all of this
case AstAction.TypePointer:
case AstAction.TypeReference:
getRelatedTokens(node.firstNodeOperand, output);
break;
case AstAction.SingleArgumentOperation:
getRelatedTokens(node.singleArgumentOperationNodeData.value, output);
break;
Expand Down Expand Up @@ -558,3 +516,16 @@ Nullable!AstNode nextNonWhiteNode(AstNode[] nodes, ref size_t index)
}
return found;
}

Nullable!AstNode lowerBoundNonWhiteTest(AstNode[] nodes, size_t index)
{
while (1)
{
index--;
if (!index)
return Nullable!AstNode(null);
if (nodes[index].isWhite)
continue;
return Nullable!AstNode(nodes[index]);
}
}
Loading
Loading