Gay

mail@pastecode.io avatar
unknown
plain_text
a month ago
8.0 kB
3
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "main.h"
#include "list.h"

#define debug printf("%s:%d: ############### debug\n", __FILE__, __LINE__)

#define toupper(_char) (_char - (char)32)

const char* objectTypeName[] = {
    [OBJECT_TYPE_UNDEFINED] = "undefined",
    [OBJECT_TYPE_VOID] = "void",
    [OBJECT_TYPE_INT] = "int",
    [OBJECT_TYPE_FLOAT] = "float",
    [OBJECT_TYPE_BOOL] = "bool",
    [OBJECT_TYPE_STR] = "string",
    [OBJECT_TYPE_FUNCTION] = "function",
};

char* yyInputFileName;
bool compileError;

int indent = 0;
int scopeLevel = -1;
int funcLineNo = 0;
int variableAddress = -1;
ObjectType variableIdentType;

struct list_scope *current = NULL;
struct list_head chainHead;



void pushScope() {
    printf("> Create symbol table (scope level %d)\n", ++scopeLevel);

    return;
}

void dumpScope() {
    printf("\n> Dump symbol table (scope level: %d)\n", scopeLevel--);
    printf("Index     Name                Type      Addr      Lineno    Func_sig  \n");

    struct list_variable *temp, *next;

    // Traverse the list and print each element in the current scope
    list_for_each_entry_safe(temp, next, current->queue, list) {
        printf(
            "%-10d%-20s%-10s%-10lld%-10d%-10s\n",
            temp->variableInfo.symbol->index,
            temp->variableInfo.symbol->name,
            objectTypeName[temp->variableInfo.type],
            temp->variableInfo.symbol->addr,
            temp->variableInfo.symbol->lineno,
            temp->variableInfo.symbol->func_sig
        );

        // Remove the element from the list
        list_del(&temp->list);
        free(temp->variableInfo.symbol);
        free(temp);
    }

    // Check if the queue is now empty and delete the scope
    if (current->queue->next == current->queue && current->queue->prev == current->queue) {
        struct list_scope *oldCurrent = current;

        // Shift to the next scope in the chain
        current = list_entry(current->chain.next, struct list_scope, chain);

        // Remove the old scope from the chain and free its memory
        list_del(&oldCurrent->chain);
        free(oldCurrent->queue);
        free(oldCurrent);
    }
}

Object* createVariable(ObjectType variableType, char* variableName, int variableFlag) {
    return NULL;
}

void pushFunParm(ObjectType variableType, char* variableName, int variableFlag) {
    struct list_variable *temp = malloc(sizeof(struct list_variable));
    if (!temp) { perror("Failed to allocate memory for temp"); exit(1); }

    INIT_LIST_HEAD(&temp->list);

    SymbolData *tempSyb = malloc(sizeof(SymbolData));
    if (!tempSyb) { perror("Failed to allocate memory for tempSyb"); exit(1); }
    tempSyb->addr = variableAddress++;
    tempSyb->func_sig = "-";
    tempSyb->index = 0;
    tempSyb->lineno = yylineno;
    tempSyb->name = variableName;
    temp->variableInfo.symbol = tempSyb;
    temp->variableInfo.type = variableType;

    struct list_scope *newScope = malloc(sizeof(struct list_scope));
    if (!newScope) { perror("Failed to allocate memory for newScope"); exit(1); }
    struct list_head *new_qhead = malloc(sizeof(struct list_head));
    if (!new_qhead) { perror("Failed to allocate memory for new_qhead"); exit(1); }
    INIT_LIST_HEAD(new_qhead);
    newScope->queue = new_qhead;
    INIT_LIST_HEAD(&newScope->chain);
    list_add_tail(&newScope->chain, &current->chain);

    current = newScope;
    list_add_tail(&temp->list, current->queue);
    printf("> Insert `%s` (addr: %lld) to scope level %d\n", variableName, tempSyb->addr, scopeLevel);

}

