compiler

Compiler for the Compilers classes.
Log | Files | Refs

compile.c (1436B)


      1 #include "arena.h"
      2 #include "codegen.h"
      3 #include "compiler.h"
      4 #include "parser.h"
      5 #include "semantics.h"
      6 #include "string.h"
      7 #include "tokenizer.h"
      8 
      9 #include <assert.h>
     10 #include <stdio.h>
     11 #include <string.h>
     12 
     13 bool ErrorOk(Error error)
     14 {
     15     return error.location.column == 0 && error.location.line == 0 &&
     16         StringEqCstr(error.message, "") && StringEqCstr(error.dynamic, "");
     17 }
     18 
     19 void PrintError(Error error, const char* file)
     20 {
     21     if (!StringEqCstr(error.dynamic, "")) {
     22         printf(
     23             "%s:%d:%d: %.*s: %.*s.\n",
     24             file,
     25             (int) error.location.line,
     26             (int) error.location.column,
     27             (int) error.message.size,
     28             error.message.ptr,
     29             (int) error.dynamic.size,
     30             error.dynamic.ptr
     31         );
     32         return;
     33     }
     34     printf(
     35         "%s:%d:%d: %.*s.\n",
     36         file,
     37         (int) error.location.line,
     38         (int) error.location.column,
     39         (int) error.message.size,
     40         error.message.ptr
     41     );
     42 }
     43 
     44 Error Compile(Arena* arena, char* program, FILE* output)
     45 {
     46     auto error = ZERO_ERROR;
     47 
     48     auto tokens = Tokenize(arena, program, &error);
     49     if (!ErrorOk(error))
     50         return error;
     51 
     52     auto tree = Parse(arena, program, tokens, &error);
     53     if (!ErrorOk(error))
     54         return error;
     55 
     56     error = SemanticAnalysis(arena, tree);
     57     if (!ErrorOk(error))
     58         return error;
     59 
     60     Codegen(arena, tree, output);
     61 
     62     return error;
     63 }