Compare commits

..

No commits in common. "e2bca66dc4a289bdc3674ee85ce972872acd4a3a" and "cbeb8d3ce2422c486774b8a496faa8a2f2451bfd" have entirely different histories.

10 changed files with 16 additions and 304 deletions

View File

@ -23,7 +23,7 @@ find_package(LLVM)
include_directories(${CMAKE_SOURCE_DIR})
BISON_TARGET(Parser generators/wraith.y ${CMAKE_CURRENT_BINARY_DIR}/y.tab.c COMPILE_FLAGS "-d -v -t")
BISON_TARGET(Parser generators/wraith.y ${CMAKE_CURRENT_BINARY_DIR}/y.tab.c COMPILE_FLAGS "-d -v -t -Wcounterexamples")
FLEX_TARGET(Scanner generators/wraith.lex ${CMAKE_CURRENT_BINARY_DIR}/lex.yy.c)
ADD_FLEX_BISON_DEPENDENCY(Scanner Parser)
@ -41,11 +41,9 @@ add_executable(
# Source
src/ast.h
src/codegen.h
src/identcheck.h
src/parser.h
src/ast.c
src/codegen.c
src/identcheck.c
src/parser.c
src/main.c
# Generated code

View File

@ -1,6 +0,0 @@
struct Program {
// This triggers a parse error
static Main(): int {
return 0;
}
}

View File

@ -1,28 +0,0 @@
struct Program {
static Main(): int {
myInt: int = 54;
if (myInt < 0) {
signTag: int = 0 - 1;
} else if (myInt == 0) {
signTag: int = 0;
} else {
signTag: int = 1;
}
lol: int = 420;
myBool: bool;
if (myBool) {
if (myBool) {
if (myBool) {
if (myBool) {
if (myBool) {
lol: int = 69;
}
}
}
}
}
return 0;
}
}

View File

@ -2,8 +2,21 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "util.h"
char* strdup (const char* s)
{
size_t slen = strlen(s);
char* result = malloc(slen + 1);
if(result == NULL)
{
return NULL;
}
memcpy(result, s, slen+1);
return result;
}
const char* SyntaxKindString(SyntaxKind syntaxKind)
{
@ -16,8 +29,6 @@ const char* SyntaxKindString(SyntaxKind syntaxKind)
case Comment: return "Comment";
case CustomTypeNode: return "CustomTypeNode";
case Declaration: return "Declaration";
case Expression: return "Expression";
case ForLoop: return "ForLoop";
case DeclarationSequence: return "DeclarationSequence";
case FunctionArgumentSequence: return "FunctionArgumentSequence";
case FunctionCallExpression: return "FunctionCallExpression";

View File

@ -89,6 +89,7 @@ typedef struct Node
PrimitiveType primitiveType;
} Node;
char* strdup (const char* s);
const char* SyntaxKindString(SyntaxKind syntaxKind);
uint8_t IsPrimitiveType(Node *typeNode);

View File

@ -1,196 +0,0 @@
#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 *node = (IdNode*)malloc(sizeof(IdNode));
node->type = type;
node->name = strdup(name);
node->parent = parent;
node->childCount = 0;
node->childCapacity = 0;
node->children = NULL;
return node;
}
void AddChildToNode(IdNode *node, IdNode *child) {
if (child == NULL) return;
if (node->children == NULL) {
node->childCapacity = 2;
node->children = (IdNode**)malloc(sizeof(IdNode*) * node->childCapacity);
} else if (node->childCount == node->childCapacity) {
node->childCapacity *= 2;
node->children = realloc(node->children, sizeof(IdNode*) * node->childCapacity);
}
node->children[node->childCount] = child;
node->childCount += 1;
}
IdNode* MakeIdTree(Node *astNode, IdNode *parent) {
uint32_t i;
switch (astNode->syntaxKind) {
case Assignment:
return (astNode->children[0]->syntaxKind == Declaration)
? MakeIdTree(astNode->children[0], parent)
: NULL;
case IfStatement: {
Node *stmtSeq = astNode->children[1];
IdNode *ifNode = MakeIdNode(LexicalScope, "if", parent);
for (i = 0; i < stmtSeq->childCount; i++) {
AddChildToNode(ifNode, MakeIdTree(stmtSeq->children[i], ifNode));
}
return ifNode;
}
case IfElseStatement: {
Node *ifNode = astNode->children[0];
Node *elseStmts = astNode->children[1];
IdNode *ifElseNode = MakeIdNode(LexicalScope, "if-else", parent);
IdNode *ifBranch = MakeIdTree(ifNode, ifElseNode);
IdNode *elseBranch = MakeIdNode(LexicalScope, "else", ifElseNode);
AddChildToNode(ifElseNode, ifBranch);
AddChildToNode(ifElseNode, elseBranch);
for (i = 0; i < elseStmts->childCount; i++) {
AddChildToNode(elseBranch, MakeIdTree(elseStmts->children[i], elseBranch));
}
return ifElseNode;
}
case ForLoop: {
Node *loopDecl = astNode->children[0];
Node *loopBody = astNode->children[3];
IdNode *loopNode = MakeIdNode(LexicalScope, "for-loop", parent);
AddChildToNode(loopNode, MakeIdTree(loopDecl, loopNode));
for (i = 0; i < loopBody->childCount; i++) {
AddChildToNode(loopNode, MakeIdTree(loopBody->children[i], loopNode));
}
return loopNode;
}
case Declaration:
return MakeIdNode(Variable, astNode->children[1]->value.string, parent);
case StructDeclaration: {
Node *idNode = astNode->children[0];
Node *declsNode = astNode->children[1];
IdNode *structNode = MakeIdNode(Struct, idNode->value.string, parent);
for (i = 0; i < declsNode->childCount; i++) {
AddChildToNode(structNode, MakeIdTree(declsNode->children[i], structNode));
}
return structNode;
}
case FunctionDeclaration: {
Node *sigNode = astNode->children[0];
Node *funcNameNode = sigNode->children[0];
Node *funcArgsNode = sigNode->children[2];
Node *bodyStatementsNode = astNode->children[1];
IdNode *funcNode = MakeIdNode(Function, funcNameNode->value.string, parent);
for (i = 0; i < funcArgsNode->childCount; i++) {
AddChildToNode(funcNode, MakeIdTree(funcArgsNode->children[i], funcNode));
}
for (i = 0; i < bodyStatementsNode->childCount; i++) {
AddChildToNode(funcNode, MakeIdTree(bodyStatementsNode->children[i], funcNode));
}
return funcNode;
}
case DeclarationSequence: {
IdNode *declSeqNode = MakeIdNode(LexicalScope, "", parent);
for (i = 0; i < astNode->childCount; i++) {
AddChildToNode(declSeqNode, MakeIdTree(astNode->children[i], declSeqNode));
}
return declSeqNode;
}
default:
return NULL;
}
}
void PrintIdNode(IdNode *node) {
switch(node->type) {
case LexicalScope: printf("Scope (%s)\n", node->name); break;
case Struct: printf("%s : Struct\n", node->name); break;
case Function: printf("%s : Function\n", node->name); break;
case Variable: printf("%s : Variable\n", node->name); break;
}
}
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++) {
printf("| ");
}
PrintIdNode(tree);
for (i = 0; i < tree->childCount; i++) {
PrintIdTree(tree->children[i], tabCount + 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++) {
printf(" ");
}
PrintIdNode(node);
return indent;
}
IdNode* FindId(IdNode *root, NodeType targetType, char *targetName) {
if (root == NULL) {
fprintf(stderr, "wraith: Attempted to call FindId on a null value.\n");
return NULL;
}
IdNode *result = NULL;
IdNode **frontier = (IdNode**)malloc(sizeof(IdNode*));
frontier[0] = root;
uint32_t frontierCount = 1;
while (frontierCount > 0) {
IdNode *current = frontier[0];
if (current->type == targetType && strcmp(current->name, targetName) == 0) {
result = current;
break;
}
uint32_t i;
for(i = 1; i < frontierCount; i++) {
frontier[i-1] = frontier[i];
}
size_t newSize = frontierCount + current->childCount - 1;
if (frontierCount != newSize) {
frontier = realloc(frontier, sizeof(IdNode*) * newSize);
}
for (i = 0; i < current->childCount; i++) {
frontier[frontierCount + i - 1] = current->children[i];
}
frontierCount = newSize;
}
free(frontier);
return result;
}