void createFunction(ObjectType variableType, char* funcName) {
    struct list_variable *temp = malloc(sizeof(struct list_variable));
    if (!temp) { perror("Failed to allocate memory for temp"); exit(1); }

    INIT_LIST_HEAD(&temp->list);

    SymbolData *tempSyb = malloc(sizeof(SymbolData));
    if (!tempSyb) { perror("Failed to allocate memory for tempSyb"); exit(1); }
    tempSyb->addr = variableAddress++;
    tempSyb->func_sig = "([Ljava/lang/String;)I";
    tempSyb->index = 0;
    tempSyb->lineno = yylineno;
    tempSyb->name = funcName;
    temp->variableInfo.symbol = tempSyb;
    temp->variableInfo.type = OBJECT_TYPE_FUNCTION;

    struct list_scope *newScope = malloc(sizeof(struct list_scope));
    if (!newScope) { perror("Failed to allocate memory for newScope"); exit(1); }
    struct list_head *new_qhead = malloc(sizeof(struct list_head));
    if (!new_qhead) { perror("Failed to allocate memory for new_qhead"); exit(1); }
    INIT_LIST_HEAD(new_qhead);
    newScope->queue = new_qhead;
    INIT_LIST_HEAD(&newScope->chain);
    list_add_tail(&newScope->chain, &current->chain);

    current = newScope;
    list_add_tail(&temp->list, current->queue);
    printf("func: %s\n", funcName);
    printf("> Insert `%s` (addr: %lld) to scope level %d\n", funcName, tempSyb->addr, scopeLevel);
    printf("> Create symbol table (scope level %d)\n", ++scopeLevel);
}

void debugPrintInst(char instc, Object* a, Object* b, Object* out) {
}

bool objectExpression(char op, Object* dest, Object* val, Object* out) {

    return false;
}

bool objectExpBinary(char op, Object* a, Object* b, Object* out) {
    return false;
}

bool objectExpBoolean(char op, Object* a, Object* b, Object* out) {
    return false;
}

bool objectExpAssign(char op, Object* dest, Object* val, Object* out) {
    return false;
}

bool objectValueAssign(Object* dest, Object* val, Object* out) {
    return false;
}

bool objectNotBinaryExpression(Object* dest, Object* out) {
    return false;
}

bool objectNegExpression(Object* dest, Object* out) {
    return false;
}
bool objectNotExpression(Object* dest, Object* out) {
    return false;
}

bool objectIncAssign(Object* a, Object* out) {
    return false;
}

bool objectDecAssign(Object* a, Object* out) {
    return false;
}

bool objectCast(ObjectType variableType, Object* dest, Object* out) {
    return false;
}

Object* findVariable(char* variableName) {
    Object* variable = NULL;
    return variable;
}

char toPrint[100];
int bufIdx = 0;

char *getObjName(int in){
    switch (in)
    {
        case 0:
            return "undefine";
            break;
        case 1:
            return "auto";
            break;
        case 2:
            return "void";
            break;
        case 3:
            return "char";
            break;
        case 4:
            return "int";
            break;
        case 5:
            return "long";
            break;
        case 6:
            return "float";
            break;
        case 7:
            return "double";
            break;
        case 8:
            return "bool";
        case 9:
            return "string";
        case 10:
            return "function";
    }
    return "undefine";
}
void pushFunInParm(Object* variable) {
    if(bufIdx == 0) strcpy(toPrint,"cout");
    char *temp = getObjName(variable->type);
    char *space = " ";
    strcat(toPrint, space);
    strcat(toPrint, temp);
    bufIdx++;
}

void stdoutPrint() {
    printf("%s\n", toPrint);
    bufIdx = 0;
}

int main(int argc, char* argv[]) {
    if (argc == 2) {
        yyin = fopen(yyInputFileName = argv[1], "r");
    } else {
        yyin = stdin;
    }
    if (!yyin) {
        printf("file `%s` doesn't exists or cannot be opened\n", yyInputFileName);
        exit(1);
    }

    toPrint[0] = '\0';

    // Start parsing
    struct list_scope *currentTemp = malloc(sizeof(struct list_scope));
    if (!currentTemp) { perror("Failed to allocate memory for currentTemp"); exit(1); }
    INIT_LIST_HEAD(&chainHead);
    struct list_head *new_qhead = malloc(sizeof(struct list_head));
    if (!new_qhead) { perror("Failed to allocate memory for new_qhead"); exit(1); }
    INIT_LIST_HEAD(new_qhead);
    currentTemp->queue = new_qhead;
    INIT_LIST_HEAD(&currentTemp->chain);
    list_add_tail(&currentTemp->chain, &chainHead);
    current = currentTemp;

    yyparse();
    printf("Total lines: %d\n", yylineno);
    fclose(yyin);

    yylex_destroy();
    return 0;
}
Leave a Comment