Skip to content

Commit

Permalink
More for arrays (including index op). Also some scaffolding and gener…
Browse files Browse the repository at this point in the history
…ation niceties.
  • Loading branch information
pieman72 committed Aug 14, 2014
1 parent e334e68 commit 994b74c
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 72 deletions.
55 changes: 45 additions & 10 deletions adhoc.h
Expand Up @@ -53,7 +53,7 @@ bool fileAcc(char* path, char* mode){
}

// A walkable simple print function
void adhoc_printNode(ASTnode* n, int d){
void adhoc_printNode(ASTnode* n, int d, char* errBuf){
char* buf = calloc(20, sizeof(char));
sprintf(buf, "%%-%ds%%2d %%s%%s (%%s)\n", d*3);
fprintf(
Expand All @@ -69,7 +69,7 @@ void adhoc_printNode(ASTnode* n, int d){
}

// A walkable name checker
void adhoc_renameNode(ASTnode* n, int d){
void adhoc_renameNode(ASTnode* n, int d, char* errBuf){
char* p;
while(p = strchr(n->package, ' ')) *p = '_';
while(p = strchr(n->name, ' ')) *p = '_';
Expand All @@ -91,9 +91,10 @@ void adhoc_renameNode(ASTnode* n, int d){
}

// Post-walkable function for determining the data-Type of a node
void adhoc_determineType(ASTnode* n, int d){
void adhoc_determineType(ASTnode* n, int d, char* errBuf){
if(n->reference){
n->dataType = n->reference->dataType;
n->childDataType = n->reference->childDataType;
return;
}
int i;
Expand Down Expand Up @@ -162,8 +163,14 @@ void adhoc_determineType(ASTnode* n, int d){
break;

case OPERATOR_ARIND:
n->dataType = TYPE_VOID;
// TODO
// If the array hasn't been given a type, report an error
if(n->children[0]->childDataType == TYPE_VOID){
adhoc_errorNode = n;
sprintf(errBuf, "Array index being accessed before array was given contents.");
}

// Get the type from the array being referenced
n->dataType = n->children[0]->childDataType;
break;

case OPERATOR_TRNIF:
Expand All @@ -182,7 +189,9 @@ void adhoc_determineType(ASTnode* n, int d){

case ASSIGNMENT_EQUAL:
n->dataType = n->children[1]->dataType;
n->childDataType = n->children[1]->childDataType;
n->children[0]->dataType = n->dataType;
n->children[0]->childDataType = n->childDataType;
break;

case ASSIGNMENT_PLUS:
Expand All @@ -197,12 +206,14 @@ void adhoc_determineType(ASTnode* n, int d){
break;

case VARIABLE_ASIGN:
if(n->childType==PARAMETER || n->childType==INITIALIZATION)
if(n->childType==PARAMETER || n->childType==INITIALIZATION){
n->dataType = n->children[0]->dataType;
n->childDataType = n->children[0]->childDataType;
}
break;

case VARIABLE_EVAL:
// TODO: Get type from reference
// Type is determined above by reference
break;

case LITERAL_INT:
Expand All @@ -216,6 +227,18 @@ void adhoc_determineType(ASTnode* n, int d){
break;
case LITERAL_ARRAY:
n->dataType = TYPE_ARRAY;
if(n->countChildren){
if(atoi(n->children[0]->value)<0){
n->childDataType = atoi(n->children[0]->name);
n->children[0] = NULL;
n->countChildren = 0;
}else{
n->childDataType = n->children[0]->children[0]->dataType;
}
}else{
adhoc_errorNode = n;
sprintf(errBuf, "Literal array not given a child datatype.");
}
break;
case LITERAL_HASH:
n->dataType = TYPE_HASH;
Expand Down Expand Up @@ -503,22 +526,34 @@ void adhoc_insertNode(ASTnode* n){
// Validate and optimize the abstract syntax tree
void adhoc_validate(char* errBuf){
if(ADHOC_DEBUG_INFO){
adhoc_treeWalk(adhoc_printNode, ASTroot, 0);
adhoc_treeWalk(adhoc_printNode, ASTroot, 0, errBuf);
fprintf(
stderr
,"\n\n%s-- Begin Generation --%s\n"
,(ADHOC_OUPUT_COLOR ? "" : "")
,(ADHOC_OUPUT_COLOR ? "" : "")
);
if(strlen(errBuf)) return;
}

// Check child types and counts
// TODO
if(strlen(errBuf)) return;

// Determine scopes for all nodes
// TODO
if(strlen(errBuf)) return;

// Rename system calls, and nodes with spaces in their names
adhoc_treeWalk(adhoc_renameNode, ASTroot, 0);
adhoc_treeWalk(adhoc_renameNode, ASTroot, 0, errBuf);
if(strlen(errBuf)) return;

// Determine node dataTypes
adhoc_treePostWalk(adhoc_determineType, ASTroot, 0);
adhoc_treePostWalk(adhoc_determineType, ASTroot, 0, errBuf);
if(strlen(errBuf)) return;

// Final check for all node info
// TODO
}

// Generate the target language code
Expand Down
18 changes: 11 additions & 7 deletions adhoc_types.h
Expand Up @@ -225,6 +225,7 @@ typedef struct ASTnode {
nodeWhich which;
nodeChildType childType;
dataType dataType;
dataType childDataType;
bool defined;
char* package;
char* name;
Expand Down Expand Up @@ -252,6 +253,7 @@ ASTnode* adhoc_createBlankNode(){
ret->which = WHICH_NULL;
ret->childType = CHILD_NULL;
ret->dataType = TYPE_VOID;
ret->childDataType = TYPE_VOID;
ret->defined = false;
ret->package = NULL;
ret->name = NULL;
Expand Down Expand Up @@ -296,24 +298,26 @@ const char* adhoc_getNodeSubLabel(ASTnode* n){
}

// Generic function pointer type for walks of the abstract syntax tree
typedef void (*walk_func)(ASTnode*, int);
typedef void (*walk_func)(ASTnode*, int, char*);

// Walk an AST with a function to perform on each node
void adhoc_treeWalk(walk_func f, ASTnode* n, int d){
f(n, d);
void adhoc_treeWalk(walk_func f, ASTnode* n, int d, char* errBuf){
f(n, d, errBuf);
if(strlen(errBuf)) return;
int i;
for(i=0; i<n->countChildren; ++i){
adhoc_treeWalk(f, n->children[i], d+1);
adhoc_treeWalk(f, n->children[i], d+1, errBuf);
}
}

// Walk an AST in post-order with a function to perform on each node
void adhoc_treePostWalk(walk_func f, ASTnode* n, int d){
void adhoc_treePostWalk(walk_func f, ASTnode* n, int d, char* errBuf){
int i;
for(i=0; i<n->countChildren; ++i){
adhoc_treePostWalk(f, n->children[i], d+1);
adhoc_treePostWalk(f, n->children[i], d+1, errBuf);
if(strlen(errBuf)) return;
}
f(n, d);
f(n, d, errBuf);
}

// Simple functions for hashing AST nodes
Expand Down

0 comments on commit 994b74c

Please sign in to comment.