View File

@ -1,39 +0,0 @@
// Validates identifier usage in an AST.
#ifndef WRAITH_IDENTCHECK_H
#define WRAITH_IDENTCHECK_H
#include <stdint.h>
#include "ast.h"
typedef enum NodeType {
LexicalScope,
Struct,
Function,
Variable
} NodeType;
typedef struct IdNode {
NodeType type;
char *name;
struct IdNode *parent;
struct IdNode **children;
uint32_t childCount;
uint32_t childCapacity;
} IdNode;
typedef struct IdStatus {
enum StatusCode {
Valid,
} StatusCode;
} IdStatus;
IdNode* FindId(IdNode *root, NodeType targetType, char *targetName);
IdNode* MakeIdTree(Node *astNode, IdNode *parent);
void PrintIdTree(IdNode *tree, uint32_t tabCount);
int PrintAncestors(IdNode *node);
//IdStatus CheckIds(Node *root);
#endif /* WRAITH_IDENTCHECK_H */

View File

@ -3,7 +3,6 @@
#include "parser.h"
#include "codegen.h"
#include "identcheck.h"
int main(int argc, char *argv[])
{
@ -65,10 +64,6 @@ int main(int argc, char *argv[])
}
else
{
if (parseVerbose) {
IdNode *idTree = MakeIdTree(rootNode, NULL);
PrintIdTree(idTree, /*tabCount=*/0);
}
exitCode = Codegen(rootNode, optimizationLevel);
}
}

View File

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

View File

@ -1,8 +0,0 @@
#ifndef WRAITH_UTIL_H
#define WRAITH_UTIL_H
#include <string.h>
char* strdup (const char* s);
#endif /* WRAITH_UTIL_H */