pANTLR3_BASE_TREE pythonbriefInsertArguments( struct pythonbriefParser_Ctx_struct * ctx, pANTLR3_VECTOR args ) { ANTLR3_UINT32 n = args->count; if ( n == 0 ) return NULL; /* No arguments so do not insert the ARGUMENTS node */ pANTLR3_BASE_TREE arguments_root = ctx->adaptor->nilNode( ctx->adaptor ); arguments_root = ctx->adaptor->becomeRoot( ctx->adaptor, ctx->adaptor->createTypeText( ctx->adaptor, ARGUMENTS, (pANTLR3_UINT8) "ARGUMENTS" ), arguments_root ); /* Add children stored in the vector of args */ ANTLR3_UINT32 k; for ( k = 0; k < n; ++k ) { struct function_argument * item = (struct function_argument *) (vectorGet( args, k )); pANTLR3_BASE_TREE child = ctx->adaptor->createTypeText( ctx->adaptor, item->type, item->name ); ctx->adaptor->addChild( ctx->adaptor, arguments_root, child ); } return arguments_root; }
pANTLR3_BASE_TREE pythonbriefInsertInheritance( struct pythonbriefParser_Ctx_struct * ctx, pANTLR3_VECTOR args ) { ANTLR3_UINT32 n = args->count; if ( n == 0 ) return NULL; /* No base classes, so do not create the CLASS_INHERITANCE node */ pANTLR3_BASE_TREE inheritance_root = ctx->adaptor->nilNode( ctx->adaptor ); inheritance_root = ctx->adaptor->becomeRoot( ctx->adaptor, ctx->adaptor->createTypeText( ctx->adaptor, CLASS_INHERITANCE, (pANTLR3_UINT8) "CLASS_INHERITANCE" ), inheritance_root ); /* Add children stored in the vector of args */ ANTLR3_UINT32 k; for ( k = 0; k < n; ++k ) { const char * item = (const char *) (vectorGet( args, k )); pANTLR3_BASE_TREE child = ctx->adaptor->createTypeText( ctx->adaptor, CLASS_INHERITANCE, (pANTLR3_UINT8) item ); ctx->adaptor->addChild( ctx->adaptor, inheritance_root, child ); } return inheritance_root; }
static bool driver (config conf) { bool fail = false; compilerCtx comp; compilerInit(&comp, &conf.arch, &conf.includeSearchPaths); /*Compile each of the inputs to assembly*/ for (int i = 0; i < conf.inputs.length; i++) { compiler(&comp, vectorGet(&conf.inputs, i), vectorGet(&conf.intermediates, i)); } compilerEnd(&comp); if (comp.errors != 0 || comp.warnings != 0) printf("Compilation complete with %d error%s and %d warning%s\n", comp.errors, plural(comp.errors), comp.warnings, plural(comp.warnings)); else if (internalErrors) printf("Compilation complete with %d internal error%s\n", internalErrors, plural(internalErrors)); /*Assemble/link*/ else if (conf.mode != modeNoAssemble) { /*Produce a string list of all the intermediates*/ char* intermediates = strjoinwith((char**) conf.intermediates.buffer, conf.intermediates.length, " ", malloc); if (conf.mode == modeNoLink) fail |= systemf("gcc %s -c %s", conf.arch.asflags, intermediates) != 0; else { fail |= systemf("gcc %s %s -o %s", conf.arch.ldflags, intermediates, conf.output) != 0; if (conf.deleteAsm && !fail) systemf("rm %s", intermediates); } free(intermediates); } return fail || comp.errors != 0 || internalErrors != 0; }
static inline ScopeElement *inLocalScope(Scope *scope, char *identifier) { if(scope) { Vector *variables = scope->variables; for(int i = 0; i < variables->size; i++) { ScopeElement *element = vectorGet(variables, i); if(element && strcmp(element->identifier, identifier) == 0) { return element; } } } return NULL; }
Scope *flattenScope(Scope *scope) { Scope *destinationScope = newScope(NULL); // Traverse every level of scope while(scope) { Vector *vars = scope->variables; for(int i = 0; i < vars->size; i++) { ScopeElement *elem = vectorGet(vars, i); char *identifier = elem->identifier; // If it hasn't already been declared in the flattened scope, "declare it" if(inLocalScope(destinationScope, identifier)) { error(REDECL_GLOBAL_VAR, identifier); } else { vectorAdd(destinationScope->variables, elem); } } scope = scope->enclosingScope; } return destinationScope; }
bool declareFunction(Scope *scope, Type returnType, char *identifier, Vector *parameters, bool declaredExtern, bool isPrototype) { ScopeElement *foundVar = findScopeElement(scope, identifier); bool validDeclaration = true; // Determine if something with that name already exists if(foundVar) { // If that thing is a function, check some properties if(foundVar->elementType == SCOPE_FUNC) { ScopeFunction *func = foundVar->function; // Determine whether or not a function prototype has been duplicated if(isPrototype) { error(REDEF_PROTOTYPE, identifier); validDeclaration = false; } // Check if the function's return type has been changed if(returnType != func->returnType) { error(CHANGE_RET_TYPE, identifier, typeName(func->returnType), typeName(returnType)); validDeclaration = false; } // Determine if the function has been implemented or declared as extern if(func->implemented) { // An already implemented function cannot be reimplemented error(REDEF_FUNCTION, identifier); validDeclaration = false; } else if(func->declaredExtern) { // An extern function cannot be declared in the same file error(REDEF_EXTERN, identifier); validDeclaration = false; } else { // Ensure that the prototype and declaration match Vector *expectedParams = func->parameters; int declared = parameters->size; int expected = expectedParams->size; int min = declared < expected ? declared : expected; // Check the types of each declared and expected parameter for(int i = 0; i < min; i++) { FunctionParameter *declaredParam = vectorGet(parameters, i); FunctionParameter *expectedParam = vectorGet(expectedParams, i); Type declaredType = declaredParam->type; Type expectedType = expectedParam->type; if(! typesCompatible(declaredType, expectedType)) { error(ARG_TYPE_CHANGE, i, identifier, typeName(declaredType), typeName(expectedType)); validDeclaration = false; } } if(declared == 0 && expected != 0) { error(REDEF_WITHOUT_ARGS, identifier); validDeclaration = false; } else if(declared != 0 && expected == 0) { error(REDEF_WITH_ARGS, identifier); validDeclaration = false; } else if(declared != expected) { error(ARG_NUM_CHANGE, identifier, expected, declared); validDeclaration = false; } func->implemented = true; } } else { // If it's a variable, then a variable can't be defined as a scope error(REDEF_VAR_AS_FUNC, identifier); validDeclaration = false; } } else { // Add the function to scope debug(E_DEBUG, "Declaring function %s of type %s on line %d.\n", identifier, typeName(returnType), mylineno); ScopeFunction *scopeFunction = malloc(sizeof(ScopeFunction)); scopeFunction->returnType = returnType; scopeFunction->parameters = parameters; scopeFunction->implemented = false; scopeFunction->declaredExtern = declaredExtern; ScopeElement *elem = malloc(sizeof(ScopeElement)); elem->identifier = identifier; // Don't override the function name "main" since it is necessary for execution entry if(strcmp(identifier, "main") == 0) { elem->protectedIdentifier = identifier; } else { int newIdentifierLength = strlen(identifier) + 2; char *protectedIdentifier = malloc(newIdentifierLength); snprintf(protectedIdentifier, newIdentifierLength, "_%s", identifier); elem->protectedIdentifier = protectedIdentifier; } elem->elementType = SCOPE_FUNC; elem->function = scopeFunction; vectorAdd(scope->variables, elem); return true; } return validDeclaration; }
void *vectorGetAny(vector_t *vec, size_t index) { value_t res = vectorGet(vec, index); return (res.ptrAny == NONE_VALUE.ptrAny) ? NULL : res.ptrAny; }
char *vectorGetString(vector_t *vec, size_t index) { value_t res = vectorGet(vec, index); return (res.ptrAny == NONE_VALUE.ptrAny) ? NULL : res.string; }
unsigned vectorGetUnsigned(vector_t *vec, size_t index) { value_t res = vectorGet(vec, index); return (res.ptrAny == NONE_VALUE.ptrAny) ? 0 : res.numberUnsigned; }
double vectorGetDouble(vector_t *vec, size_t index) { value_t res = vectorGet(vec, index); return (res.ptrAny == NONE_VALUE.ptrAny) ? 0.0 : res.numberDouble; }
int vectorGetInt(vector_t *vec, size_t index) { value_t res = vectorGet(vec, index); return (res.ptrAny == NONE_VALUE.ptrAny) ? 0 : res.numberInteger; }