zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

blob e3392fed (2373B) - Raw


      1 #include "ast.h"
      2 #include "astgen.h"
      3 #include "intern_pool.h"
      4 #include "sema.h"
      5 #include "zir.h"
      6 
      7 #include <stdbool.h>
      8 #include <stdio.h>
      9 #include <stdlib.h>
     10 #include <string.h>
     11 
     12 // API:
     13 // - code = 0: program successfully terminated.
     14 // - code = 1: panicked, panic message in msg. Caller should free msg.
     15 // - code = 2: interpreter error, error in msg. Caller should free msg.
     16 static int zig0Run(const char* program, char** msg) {
     17     uint32_t len = (uint32_t)strlen(program);
     18     Ast ast = astParse(program, len);
     19     if (ast.has_error) {
     20         *msg = ast.err_msg;
     21         ast.err_msg = NULL;
     22         astDeinit(&ast);
     23         return 2;
     24     }
     25 
     26     fprintf(stderr, "tokens: %u, nodes: %u, first token: %s\n", ast.tokens.len,
     27         ast.nodes.len, tokenizerGetTagString(ast.tokens.tags[0]));
     28 
     29     Zir zir = astGen(&ast);
     30     astDeinit(&ast);
     31 
     32     if (zir.has_compile_errors) {
     33         const char err[] = "astgen failed";
     34         *msg = malloc(sizeof(err));
     35         memcpy(*msg, err, sizeof(err));
     36         zirDeinit(&zir);
     37         return 2;
     38     }
     39 
     40     fprintf(stderr, "zir: %u instructions, %u extra, %u string bytes\n",
     41         zir.inst_len, zir.extra_len, zir.string_bytes_len);
     42 
     43     InternPool ip = ipInit();
     44     Sema sema = semaInit(&ip, zir);
     45     Air air = semaAnalyze(&sema);
     46     semaDeinit(&sema);
     47     airDeinit(&air);
     48     ipDeinit(&ip);
     49     zirDeinit(&zir);
     50     return 0;
     51 }
     52 
     53 // API: run and:
     54 // code = 3: abnormal error, expect something in stderr.
     55 int zig0RunFile(const char* fname, char** msg) {
     56     FILE* f = fopen(fname, "r");
     57     if (f == NULL) {
     58         perror("fopen");
     59         return 3;
     60     }
     61     fseek(f, 0, SEEK_END);
     62     long fsizel = ftell(f);
     63     if (fsizel == -1) {
     64         perror("ftell");
     65         fclose(f);
     66         return 3;
     67     }
     68     unsigned long fsize = (unsigned long)fsizel;
     69     fseek(f, 0, SEEK_SET);
     70 
     71     char* program = malloc(fsize + 1);
     72     if (program == NULL) {
     73         perror("malloc");
     74         fclose(f);
     75         return 3;
     76     }
     77 
     78     size_t bytes_read = fread(program, 1, fsize, f);
     79     if (bytes_read < fsize) {
     80         if (ferror(f)) {
     81             perror("fread");
     82         } else {
     83             fprintf(stderr, "Unexpected end of file\n");
     84         }
     85         free(program);
     86         fclose(f);
     87         return 3;
     88     }
     89     fclose(f);
     90     program[fsize] = 0;
     91 
     92     int code = zig0Run(program, msg);
     93     free(program);
     94     return code;
     95 }