Untitled

 avatar
unknown
plain_text
2 years ago
1.4 kB
4
Indexable
#include <stdio.h>
#include <stdlib.h>
#include "lexer.h"
#include "parser.h"

int main(int argc, char* argv[]) {
    if (argc < 2) {
        printf("Usage: TextJedi <filename>\n");
        return 1;
    }

    char* filename = argv[1];
    FILE* file = fopen(filename, "r");
    if (file == NULL) {
        printf("Failed to open the file: %s\n", filename);
        return 1;
    }

    // Determine the size of the file
    fseek(file, 0, SEEK_END);
    long file_size = ftell(file);
    rewind(file);

    // Allocate memory for the source code
    char* source_code = malloc(file_size + 1);
    if (source_code == NULL) {
        printf("Failed to allocate memory for the source code.\n");
        fclose(file);
        return 1;
    }

    // Read the source code from the file
    size_t read_size = fread(source_code, 1, file_size, file);
    source_code[read_size] = '\0';

    // Close the file
    fclose(file);

    // Tokenize the source code
    Token* tokens = tokenize(source_code);
    if (tokens != NULL) {
        printf("Lexical Analysis:\n");
        printTokens(tokens);

        printf("\nParsing:\n");
        parse(tokens);

        // Clean up tokens
        free(tokens);
    } else {
        printf("Tokenization failed.\n");
    }

    // Clean up memory
    free(source_code);

    return 0;
}
Editor is loading...