clang-format config

main
cosmonaut 2021-05-16 00:42:37 -07:00
parent 876e8deb6e
commit 473b706ad9
10 changed files with 1520 additions and 1005 deletions

166
.clang-format Normal file
View File

@ -0,0 +1,166 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveMacros: None
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: None
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
AttributeMacros:
- __capability
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: true
BreakBeforeBraces: Allman
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
StatementAttributeLikeMacros:
- Q_EMIT
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 1
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentCaseLabels: false
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentRequires: false
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 4
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 1000000
PenaltyIndentedWhitespace: 0
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceAroundPointerQualifiers: Default
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
BitFieldColonSpacing: Both
Standard: Latest
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
- NS_SWIFT_NAME
- CF_SWIFT_NAME
...

345
src/ast.c
View File

@ -1,7 +1,7 @@
#include "ast.h" #include "ast.h"
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "util.h" #include "util.h"
@ -9,83 +9,106 @@ const char* SyntaxKindString(SyntaxKind syntaxKind)
{ {
switch (syntaxKind) switch (syntaxKind)
{ {
case AccessExpression: return "AccessExpression"; case AccessExpression:
case AllocExpression: return "Alloc"; return "AccessExpression";
case Assignment: return "Assignment"; case AllocExpression:
case BinaryExpression: return "BinaryExpression"; return "Alloc";
case Comment: return "Comment"; case Assignment:
case CustomTypeNode: return "CustomTypeNode"; return "Assignment";
case Declaration: return "Declaration"; case BinaryExpression:
case ForLoop: return "ForLoop"; return "BinaryExpression";
case DeclarationSequence: return "DeclarationSequence"; case Comment:
case FunctionArgumentSequence: return "FunctionArgumentSequence"; return "Comment";
case FunctionCallExpression: return "FunctionCallExpression"; case CustomTypeNode:
case FunctionDeclaration: return "FunctionDeclaration"; return "CustomTypeNode";
case FunctionModifiers: return "FunctionModifiers"; case Declaration:
case FunctionSignature: return "FunctionSignature"; return "Declaration";
case FunctionSignatureArguments: return "FunctionSignatureArguments"; case ForLoop:
case Identifier: return "Identifier"; return "ForLoop";
case IfStatement: return "If"; case DeclarationSequence:
case IfElseStatement: return "IfElse"; return "DeclarationSequence";
case Number: return "Number"; case FunctionArgumentSequence:
case PrimitiveTypeNode: return "PrimitiveTypeNode"; return "FunctionArgumentSequence";
case ReferenceTypeNode: return "ReferenceTypeNode"; case FunctionCallExpression:
case Return: return "Return"; return "FunctionCallExpression";
case StatementSequence: return "StatementSequence"; case FunctionDeclaration:
case StaticModifier: return "StaticModifier"; return "FunctionDeclaration";
case StringLiteral: return "StringLiteral"; case FunctionModifiers:
case StructDeclaration: return "StructDeclaration"; return "FunctionModifiers";
case Type: return "Type"; case FunctionSignature:
case UnaryExpression: return "UnaryExpression"; return "FunctionSignature";
default: return "Unknown"; case FunctionSignatureArguments:
return "FunctionSignatureArguments";
case Identifier:
return "Identifier";
case IfStatement:
return "If";
case IfElseStatement:
return "IfElse";
case Number:
return "Number";
case PrimitiveTypeNode:
return "PrimitiveTypeNode";
case ReferenceTypeNode:
return "ReferenceTypeNode";
case Return:
return "Return";
case StatementSequence:
return "StatementSequence";
case StaticModifier:
return "StaticModifier";
case StringLiteral:
return "StringLiteral";
case StructDeclaration:
return "StructDeclaration";
case Type:
return "Type";
case UnaryExpression:
return "UnaryExpression";
default:
return "Unknown";
} }
} }
uint8_t IsPrimitiveType( uint8_t IsPrimitiveType(Node *typeNode)
Node *typeNode {
) {
return typeNode->type.typeNode->syntaxKind == PrimitiveTypeNode; return typeNode->type.typeNode->syntaxKind == PrimitiveTypeNode;
} }
Node* MakePrimitiveTypeNode( Node *MakePrimitiveTypeNode(PrimitiveType type)
PrimitiveType type {
) {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = PrimitiveTypeNode; node->syntaxKind = PrimitiveTypeNode;
node->primitiveType.type = type; node->primitiveType.type = type;
return node; return node;
} }
Node* MakeCustomTypeNode( Node *MakeCustomTypeNode(char *name)
char *name {
) {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = CustomTypeNode; node->syntaxKind = CustomTypeNode;
node->customType.name = strdup(name); node->customType.name = strdup(name);
return node; return node;
} }
Node* MakeReferenceTypeNode( Node *MakeReferenceTypeNode(Node *typeNode)
Node *typeNode {
) {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = ReferenceTypeNode; node->syntaxKind = ReferenceTypeNode;
node->referenceType.type = typeNode; node->referenceType.type = typeNode;
return node; return node;
} }
Node* MakeTypeNode( Node *MakeTypeNode(Node *typeNode)
Node* typeNode {
) {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = Type; node->syntaxKind = Type;
node->type.typeNode = typeNode; node->type.typeNode = typeNode;
return node; return node;
} }
Node* MakeIdentifierNode( Node *MakeIdentifierNode(const char *id)
const char *id {
) {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = Identifier; node->syntaxKind = Identifier;
node->identifier.name = strdup(id); node->identifier.name = strdup(id);
@ -93,9 +116,8 @@ Node* MakeIdentifierNode(
return node; return node;
} }
Node* MakeNumberNode( Node *MakeNumberNode(const char *numberString)
const char *numberString {
) {
char *ptr; char *ptr;
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = Number; node->syntaxKind = Number;
@ -103,9 +125,8 @@ Node* MakeNumberNode(
return node; return node;
} }
Node* MakeStringNode( Node *MakeStringNode(const char *string)
const char *string {
) {
size_t slen = strlen(string); size_t slen = strlen(string);
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = StringLiteral; node->syntaxKind = StringLiteral;
@ -121,10 +142,8 @@ Node* MakeStaticNode()
} }
/* FIXME: this sucks */ /* FIXME: this sucks */
Node* MakeFunctionModifiersNode( Node *MakeFunctionModifiersNode(Node **pModifierNodes, uint32_t modifierCount)
Node **pModifierNodes, {
uint32_t modifierCount
) {
uint32_t i; uint32_t i;
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = FunctionModifiers; node->syntaxKind = FunctionModifiers;
@ -132,7 +151,8 @@ Node* MakeFunctionModifiersNode(
node->functionModifiers.sequence = NULL; node->functionModifiers.sequence = NULL;
if (modifierCount > 0) if (modifierCount > 0)
{ {
node->functionModifiers.sequence = malloc(sizeof(Node*) * node->functionModifiers.count); node->functionModifiers.sequence =
malloc(sizeof(Node *) * node->functionModifiers.count);
for (i = 0; i < modifierCount; i += 1) for (i = 0; i < modifierCount; i += 1)
{ {
node->functionModifiers.sequence[i] = pModifierNodes[i]; node->functionModifiers.sequence[i] = pModifierNodes[i];
@ -142,10 +162,8 @@ Node* MakeFunctionModifiersNode(
return node; return node;
} }
Node* MakeUnaryNode( Node *MakeUnaryNode(UnaryOperator operator, Node * child)
UnaryOperator operator, {
Node *child
) {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = UnaryExpression; node->syntaxKind = UnaryExpression;
node->unaryExpression.operator= operator; node->unaryExpression.operator= operator;
@ -153,11 +171,8 @@ Node* MakeUnaryNode(
return node; return node;
} }
Node* MakeBinaryNode( Node *MakeBinaryNode(BinaryOperator operator, Node * left, Node *right)
BinaryOperator operator, {
Node *left,
Node *right
) {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = BinaryExpression; node->syntaxKind = BinaryExpression;
node->binaryExpression.left = left; node->binaryExpression.left = left;
@ -166,10 +181,8 @@ Node* MakeBinaryNode(
return node; return node;
} }
Node* MakeDeclarationNode( Node *MakeDeclarationNode(Node *typeNode, Node *identifierNode)
Node* typeNode, {
Node* identifierNode
) {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = Declaration; node->syntaxKind = Declaration;
node->declaration.type = typeNode; node->declaration.type = typeNode;
@ -177,10 +190,8 @@ Node* MakeDeclarationNode(
return node; return node;
} }
Node* MakeAssignmentNode( Node *MakeAssignmentNode(Node *left, Node *right)
Node *left, {
Node *right
) {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = Assignment; node->syntaxKind = Assignment;
node->assignmentStatement.left = left; node->assignmentStatement.left = left;
@ -188,9 +199,8 @@ Node* MakeAssignmentNode(
return node; return node;
} }
Node* StartStatementSequenceNode( Node *StartStatementSequenceNode(Node *statementNode)
Node *statementNode {
) {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = StatementSequence; node->syntaxKind = StatementSequence;
node->statementSequence.sequence = (Node **)malloc(sizeof(Node *)); node->statementSequence.sequence = (Node **)malloc(sizeof(Node *));
@ -199,19 +209,20 @@ Node* StartStatementSequenceNode(
return node; return node;
} }
Node* AddStatement( Node *AddStatement(Node *statementSequenceNode, Node *statementNode)
Node* statementSequenceNode, {
Node *statementNode statementSequenceNode->statementSequence.sequence = realloc(
) { statementSequenceNode->statementSequence.sequence,
statementSequenceNode->statementSequence.sequence = realloc(statementSequenceNode->statementSequence.sequence, sizeof(Node*) * (statementSequenceNode->statementSequence.count + 1)); sizeof(Node *) * (statementSequenceNode->statementSequence.count + 1));
statementSequenceNode->statementSequence.sequence[statementSequenceNode->statementSequence.count] = statementNode; statementSequenceNode->statementSequence
.sequence[statementSequenceNode->statementSequence.count] =
statementNode;
statementSequenceNode->statementSequence.count += 1; statementSequenceNode->statementSequence.count += 1;
return statementSequenceNode; return statementSequenceNode;
} }
Node* MakeReturnStatementNode( Node *MakeReturnStatementNode(Node *expressionNode)
Node *expressionNode {
) {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = Return; node->syntaxKind = Return;
node->returnStatement.expression = expressionNode; node->returnStatement.expression = expressionNode;
@ -225,9 +236,8 @@ Node* MakeReturnVoidStatementNode()
return node; return node;
} }
Node *StartFunctionSignatureArgumentsNode( Node *StartFunctionSignatureArgumentsNode(Node *argumentNode)
Node *argumentNode {
) {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = FunctionSignatureArguments; node->syntaxKind = FunctionSignatureArguments;
node->functionSignatureArguments.sequence = (Node **)malloc(sizeof(Node *)); node->functionSignatureArguments.sequence = (Node **)malloc(sizeof(Node *));
@ -236,12 +246,14 @@ Node *StartFunctionSignatureArgumentsNode(
return node; return node;
} }
Node* AddFunctionSignatureArgumentNode( Node *AddFunctionSignatureArgumentNode(Node *argumentsNode, Node *argumentNode)
Node *argumentsNode, {
Node *argumentNode argumentsNode->functionSignatureArguments.sequence = realloc(
) { argumentsNode->functionSignatureArguments.sequence,
argumentsNode->functionSignatureArguments.sequence = realloc(argumentsNode->functionSignatureArguments.sequence, sizeof(Node*) * (argumentsNode->functionSignatureArguments.count + 1)); sizeof(Node *) * (argumentsNode->functionSignatureArguments.count + 1));
argumentsNode->functionSignatureArguments.sequence[argumentsNode->functionSignatureArguments.count] = argumentNode; argumentsNode->functionSignatureArguments
.sequence[argumentsNode->functionSignatureArguments.count] =
argumentNode;
argumentsNode->functionSignatureArguments.count += 1; argumentsNode->functionSignatureArguments.count += 1;
return argumentsNode; return argumentsNode;
} }
@ -259,8 +271,8 @@ Node* MakeFunctionSignatureNode(
Node *identifierNode, Node *identifierNode,
Node *typeNode, Node *typeNode,
Node *arguments, Node *arguments,
Node* modifiersNode Node *modifiersNode)
) { {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = FunctionSignature; node->syntaxKind = FunctionSignature;
node->functionSignature.identifier = identifierNode; node->functionSignature.identifier = identifierNode;
@ -272,8 +284,8 @@ Node* MakeFunctionSignatureNode(
Node *MakeFunctionDeclarationNode( Node *MakeFunctionDeclarationNode(
Node *functionSignatureNode, Node *functionSignatureNode,
Node* functionBodyNode Node *functionBodyNode)
) { {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = FunctionDeclaration; node->syntaxKind = FunctionDeclaration;
node->functionDeclaration.functionSignature = functionSignatureNode; node->functionDeclaration.functionSignature = functionSignatureNode;
@ -283,8 +295,8 @@ Node* MakeFunctionDeclarationNode(
Node *MakeStructDeclarationNode( Node *MakeStructDeclarationNode(
Node *identifierNode, Node *identifierNode,
Node *declarationSequenceNode Node *declarationSequenceNode)
) { {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = StructDeclaration; node->syntaxKind = StructDeclaration;
node->structDeclaration.identifier = identifierNode; node->structDeclaration.identifier = identifierNode;
@ -292,9 +304,8 @@ Node* MakeStructDeclarationNode(
return node; return node;
} }
Node* StartDeclarationSequenceNode( Node *StartDeclarationSequenceNode(Node *declarationNode)
Node *declarationNode {
) {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = DeclarationSequence; node->syntaxKind = DeclarationSequence;
node->declarationSequence.sequence = (Node **)malloc(sizeof(Node *)); node->declarationSequence.sequence = (Node **)malloc(sizeof(Node *));
@ -303,19 +314,21 @@ Node* StartDeclarationSequenceNode(
return node; return node;
} }
Node* AddDeclarationNode( Node *AddDeclarationNode(Node *declarationSequenceNode, Node *declarationNode)
Node *declarationSequenceNode, {
Node *declarationNode declarationSequenceNode->declarationSequence.sequence = (Node **)realloc(
) { declarationSequenceNode->declarationSequence.sequence,
declarationSequenceNode->declarationSequence.sequence = (Node**) realloc(declarationSequenceNode->declarationSequence.sequence, sizeof(Node*) * (declarationSequenceNode->declarationSequence.count + 1)); sizeof(Node *) *
declarationSequenceNode->declarationSequence.sequence[declarationSequenceNode->declarationSequence.count] = declarationNode; (declarationSequenceNode->declarationSequence.count + 1));
declarationSequenceNode->declarationSequence
.sequence[declarationSequenceNode->declarationSequence.count] =
declarationNode;
declarationSequenceNode->declarationSequence.count += 1; declarationSequenceNode->declarationSequence.count += 1;
return declarationSequenceNode; return declarationSequenceNode;
} }
Node* StartFunctionArgumentSequenceNode( Node *StartFunctionArgumentSequenceNode(Node *argumentNode)
Node *argumentNode {
) {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = FunctionArgumentSequence; node->syntaxKind = FunctionArgumentSequence;
node->functionArgumentSequence.sequence = (Node **)malloc(sizeof(Node *)); node->functionArgumentSequence.sequence = (Node **)malloc(sizeof(Node *));
@ -324,12 +337,15 @@ Node* StartFunctionArgumentSequenceNode(
return node; return node;
} }
Node* AddFunctionArgumentNode( Node *AddFunctionArgumentNode(Node *argumentSequenceNode, Node *argumentNode)
Node *argumentSequenceNode, {
Node *argumentNode argumentSequenceNode->functionArgumentSequence.sequence = (Node **)realloc(
) { argumentSequenceNode->functionArgumentSequence.sequence,
argumentSequenceNode->functionArgumentSequence.sequence = (Node**) realloc(argumentSequenceNode->functionArgumentSequence.sequence, sizeof(Node*) * (argumentSequenceNode->functionArgumentSequence.count + 1)); sizeof(Node *) *
argumentSequenceNode->functionArgumentSequence.sequence[argumentSequenceNode->functionArgumentSequence.count] = argumentNode; (argumentSequenceNode->functionArgumentSequence.count + 1));
argumentSequenceNode->functionArgumentSequence
.sequence[argumentSequenceNode->functionArgumentSequence.count] =
argumentNode;
argumentSequenceNode->functionArgumentSequence.count += 1; argumentSequenceNode->functionArgumentSequence.count += 1;
return argumentSequenceNode; return argumentSequenceNode;
} }
@ -345,8 +361,8 @@ Node *MakeEmptyFunctionArgumentSequenceNode()
Node *MakeFunctionCallExpressionNode( Node *MakeFunctionCallExpressionNode(
Node *identifierNode, Node *identifierNode,
Node *argumentSequenceNode Node *argumentSequenceNode)
) { {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = FunctionCallExpression; node->syntaxKind = FunctionCallExpression;
node->functionCallExpression.identifier = identifierNode; node->functionCallExpression.identifier = identifierNode;
@ -354,10 +370,8 @@ Node* MakeFunctionCallExpressionNode(
return node; return node;
} }
Node* MakeAccessExpressionNode( Node *MakeAccessExpressionNode(Node *accessee, Node *accessor)
Node *accessee, {
Node *accessor
) {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = AccessExpression; node->syntaxKind = AccessExpression;
node->accessExpression.accessee = accessee; node->accessExpression.accessee = accessee;
@ -373,10 +387,8 @@ Node* MakeAllocNode(Node *typeNode)
return node; return node;
} }
Node* MakeIfNode( Node *MakeIfNode(Node *expressionNode, Node *statementSequenceNode)
Node *expressionNode, {
Node *statementSequenceNode
) {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = IfStatement; node->syntaxKind = IfStatement;
node->ifStatement.expression = expressionNode; node->ifStatement.expression = expressionNode;
@ -384,10 +396,8 @@ Node* MakeIfNode(
return node; return node;
} }
Node* MakeIfElseNode( Node *MakeIfElseNode(Node *ifNode, Node *elseNode)
Node *ifNode, {
Node *elseNode
) {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = IfElseStatement; node->syntaxKind = IfElseStatement;
node->ifElseStatement.ifStatement = ifNode; node->ifElseStatement.ifStatement = ifNode;
@ -399,8 +409,8 @@ Node* MakeForLoopNode(
Node *declarationNode, Node *declarationNode,
Node *startNumberNode, Node *startNumberNode,
Node *endNumberNode, Node *endNumberNode,
Node *statementSequenceNode Node *statementSequenceNode)
) { {
Node *node = (Node *)malloc(sizeof(Node)); Node *node = (Node *)malloc(sizeof(Node));
node->syntaxKind = ForLoop; node->syntaxKind = ForLoop;
node->forLoop.declaration = declarationNode; node->forLoop.declaration = declarationNode;
@ -414,10 +424,14 @@ static const char* PrimitiveTypeToString(PrimitiveType type)
{ {
switch (type) switch (type)
{ {
case Int: return "Int"; case Int:
case UInt: return "UInt"; return "Int";
case Bool: return "Bool"; case UInt:
case Void: return "Void"; return "UInt";
case Bool:
return "Bool";
case Void:
return "Void";
} }
return "Unknown"; return "Unknown";
@ -552,14 +566,19 @@ void PrintNode(Node *node, uint32_t tabCount)
printf("\n"); printf("\n");
for (i = 0; i < node->functionSignatureArguments.count; i += 1) for (i = 0; i < node->functionSignatureArguments.count; i += 1)
{ {
PrintNode(node->functionSignatureArguments.sequence[i], tabCount + 1); PrintNode(
node->functionSignatureArguments.sequence[i],
tabCount + 1);
} }
return; return;
case Identifier: case Identifier:
if (node->typeTag == NULL) { if (node->typeTag == NULL)
{
printf("%s\n", node->identifier.name); printf("%s\n", node->identifier.name);
} else { }
else
{
char *type = TypeTagToString(node->typeTag); char *type = TypeTagToString(node->typeTag);
printf("%s<%s>\n", node->identifier.name, type); printf("%s<%s>\n", node->identifier.name, type);
} }
@ -632,14 +651,19 @@ void PrintNode(Node *node, uint32_t tabCount)
} }
} }
TypeTag* MakeTypeTag(Node *node) { TypeTag *MakeTypeTag(Node *node)
if (node == NULL) { {
fprintf(stderr, "wraith: Attempted to call MakeTypeTag on null value.\n"); if (node == NULL)
{
fprintf(
stderr,
"wraith: Attempted to call MakeTypeTag on null value.\n");
return NULL; return NULL;
} }
TypeTag *tag = (TypeTag *)malloc(sizeof(TypeTag)); TypeTag *tag = (TypeTag *)malloc(sizeof(TypeTag));
switch (node->syntaxKind) { switch (node->syntaxKind)
{
case Type: case Type:
tag = MakeTypeTag(node->type.typeNode); tag = MakeTypeTag(node->type.typeNode);
break; break;
@ -665,15 +689,18 @@ TypeTag* MakeTypeTag(Node *node) {
case StructDeclaration: case StructDeclaration:
tag->type = Custom; tag->type = Custom;
tag->value.customType = strdup(node->structDeclaration.identifier->identifier.name); tag->value.customType =
strdup(node->structDeclaration.identifier->identifier.name);
break; break;
case FunctionDeclaration: case FunctionDeclaration:
tag = MakeTypeTag(node->functionDeclaration.functionSignature->functionSignature.type); tag = MakeTypeTag(node->functionDeclaration.functionSignature
->functionSignature.type);
break; break;
default: default:
fprintf(stderr, fprintf(
stderr,
"wraith: Attempted to call MakeTypeTag on" "wraith: Attempted to call MakeTypeTag on"
" node with unsupported SyntaxKind: %s\n", " node with unsupported SyntaxKind: %s\n",
SyntaxKindString(node->syntaxKind)); SyntaxKindString(node->syntaxKind));
@ -682,18 +709,24 @@ TypeTag* MakeTypeTag(Node *node) {
return tag; return tag;
} }
char* TypeTagToString(TypeTag *tag) { char *TypeTagToString(TypeTag *tag)
if (tag == NULL) { {
fprintf(stderr, "wraith: Attempted to call TypeTagToString with null value\n"); if (tag == NULL)
{
fprintf(
stderr,
"wraith: Attempted to call TypeTagToString with null value\n");
return NULL; return NULL;
} }
switch (tag->type) { switch (tag->type)
{
case Unknown: case Unknown:
return "Unknown"; return "Unknown";
case Primitive: case Primitive:
return PrimitiveTypeToString(tag->value.primitiveType); return PrimitiveTypeToString(tag->value.primitiveType);
case Reference: { case Reference:
{
char *inner = TypeTagToString(tag->value.referenceType); char *inner = TypeTagToString(tag->value.referenceType);
size_t innerStrLen = strlen(inner); size_t innerStrLen = strlen(inner);
char *result = malloc(sizeof(char) * (innerStrLen + 5)); char *result = malloc(sizeof(char) * (innerStrLen + 5));

125
src/ast.h
View File

@ -1,8 +1,8 @@
#ifndef WRAITH_AST_H #ifndef WRAITH_AST_H
#define WRAITH_AST_H #define WRAITH_AST_H
#include <stdint.h>
#include "identcheck.h" #include "identcheck.h"
#include <stdint.h>
/* -Wpedantic nameless union/struct silencing */ /* -Wpedantic nameless union/struct silencing */
#ifndef WRAITHNAMELESS #ifndef WRAITHNAMELESS
@ -282,112 +282,48 @@ struct Node
const char *SyntaxKindString(SyntaxKind syntaxKind); const char *SyntaxKindString(SyntaxKind syntaxKind);
uint8_t IsPrimitiveType(Node *typeNode); uint8_t IsPrimitiveType(Node *typeNode);
Node* MakePrimitiveTypeNode( Node *MakePrimitiveTypeNode(PrimitiveType type);
PrimitiveType type Node *MakeCustomTypeNode(char *string);
); Node *MakeReferenceTypeNode(Node *typeNode);
Node* MakeCustomTypeNode( Node *MakeTypeNode(Node *typeNode);
char *string Node *MakeIdentifierNode(const char *id);
); Node *MakeNumberNode(const char *numberString);
Node* MakeReferenceTypeNode( Node *MakeStringNode(const char *string);
Node *typeNode
);
Node* MakeTypeNode(
Node *typeNode /* can be primitive, custom, or reference */
);
Node* MakeIdentifierNode(
const char *id
);
Node* MakeNumberNode(
const char *numberString
);
Node* MakeStringNode(
const char *string
);
Node *MakeStaticNode(); Node *MakeStaticNode();
Node* MakeFunctionModifiersNode( Node *MakeFunctionModifiersNode(Node **pModifierNodes, uint32_t modifierCount);
Node **pModifierNodes, Node *MakeUnaryNode(UnaryOperator operator, Node * child);
uint32_t modifierCount Node *MakeBinaryNode(BinaryOperator operator, Node * left, Node *right);
); Node *MakeDeclarationNode(Node *typeNode, Node *identifierNode);
Node* MakeUnaryNode( Node *MakeAssignmentNode(Node *left, Node *right);
UnaryOperator operator, Node *StartStatementSequenceNode(Node *statementNode);
Node *child Node *AddStatement(Node *statementSequenceNode, Node *statementNode);
); Node *MakeReturnStatementNode(Node *expressionNode);
Node* MakeBinaryNode(
BinaryOperator operator,
Node *left,
Node *right
);
Node* MakeDeclarationNode(
Node* typeNode,
Node* identifierNode
);
Node* MakeAssignmentNode(
Node *left,
Node *right
);
Node* StartStatementSequenceNode(
Node* statementNode
);
Node* AddStatement(
Node* statementSequenceNode,
Node *statementNode
);
Node* MakeReturnStatementNode(
Node *expressionNode
);
Node *MakeReturnVoidStatementNode(); Node *MakeReturnVoidStatementNode();
Node* StartFunctionSignatureArgumentsNode( Node *StartFunctionSignatureArgumentsNode(Node *argumentNode);
Node *argumentNode Node *AddFunctionSignatureArgumentNode(Node *argumentsNode, Node *argumentNode);
);
Node* AddFunctionSignatureArgumentNode(
Node *argumentsNode,
Node *argumentNode
);
Node *MakeEmptyFunctionSignatureArgumentsNode(); Node *MakeEmptyFunctionSignatureArgumentsNode();
Node *MakeFunctionSignatureNode( Node *MakeFunctionSignatureNode(
Node *identifierNode, Node *identifierNode,
Node *typeNode, Node *typeNode,
Node *argumentsNode, Node *argumentsNode,
Node* modifiersNode Node *modifiersNode);
);
Node *MakeFunctionDeclarationNode( Node *MakeFunctionDeclarationNode(
Node *functionSignatureNode, Node *functionSignatureNode,
Node* functionBodyNode Node *functionBodyNode);
);
Node *MakeStructDeclarationNode( Node *MakeStructDeclarationNode(
Node *identifierNode, Node *identifierNode,
Node *declarationSequenceNode Node *declarationSequenceNode);
); Node *StartDeclarationSequenceNode(Node *declarationNode);
Node* StartDeclarationSequenceNode( Node *AddDeclarationNode(Node *declarationSequenceNode, Node *declarationNode);
Node *declarationNode Node *StartFunctionArgumentSequenceNode(Node *argumentNode);
); Node *AddFunctionArgumentNode(Node *argumentSequenceNode, Node *argumentNode);
Node* AddDeclarationNode(
Node *declarationSequenceNode,
Node *declarationNode
);
Node *StartFunctionArgumentSequenceNode(
Node *argumentNode
);
Node *AddFunctionArgumentNode(
Node *argumentSequenceNode,
Node *argumentNode
);
Node *MakeEmptyFunctionArgumentSequenceNode(); Node *MakeEmptyFunctionArgumentSequenceNode();
Node *MakeFunctionCallExpressionNode( Node *MakeFunctionCallExpressionNode(
Node *identifierNode, Node *identifierNode,
Node *argumentSequenceNode Node *argumentSequenceNode);
); Node *MakeAccessExpressionNode(Node *accessee, Node *accessor);
Node* MakeAccessExpressionNode( Node *MakeAllocNode(Node *typeNode);
Node *accessee, Node *MakeIfNode(Node *expressionNode, Node *statementSequenceNode);
Node *accessor
);
Node* MakeAllocNode(
Node *typeNode
);
Node* MakeIfNode(
Node *expressionNode,
Node *statementSequenceNode
);
Node *MakeIfElseNode( Node *MakeIfElseNode(
Node *ifNode, Node *ifNode,
Node *elseNode /* can be a conditional or a statement sequence */ Node *elseNode /* can be a conditional or a statement sequence */
@ -396,8 +332,7 @@ Node* MakeForLoopNode(
Node *identifierNode, Node *identifierNode,
Node *startNumberNode, Node *startNumberNode,
Node *endNumberNode, Node *endNumberNode,
Node *statementSequenceNode Node *statementSequenceNode);
);
void PrintNode(Node *node, uint32_t tabCount); void PrintNode(Node *node, uint32_t tabCount);
const char *SyntaxKindString(SyntaxKind syntaxKind); const char *SyntaxKindString(SyntaxKind syntaxKind);

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,14 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h>
#include <string.h> #include <string.h>
#include "ast.h" #include "ast.h"
#include "identcheck.h" #include "identcheck.h"
IdNode* MakeIdNode(NodeType type, char *name, IdNode *parent) { IdNode *MakeIdNode(NodeType type, char *name, IdNode *parent)
{
IdNode *node = (IdNode *)malloc(sizeof(IdNode)); IdNode *node = (IdNode *)malloc(sizeof(IdNode));
node->type = type; node->type = type;
node->name = strdup(name); node->name = strdup(name);
@ -19,50 +20,79 @@ IdNode* MakeIdNode(NodeType type, char *name, IdNode *parent) {
return node; return node;
} }
void AddChildToNode(IdNode *node, IdNode *child) { void AddChildToNode(IdNode *node, IdNode *child)
if (child == NULL) return; {
if (child == NULL)
return;
if (node->children == NULL) { if (node->children == NULL)
{
node->childCapacity = 2; node->childCapacity = 2;
node->children = (IdNode**) malloc(sizeof(IdNode*) * node->childCapacity); node->children =
} else if (node->childCount == node->childCapacity) { (IdNode **)malloc(sizeof(IdNode *) * node->childCapacity);
}
else if (node->childCount == node->childCapacity)
{
node->childCapacity *= 2; node->childCapacity *= 2;
node->children = (IdNode**) realloc(node->children, sizeof(IdNode*) * node->childCapacity); node->children = (IdNode **)realloc(
node->children,
sizeof(IdNode *) * node->childCapacity);
} }
node->children[node->childCount] = child; node->children[node->childCount] = child;
node->childCount += 1; node->childCount += 1;
} }
IdNode* MakeIdTree(Node *astNode, IdNode *parent) { IdNode *MakeIdTree(Node *astNode, IdNode *parent)
{
uint32_t i; uint32_t i;
IdNode *mainNode; IdNode *mainNode;
switch (astNode->syntaxKind) { switch (astNode->syntaxKind)
{
case AccessExpression: case AccessExpression:
AddChildToNode(parent, MakeIdTree(astNode->accessExpression.accessee, parent)); AddChildToNode(
AddChildToNode(parent, MakeIdTree(astNode->accessExpression.accessor, parent)); parent,
MakeIdTree(astNode->accessExpression.accessee, parent));
AddChildToNode(
parent,
MakeIdTree(astNode->accessExpression.accessor, parent));
return NULL; return NULL;
case AllocExpression: case AllocExpression:
AddChildToNode(parent, MakeIdTree(astNode->allocExpression.type, parent)); AddChildToNode(
parent,
MakeIdTree(astNode->allocExpression.type, parent));
return NULL; return NULL;
case Assignment: { case Assignment:
if (astNode->assignmentStatement.left->syntaxKind == Declaration) { {
if (astNode->assignmentStatement.left->syntaxKind == Declaration)
{
return MakeIdTree(astNode->assignmentStatement.left, parent); return MakeIdTree(astNode->assignmentStatement.left, parent);
} else { }
AddChildToNode(parent, MakeIdTree(astNode->assignmentStatement.left, parent)); else
AddChildToNode(parent, MakeIdTree(astNode->assignmentStatement.right, parent)); {
AddChildToNode(
parent,
MakeIdTree(astNode->assignmentStatement.left, parent));
AddChildToNode(
parent,
MakeIdTree(astNode->assignmentStatement.right, parent));
return NULL; return NULL;
} }
} }
case BinaryExpression: case BinaryExpression:
AddChildToNode(parent, MakeIdTree(astNode->binaryExpression.left, parent)); AddChildToNode(
AddChildToNode(parent, MakeIdTree(astNode->binaryExpression.right, parent)); parent,
MakeIdTree(astNode->binaryExpression.left, parent));
AddChildToNode(
parent,
MakeIdTree(astNode->binaryExpression.right, parent));
return NULL; return NULL;
case Declaration: { case Declaration:
{
Node *idNode = astNode->declaration.identifier; Node *idNode = astNode->declaration.identifier;
mainNode = MakeIdNode(Variable, idNode->identifier.name, parent); mainNode = MakeIdNode(Variable, idNode->identifier.name, parent);
mainNode->typeTag = MakeTypeTag(astNode); mainNode->typeTag = MakeTypeTag(astNode);
@ -70,16 +100,20 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
break; break;
} }
case DeclarationSequence: { case DeclarationSequence:
{
mainNode = MakeIdNode(UnorderedScope, "", parent); mainNode = MakeIdNode(UnorderedScope, "", parent);
for (i = 0; i < astNode->declarationSequence.count; i++) { for (i = 0; i < astNode->declarationSequence.count; i++)
{
AddChildToNode( AddChildToNode(
mainNode, MakeIdTree(astNode->declarationSequence.sequence[i], mainNode)); mainNode,
MakeIdTree(astNode->declarationSequence.sequence[i], mainNode));
} }
break; break;
} }
case ForLoop: { case ForLoop:
{
Node *loopDecl = astNode->forLoop.declaration; Node *loopDecl = astNode->forLoop.declaration;
Node *loopBody = astNode->forLoop.statementSequence; Node *loopBody = astNode->forLoop.statementSequence;
mainNode = MakeIdNode(OrderedScope, "for-loop", parent); mainNode = MakeIdNode(OrderedScope, "for-loop", parent);
@ -89,19 +123,29 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
} }
case FunctionArgumentSequence: case FunctionArgumentSequence:
for (i = 0; i < astNode->functionArgumentSequence.count; i++) { for (i = 0; i < astNode->functionArgumentSequence.count; i++)
{
AddChildToNode( AddChildToNode(
parent, MakeIdTree(astNode->functionArgumentSequence.sequence[i], parent)); parent,
MakeIdTree(
astNode->functionArgumentSequence.sequence[i],
parent));
} }
return NULL; return NULL;
case FunctionCallExpression: case FunctionCallExpression:
AddChildToNode(parent, MakeIdTree(astNode->functionCallExpression.identifier, parent));
AddChildToNode( AddChildToNode(
parent, MakeIdTree(astNode->functionCallExpression.argumentSequence, parent)); parent,
MakeIdTree(astNode->functionCallExpression.identifier, parent));
AddChildToNode(
parent,
MakeIdTree(
astNode->functionCallExpression.argumentSequence,
parent));
return NULL; return NULL;
case FunctionDeclaration: { case FunctionDeclaration:
{
Node *sigNode = astNode->functionDeclaration.functionSignature; Node *sigNode = astNode->functionDeclaration.functionSignature;
Node *idNode = sigNode->functionSignature.identifier; Node *idNode = sigNode->functionSignature.identifier;
char *funcName = idNode->identifier.name; char *funcName = idNode->identifier.name;
@ -113,30 +157,37 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
break; break;
} }
case FunctionSignatureArguments: { case FunctionSignatureArguments:
for (i = 0; i < astNode->functionSignatureArguments.count; i++) { {
for (i = 0; i < astNode->functionSignatureArguments.count; i++)
{
Node *argNode = astNode->functionSignatureArguments.sequence[i]; Node *argNode = astNode->functionSignatureArguments.sequence[i];
AddChildToNode(parent, MakeIdTree(argNode, parent)); AddChildToNode(parent, MakeIdTree(argNode, parent));
} }
return NULL; return NULL;
} }
case Identifier: { case Identifier:
{
char *name = astNode->identifier.name; char *name = astNode->identifier.name;
mainNode = MakeIdNode(Placeholder, name, parent); mainNode = MakeIdNode(Placeholder, name, parent);
IdNode *lookupNode = LookupId(mainNode, NULL, name); IdNode *lookupNode = LookupId(mainNode, NULL, name);
if (lookupNode == NULL) { if (lookupNode == NULL)
{
fprintf(stderr, "wraith: Could not find IdNode for id %s\n", name); fprintf(stderr, "wraith: Could not find IdNode for id %s\n", name);
TypeTag *tag = (TypeTag *)malloc(sizeof(TypeTag)); TypeTag *tag = (TypeTag *)malloc(sizeof(TypeTag));
tag->type = Unknown; tag->type = Unknown;
astNode->typeTag = tag; astNode->typeTag = tag;
} else { }
else
{
astNode->typeTag = lookupNode->typeTag; astNode->typeTag = lookupNode->typeTag;
} }
break; break;
} }
case IfStatement: { case IfStatement:
{
Node *clause = astNode->ifStatement.expression; Node *clause = astNode->ifStatement.expression;
Node *stmtSeq = astNode->ifStatement.statementSequence; Node *stmtSeq = astNode->ifStatement.statementSequence;
mainNode = MakeIdNode(OrderedScope, "if", parent); mainNode = MakeIdNode(OrderedScope, "if", parent);
@ -145,7 +196,8 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
break; break;
} }
case IfElseStatement: { case IfElseStatement:
{
Node *ifNode = astNode->ifElseStatement.ifStatement; Node *ifNode = astNode->ifElseStatement.ifStatement;
Node *elseStmts = astNode->ifElseStatement.elseStatement; Node *elseStmts = astNode->ifElseStatement.elseStatement;
mainNode = MakeIdNode(OrderedScope, "if-else", parent); mainNode = MakeIdNode(OrderedScope, "if-else", parent);
@ -162,23 +214,29 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
return NULL; return NULL;
case Return: case Return:
AddChildToNode(parent, MakeIdTree(astNode->returnStatement.expression, parent)); AddChildToNode(
parent,
MakeIdTree(astNode->returnStatement.expression, parent));
return NULL; return NULL;
case StatementSequence: { case StatementSequence:
for (i = 0; i < astNode->statementSequence.count; i++) { {
for (i = 0; i < astNode->statementSequence.count; i++)
{
Node *argNode = astNode->statementSequence.sequence[i]; Node *argNode = astNode->statementSequence.sequence[i];
AddChildToNode(parent, MakeIdTree(argNode, parent)); AddChildToNode(parent, MakeIdTree(argNode, parent));
} }
return NULL; return NULL;
} }
case StructDeclaration: { case StructDeclaration:
{
Node *idNode = astNode->structDeclaration.identifier; Node *idNode = astNode->structDeclaration.identifier;
Node *declsNode = astNode->structDeclaration.declarationSequence; Node *declsNode = astNode->structDeclaration.declarationSequence;
mainNode = MakeIdNode(Struct, idNode->identifier.name, parent); mainNode = MakeIdNode(Struct, idNode->identifier.name, parent);
mainNode->typeTag = MakeTypeTag(astNode); mainNode->typeTag = MakeTypeTag(astNode);
for (i = 0; i < declsNode->declarationSequence.count; i++) { for (i = 0; i < declsNode->declarationSequence.count; i++)
{
Node *decl = declsNode->declarationSequence.sequence[i]; Node *decl = declsNode->declarationSequence.sequence[i];
AddChildToNode(mainNode, MakeIdTree(decl, mainNode)); AddChildToNode(mainNode, MakeIdTree(decl, mainNode));
} }
@ -190,7 +248,9 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
return NULL; return NULL;
case UnaryExpression: case UnaryExpression:
AddChildToNode(parent, MakeIdTree(astNode->unaryExpression.child, parent)); AddChildToNode(
parent,
MakeIdTree(astNode->unaryExpression.child, parent));
return NULL; return NULL;
case Comment: case Comment:
@ -209,13 +269,18 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
return mainNode; return mainNode;
} }
void PrintIdNode(IdNode *node) { void PrintIdNode(IdNode *node)
if (node == NULL) { {
fprintf(stderr, "wraith: Attempted to call PrintIdNode with null value.\n"); if (node == NULL)
{
fprintf(
stderr,
"wraith: Attempted to call PrintIdNode with null value.\n");
return; return;
} }
switch(node->type) { switch (node->type)
{
case Placeholder: case Placeholder:
printf("Placeholder (%s)\n", node->name); printf("Placeholder (%s)\n", node->name);
break; break;
@ -229,7 +294,10 @@ void PrintIdNode(IdNode *node) {
printf("%s : %s\n", node->name, TypeTagToString(node->typeTag)); printf("%s : %s\n", node->name, TypeTagToString(node->typeTag));
break; break;
case Function: case Function:
printf("%s : Function<%s>\n", node->name, TypeTagToString(node->typeTag)); printf(
"%s : Function<%s>\n",
node->name,
TypeTagToString(node->typeTag));
break; break;
case Variable: case Variable:
printf("%s : %s\n", node->name, TypeTagToString(node->typeTag)); printf("%s : %s\n", node->name, TypeTagToString(node->typeTag));
@ -237,40 +305,53 @@ void PrintIdNode(IdNode *node) {
} }
} }
void PrintIdTree(IdNode *tree, uint32_t tabCount) { void PrintIdTree(IdNode *tree, uint32_t tabCount)
if (tree == NULL) { {
fprintf(stderr, "wraith: Attempted to call PrintIdTree on a null value.\n"); if (tree == NULL)
{
fprintf(
stderr,
"wraith: Attempted to call PrintIdTree on a null value.\n");
return; return;
} }
uint32_t i; uint32_t i;
for (i = 0; i < tabCount; i++) { for (i = 0; i < tabCount; i++)
{
printf("| "); printf("| ");
} }
PrintIdNode(tree); PrintIdNode(tree);
for (i = 0; i < tree->childCount; i++) { for (i = 0; i < tree->childCount; i++)
{
PrintIdTree(tree->children[i], tabCount + 1); PrintIdTree(tree->children[i], tabCount + 1);
} }
} }
int PrintAncestors(IdNode *node) { int PrintAncestors(IdNode *node)
if (node == NULL) return -1; {
if (node == NULL)
return -1;
int i; int i;
int indent = 1; int indent = 1;
indent += PrintAncestors(node->parent); indent += PrintAncestors(node->parent);
for (i = 0; i < indent; i++) { for (i = 0; i < indent; i++)
{
printf(" "); printf(" ");
} }
PrintIdNode(node); PrintIdNode(node);
return indent; return indent;
} }
IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) { IdNode *LookdownId(IdNode *root, NodeType targetType, char *targetName)
if (root == NULL) { {
fprintf(stderr, "wraith: Attempted to call LookdownId on a null value.\n"); if (root == NULL)
{
fprintf(
stderr,
"wraith: Attempted to call LookdownId on a null value.\n");
return NULL; return NULL;
} }
@ -279,23 +360,29 @@ IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) {
frontier[0] = root; frontier[0] = root;
uint32_t frontierCount = 1; uint32_t frontierCount = 1;
while (frontierCount > 0) { while (frontierCount > 0)
{
IdNode *current = frontier[0]; IdNode *current = frontier[0];
if (current->type == targetType && strcmp(current->name, targetName) == 0) { if (current->type == targetType &&
strcmp(current->name, targetName) == 0)
{
result = current; result = current;
break; break;
} }
uint32_t i; uint32_t i;
for(i = 1; i < frontierCount; i++) { for (i = 1; i < frontierCount; i++)
{
frontier[i - 1] = frontier[i]; frontier[i - 1] = frontier[i];
} }
size_t newSize = frontierCount + current->childCount - 1; size_t newSize = frontierCount + current->childCount - 1;
if (frontierCount != newSize) { if (frontierCount != newSize)
{
frontier = (IdNode **)realloc(frontier, sizeof(IdNode *) * newSize); frontier = (IdNode **)realloc(frontier, sizeof(IdNode *) * newSize);
} }
for (i = 0; i < current->childCount; i++) { for (i = 0; i < current->childCount; i++)
{
frontier[frontierCount + i - 1] = current->children[i]; frontier[frontierCount + i - 1] = current->children[i];
} }
frontierCount = newSize; frontierCount = newSize;
@ -305,8 +392,10 @@ IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) {
return result; return result;
} }
bool ScopeHasOrdering(IdNode *node) { bool ScopeHasOrdering(IdNode *node)
switch (node->type) { {
switch (node->type)
{
case OrderedScope: case OrderedScope:
case Function: case Function:
case Variable: /* this is only technically true */ case Variable: /* this is only technically true */
@ -316,59 +405,76 @@ bool ScopeHasOrdering(IdNode *node) {
} }
} }
IdNode* LookupId(IdNode *node, IdNode *prev, char *target) { IdNode *LookupId(IdNode *node, IdNode *prev, char *target)
if (node == NULL) { {
if (node == NULL)
{
return NULL; return NULL;
} }
if (strcmp(node->name, target) == 0 && node->type != Placeholder) { if (strcmp(node->name, target) == 0 && node->type != Placeholder)
{
return node; return node;
} }
/* If this is the start of our search, we should not attempt to look at child nodes. Only /* If this is the start of our search, we should not attempt to look at
* looking up the scope tree is valid at this point. * child nodes. Only looking up the scope tree is valid at this point.
* *
* This has the notable side-effect that this function will return NULL if you attempt to look * This has the notable side-effect that this function will return NULL if
* up a struct's internals starting from the node representing the struct itself. This is * you attempt to look up a struct's internals starting from the node
* because an IdNode corresponds to the location *where an identifier is first declared.* Thus, * representing the struct itself. This is because an IdNode corresponds to
* an identifier has no knowledge of identifiers declared "inside" of it. * the location *where an identifier is first declared.* Thus, an identifier
* has no knowledge of identifiers declared "inside" of it.
*/ */
if (prev == NULL) { if (prev == NULL)
{
return LookupId(node->parent, node, target); return LookupId(node->parent, node, target);
} }
/* If the current node forms an ordered scope then we want to prevent ourselves from looking /* If the current node forms an ordered scope then we want to prevent
* up identifiers declared after the scope we have just come from. * ourselves from looking up identifiers declared after the scope we have
* just come from.
*/ */
uint32_t idxLimit; uint32_t idxLimit;
if (ScopeHasOrdering(node)) { if (ScopeHasOrdering(node))
{
uint32_t i; uint32_t i;
for (i = 0, idxLimit = 0; i < node->childCount; i++, idxLimit++) { for (i = 0, idxLimit = 0; i < node->childCount; i++, idxLimit++)
if (node->children[i] == prev) { {
if (node->children[i] == prev)
{
break; break;
} }
} }
} else { }
else
{
idxLimit = node->childCount; idxLimit = node->childCount;
} }
uint32_t i; uint32_t i;
for (i = 0; i < idxLimit; i++) { for (i = 0; i < idxLimit; i++)
{
IdNode *child = node->children[i]; IdNode *child = node->children[i];
if (child == prev || child->type == Placeholder) { if (child == prev || child->type == Placeholder)
{
/* Do not inspect the node we just came from or placeholders. */ /* Do not inspect the node we just came from or placeholders. */
continue; continue;
} }
if (strcmp(child->name, target) == 0) { if (strcmp(child->name, target) == 0)
{
return child; return child;
} }
if (child->type == Struct) { if (child->type == Struct)
{
uint32_t j; uint32_t j;
for (j = 0; j < child->childCount; j++) { for (j = 0; j < child->childCount; j++)
{
IdNode *grandchild = child->children[j]; IdNode *grandchild = child->children[j];
if (strcmp(grandchild->name, target) == 0) { if (strcmp(grandchild->name, target) == 0)
{
return grandchild; return grandchild;
} }
} }

View File

@ -10,7 +10,8 @@
struct TypeTag; struct TypeTag;
struct Node; struct Node;
typedef enum NodeType { typedef enum NodeType
{
Placeholder, Placeholder,
UnorderedScope, UnorderedScope,
OrderedScope, OrderedScope,
@ -19,7 +20,8 @@ typedef enum NodeType {
Variable Variable
} NodeType; } NodeType;
typedef struct IdNode { typedef struct IdNode
{
NodeType type; NodeType type;
char *name; char *name;
struct TypeTag *typeTag; struct TypeTag *typeTag;
@ -29,13 +31,14 @@ typedef struct IdNode {
uint32_t childCapacity; uint32_t childCapacity;
} IdNode; } IdNode;
typedef struct IdStatus { typedef struct IdStatus
enum StatusCode { {
enum StatusCode
{
Valid, Valid,
} StatusCode; } StatusCode;
} IdStatus; } IdStatus;
IdNode *MakeIdTree(struct Node *astNode, IdNode *parent); IdNode *MakeIdTree(struct Node *astNode, IdNode *parent);
void PrintIdNode(IdNode *node); void PrintIdNode(IdNode *node);
void PrintIdTree(IdNode *tree, uint32_t tabCount); void PrintIdTree(IdNode *tree, uint32_t tabCount);

View File

@ -1,9 +1,9 @@
#include <stdlib.h>
#include "../lib/dropt/dropt.h" #include "../lib/dropt/dropt.h"
#include <stdlib.h>
#include "parser.h"
#include "codegen.h" #include "codegen.h"
#include "identcheck.h" #include "identcheck.h"
#include "parser.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -14,9 +14,26 @@ int main(int argc, char *argv[])
int exitCode = EXIT_SUCCESS; int exitCode = EXIT_SUCCESS;
dropt_option options[] = { dropt_option options[] = {
{ 'h', "help", "Shows help.", NULL, dropt_handle_bool, &showHelp, dropt_attr_halt }, {'h',
{ 'v', "parse-verbose", "Shows verbose parser output.", NULL, dropt_handle_bool, &parseVerbose }, "help",
{ 'O', "optimize", "Sets optimization level of the output IR. Must be a value between 0 and 3.", "number", dropt_handle_uint, &optimizationLevel }, "Shows help.",
NULL,
dropt_handle_bool,
&showHelp,
dropt_attr_halt},
{'v',
"parse-verbose",
"Shows verbose parser output.",
NULL,
dropt_handle_bool,
&parseVerbose},
{'O',
"optimize",
"Sets optimization level of the output IR. Must be a value between 0 "
"and 3.",
"number",
dropt_handle_uint,
&optimizationLevel},
{0} /* Required sentinel value. */ {0} /* Required sentinel value. */
}; };
@ -36,7 +53,10 @@ int main(int argc, char *argv[])
char **rest = dropt_parse(droptContext, -1, &argv[1]); char **rest = dropt_parse(droptContext, -1, &argv[1]);
if (dropt_get_error(droptContext) != dropt_error_none) if (dropt_get_error(droptContext) != dropt_error_none)
{ {
fprintf(stderr, "wraith: %s\n", dropt_get_error_message(droptContext)); fprintf(
stderr,
"wraith: %s\n",
dropt_get_error_message(droptContext));
exitCode = EXIT_FAILURE; exitCode = EXIT_FAILURE;
} }
else if (showHelp) else if (showHelp)

View File

@ -1,7 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include "y.tab.h"
#include "ast.h" #include "ast.h"
#include "y.tab.h"
extern FILE *yyin; extern FILE *yyin;
extern int yydebug; extern int yydebug;

View File

@ -1,11 +1,12 @@
#include "util.h" #include "util.h"
#include <stdlib.h>
#include <string.h> #include <string.h>
char *strdup(const char *s) char *strdup(const char *s)
{ {
size_t slen = strlen(s); size_t slen = strlen(s);
char* result = malloc(slen + 1); char *result = (char *)malloc(slen + 1);
if (result == NULL) if (result == NULL)
{ {
return NULL; return NULL;