void startCompiler(Compiler *self) { if (!self->sourceFiles || self->sourceFiles->size == 0) { printf("not running \n"); return; } // lex file verboseModeMessage("Started lexing"); self->lexer = createLexer(self->sourceFiles); startLexingFiles(self->lexer, self->sourceFiles); if (self->lexer->failed) { return; } verboseModeMessage("Finished lexing"); // initialise parser after we tokenize verboseModeMessage("Started parsing"); self->parser = createParser(); startParsingSourceFiles(self->parser, self->sourceFiles); verboseModeMessage("Finished parsing"); // failed parsing stage if (self->parser->failed) { return; // don't do stuff after this } self->semantic = createSemanticAnalyzer(self->sourceFiles); startSemanticAnalysis(self->semantic); if (self->semantic->failed) { return; } self->generatorLLVM = createLLVMCodeGenerator(self->sourceFiles); startLLVMCodeGeneration(self->generatorLLVM); }
void destroyLexer(Lexer *self) { if (self->tokenStream != NULL) { for (int i = 0; i < self->tokenStream->size; i++) { Token *tok = getVectorItem(self->tokenStream, i); // eof's content isnt malloc'd so free would give us some errors if (tok->type != TOKEN_END_OF_FILE) { verboseModeMessage("Freed `%s` token", tok->content); sdsfree(tok->content); } destroyToken(tok); } } verboseModeMessage("Destroyed lexer"); free(self); }
/** pushes a token with content */ void pushInitializedToken(Lexer *self, int type, char *content) { Token *tok = createToken(self->lineNumber, self->charNumber, self->fileName); tok->type = type; tok->content = content; verboseModeMessage("Lexed token: %-15s, type %s", tok->content, getTokenTypeName(tok->type)); pushBackItem(self->tokenStream, tok); }
/** pushes a token with no content */ void pushToken(Lexer *self, int type) { Token *tok = createToken(self->lineNumber, self->charNumber, self->fileName); tok->type = type; tok->content = extractToken(self, self->startPos, self->pos - self->startPos); verboseModeMessage("Lexed token: %-15s, type %s", tok->content, getTokenTypeName(tok->type)); pushBackItem(self->tokenStream, tok); }
/** pushes a token with content */ void pushInitializedToken(Lexer *self, int type, char *content) { int tokenLength = self->pos - self->startPos; // The length (in characters) of this token Token *tok = createToken(self->lineNumber, self->startPos, self->charNumber - tokenLength, self->charNumber, self->fileName); tok->type = type; tok->content = content; verboseModeMessage("Lexed token: %-15s, type %s", tok->content, getTokenTypeName(tok->type)); pushBackItem(self->tokenStream, tok); }
void destroyCompiler(Compiler *self) { if (self) { if (self->lexer) destroyLexer(self->lexer); if (self->parser) destroyParser(self->parser); if (self->generatorLLVM) destroyLLVMCodeGenerator(self->generatorLLVM); if (self->semantic) destroySemanticAnalyzer(self->semantic); if (self->sourceFiles) destroyVector(self->sourceFiles); free(self); verboseModeMessage("Destroyed compiler"); } }