Skip to content

Commit

Permalink
A number of things I forgot to commit before.
Browse files Browse the repository at this point in the history
  • Loading branch information
pieman72 committed Feb 12, 2015
1 parent c3a1664 commit 2866d3f
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 5 deletions.
18 changes: 18 additions & 0 deletions adhoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,23 @@ void adhoc_determineType(ASTnode* n, int d, char* errBuf){
}
}

// A walkable final check for validity items
void adhoc_finalCheckNode(ASTnode* n, int d, char* errBuf){
if(n->nodeType==OPERATOR || n->nodeType==ASSIGNMENT){
if(n->countChildren==2
&& n->children[0]->nodeType==ASSIGNMENT
&& n->children[1]->nodeType==ASSIGNMENT
&& !strcmp(
n->children[0]->children[0]->name,
n->children[1]->children[0]->name
)
){
adhoc_errorNode = n;
sprintf(errBuf, "You should be shot for writing this!");
}
}
}

// Simple function for hashing other structs
hashMap_uint adhoc_hashItemLocation(void* i){
return hashMap_hashString(((itemLocation*) i)->item);
Expand Down Expand Up @@ -594,6 +611,7 @@ void adhoc_validate(char* errBuf){

// Final check for all node info
// TODO
adhoc_treeWalk(adhoc_finalCheckNode, ASTroot, 0, errBuf);
}

// Generate the target language code
Expand Down
49 changes: 44 additions & 5 deletions c.h
Original file line number Diff line number Diff line change
Expand Up @@ -658,14 +658,43 @@ void lang_c_generate_action(bool isInit, bool defin, ASTnode* n, short indent, F
}
fprintf(outFile, "\", ");

// find max value - finds the max value in an array
}else if(!strcmp(n->name, "adhoc_find_max_value")){
if(n->children[0]->dataType != TYPE_ARRAY){
adhoc_errorNode = n->children[0];
sprintf(
errBuf
,"Node %d: First parameter to 'find max value' must be an array"
,n->children[0]->id
);
}
fprintf(outFile, "*(%s*)%s("
,adhoc_dataType_names[n->children[0]->dataType]
,n->name
);

// find max value index - finds the index of the max value in an array
}else if(!strcmp(n->name, "adhoc_find_max_value_index")){
if(n->children[0]->dataType != TYPE_ARRAY){
adhoc_errorNode = n->children[0];
sprintf(
errBuf
,"Node %d: First parameter to 'find max value index' must be an array"
,n->children[0]->id
);
}
fprintf(outFile, "%s("
,n->name
);

// Unrecognized library function!
}else{
adhoc_errorNode = n;
sprintf(
errBuf
,"Node %d: Datatype cannot be concatenated: %s"
,"Node %d: Unrecognized library function: %s"
,n->id
,adhoc_dataType_names[n->dataType]
,n->name
);
}

Expand Down Expand Up @@ -1222,9 +1251,19 @@ void lang_c_generate_operator(bool isInit, ASTnode* n, short indent, FILE* outFi
case OPERATOR_GRTEQ:
case OPERATOR_LESEQ:
case OPERATOR_NOTEQ:
lang_c_generate(false, n->children[0], indent+1, outFile, nodes, errBuf);
fprintf(outFile, " %s ", adhoc_nodeWhich_names[n->which]);
lang_c_generate(false, n->children[1], indent+1, outFile, nodes, errBuf);
if(n->children[0]->dataType==TYPE_STRNG
&& n->children[1]->dataType==TYPE_STRNG
){
fprintf(outFile, " (strcmp(");
lang_c_generate(false, n->children[0], indent+1, outFile, nodes, errBuf);
fprintf(outFile, ", ");
lang_c_generate(false, n->children[1], indent+1, outFile, nodes, errBuf);
fprintf(outFile, ") %s 0) ", adhoc_nodeWhich_names[n->which]);
}else{
lang_c_generate(false, n->children[0], indent+1, outFile, nodes, errBuf);
fprintf(outFile, " %s ", adhoc_nodeWhich_names[n->which]);
lang_c_generate(false, n->children[1], indent+1, outFile, nodes, errBuf);
}
break;
case OPERATOR_TRNIF:
lang_c_generate(false, n->children[0], indent+1, outFile, nodes, errBuf);
Expand Down
73 changes: 73 additions & 0 deletions libadhoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,76 @@ void adhoc_append_to_array(char* format, adhoc_data* baseArray, ...){

adhoc_unreferenceData(baseArray);
}

// Find the max value in an array
void* adhoc_find_max_value(adhoc_data* inputArray){
adhoc_referenceData(inputArray);

int index = adhoc_find_max_value_index(inputArray);
void* result = adhoc_getSArrayData(inputArray, index);

adhoc_unreferenceData(inputArray);
return result;
}

// Find the index of the max value in an array
int adhoc_find_max_value_index(adhoc_data* inputArray){
adhoc_referenceData(inputArray);

int i, count, bestIndex=-1;
bool bestBool = false;
int bestInt = 0;
float bestFloat = 0;
void* tempData;
int arrCount = adhoc_countC(inputArray);
int arrSize = adhoc_sizeC(inputArray);
if(arrCount<1){
adhoc_unreferenceData(inputArray);
return bestIndex;
}

for(i=0,count=0; i<arrSize&&count<arrCount; ++i){
if(!adhoc_isset_array(inputArray, i)) continue;
tempData = adhoc_getSArrayData(inputArray, i);
if(bestIndex == -1){
bestIndex = i;
switch(inputArray->dataType){
case DATA_BOOL: bestBool = *(bool*)tempData; break;
case DATA_INT: bestInt = *(int*)tempData; break;
case DATA_FLOAT: bestFloat = *(float*)tempData; break;
default:
adhoc_unreferenceData(inputArray);
return -1;
}
}

switch(inputArray->dataType){
case DATA_BOOL:
;bool tempBool = *(bool*)tempData;
if(tempBool && !bestBool){
bestBool = true;
bestIndex = i;
}
break;
case DATA_INT:
;int tempInt = *(int*)tempData;
if(tempInt > bestInt){
bestInt = tempInt;
bestIndex = i;
}
break;
case DATA_FLOAT:
;float tempFloat = *(float*)tempData;
if(tempFloat > bestFloat){
bestFloat = tempFloat;
bestIndex = i;
}
break;
default:
break;
}
}

adhoc_unreferenceData(inputArray);
return bestIndex;
}
6 changes: 6 additions & 0 deletions libadhoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,10 @@ bool adhoc_isset_array(adhoc_data* arr, int i);
// Append one item to an existing array
void adhoc_append_to_array(char* format, adhoc_data* baseArray, ...);

// Find the max value in an array
void* adhoc_find_max_value(adhoc_data* inputArray);

// Find the index of the max value in an array
int adhoc_find_max_value_index(adhoc_data* inputArray);

#endif
Binary file modified programs/quicksort.adh
Binary file not shown.
Binary file modified programs/test.adh
Binary file not shown.

0 comments on commit 2866d3f

Please sign in to comment.