clang-format config
parent
876e8deb6e
commit
473b706ad9
|
@ -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
345
src/ast.c
|
@ -1,7 +1,7 @@
|
|||
#include "ast.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
|
@ -9,83 +9,106 @@ const char* SyntaxKindString(SyntaxKind syntaxKind)
|
|||
{
|
||||
switch (syntaxKind)
|
||||
{
|
||||
case AccessExpression: return "AccessExpression";
|
||||
case AllocExpression: return "Alloc";
|
||||
case Assignment: return "Assignment";
|
||||
case BinaryExpression: return "BinaryExpression";
|
||||
case Comment: return "Comment";
|
||||
case CustomTypeNode: return "CustomTypeNode";
|
||||
case Declaration: return "Declaration";
|
||||
case ForLoop: return "ForLoop";
|
||||
case DeclarationSequence: return "DeclarationSequence";
|
||||
case FunctionArgumentSequence: return "FunctionArgumentSequence";
|
||||
case FunctionCallExpression: return "FunctionCallExpression";
|
||||
case FunctionDeclaration: return "FunctionDeclaration";
|
||||
case FunctionModifiers: return "FunctionModifiers";
|
||||
case FunctionSignature: return "FunctionSignature";
|
||||
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";
|
||||
case AccessExpression:
|
||||
return "AccessExpression";
|
||||
case AllocExpression:
|
||||
return "Alloc";
|
||||
case Assignment:
|
||||
return "Assignment";
|
||||
case BinaryExpression:
|
||||
return "BinaryExpression";
|
||||
case Comment:
|
||||
return "Comment";
|
||||
case CustomTypeNode:
|
||||
return "CustomTypeNode";
|
||||
case Declaration:
|
||||
return "Declaration";
|
||||
case ForLoop:
|
||||
return "ForLoop";
|
||||
case DeclarationSequence:
|
||||
return "DeclarationSequence";
|
||||
case FunctionArgumentSequence:
|
||||
return "FunctionArgumentSequence";
|
||||
case FunctionCallExpression:
|
||||
return "FunctionCallExpression";
|
||||
case FunctionDeclaration:
|
||||
return "FunctionDeclaration";
|
||||
case FunctionModifiers:
|
||||
return "FunctionModifiers";
|
||||
case FunctionSignature:
|
||||
return "FunctionSignature";
|
||||
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(
|
||||
Node *typeNode
|
||||
) {
|
||||
uint8_t IsPrimitiveType(Node *typeNode)
|
||||
{
|
||||
return typeNode->type.typeNode->syntaxKind == PrimitiveTypeNode;
|
||||
}
|
||||
|
||||
Node* MakePrimitiveTypeNode(
|
||||
PrimitiveType type
|
||||
) {
|
||||
Node *MakePrimitiveTypeNode(PrimitiveType type)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = PrimitiveTypeNode;
|
||||
node->primitiveType.type = type;
|
||||
return node;
|
||||
}
|
||||
|
||||
Node* MakeCustomTypeNode(
|
||||
char *name
|
||||
) {
|
||||
Node *MakeCustomTypeNode(char *name)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = CustomTypeNode;
|
||||
node->customType.name = strdup(name);
|
||||
return node;
|
||||
}
|
||||
|
||||
Node* MakeReferenceTypeNode(
|
||||
Node *typeNode
|
||||
) {
|
||||
Node *MakeReferenceTypeNode(Node *typeNode)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = ReferenceTypeNode;
|
||||
node->referenceType.type = typeNode;
|
||||
return node;
|
||||
}
|
||||
|
||||
Node* MakeTypeNode(
|
||||
Node* typeNode
|
||||
) {
|
||||
Node *MakeTypeNode(Node *typeNode)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = Type;
|
||||
node->type.typeNode = typeNode;
|
||||
return node;
|
||||
}
|
||||
|
||||
Node* MakeIdentifierNode(
|
||||
const char *id
|
||||
) {
|
||||
Node *MakeIdentifierNode(const char *id)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = Identifier;
|
||||
node->identifier.name = strdup(id);
|
||||
|
@ -93,9 +116,8 @@ Node* MakeIdentifierNode(
|
|||
return node;
|
||||
}
|
||||
|
||||
Node* MakeNumberNode(
|
||||
const char *numberString
|
||||
) {
|
||||
Node *MakeNumberNode(const char *numberString)
|
||||
{
|
||||
char *ptr;
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = Number;
|
||||
|
@ -103,9 +125,8 @@ Node* MakeNumberNode(
|
|||
return node;
|
||||
}
|
||||
|
||||
Node* MakeStringNode(
|
||||
const char *string
|
||||
) {
|
||||
Node *MakeStringNode(const char *string)
|
||||
{
|
||||
size_t slen = strlen(string);
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = StringLiteral;
|
||||
|
@ -121,10 +142,8 @@ Node* MakeStaticNode()
|
|||
}
|
||||
|
||||
/* FIXME: this sucks */
|
||||
Node* MakeFunctionModifiersNode(
|
||||
Node **pModifierNodes,
|
||||
uint32_t modifierCount
|
||||
) {
|
||||
Node *MakeFunctionModifiersNode(Node **pModifierNodes, uint32_t modifierCount)
|
||||
{
|
||||
uint32_t i;
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = FunctionModifiers;
|
||||
|
@ -132,7 +151,8 @@ Node* MakeFunctionModifiersNode(
|
|||
node->functionModifiers.sequence = NULL;
|
||||
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)
|
||||
{
|
||||
node->functionModifiers.sequence[i] = pModifierNodes[i];
|
||||
|
@ -142,10 +162,8 @@ Node* MakeFunctionModifiersNode(
|
|||
return node;
|
||||
}
|
||||
|
||||
Node* MakeUnaryNode(
|
||||
UnaryOperator operator,
|
||||
Node *child
|
||||
) {
|
||||
Node *MakeUnaryNode(UnaryOperator operator, Node * child)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = UnaryExpression;
|
||||
node->unaryExpression.operator= operator;
|
||||
|
@ -153,11 +171,8 @@ Node* MakeUnaryNode(
|
|||
return node;
|
||||
}
|
||||
|
||||
Node* MakeBinaryNode(
|
||||
BinaryOperator operator,
|
||||
Node *left,
|
||||
Node *right
|
||||
) {
|
||||
Node *MakeBinaryNode(BinaryOperator operator, Node * left, Node *right)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = BinaryExpression;
|
||||
node->binaryExpression.left = left;
|
||||
|
@ -166,10 +181,8 @@ Node* MakeBinaryNode(
|
|||
return node;
|
||||
}
|
||||
|
||||
Node* MakeDeclarationNode(
|
||||
Node* typeNode,
|
||||
Node* identifierNode
|
||||
) {
|
||||
Node *MakeDeclarationNode(Node *typeNode, Node *identifierNode)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = Declaration;
|
||||
node->declaration.type = typeNode;
|
||||
|
@ -177,10 +190,8 @@ Node* MakeDeclarationNode(
|
|||
return node;
|
||||
}
|
||||
|
||||
Node* MakeAssignmentNode(
|
||||
Node *left,
|
||||
Node *right
|
||||
) {
|
||||
Node *MakeAssignmentNode(Node *left, Node *right)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = Assignment;
|
||||
node->assignmentStatement.left = left;
|
||||
|
@ -188,9 +199,8 @@ Node* MakeAssignmentNode(
|
|||
return node;
|
||||
}
|
||||
|
||||
Node* StartStatementSequenceNode(
|
||||
Node *statementNode
|
||||
) {
|
||||
Node *StartStatementSequenceNode(Node *statementNode)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = StatementSequence;
|
||||
node->statementSequence.sequence = (Node **)malloc(sizeof(Node *));
|
||||
|
@ -199,19 +209,20 @@ Node* StartStatementSequenceNode(
|
|||
return node;
|
||||
}
|
||||
|
||||
Node* AddStatement(
|
||||
Node* statementSequenceNode,
|
||||
Node *statementNode
|
||||
) {
|
||||
statementSequenceNode->statementSequence.sequence = realloc(statementSequenceNode->statementSequence.sequence, sizeof(Node*) * (statementSequenceNode->statementSequence.count + 1));
|
||||
statementSequenceNode->statementSequence.sequence[statementSequenceNode->statementSequence.count] = statementNode;
|
||||
Node *AddStatement(Node *statementSequenceNode, Node *statementNode)
|
||||
{
|
||||
statementSequenceNode->statementSequence.sequence = realloc(
|
||||
statementSequenceNode->statementSequence.sequence,
|
||||
sizeof(Node *) * (statementSequenceNode->statementSequence.count + 1));
|
||||
statementSequenceNode->statementSequence
|
||||
.sequence[statementSequenceNode->statementSequence.count] =
|
||||
statementNode;
|
||||
statementSequenceNode->statementSequence.count += 1;
|
||||
return statementSequenceNode;
|
||||
}
|
||||
|
||||
Node* MakeReturnStatementNode(
|
||||
Node *expressionNode
|
||||
) {
|
||||
Node *MakeReturnStatementNode(Node *expressionNode)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = Return;
|
||||
node->returnStatement.expression = expressionNode;
|
||||
|
@ -225,9 +236,8 @@ Node* MakeReturnVoidStatementNode()
|
|||
return node;
|
||||
}
|
||||
|
||||
Node *StartFunctionSignatureArgumentsNode(
|
||||
Node *argumentNode
|
||||
) {
|
||||
Node *StartFunctionSignatureArgumentsNode(Node *argumentNode)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = FunctionSignatureArguments;
|
||||
node->functionSignatureArguments.sequence = (Node **)malloc(sizeof(Node *));
|
||||
|
@ -236,12 +246,14 @@ Node *StartFunctionSignatureArgumentsNode(
|
|||
return node;
|
||||
}
|
||||
|
||||
Node* AddFunctionSignatureArgumentNode(
|
||||
Node *argumentsNode,
|
||||
Node *argumentNode
|
||||
) {
|
||||
argumentsNode->functionSignatureArguments.sequence = realloc(argumentsNode->functionSignatureArguments.sequence, sizeof(Node*) * (argumentsNode->functionSignatureArguments.count + 1));
|
||||
argumentsNode->functionSignatureArguments.sequence[argumentsNode->functionSignatureArguments.count] = argumentNode;
|
||||
Node *AddFunctionSignatureArgumentNode(Node *argumentsNode, Node *argumentNode)
|
||||
{
|
||||
argumentsNode->functionSignatureArguments.sequence = realloc(
|
||||
argumentsNode->functionSignatureArguments.sequence,
|
||||
sizeof(Node *) * (argumentsNode->functionSignatureArguments.count + 1));
|
||||
argumentsNode->functionSignatureArguments
|
||||
.sequence[argumentsNode->functionSignatureArguments.count] =
|
||||
argumentNode;
|
||||
argumentsNode->functionSignatureArguments.count += 1;
|
||||
return argumentsNode;
|
||||
}
|
||||
|
@ -259,8 +271,8 @@ Node* MakeFunctionSignatureNode(
|
|||
Node *identifierNode,
|
||||
Node *typeNode,
|
||||
Node *arguments,
|
||||
Node* modifiersNode
|
||||
) {
|
||||
Node *modifiersNode)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = FunctionSignature;
|
||||
node->functionSignature.identifier = identifierNode;
|
||||
|
@ -272,8 +284,8 @@ Node* MakeFunctionSignatureNode(
|
|||
|
||||
Node *MakeFunctionDeclarationNode(
|
||||
Node *functionSignatureNode,
|
||||
Node* functionBodyNode
|
||||
) {
|
||||
Node *functionBodyNode)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = FunctionDeclaration;
|
||||
node->functionDeclaration.functionSignature = functionSignatureNode;
|
||||
|
@ -283,8 +295,8 @@ Node* MakeFunctionDeclarationNode(
|
|||
|
||||
Node *MakeStructDeclarationNode(
|
||||
Node *identifierNode,
|
||||
Node *declarationSequenceNode
|
||||
) {
|
||||
Node *declarationSequenceNode)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = StructDeclaration;
|
||||
node->structDeclaration.identifier = identifierNode;
|
||||
|
@ -292,9 +304,8 @@ Node* MakeStructDeclarationNode(
|
|||
return node;
|
||||
}
|
||||
|
||||
Node* StartDeclarationSequenceNode(
|
||||
Node *declarationNode
|
||||
) {
|
||||
Node *StartDeclarationSequenceNode(Node *declarationNode)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = DeclarationSequence;
|
||||
node->declarationSequence.sequence = (Node **)malloc(sizeof(Node *));
|
||||
|
@ -303,19 +314,21 @@ Node* StartDeclarationSequenceNode(
|
|||
return node;
|
||||
}
|
||||
|
||||
Node* AddDeclarationNode(
|
||||
Node *declarationSequenceNode,
|
||||
Node *declarationNode
|
||||
) {
|
||||
declarationSequenceNode->declarationSequence.sequence = (Node**) realloc(declarationSequenceNode->declarationSequence.sequence, sizeof(Node*) * (declarationSequenceNode->declarationSequence.count + 1));
|
||||
declarationSequenceNode->declarationSequence.sequence[declarationSequenceNode->declarationSequence.count] = declarationNode;
|
||||
Node *AddDeclarationNode(Node *declarationSequenceNode, Node *declarationNode)
|
||||
{
|
||||
declarationSequenceNode->declarationSequence.sequence = (Node **)realloc(
|
||||
declarationSequenceNode->declarationSequence.sequence,
|
||||
sizeof(Node *) *
|
||||
(declarationSequenceNode->declarationSequence.count + 1));
|
||||
declarationSequenceNode->declarationSequence
|
||||
.sequence[declarationSequenceNode->declarationSequence.count] =
|
||||
declarationNode;
|
||||
declarationSequenceNode->declarationSequence.count += 1;
|
||||
return declarationSequenceNode;
|
||||
}
|
||||
|
||||
Node* StartFunctionArgumentSequenceNode(
|
||||
Node *argumentNode
|
||||
) {
|
||||
Node *StartFunctionArgumentSequenceNode(Node *argumentNode)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = FunctionArgumentSequence;
|
||||
node->functionArgumentSequence.sequence = (Node **)malloc(sizeof(Node *));
|
||||
|
@ -324,12 +337,15 @@ Node* StartFunctionArgumentSequenceNode(
|
|||
return node;
|
||||
}
|
||||
|
||||
Node* AddFunctionArgumentNode(
|
||||
Node *argumentSequenceNode,
|
||||
Node *argumentNode
|
||||
) {
|
||||
argumentSequenceNode->functionArgumentSequence.sequence = (Node**) realloc(argumentSequenceNode->functionArgumentSequence.sequence, sizeof(Node*) * (argumentSequenceNode->functionArgumentSequence.count + 1));
|
||||
argumentSequenceNode->functionArgumentSequence.sequence[argumentSequenceNode->functionArgumentSequence.count] = argumentNode;
|
||||
Node *AddFunctionArgumentNode(Node *argumentSequenceNode, Node *argumentNode)
|
||||
{
|
||||
argumentSequenceNode->functionArgumentSequence.sequence = (Node **)realloc(
|
||||
argumentSequenceNode->functionArgumentSequence.sequence,
|
||||
sizeof(Node *) *
|
||||
(argumentSequenceNode->functionArgumentSequence.count + 1));
|
||||
argumentSequenceNode->functionArgumentSequence
|
||||
.sequence[argumentSequenceNode->functionArgumentSequence.count] =
|
||||
argumentNode;
|
||||
argumentSequenceNode->functionArgumentSequence.count += 1;
|
||||
return argumentSequenceNode;
|
||||
}
|
||||
|
@ -345,8 +361,8 @@ Node *MakeEmptyFunctionArgumentSequenceNode()
|
|||
|
||||
Node *MakeFunctionCallExpressionNode(
|
||||
Node *identifierNode,
|
||||
Node *argumentSequenceNode
|
||||
) {
|
||||
Node *argumentSequenceNode)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = FunctionCallExpression;
|
||||
node->functionCallExpression.identifier = identifierNode;
|
||||
|
@ -354,10 +370,8 @@ Node* MakeFunctionCallExpressionNode(
|
|||
return node;
|
||||
}
|
||||
|
||||
Node* MakeAccessExpressionNode(
|
||||
Node *accessee,
|
||||
Node *accessor
|
||||
) {
|
||||
Node *MakeAccessExpressionNode(Node *accessee, Node *accessor)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = AccessExpression;
|
||||
node->accessExpression.accessee = accessee;
|
||||
|
@ -373,10 +387,8 @@ Node* MakeAllocNode(Node *typeNode)
|
|||
return node;
|
||||
}
|
||||
|
||||
Node* MakeIfNode(
|
||||
Node *expressionNode,
|
||||
Node *statementSequenceNode
|
||||
) {
|
||||
Node *MakeIfNode(Node *expressionNode, Node *statementSequenceNode)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = IfStatement;
|
||||
node->ifStatement.expression = expressionNode;
|
||||
|
@ -384,10 +396,8 @@ Node* MakeIfNode(
|
|||
return node;
|
||||
}
|
||||
|
||||
Node* MakeIfElseNode(
|
||||
Node *ifNode,
|
||||
Node *elseNode
|
||||
) {
|
||||
Node *MakeIfElseNode(Node *ifNode, Node *elseNode)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = IfElseStatement;
|
||||
node->ifElseStatement.ifStatement = ifNode;
|
||||
|
@ -399,8 +409,8 @@ Node* MakeForLoopNode(
|
|||
Node *declarationNode,
|
||||
Node *startNumberNode,
|
||||
Node *endNumberNode,
|
||||
Node *statementSequenceNode
|
||||
) {
|
||||
Node *statementSequenceNode)
|
||||
{
|
||||
Node *node = (Node *)malloc(sizeof(Node));
|
||||
node->syntaxKind = ForLoop;
|
||||
node->forLoop.declaration = declarationNode;
|
||||
|
@ -414,10 +424,14 @@ static const char* PrimitiveTypeToString(PrimitiveType type)
|
|||
{
|
||||
switch (type)
|
||||
{
|
||||
case Int: return "Int";
|
||||
case UInt: return "UInt";
|
||||
case Bool: return "Bool";
|
||||
case Void: return "Void";
|
||||
case Int:
|
||||
return "Int";
|
||||
case UInt:
|
||||
return "UInt";
|
||||
case Bool:
|
||||
return "Bool";
|
||||
case Void:
|
||||
return "Void";
|
||||
}
|
||||
|
||||
return "Unknown";
|
||||
|
@ -552,14 +566,19 @@ void PrintNode(Node *node, uint32_t tabCount)
|
|||
printf("\n");
|
||||
for (i = 0; i < node->functionSignatureArguments.count; i += 1)
|
||||
{
|
||||
PrintNode(node->functionSignatureArguments.sequence[i], tabCount + 1);
|
||||
PrintNode(
|
||||
node->functionSignatureArguments.sequence[i],
|
||||
tabCount + 1);
|
||||
}
|
||||
return;
|
||||
|
||||
case Identifier:
|
||||
if (node->typeTag == NULL) {
|
||||
if (node->typeTag == NULL)
|
||||
{
|
||||
printf("%s\n", node->identifier.name);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
char *type = TypeTagToString(node->typeTag);
|
||||
printf("%s<%s>\n", node->identifier.name, type);
|
||||
}
|
||||
|
@ -632,14 +651,19 @@ void PrintNode(Node *node, uint32_t tabCount)
|
|||
}
|
||||
}
|
||||
|
||||
TypeTag* MakeTypeTag(Node *node) {
|
||||
if (node == NULL) {
|
||||
fprintf(stderr, "wraith: Attempted to call MakeTypeTag on null value.\n");
|
||||
TypeTag *MakeTypeTag(Node *node)
|
||||
{
|
||||
if (node == NULL)
|
||||
{
|
||||
fprintf(
|
||||
stderr,
|
||||
"wraith: Attempted to call MakeTypeTag on null value.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TypeTag *tag = (TypeTag *)malloc(sizeof(TypeTag));
|
||||
switch (node->syntaxKind) {
|
||||
switch (node->syntaxKind)
|
||||
{
|
||||
case Type:
|
||||
tag = MakeTypeTag(node->type.typeNode);
|
||||
break;
|
||||
|
@ -665,15 +689,18 @@ TypeTag* MakeTypeTag(Node *node) {
|
|||
|
||||
case StructDeclaration:
|
||||
tag->type = Custom;
|
||||
tag->value.customType = strdup(node->structDeclaration.identifier->identifier.name);
|
||||
tag->value.customType =
|
||||
strdup(node->structDeclaration.identifier->identifier.name);
|
||||
break;
|
||||
|
||||
case FunctionDeclaration:
|
||||
tag = MakeTypeTag(node->functionDeclaration.functionSignature->functionSignature.type);
|
||||
tag = MakeTypeTag(node->functionDeclaration.functionSignature
|
||||
->functionSignature.type);
|
||||
break;
|
||||
|
||||
default:
|
||||
fprintf(stderr,
|
||||
fprintf(
|
||||
stderr,
|
||||
"wraith: Attempted to call MakeTypeTag on"
|
||||
" node with unsupported SyntaxKind: %s\n",
|
||||
SyntaxKindString(node->syntaxKind));
|
||||
|
@ -682,18 +709,24 @@ TypeTag* MakeTypeTag(Node *node) {
|
|||
return tag;
|
||||
}
|
||||
|
||||
char* TypeTagToString(TypeTag *tag) {
|
||||
if (tag == NULL) {
|
||||
fprintf(stderr, "wraith: Attempted to call TypeTagToString with null value\n");
|
||||
char *TypeTagToString(TypeTag *tag)
|
||||
{
|
||||
if (tag == NULL)
|
||||
{
|
||||
fprintf(
|
||||
stderr,
|
||||
"wraith: Attempted to call TypeTagToString with null value\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
switch (tag->type) {
|
||||
switch (tag->type)
|
||||
{
|
||||
case Unknown:
|
||||
return "Unknown";
|
||||
case Primitive:
|
||||
return PrimitiveTypeToString(tag->value.primitiveType);
|
||||
case Reference: {
|
||||
case Reference:
|
||||
{
|
||||
char *inner = TypeTagToString(tag->value.referenceType);
|
||||
size_t innerStrLen = strlen(inner);
|
||||
char *result = malloc(sizeof(char) * (innerStrLen + 5));
|
||||
|
|
125
src/ast.h
125
src/ast.h
|
@ -1,8 +1,8 @@
|
|||
#ifndef WRAITH_AST_H
|
||||
#define WRAITH_AST_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "identcheck.h"
|
||||
#include <stdint.h>
|
||||
|
||||
/* -Wpedantic nameless union/struct silencing */
|
||||
#ifndef WRAITHNAMELESS
|
||||
|
@ -282,112 +282,48 @@ struct Node
|
|||
const char *SyntaxKindString(SyntaxKind syntaxKind);
|
||||
|
||||
uint8_t IsPrimitiveType(Node *typeNode);
|
||||
Node* MakePrimitiveTypeNode(
|
||||
PrimitiveType type
|
||||
);
|
||||
Node* MakeCustomTypeNode(
|
||||
char *string
|
||||
);
|
||||
Node* MakeReferenceTypeNode(
|
||||
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 *MakePrimitiveTypeNode(PrimitiveType type);
|
||||
Node *MakeCustomTypeNode(char *string);
|
||||
Node *MakeReferenceTypeNode(Node *typeNode);
|
||||
Node *MakeTypeNode(Node *typeNode);
|
||||
Node *MakeIdentifierNode(const char *id);
|
||||
Node *MakeNumberNode(const char *numberString);
|
||||
Node *MakeStringNode(const char *string);
|
||||
Node *MakeStaticNode();
|
||||
Node* MakeFunctionModifiersNode(
|
||||
Node **pModifierNodes,
|
||||
uint32_t modifierCount
|
||||
);
|
||||
Node* MakeUnaryNode(
|
||||
UnaryOperator operator,
|
||||
Node *child
|
||||
);
|
||||
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 *MakeFunctionModifiersNode(Node **pModifierNodes, uint32_t modifierCount);
|
||||
Node *MakeUnaryNode(UnaryOperator operator, Node * child);
|
||||
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* StartFunctionSignatureArgumentsNode(
|
||||
Node *argumentNode
|
||||
);
|
||||
Node* AddFunctionSignatureArgumentNode(
|
||||
Node *argumentsNode,
|
||||
Node *argumentNode
|
||||
);
|
||||
Node *StartFunctionSignatureArgumentsNode(Node *argumentNode);
|
||||
Node *AddFunctionSignatureArgumentNode(Node *argumentsNode, Node *argumentNode);
|
||||
Node *MakeEmptyFunctionSignatureArgumentsNode();
|
||||
Node *MakeFunctionSignatureNode(
|
||||
Node *identifierNode,
|
||||
Node *typeNode,
|
||||
Node *argumentsNode,
|
||||
Node* modifiersNode
|
||||
);
|
||||
Node *modifiersNode);
|
||||
Node *MakeFunctionDeclarationNode(
|
||||
Node *functionSignatureNode,
|
||||
Node* functionBodyNode
|
||||
);
|
||||
Node *functionBodyNode);
|
||||
Node *MakeStructDeclarationNode(
|
||||
Node *identifierNode,
|
||||
Node *declarationSequenceNode
|
||||
);
|
||||
Node* StartDeclarationSequenceNode(
|
||||
Node *declarationNode
|
||||
);
|
||||
Node* AddDeclarationNode(
|
||||
Node *declarationSequenceNode,
|
||||
Node *declarationNode
|
||||
);
|
||||
Node *StartFunctionArgumentSequenceNode(
|
||||
Node *argumentNode
|
||||
);
|
||||
Node *AddFunctionArgumentNode(
|
||||
Node *argumentSequenceNode,
|
||||
Node *argumentNode
|
||||
);
|
||||
Node *declarationSequenceNode);
|
||||
Node *StartDeclarationSequenceNode(Node *declarationNode);
|
||||
Node *AddDeclarationNode(Node *declarationSequenceNode, Node *declarationNode);
|
||||
Node *StartFunctionArgumentSequenceNode(Node *argumentNode);
|
||||
Node *AddFunctionArgumentNode(Node *argumentSequenceNode, Node *argumentNode);
|
||||
Node *MakeEmptyFunctionArgumentSequenceNode();
|
||||
Node *MakeFunctionCallExpressionNode(
|
||||
Node *identifierNode,
|
||||
Node *argumentSequenceNode
|
||||
);
|
||||
Node* MakeAccessExpressionNode(
|
||||
Node *accessee,
|
||||
Node *accessor
|
||||
);
|
||||
Node* MakeAllocNode(
|
||||
Node *typeNode
|
||||
);
|
||||
Node* MakeIfNode(
|
||||
Node *expressionNode,
|
||||
Node *statementSequenceNode
|
||||
);
|
||||
Node *argumentSequenceNode);
|
||||
Node *MakeAccessExpressionNode(Node *accessee, Node *accessor);
|
||||
Node *MakeAllocNode(Node *typeNode);
|
||||
Node *MakeIfNode(Node *expressionNode, Node *statementSequenceNode);
|
||||
Node *MakeIfElseNode(
|
||||
Node *ifNode,
|
||||
Node *elseNode /* can be a conditional or a statement sequence */
|
||||
|
@ -396,8 +332,7 @@ Node* MakeForLoopNode(
|
|||
Node *identifierNode,
|
||||
Node *startNumberNode,
|
||||
Node *endNumberNode,
|
||||
Node *statementSequenceNode
|
||||
);
|
||||
Node *statementSequenceNode);
|
||||
|
||||
void PrintNode(Node *node, uint32_t tabCount);
|
||||
const char *SyntaxKindString(SyntaxKind syntaxKind);
|
||||
|
|
607
src/codegen.c
607
src/codegen.c
File diff suppressed because it is too large
Load Diff
282
src/identcheck.c
282
src/identcheck.c
|
@ -1,13 +1,14 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "ast.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));
|
||||
node->type = type;
|
||||
node->name = strdup(name);
|
||||
|
@ -19,50 +20,79 @@ IdNode* MakeIdNode(NodeType type, char *name, IdNode *parent) {
|
|||
return node;
|
||||
}
|
||||
|
||||
void AddChildToNode(IdNode *node, IdNode *child) {
|
||||
if (child == NULL) return;
|
||||
void AddChildToNode(IdNode *node, IdNode *child)
|
||||
{
|
||||
if (child == NULL)
|
||||
return;
|
||||
|
||||
if (node->children == NULL) {
|
||||
if (node->children == NULL)
|
||||
{
|
||||
node->childCapacity = 2;
|
||||
node->children = (IdNode**) malloc(sizeof(IdNode*) * node->childCapacity);
|
||||
} else if (node->childCount == node->childCapacity) {
|
||||
node->children =
|
||||
(IdNode **)malloc(sizeof(IdNode *) * node->childCapacity);
|
||||
}
|
||||
else if (node->childCount == node->childCapacity)
|
||||
{
|
||||
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->childCount += 1;
|
||||
}
|
||||
|
||||
IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
|
||||
IdNode *MakeIdTree(Node *astNode, IdNode *parent)
|
||||
{
|
||||
uint32_t i;
|
||||
IdNode *mainNode;
|
||||
switch (astNode->syntaxKind) {
|
||||
switch (astNode->syntaxKind)
|
||||
{
|
||||
case AccessExpression:
|
||||
AddChildToNode(parent, MakeIdTree(astNode->accessExpression.accessee, parent));
|
||||
AddChildToNode(parent, MakeIdTree(astNode->accessExpression.accessor, parent));
|
||||
AddChildToNode(
|
||||
parent,
|
||||
MakeIdTree(astNode->accessExpression.accessee, parent));
|
||||
AddChildToNode(
|
||||
parent,
|
||||
MakeIdTree(astNode->accessExpression.accessor, parent));
|
||||
return NULL;
|
||||
|
||||
case AllocExpression:
|
||||
AddChildToNode(parent, MakeIdTree(astNode->allocExpression.type, parent));
|
||||
AddChildToNode(
|
||||
parent,
|
||||
MakeIdTree(astNode->allocExpression.type, parent));
|
||||
return NULL;
|
||||
|
||||
case Assignment: {
|
||||
if (astNode->assignmentStatement.left->syntaxKind == Declaration) {
|
||||
case Assignment:
|
||||
{
|
||||
if (astNode->assignmentStatement.left->syntaxKind == Declaration)
|
||||
{
|
||||
return MakeIdTree(astNode->assignmentStatement.left, parent);
|
||||
} else {
|
||||
AddChildToNode(parent, MakeIdTree(astNode->assignmentStatement.left, parent));
|
||||
AddChildToNode(parent, MakeIdTree(astNode->assignmentStatement.right, parent));
|
||||
}
|
||||
else
|
||||
{
|
||||
AddChildToNode(
|
||||
parent,
|
||||
MakeIdTree(astNode->assignmentStatement.left, parent));
|
||||
AddChildToNode(
|
||||
parent,
|
||||
MakeIdTree(astNode->assignmentStatement.right, parent));
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
case BinaryExpression:
|
||||
AddChildToNode(parent, MakeIdTree(astNode->binaryExpression.left, parent));
|
||||
AddChildToNode(parent, MakeIdTree(astNode->binaryExpression.right, parent));
|
||||
AddChildToNode(
|
||||
parent,
|
||||
MakeIdTree(astNode->binaryExpression.left, parent));
|
||||
AddChildToNode(
|
||||
parent,
|
||||
MakeIdTree(astNode->binaryExpression.right, parent));
|
||||
return NULL;
|
||||
|
||||
case Declaration: {
|
||||
case Declaration:
|
||||
{
|
||||
Node *idNode = astNode->declaration.identifier;
|
||||
mainNode = MakeIdNode(Variable, idNode->identifier.name, parent);
|
||||
mainNode->typeTag = MakeTypeTag(astNode);
|
||||
|
@ -70,16 +100,20 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
|
|||
break;
|
||||
}
|
||||
|
||||
case DeclarationSequence: {
|
||||
case DeclarationSequence:
|
||||
{
|
||||
mainNode = MakeIdNode(UnorderedScope, "", parent);
|
||||
for (i = 0; i < astNode->declarationSequence.count; i++) {
|
||||
for (i = 0; i < astNode->declarationSequence.count; i++)
|
||||
{
|
||||
AddChildToNode(
|
||||
mainNode, MakeIdTree(astNode->declarationSequence.sequence[i], mainNode));
|
||||
mainNode,
|
||||
MakeIdTree(astNode->declarationSequence.sequence[i], mainNode));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case ForLoop: {
|
||||
case ForLoop:
|
||||
{
|
||||
Node *loopDecl = astNode->forLoop.declaration;
|
||||
Node *loopBody = astNode->forLoop.statementSequence;
|
||||
mainNode = MakeIdNode(OrderedScope, "for-loop", parent);
|
||||
|
@ -89,19 +123,29 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
|
|||
}
|
||||
|
||||
case FunctionArgumentSequence:
|
||||
for (i = 0; i < astNode->functionArgumentSequence.count; i++) {
|
||||
for (i = 0; i < astNode->functionArgumentSequence.count; i++)
|
||||
{
|
||||
AddChildToNode(
|
||||
parent, MakeIdTree(astNode->functionArgumentSequence.sequence[i], parent));
|
||||
parent,
|
||||
MakeIdTree(
|
||||
astNode->functionArgumentSequence.sequence[i],
|
||||
parent));
|
||||
}
|
||||
return NULL;
|
||||
|
||||
case FunctionCallExpression:
|
||||
AddChildToNode(parent, MakeIdTree(astNode->functionCallExpression.identifier, parent));
|
||||
AddChildToNode(
|
||||
parent, MakeIdTree(astNode->functionCallExpression.argumentSequence, parent));
|
||||
parent,
|
||||
MakeIdTree(astNode->functionCallExpression.identifier, parent));
|
||||
AddChildToNode(
|
||||
parent,
|
||||
MakeIdTree(
|
||||
astNode->functionCallExpression.argumentSequence,
|
||||
parent));
|
||||
return NULL;
|
||||
|
||||
case FunctionDeclaration: {
|
||||
case FunctionDeclaration:
|
||||
{
|
||||
Node *sigNode = astNode->functionDeclaration.functionSignature;
|
||||
Node *idNode = sigNode->functionSignature.identifier;
|
||||
char *funcName = idNode->identifier.name;
|
||||
|
@ -113,30 +157,37 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
|
|||
break;
|
||||
}
|
||||
|
||||
case FunctionSignatureArguments: {
|
||||
for (i = 0; i < astNode->functionSignatureArguments.count; i++) {
|
||||
case FunctionSignatureArguments:
|
||||
{
|
||||
for (i = 0; i < astNode->functionSignatureArguments.count; i++)
|
||||
{
|
||||
Node *argNode = astNode->functionSignatureArguments.sequence[i];
|
||||
AddChildToNode(parent, MakeIdTree(argNode, parent));
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
case Identifier: {
|
||||
case Identifier:
|
||||
{
|
||||
char *name = astNode->identifier.name;
|
||||
mainNode = MakeIdNode(Placeholder, name, parent);
|
||||
IdNode *lookupNode = LookupId(mainNode, NULL, name);
|
||||
if (lookupNode == NULL) {
|
||||
if (lookupNode == NULL)
|
||||
{
|
||||
fprintf(stderr, "wraith: Could not find IdNode for id %s\n", name);
|
||||
TypeTag *tag = (TypeTag *)malloc(sizeof(TypeTag));
|
||||
tag->type = Unknown;
|
||||
astNode->typeTag = tag;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
astNode->typeTag = lookupNode->typeTag;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case IfStatement: {
|
||||
case IfStatement:
|
||||
{
|
||||
Node *clause = astNode->ifStatement.expression;
|
||||
Node *stmtSeq = astNode->ifStatement.statementSequence;
|
||||
mainNode = MakeIdNode(OrderedScope, "if", parent);
|
||||
|
@ -145,7 +196,8 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
|
|||
break;
|
||||
}
|
||||
|
||||
case IfElseStatement: {
|
||||
case IfElseStatement:
|
||||
{
|
||||
Node *ifNode = astNode->ifElseStatement.ifStatement;
|
||||
Node *elseStmts = astNode->ifElseStatement.elseStatement;
|
||||
mainNode = MakeIdNode(OrderedScope, "if-else", parent);
|
||||
|
@ -162,23 +214,29 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
|
|||
return NULL;
|
||||
|
||||
case Return:
|
||||
AddChildToNode(parent, MakeIdTree(astNode->returnStatement.expression, parent));
|
||||
AddChildToNode(
|
||||
parent,
|
||||
MakeIdTree(astNode->returnStatement.expression, parent));
|
||||
return NULL;
|
||||
|
||||
case StatementSequence: {
|
||||
for (i = 0; i < astNode->statementSequence.count; i++) {
|
||||
case StatementSequence:
|
||||
{
|
||||
for (i = 0; i < astNode->statementSequence.count; i++)
|
||||
{
|
||||
Node *argNode = astNode->statementSequence.sequence[i];
|
||||
AddChildToNode(parent, MakeIdTree(argNode, parent));
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
case StructDeclaration: {
|
||||
case StructDeclaration:
|
||||
{
|
||||
Node *idNode = astNode->structDeclaration.identifier;
|
||||
Node *declsNode = astNode->structDeclaration.declarationSequence;
|
||||
mainNode = MakeIdNode(Struct, idNode->identifier.name, parent);
|
||||
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];
|
||||
AddChildToNode(mainNode, MakeIdTree(decl, mainNode));
|
||||
}
|
||||
|
@ -190,7 +248,9 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
|
|||
return NULL;
|
||||
|
||||
case UnaryExpression:
|
||||
AddChildToNode(parent, MakeIdTree(astNode->unaryExpression.child, parent));
|
||||
AddChildToNode(
|
||||
parent,
|
||||
MakeIdTree(astNode->unaryExpression.child, parent));
|
||||
return NULL;
|
||||
|
||||
case Comment:
|
||||
|
@ -209,13 +269,18 @@ IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
|
|||
return mainNode;
|
||||
}
|
||||
|
||||
void PrintIdNode(IdNode *node) {
|
||||
if (node == NULL) {
|
||||
fprintf(stderr, "wraith: Attempted to call PrintIdNode with null value.\n");
|
||||
void PrintIdNode(IdNode *node)
|
||||
{
|
||||
if (node == NULL)
|
||||
{
|
||||
fprintf(
|
||||
stderr,
|
||||
"wraith: Attempted to call PrintIdNode with null value.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
switch(node->type) {
|
||||
switch (node->type)
|
||||
{
|
||||
case Placeholder:
|
||||
printf("Placeholder (%s)\n", node->name);
|
||||
break;
|
||||
|
@ -229,7 +294,10 @@ void PrintIdNode(IdNode *node) {
|
|||
printf("%s : %s\n", node->name, TypeTagToString(node->typeTag));
|
||||
break;
|
||||
case Function:
|
||||
printf("%s : Function<%s>\n", node->name, TypeTagToString(node->typeTag));
|
||||
printf(
|
||||
"%s : Function<%s>\n",
|
||||
node->name,
|
||||
TypeTagToString(node->typeTag));
|
||||
break;
|
||||
case Variable:
|
||||
printf("%s : %s\n", node->name, TypeTagToString(node->typeTag));
|
||||
|
@ -237,40 +305,53 @@ void PrintIdNode(IdNode *node) {
|
|||
}
|
||||
}
|
||||
|
||||
void PrintIdTree(IdNode *tree, uint32_t tabCount) {
|
||||
if (tree == NULL) {
|
||||
fprintf(stderr, "wraith: Attempted to call PrintIdTree on a null value.\n");
|
||||
void PrintIdTree(IdNode *tree, uint32_t tabCount)
|
||||
{
|
||||
if (tree == NULL)
|
||||
{
|
||||
fprintf(
|
||||
stderr,
|
||||
"wraith: Attempted to call PrintIdTree on a null value.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t i;
|
||||
for (i = 0; i < tabCount; i++) {
|
||||
for (i = 0; i < tabCount; i++)
|
||||
{
|
||||
printf("| ");
|
||||
}
|
||||
|
||||
PrintIdNode(tree);
|
||||
|
||||
for (i = 0; i < tree->childCount; i++) {
|
||||
for (i = 0; i < tree->childCount; i++)
|
||||
{
|
||||
PrintIdTree(tree->children[i], tabCount + 1);
|
||||
}
|
||||
}
|
||||
|
||||
int PrintAncestors(IdNode *node) {
|
||||
if (node == NULL) return -1;
|
||||
int PrintAncestors(IdNode *node)
|
||||
{
|
||||
if (node == NULL)
|
||||
return -1;
|
||||
|
||||
int i;
|
||||
int indent = 1;
|
||||
indent += PrintAncestors(node->parent);
|
||||
for (i = 0; i < indent; i++) {
|
||||
for (i = 0; i < indent; i++)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
PrintIdNode(node);
|
||||
return indent;
|
||||
}
|
||||
|
||||
IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) {
|
||||
if (root == NULL) {
|
||||
fprintf(stderr, "wraith: Attempted to call LookdownId on a null value.\n");
|
||||
IdNode *LookdownId(IdNode *root, NodeType targetType, char *targetName)
|
||||
{
|
||||
if (root == NULL)
|
||||
{
|
||||
fprintf(
|
||||
stderr,
|
||||
"wraith: Attempted to call LookdownId on a null value.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -279,23 +360,29 @@ IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) {
|
|||
frontier[0] = root;
|
||||
uint32_t frontierCount = 1;
|
||||
|
||||
while (frontierCount > 0) {
|
||||
while (frontierCount > 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;
|
||||
break;
|
||||
}
|
||||
|
||||
uint32_t i;
|
||||
for(i = 1; i < frontierCount; i++) {
|
||||
for (i = 1; i < frontierCount; i++)
|
||||
{
|
||||
frontier[i - 1] = frontier[i];
|
||||
}
|
||||
size_t newSize = frontierCount + current->childCount - 1;
|
||||
if (frontierCount != newSize) {
|
||||
if (frontierCount != 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];
|
||||
}
|
||||
frontierCount = newSize;
|
||||
|
@ -305,8 +392,10 @@ IdNode* LookdownId(IdNode *root, NodeType targetType, char *targetName) {
|
|||
return result;
|
||||
}
|
||||
|
||||
bool ScopeHasOrdering(IdNode *node) {
|
||||
switch (node->type) {
|
||||
bool ScopeHasOrdering(IdNode *node)
|
||||
{
|
||||
switch (node->type)
|
||||
{
|
||||
case OrderedScope:
|
||||
case Function:
|
||||
case Variable: /* this is only technically true */
|
||||
|
@ -316,59 +405,76 @@ bool ScopeHasOrdering(IdNode *node) {
|
|||
}
|
||||
}
|
||||
|
||||
IdNode* LookupId(IdNode *node, IdNode *prev, char *target) {
|
||||
if (node == NULL) {
|
||||
IdNode *LookupId(IdNode *node, IdNode *prev, char *target)
|
||||
{
|
||||
if (node == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (strcmp(node->name, target) == 0 && node->type != Placeholder) {
|
||||
if (strcmp(node->name, target) == 0 && node->type != Placeholder)
|
||||
{
|
||||
return node;
|
||||
}
|
||||
|
||||
/* If this is the start of our search, we should not attempt to look at child nodes. Only
|
||||
* looking up the scope tree is valid at this point.
|
||||
/* If this is the start of our search, we should not attempt to look at
|
||||
* 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
|
||||
* up a struct's internals starting from the node representing the struct itself. This is
|
||||
* because an IdNode corresponds to the location *where an identifier is first declared.* Thus,
|
||||
* an identifier has no knowledge of identifiers declared "inside" of it.
|
||||
* This has the notable side-effect that this function will return NULL if
|
||||
* you attempt to look up a struct's internals starting from the node
|
||||
* representing the struct itself. This is because an IdNode corresponds to
|
||||
* 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);
|
||||
}
|
||||
|
||||
/* If the current node forms an ordered scope then we want to prevent ourselves from looking
|
||||
* up identifiers declared after the scope we have just come from.
|
||||
/* If the current node forms an ordered scope then we want to prevent
|
||||
* ourselves from looking up identifiers declared after the scope we have
|
||||
* just come from.
|
||||
*/
|
||||
uint32_t idxLimit;
|
||||
if (ScopeHasOrdering(node)) {
|
||||
if (ScopeHasOrdering(node))
|
||||
{
|
||||
uint32_t i;
|
||||
for (i = 0, idxLimit = 0; i < node->childCount; i++, idxLimit++) {
|
||||
if (node->children[i] == prev) {
|
||||
for (i = 0, idxLimit = 0; i < node->childCount; i++, idxLimit++)
|
||||
{
|
||||
if (node->children[i] == prev)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
idxLimit = node->childCount;
|
||||
}
|
||||
|
||||
uint32_t i;
|
||||
for (i = 0; i < idxLimit; i++) {
|
||||
for (i = 0; i < idxLimit; 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. */
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strcmp(child->name, target) == 0) {
|
||||
if (strcmp(child->name, target) == 0)
|
||||
{
|
||||
return child;
|
||||
}
|
||||
|
||||
if (child->type == Struct) {
|
||||
if (child->type == Struct)
|
||||
{
|
||||
uint32_t j;
|
||||
for (j = 0; j < child->childCount; j++) {
|
||||
for (j = 0; j < child->childCount; j++)
|
||||
{
|
||||
IdNode *grandchild = child->children[j];
|
||||
if (strcmp(grandchild->name, target) == 0) {
|
||||
if (strcmp(grandchild->name, target) == 0)
|
||||
{
|
||||
return grandchild;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,8 @@
|
|||
struct TypeTag;
|
||||
struct Node;
|
||||
|
||||
typedef enum NodeType {
|
||||
typedef enum NodeType
|
||||
{
|
||||
Placeholder,
|
||||
UnorderedScope,
|
||||
OrderedScope,
|
||||
|
@ -19,7 +20,8 @@ typedef enum NodeType {
|
|||
Variable
|
||||
} NodeType;
|
||||
|
||||
typedef struct IdNode {
|
||||
typedef struct IdNode
|
||||
{
|
||||
NodeType type;
|
||||
char *name;
|
||||
struct TypeTag *typeTag;
|
||||
|
@ -29,13 +31,14 @@ typedef struct IdNode {
|
|||
uint32_t childCapacity;
|
||||
} IdNode;
|
||||
|
||||
typedef struct IdStatus {
|
||||
enum StatusCode {
|
||||
typedef struct IdStatus
|
||||
{
|
||||
enum StatusCode
|
||||
{
|
||||
Valid,
|
||||
} StatusCode;
|
||||
} IdStatus;
|
||||
|
||||
|
||||
IdNode *MakeIdTree(struct Node *astNode, IdNode *parent);
|
||||
void PrintIdNode(IdNode *node);
|
||||
void PrintIdTree(IdNode *tree, uint32_t tabCount);
|
||||
|
|
32
src/main.c
32
src/main.c
|
@ -1,9 +1,9 @@
|
|||
#include <stdlib.h>
|
||||
#include "../lib/dropt/dropt.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "parser.h"
|
||||
#include "codegen.h"
|
||||
#include "identcheck.h"
|
||||
#include "parser.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
@ -14,9 +14,26 @@ int main(int argc, char *argv[])
|
|||
int exitCode = EXIT_SUCCESS;
|
||||
|
||||
dropt_option options[] = {
|
||||
{ 'h', "help", "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 },
|
||||
{'h',
|
||||
"help",
|
||||
"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. */
|
||||
};
|
||||
|
||||
|
@ -36,7 +53,10 @@ int main(int argc, char *argv[])
|
|||
char **rest = dropt_parse(droptContext, -1, &argv[1]);
|
||||
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;
|
||||
}
|
||||
else if (showHelp)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#include "y.tab.h"
|
||||
#include "ast.h"
|
||||
#include "y.tab.h"
|
||||
|
||||
extern FILE *yyin;
|
||||
extern int yydebug;
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
#include "util.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *strdup(const char *s)
|
||||
{
|
||||
size_t slen = strlen(s);
|
||||
char* result = malloc(slen + 1);
|
||||
char *result = (char *)malloc(slen + 1);
|
||||
if (result == NULL)
|
||||
{
|
||||
return NULL;
|
||||
|
|
Loading…
Reference in New Issue