Cleaned up and organized vm_compile.c/.h - will add expression parsing and dynamic memory management within the vm soon

master
kts 2014-04-28 19:59:46 -07:00
parent 6f1ffe3563
commit 22a8e518e1
4 changed files with 121 additions and 126 deletions

View File

@ -46,8 +46,8 @@ table_test.o: table_test.c
conf.o: conf.c
$(CC) $(CFLAGS) -c conf.c
conf: conf.o ../common/data.o ../common/fio.o ../common/llist.o
$(CC) conf.o ../common/data.o ../common/fio.o ../common/llist.o -o conf
conf: conf.o ../common/data.o ../common/fio.o ../common/llist.o ../common/c_extra.o
$(CC) conf.o ../common/data.o ../common/fio.o ../common/llist.o ../common/c_extra.o -o conf
vm_compile.o: vm_compile.c
$(CC) $(CFLAGS) -c vm_compile.c

View File

@ -1,5 +1,6 @@
#include <stdlib.h>
#include "../common/data.h"
#include "../common/c_extra.h"
int dumpTable(struct Table *table, int depth) {
int i = 0;

View File

@ -3,66 +3,68 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h> // isdigit
// let's roll with capitlized data types
/*** DEFINES ***/
char *vm_datatypes[] = {
#define MAX_TYPES 7
#define TYPE_CHAR 0
"Char",
#define TYPE_INT 1
"Int",
#define TYPE_FLOAT 2
"Float",
#define TYPE_STRING 3
"String",
#define TYPE_TABLE 4
"Table",
#define TYPE_TILE 5
"Tile",
#define TYPE_MAP 6
"Map",
#define TYPE_PLAYER 7
"Player"
};
#define TYPE_CHAR 0
#define TYPE_INT 1
#define TYPE_FLOAT 2
#define TYPE_STRING 3
#define TYPE_TABLE 4
#define TYPE_TILE 5
#define TYPE_MAP 6
#define TYPE_PLAYER 7
#define MAX_TYPES 7
char *vm_expressions[] = {
#define MAX_EXPS 3
"return",
#define EXP_RETURN 0
"if",
#define EXP_IF 1
"while",
#define EXP_WHILE 2
"for"
#define EXP_FOR 3
};
#define EXP_RETURN 0
#define EXP_IF 1
#define EXP_WHILE 2
#define EXP_FOR 3
#define MAX_EXPS 3
char *vm_assignments[] = {
#define MAX_ASS 4
#define ASS_EQ 0
"=",
#define ASS_ADD 1
"+=",
#define ASS_MIN 2
"-=",
#define ASS_MUL 3
"*=",
#define ASS_DIV 4
"/="
};
#define ASS_EQ 0
#define ASS_ADD 1
#define ASS_MIN 2
#define ASS_MUL 3
#define ASS_DIV 4
#define MAX_ASS 4
char *vm_operations[] = {
#define MAX_OPS 3
#define OP_ADD 0
"+",
#define OP_MIN 1
"-",
#define OP_MUL 2
"*",
#define OP_DIV 3
"/"
};
#define OP_ADD 0
#define OP_MIN 1
#define OP_MUL 2
#define OP_DIV 3
#define MAX_OPS 3
/*** MAIN ***/
int main(int argc, char *argv[]) {
vm_pc_global_scope.variables = newTable(32);
vm_pc_global_scope.functions = newTable(32);
@ -96,6 +98,7 @@ int main(int argc, char *argv[]) {
return 0;
}
/*** PARSING ***/
/*
================================
int vm_precompileFile(const char *file_name)
@ -232,7 +235,6 @@ int vm_precompileFile(const char *file_name) {
return total_errors; // success, baby
}
#define DISCOVER 0
int vm_precompileSection(FILE *file, int *lpos_, int *cpos_, struct vm_pc_Scope *global, struct vm_pc_Scope *group, struct vm_pc_Scope *local) {
int lpos = *lpos_;
int cpos = *cpos_;
@ -587,8 +589,8 @@ int vm_precompileFunction(FILE *file, int *lpos_, int *cpos_, struct vm_pc_Scope
return error;
}
/*** POPULATING ***/
/*
Returns:
0 on success
1 if the variable is already defined
@ -634,7 +636,18 @@ int vm_pc_addFunctionToScope(struct vm_pc_Scope *scope, struct vm_pc_Function *f
return 2;
}
int vm_pc_setVariable(struct vm_pc_Variable *var, const char *data) {
// TODO: check for var types - do not set strings to ints, etc.
if (var == NULL) {
return -1;
}
int data_size = strlen(data)+1;
var->data = realloc(var->data, data_size);
memcpy(var->data, data, data_size);
return 0;
}
/*** ALLOC ***/
struct vm_pc_Variable *vm_pc_newVariable(const char *type, const char *value) {
struct vm_pc_Variable *var = malloc(sizeof(struct vm_pc_Variable));
if (var == NULL) {
@ -660,17 +673,6 @@ struct vm_pc_Variable *vm_pc_newVariable(const char *type, const char *value) {
return var;
}
int vm_pc_setVariable(struct vm_pc_Variable *var, const char *data) {
// TODO: check for var types - do not set strings to ints, etc.
if (var == NULL) {
return -1;
}
int data_size = strlen(data)+1;
var->data = realloc(var->data, data_size);
memcpy(var->data, data, data_size);
return 0;
}
struct vm_pc_Function *vm_pc_newFunction(const char *return_type) {
struct vm_pc_Function *func = malloc(sizeof(struct vm_pc_Function));
if (func == NULL) {
@ -696,6 +698,69 @@ struct vm_pc_Function *vm_pc_newFunction(const char *return_type) {
return func;
}
/*** FREE ***/
int vm_pc_freeVariable(struct vm_pc_Variable *variable) {
if (variable == NULL) return 1;
if (variable->type != NULL) free(variable->type);
if (variable->data != NULL) free(variable->data);
free(variable);
return 0;
}
int vm_pc_freeVariables(struct Table *variables) {
if (variables == NULL) return 1;
int i = 0;
while (i < variables->size) {
struct TablePair *entry = variables->pair[i];
struct TablePair *next_entry = NULL;
while (entry != NULL) {
next_entry = entry->next;
struct vm_pc_Variable *var = entry->value;
vm_pc_freeVariable(var);
freeTablePair(entry);
variables->count--;
entry = next_entry;
}
variables->pair[i] = NULL;
i++;
}
return 0;
}
int vm_pc_freeFunction(struct vm_pc_Function *function) {
if (function == NULL) return 1;
if (function->variables) vm_pc_freeVariables(function->variables);
// TODO: free ops
free(function);
return 0;
}
int vm_pc_freeFunctions(struct Table *functions) {
int i =0;
while (i < functions->size) {
struct TablePair *entry = functions->pair[i];
struct TablePair *next_entry = NULL;
while (entry != NULL) {
next_entry = entry->next;
struct vm_pc_Function *func = entry->value;
vm_pc_freeFunction(func);
freeTablePair(entry);
functions->count--;
entry = next_entry;
}
i++;
}
return 0;
}
int vm_pc_freeScope(struct vm_pc_Scope *scope) {
if (scope == NULL) return 1;
vm_pc_freeVariables(scope->variables);
vm_pc_freeFunctions(scope->functions);
return 0;
}
/*** ETC ***/
int vm_isReserved(char *word) {
int checkpos;
for (checkpos = 0;checkpos <= MAX_TYPES;checkpos++) {
@ -746,64 +811,3 @@ int vm_pc_dumpScope(struct vm_pc_Scope *scope) {
}
return 0;
}
int vm_pc_freeScope(struct vm_pc_Scope *scope) {
if (scope == NULL) return 1;
vm_pc_freeVariables(scope->variables);
vm_pc_freeFunctions(scope->functions);
return 0;
}
int vm_pc_freeVariables(struct Table *variables) {
if (variables == NULL) return 1;
int i = 0;
while (i < variables->size) {
struct TablePair *entry = variables->pair[i];
struct TablePair *next_entry = NULL;
while (entry != NULL) {
next_entry = entry->next;
struct vm_pc_Variable *var = entry->value;
vm_pc_freeVariable(var);
freeTablePair(entry);
variables->count--;
entry = next_entry;
}
variables->pair[i] = NULL;
i++;
}
return 0;
}
int vm_pc_freeVariable(struct vm_pc_Variable *variable) {
if (variable == NULL) return 1;
if (variable->type != NULL) free(variable->type);
if (variable->data != NULL) free(variable->data);
free(variable);
return 0;
}
int vm_pc_freeFunctions(struct Table *functions) {
int i =0;
while (i < functions->size) {
struct TablePair *entry = functions->pair[i];
struct TablePair *next_entry = NULL;
while (entry != NULL) {
next_entry = entry->next;
struct vm_pc_Function *func = entry->value;
vm_pc_freeFunction(func);
freeTablePair(entry);
functions->count--;
entry = next_entry;
}
i++;
}
return 0;
}
int vm_pc_freeFunction(struct vm_pc_Function *function) {
if (function == NULL) return 1;
if (function->variables) vm_pc_freeVariables(function->variables);
// TODO: free ops
free(function);
return 0;
}

View File

@ -5,24 +5,21 @@
#include "../common/data.h"
#include <stdio.h> // FILE
/*** GLOBAL DATA ***/
extern char* vm_datatypes[];
extern char* vm_expressions[];
extern char* vm_assignments[];
extern char* vm_operations[];
struct Table *vm_pc_globalFunctions;
struct Table *vm_pc_groupFunctions;
struct Table *vm_pc_localFunctions;
struct Table *vm_pc_globalVariables;
struct Table *vm_pc_groupVariables;
struct Table *vm_pc_localVariables;
struct vm_pc_Scope vm_pc_global_scope;
struct Table *vm_pc_group_scopes;
struct Table *vm_pc_local_scopes;
/*** STRUCTURES ***/
struct vm_pc_Variable {
char *type; // data type as string, malloc'd and memcpy'd
char *data; // data as string, malloc'd and memcpy'd
};
struct vm_pc_Operation {
int operation;
char *var_1;
@ -30,42 +27,35 @@ struct vm_pc_Operation {
char *var_3;
char *var_4;
};
struct vm_pc_Function {
struct vm_pc_Variable *ret; // return variable
struct Table *parameters; // input params
struct Table *variables; // function local variables
struct Table *operations; // operations! :)
};
struct vm_pc_Scope {
struct Table *variables;
struct Table *functions;
};
struct vm_pc_Scope vm_pc_global_scope;
struct Table *vm_pc_group_scopes;
struct Table *vm_pc_local_scopes;
/*** PARSING ***/
int vm_precompileFile(const char *file_name);
int vm_precompileSection(FILE *file, int *lpos_, int *cpos_, struct vm_pc_Scope *global, struct vm_pc_Scope *group, struct vm_pc_Scope *local);
int vm_precompileFunction(FILE *file, int *lpos_, int *cpos_, struct vm_pc_Scope *global, struct vm_pc_Scope *group, struct vm_pc_Scope *local, struct vm_pc_Function *func);
/*** POPULATING ***/
int vm_pc_addVariableToScope(struct vm_pc_Scope *scope, const char *data_type, const char *data_name);
int vm_pc_addFunctionToScope(struct vm_pc_Scope *scope, struct vm_pc_Function *func, const char *func_name);
int vm_isReserved(char *word);
int vm_pc_freeScope(struct vm_pc_Scope *scope);
int vm_pc_setVariable(struct vm_pc_Variable *variable, const char *value);
/*** ALLOC ***/
struct vm_pc_Variable *vm_pc_newVariable(const char *type, const char *value);
struct vm_pc_Function *vm_pc_newFunction();
/*** FREE ***/
int vm_pc_freeVariable(struct vm_pc_Variable *variable);
int vm_pc_freeVariables(struct Table *variables);
int vm_pc_setVariable(struct vm_pc_Variable *variable, const char *value);
struct vm_pc_Function *vm_pc_newFunction();
int vm_pc_freeFunction(struct vm_pc_Function *function);
int vm_pc_freeFunctions(struct Table *functions);
int vm_pc_freeScope(struct vm_pc_Scope *scope);
/*** ETC ***/
int vm_pc_dumpScope(struct vm_pc_Scope *scope);
int vm_isReserved(char *word);
#endif