root/util.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. crb_get_current_interpreter
  2. crb_set_current_interpreter
  3. crb_search_function
  4. crb_malloc
  5. crb_execute_malloc
  6. crb_search_local_variable
  7. crb_search_global_variable
  8. crb_add_local_variable
  9. crb_add_global_variable
  10. crb_get_operator_string

#include <stdio.h>
#include <string.h>
#include "MEM.h"
#include "DBG.h"
#include "crowbar.h"

static CRB_Interpreter *st_current_interpreter;

CRB_Interpreter *
crb_get_current_interpreter(void)
{
    return st_current_interpreter;
}

void
crb_set_current_interpreter(CRB_Interpreter *inter)
{
    st_current_interpreter = inter;
}

/* BUGBUG
CRB_NativeFunctionProc *
crb_search_native_function(CRB_Interpreter *inter, char *name)
{
    NativeFunction *pos;

    for (pos = inter->native_function; pos; pos = pos->next) {
        if (!strcmp(pos->name, name))
            break;
    }
    if (pos) {
        return pos->proc;
    } else {
        return NULL;
    }
}
*/

FunctionDefinition *
crb_search_function(char *name)
{
    FunctionDefinition *pos;
    CRB_Interpreter *inter;

    inter = crb_get_current_interpreter();
    for (pos = inter->function_list; pos; pos = pos->next) {
        if (!strcmp(pos->name, name))
            break;
    }
    return pos;
}

void *
crb_malloc(size_t size)
{
    void *p;
    CRB_Interpreter *inter;

    inter = crb_get_current_interpreter();
    p = MEM_storage_malloc(inter->interpreter_storage, size);

    return p;
}

void *
crb_execute_malloc(CRB_Interpreter *inter, size_t size)
{
    void *p;

    p = MEM_storage_malloc(inter->execute_storage, size);

    return p;
}

CRB_Value *
crb_search_local_variable(LocalEnvironment *env, char *identifier)
{
    Variable    *pos;

    if (env == NULL)
        return NULL;
    for (pos = env->variable; pos; pos = pos->next) {
        if (!strcmp(pos->name, identifier))
            break;
    }
    if (pos == NULL) {
        return NULL;
    } else {
        return &pos->value;
    }
}

CRB_Value *
crb_search_global_variable(CRB_Interpreter *inter, char *identifier)
{
    Variable    *pos;

    for (pos = inter->variable; pos; pos = pos->next) {
        if (!strcmp(pos->name, identifier))
            break;
    }
    if (pos == NULL) {
        return NULL;
    } else {
        return &pos->value;
    }
}

void
crb_add_local_variable(LocalEnvironment *env,
                       char *identifier, CRB_Value *value)
{
    Variable    *new_variable;

    new_variable = MEM_malloc(sizeof(Variable));
    new_variable->name = identifier;
    new_variable->value = *value;
    new_variable->next = env->variable;
    env->variable = new_variable;
}

void
crb_add_global_variable(CRB_Interpreter *inter, char *identifier,
                        CRB_Value *value)
{
    Variable    *new_variable;

    new_variable = crb_execute_malloc(inter, sizeof(Variable));
    new_variable->name = crb_execute_malloc(inter, strlen(identifier) + 1);
    strcpy(new_variable->name, identifier);
    new_variable->value = *value;
    new_variable->next = inter->variable;
    inter->variable = new_variable;
}

char *
crb_get_operator_string(ExpressionType type)
{
    char        *str;

    switch (type) {
    case INT_EXPRESSION:        /* FALLTHRU */
    case DOUBLE_EXPRESSION:     /* FALLTHRU */
    case STRING_EXPRESSION:     /* FALLTHRU */
    case IDENTIFIER_EXPRESSION: /* FALLTHRU */
        DBG_assert(0, ("bad expression type..%d\n", type));
        break;
    case ASSIGN_EXPRESSION:     /* FALLTHRU */
        str = "=";
        break;
    case ADD_EXPRESSION:        /* FALLTHRU */
        str = "+";
        break;
    case SUB_EXPRESSION:        /* FALLTHRU */
        str = "-";
        break;
    case MUL_EXPRESSION:        /* FALLTHRU */
        str = "*";
        break;
    case DIV_EXPRESSION:        /* FALLTHRU */
        str = "/";
        break;
    case MOD_EXPRESSION:        /* FALLTHRU */
        str = "%";
        break;
    case EQ_EXPRESSION: /* FALLTHRU */
        str = "==";
        break;
    case NE_EXPRESSION: /* FALLTHRU */
        str = "!=";
        break;
    case GT_EXPRESSION: /* FALLTHRU */
        str = "<";
        break;
    case GE_EXPRESSION: /* FALLTHRU */
        str = "<=";
        break;
    case LT_EXPRESSION: /* FALLTHRU */
        str = ">";
        break;
    case LE_EXPRESSION: /* FALLTHRU */
        str = ">=";
        break;
    case MINUS_EXPRESSION:      /* FALLTHRU */
        str = "-";
        break;
    case FUNCTION_CALL_EXPRESSION:      /* FALLTHRU */
    case NULL_EXPRESSION:       /* FALLTHRU */
    case EXPRESSION_TYPE_COUNT_PLUS_1:  /* FALLTHRU */
    default:
        DBG_assert(0, ("bad expression type..%d\n", type));
    }

    return str;
}

/* [<][>][^][v][top][bottom][index][help] */