slr

S Lang Runtime
git clone https://codeberg.org/night0721/slr
Log | Files | Refs | README | LICENSE

slr.c (854B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <ctype.h>
      4 #include <string.h>
      5 
      6 #include "lexer.h"
      7 #include "parser.h"
      8 
      9 long long get_file_length(FILE *file)
     10 {
     11     fseeko(file, 0, SEEK_END);
     12     long long length = ftello(file);
     13     fseeko(file, 0, SEEK_SET);
     14     return length;
     15 }
     16 
     17 int main(int argc, char **argv)
     18 {
     19     if (argc < 2) {
     20         fprintf(stderr, "\033[1;37mslr:\033[0m \033[1;31mfatal error:\033[0m no input files\ninterpretation termined.\n");
     21 		return 1;
     22     }
     23     FILE *file = fopen(argv[1], "r");
     24 	if (!file) {
     25 		perror("fopen");
     26 		return 1;
     27 	}
     28     long long length = get_file_length(file);
     29     char *buf = malloc(length * sizeof(char));
     30     fread(buf, 1, length, file);
     31     fclose(file);
     32     buf[length + 1] = '\0';
     33     Token *tokens = lexer(buf);
     34 	for(size_t i = 0; tokens[i].type != END_OF_TOKENS; i++){
     35 		print_token(tokens[i]);
     36 	}
     37 }