added floating point arithmetic

master
xerox 4 years ago
parent fb95f8058c
commit 943c1c4d80

@ -6,7 +6,6 @@
#include "y.tab.h" #include "y.tab.h"
void yyerror(char *); void yyerror(char *);
extern YYSTYPE yylval; extern YYSTYPE yylval;
extern FILE *yyin; extern FILE *yyin;
int yylineno; int yylineno;
@ -28,6 +27,8 @@
"char" { return KEYWORD_C; } "char" { return KEYWORD_C; }
"++" { return INCREASE; } "++" { return INCREASE; }
"--" { return DECREASE; } "--" { return DECREASE; }
"<=" { return LESS_THAN_OR_EQUAL; }
">=" { return GREATER_THAN_OR_EQUAL; }
\"(\\.|[^"\\])*\" { yytext++; yytext[strlen(yytext)-1] = 0; yylval.str_ptr = strdup(yytext); return STRING; } \"(\\.|[^"\\])*\" { yytext++; yytext[strlen(yytext)-1] = 0; yylval.str_ptr = strdup(yytext); return STRING; }
[a-zA-Z0-9_]+ { yylval.str_ptr = strdup(yytext); return VARNAME; } [a-zA-Z0-9_]+ { yylval.str_ptr = strdup(yytext); return VARNAME; }
-?[0-9]*\.[0-9]+ { yylval.double_num = atof(yytext); return DOUBLEVAR; } -?[0-9]*\.[0-9]+ { yylval.double_num = atof(yytext); return DOUBLEVAR; }
@ -64,7 +65,6 @@ int main(int argc, char* argv[])
|| strcmp(argv[1],"d") == 0 || strcmp(argv[1],"debug") == 0) || strcmp(argv[1],"d") == 0 || strcmp(argv[1],"debug") == 0)
{ {
_DEON_DEBUG = 1; _DEON_DEBUG = 1;
printf(">>> ");
yyparse(); yyparse();
} }
else if (fh = fopen(argv[1], "r")) else if (fh = fopen(argv[1], "r"))

@ -2,26 +2,32 @@
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerCommandArguments>-d</LocalDebuggerCommandArguments> <LocalDebuggerCommandArguments>-d</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> <DebuggerFlavor>WindowsRemoteDebugger</DebuggerFlavor>
<RemoteDebuggerCommand>-d</RemoteDebuggerCommand>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LocalDebuggerCommandArguments>-d</LocalDebuggerCommandArguments> <LocalDebuggerCommandArguments>-d</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> <DebuggerFlavor>WindowsRemoteDebugger</DebuggerFlavor>
<RemoteDebuggerCommand>-d</RemoteDebuggerCommand>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerCommandArguments>-d</LocalDebuggerCommandArguments> <LocalDebuggerCommandArguments>-d</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<RemoteDebuggerCommand>-d</RemoteDebuggerCommand>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">
<LocalDebuggerCommandArguments>-d</LocalDebuggerCommandArguments> <LocalDebuggerCommandArguments>-d</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> <DebuggerFlavor>WindowsRemoteDebugger</DebuggerFlavor>
<RemoteDebuggerCommand>-d</RemoteDebuggerCommand>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LocalDebuggerCommandArguments>-d</LocalDebuggerCommandArguments> <LocalDebuggerCommandArguments>-d</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<RemoteDebuggerCommand>-d</RemoteDebuggerCommand>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">
<LocalDebuggerCommandArguments>-d</LocalDebuggerCommandArguments> <LocalDebuggerCommandArguments>-d</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> <DebuggerFlavor>WindowsRemoteDebugger</DebuggerFlavor>
<RemoteDebuggerCommand>-d</RemoteDebuggerCommand>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

@ -13,7 +13,6 @@
int yylex(void); int yylex(void);
void yyerror(char *); void yyerror(char *);
extern int yylineno; extern int yylineno;
%} %}
/* doing these before multiplication and division because they have a lower presidence */ /* doing these before multiplication and division because they have a lower presidence */
@ -33,14 +32,21 @@ extern int yylineno;
struct node_info *node_ptr; struct node_info *node_ptr;
} }
%token INCREASE PRINT_TOKEN IF END KEYWORD_S KEYWORD_I KEYWORD_C QUOTE ELSE WHILE DEF CALL
%token LESS_THAN_OR_EQUAL
%token GREATER_THAN_OR_EQUAL
%token DECREASE
%token INCREASE DECREASE PRINT_TOKEN IF END KEYWORD_S KEYWORD_I KEYWORD_C QUOTE ELSE WHILE DEF CALL
%token <int_num> INTEGER %token <int_num> INTEGER
%type <int_num> EXPR_I
%token <str_ptr> STRING VARNAME %token <str_ptr> STRING VARNAME
%token <byte_num> CHAR %token <byte_num> CHAR
%token <double_num> DOUBLEVAR %token <double_num> DOUBLEVAR
%type <double_num> EXPR_D
%token <float_num> FLOAT_NUM %token <float_num> FLOAT_NUM
%type <int_num> EXPR
%type <node_ptr> STMT PRINT VARIABLE FUNC STMT_LIST LOGIC %type <node_ptr> STMT PRINT VARIABLE FUNC STMT_LIST LOGIC
%% %%
@ -51,7 +57,7 @@ PROGRAM:
; ;
MAIN: MAIN:
MAIN FUNC ';' { if (_DEON_DEBUG == true ) { printf(">>> "); exec_stmt($2); } else { exec_stmt($2); } } MAIN FUNC ';' { exec_stmt($2); }
| |
; ;
@ -77,43 +83,62 @@ LOGIC:
IF '[' VARNAME ']' ':' '{' STMT_LIST '}' { $$ = create_logic_node(create_logic(IF_LOGIC, $7, $3)); } IF '[' VARNAME ']' ':' '{' STMT_LIST '}' { $$ = create_logic_node(create_logic(IF_LOGIC, $7, $3)); }
| WHILE '[' VARNAME ']' ':' '{' STMT_LIST '}' { $$ = create_logic_node(create_logic(WHILE_LOGIC, $7, $3)); } | WHILE '[' VARNAME ']' ':' '{' STMT_LIST '}' { $$ = create_logic_node(create_logic(WHILE_LOGIC, $7, $3)); }
; ;
VARIABLE: VARIABLE:
VARNAME '=' STRING { $$ = create_variable_const($1, (void *) $3, VAR_STRING);} VARNAME '=' STRING { $$ = create_variable($1, (void *) $3, VAR_STRING);}
| VARNAME '=' EXPR { $$ = create_variable_const($1, alloc_value(&$3, VAR_INT), VAR_INT);} | VARNAME '=' EXPR_I { $$ = create_variable($1, alloc_value(&$3, VAR_INT), VAR_INT);}
| VARNAME '=' CHAR { $$ = create_variable_const($1, alloc_value(&$3, VAR_BYTE), VAR_BYTE);} | VARNAME '=' EXPR_D { $$ = create_variable($1, alloc_value(&$3, VAR_DOUBLE), VAR_DOUBLE);}
| VARNAME '=' DOUBLEVAR { $$ = create_variable_const($1, alloc_value(&$3, VAR_DOUBLE), VAR_DOUBLE);} | VARNAME '=' CHAR { $$ = create_variable($1, alloc_value(&$3, VAR_BYTE), VAR_BYTE);}
| VARNAME '=' DOUBLEVAR { $$ = create_variable($1, alloc_value(&$3, VAR_DOUBLE), VAR_DOUBLE);}
| VARNAME '=' '{' STMT_LIST '}' { $$ = create_function($1, $4); } | VARNAME '=' '{' STMT_LIST '}' { $$ = create_function($1, $4); }
| VARNAME '=' VARNAME { $$ = move_value($1, $3); } | VARNAME '=' VARNAME { $$ = move_value($1, $3); }
; ;
EXPR: EXPR_I:
INTEGER { $$ = $1; } INTEGER { $$ = $1; }
| EXPR '+' EXPR { $$ = $1 + $3; } | EXPR_I '+' EXPR_I { $$ = $1 + $3; }
| EXPR '-' EXPR { $$ = $1 - $3; } | EXPR_I '-' EXPR_I { $$ = $1 - $3; }
| EXPR '*' EXPR { $$ = $1 * $3; } | EXPR_I '*' EXPR_I { $$ = $1 * $3; }
| EXPR '/' EXPR { $$ = $1 / $3; } | EXPR_I '/' EXPR_I { $$ = $1 / $3; }
| EXPR '%' EXPR { $$ = $1 % $3; } | EXPR_I '%' EXPR_I { $$ = $1 % $3; }
| EXPR '>' EXPR { if ($1 > $3) { $$ = 1; } else { $$ = 0;}} | EXPR_I LESS_THAN_OR_EQUAL EXPR_I { $$ = $1 <= $3; }
| EXPR '<' EXPR { if ($1 < $3) { $$ = 1; } else { $$ = 0;}} | EXPR_I GREATER_THAN_OR_EQUAL EXPR_I { $$ = $1 >= $3; }
| INCREASE EXPR { $$ = $2 + 1; } | EXPR_I '>' EXPR_I { $$ = $1 > $3; }
| EXPR INCREASE { $$ = $1 + 1; } | EXPR_I '<' EXPR_I { $$ = $1 < $3; }
| DECREASE EXPR { $$ = $2 - 1; } | INCREASE EXPR_I { $$ = $2 + 1; }
| EXPR DECREASE { $$ = $1 - 1; } | EXPR_I INCREASE { $$ = $1 + 1; }
| '(' EXPR ')' { $$ = $2; } | DECREASE EXPR_I { $$ = $2 - 1; }
| EXPR_I DECREASE { $$ = $1 - 1; }
| '(' EXPR_I ')' { $$ = $2; }
| EXPR_I { $$ = $1; }
;
EXPR_D:
DOUBLEVAR
| EXPR_D '+' EXPR_D { $$ = $1 + $3; }
| EXPR_D '-' EXPR_D { $$ = $1 - $3; }
| EXPR_D '*' EXPR_D { $$ = $1 * $3; }
| EXPR_D '/' EXPR_D { $$ = $1 / $3; }
| EXPR_D LESS_THAN_OR_EQUAL EXPR_D { $$ = $1 <= $3; }
| EXPR_D GREATER_THAN_OR_EQUAL EXPR_D { $$ = $1 >= $3; }
| EXPR_D '>' EXPR_D { $$ = $1 > $3; }
| EXPR_D '<' EXPR_D { $$ = $1 < $3; }
| INCREASE EXPR_D { $$ = $2 + 1; }
| EXPR_D INCREASE { $$ = $1 + 1; }
| DECREASE EXPR_D { $$ = $2 - 1; }
| EXPR_D DECREASE { $$ = $1 - 1; }
| '(' EXPR_D ')' { $$ = $2; }
| EXPR_D { $$ = $1; }
; ;
PRINT: PRINT:
VARNAME { $$ = create_print_variable($1); } VARNAME { $$ = create_print_variable($1); }
| EXPR { $$ = create_print_statement(VAR_INT, alloc_value(&$1, VAR_INT)); } | EXPR_I { $$ = create_print_statement(VAR_INT, alloc_value(&$1, VAR_INT)); }
| STRING { $$ = create_print_statement(VAR_STRING, $1); } | STRING { $$ = create_print_statement(VAR_STRING, $1); }
; ;
%% %%
void yyerror(char *s) void yyerror(char *s)
{ {
fprintf(stderr, "Error on line %d, %s\n", yylineno, s); fprintf(stderr, "Error on line %d, %s\n", yylineno, s);
} }

@ -8,18 +8,13 @@
create a function given the name of the function and the statements create a function given the name of the function and the statements
*/ */
static node_info *create_function(char *p_name, node_info *p_stmts) static node_info* create_function(char* p_name, node_info* p_stmts)
{ {
node_info *new_node = malloc(sizeof(node_info)); if (!p_name || !p_stmts)
if (!p_name || !p_stmts || !new_node)
return NULL; return NULL;
memset(new_node, NULL, sizeof(*new_node));
node_info *new_node = malloc(sizeof(node_info));
new_node->function_body = malloc(sizeof(node_info)); new_node->function_body = malloc(sizeof(node_info));
if (!new_node->function_body)
return NULL;
memset(new_node->function_body, NULL, sizeof(*new_node->function_body));
new_node->operation = FUNCTION_OPERATION; new_node->operation = FUNCTION_OPERATION;
new_node->operation_type.func = FUNCTION; new_node->operation_type.func = FUNCTION;
new_node->name = p_name; new_node->name = p_name;
@ -35,13 +30,12 @@ static node_info *create_function(char *p_name, node_info *p_stmts)
create a function call given the name create a function call given the name
TODO add scope. TODO add scope.
*/ */
static node_info *create_function_call(char *p_name) static node_info* create_function_call(char* p_name)
{ {
node_info *new_node = malloc(sizeof(node_info)); if (!p_name)
if (!new_node || !p_name)
return NULL; return NULL;
memset(new_node, NULL, sizeof(*new_node));
node_info *new_node = malloc(sizeof(node_info));
new_node->operation = FUNCTION_OPERATION; new_node->operation = FUNCTION_OPERATION;
new_node->operation_type.func = FUNCTION_CALL; new_node->operation_type.func = FUNCTION_CALL;
new_node->name = p_name; new_node->name = p_name;
@ -55,7 +49,7 @@ static node_info *create_function_call(char *p_name)
create compound statement given statement list and head of linked list. create compound statement given statement list and head of linked list.
*/ */
static node_info *create_compound_statement(node_info *new_statement_list, node_info *statement_head) static node_info* create_compound_statement(node_info* new_statement_list, node_info* statement_head)
{ {
if (!new_statement_list) if (!new_statement_list)
return NULL; return NULL;
@ -64,20 +58,12 @@ static node_info *create_compound_statement(node_info *new_statement_list, node_
{ {
statement_head = malloc(sizeof(node_info)); statement_head = malloc(sizeof(node_info));
node_info* new_node = malloc(sizeof(node_info)); node_info* new_node = malloc(sizeof(node_info));
if (!new_node || !statement_head)
return NULL;
memset(statement_head, NULL, sizeof(*statement_head));
memset(new_node, NULL, sizeof(*new_node));
statement_head->statement_list = malloc(sizeof(node_info)); statement_head->statement_list = malloc(sizeof(node_info));
if (!statement_head->statement_list)
return NULL;
memset(statement_head->statement_list, NULL,
sizeof(*statement_head->statement_list));
statement_head->operation = EXEC_STMT_LIST_OPERATION; statement_head->operation = EXEC_STMT_LIST_OPERATION;
statement_head->statement_list->operation = EXEC_STMT_LIST_OPERATION; statement_head->statement_list->operation = EXEC_STMT_LIST_OPERATION;
statement_head->statement_list->next = new_statement_list; statement_head->statement_list->next = new_statement_list;
statement_head->next = NULL;
statement_head->statement_list->statement_list = NULL; statement_head->statement_list->statement_list = NULL;
} }
else else
@ -88,13 +74,10 @@ static node_info *create_compound_statement(node_info *new_statement_list, node_
p_temp_head = p_temp_head->statement_list; p_temp_head = p_temp_head->statement_list;
p_temp_head->statement_list = malloc(sizeof(node_info)); p_temp_head->statement_list = malloc(sizeof(node_info));
if (!p_temp_head->statement_list)
return NULL;
memset(p_temp_head->statement_list, NULL, sizeof(*p_temp_head->statement_list));
p_temp_head->operation = EXEC_STMT_LIST_OPERATION; p_temp_head->operation = EXEC_STMT_LIST_OPERATION;
p_temp_head->statement_list->operation = EXEC_STMT_LIST_OPERATION; p_temp_head->statement_list->operation = EXEC_STMT_LIST_OPERATION;
p_temp_head->statement_list->next = new_statement_list; p_temp_head->statement_list->next = new_statement_list;
statement_head->next = NULL;
p_temp_head->statement_list->statement_list = NULL; p_temp_head->statement_list->statement_list = NULL;
} }
return statement_head; return statement_head;
@ -112,6 +95,7 @@ static node_info* add_to_compound_statement(node_info* new_statement, node_info*
return NULL; return NULL;
node_info* p_head_tmp = statement_head; node_info* p_head_tmp = statement_head;
while (p_head_tmp->statement_list) while (p_head_tmp->statement_list)
p_head_tmp = p_head_tmp->statement_list; p_head_tmp = p_head_tmp->statement_list;

@ -6,185 +6,186 @@
#include "map.h" #include "map.h"
struct map_node_t { struct map_node_t {
unsigned hash; unsigned hash;
void *value; void* value;
map_node_t *next; map_node_t* next;
/* char key[]; */ /* char key[]; */
/* char value[]; */ /* char value[]; */
}; };
static unsigned map_hash(const char *str) { static unsigned map_hash(const char* str) {
unsigned hash = 5381; unsigned hash = 5381;
while (*str) { while (*str) {
hash = ((hash << 5) + hash) ^ *str++; hash = ((hash << 5) + hash) ^ *str++;
} }
return hash; return hash;
} }
static map_node_t *map_newnode(const char *key, void *value, int vsize) { static map_node_t* map_newnode(const char* key, void* value, int vsize) {
map_node_t *node; map_node_t* node;
int ksize = strlen(key) + 1; int ksize = strlen(key) + 1;
int voffset = ksize + ((sizeof(void*) - ksize) % sizeof(void*)); int voffset = ksize + ((sizeof(void*) - ksize) % sizeof(void*));
node = malloc(sizeof(*node) + voffset + vsize); node = malloc(sizeof(*node) + voffset + vsize);
if (!node) return NULL; if (!node) return NULL;
memcpy(node + 1, key, ksize); memcpy(node + 1, key, ksize);
node->hash = map_hash(key); node->hash = map_hash(key);
node->value = ((char*) (node + 1)) + voffset; node->value = ((char*)(node + 1)) + voffset;
memcpy(node->value, value, vsize); memcpy(node->value, value, vsize);
return node; return node;
} }
static int map_bucketidx(map_base_t *m, unsigned hash) { static int map_bucketidx(map_base_t* m, unsigned hash) {
/* If the implementation is changed to allow a non-power-of-2 bucket count, /* If the implementation is changed to allow a non-power-of-2 bucket count,
* the line below should be changed to use mod instead of AND */ * the line below should be changed to use mod instead of AND */
return hash & (m->nbuckets - 1); return hash & (m->nbuckets - 1);
} }
static void map_addnode(map_base_t *m, map_node_t *node) { static void map_addnode(map_base_t* m, map_node_t* node) {
int n = map_bucketidx(m, node->hash); int n = map_bucketidx(m, node->hash);
node->next = m->buckets[n]; node->next = m->buckets[n];
m->buckets[n] = node; m->buckets[n] = node;
} }
static int map_resize(map_base_t *m, int nbuckets) { static int map_resize(map_base_t* m, int nbuckets) {
map_node_t *nodes, *node, *next; map_node_t* nodes, * node, * next;
map_node_t **buckets; map_node_t** buckets;
int i; int i;
/* Chain all nodes together */ /* Chain all nodes together */
nodes = NULL; nodes = NULL;
i = m->nbuckets; i = m->nbuckets;
while (i--) { while (i--) {
node = (m->buckets)[i]; node = (m->buckets)[i];
while (node) { while (node) {
next = node->next; next = node->next;
node->next = nodes; node->next = nodes;
nodes = node; nodes = node;
node = next; node = next;
}
}
/* Reset buckets */
buckets = realloc(m->buckets, sizeof(*m->buckets) * nbuckets);
if (buckets != NULL) {
m->buckets = buckets;
m->nbuckets = nbuckets;
} }
} if (m->buckets) {
/* Reset buckets */ memset(m->buckets, 0, sizeof(*m->buckets) * m->nbuckets);
buckets = realloc(m->buckets, sizeof(*m->buckets) * nbuckets); /* Re-add nodes to buckets */
if (buckets != NULL) { node = nodes;
m->buckets = buckets; while (node) {
m->nbuckets = nbuckets; next = node->next;
} map_addnode(m, node);
if (m->buckets) { node = next;
memset(m->buckets, 0, sizeof(*m->buckets) * m->nbuckets); }
/* Re-add nodes to buckets */
node = nodes;
while (node) {
next = node->next;
map_addnode(m, node);
node = next;
} }
} /* Return error code if realloc() failed */
/* Return error code if realloc() failed */ return (buckets == NULL) ? -1 : 0;
return (buckets == NULL) ? -1 : 0;
} }
static map_node_t **map_getref(map_base_t *m, const char *key) { static map_node_t** map_getref(map_base_t* m, const char* key) {
unsigned hash = map_hash(key); unsigned hash = map_hash(key);
map_node_t **next; map_node_t** next;
if (m->nbuckets > 0) { if (m->nbuckets > 0) {
next = &m->buckets[map_bucketidx(m, hash)]; next = &m->buckets[map_bucketidx(m, hash)];
while (*next) { while (*next) {
if ((*next)->hash == hash && !strcmp((char*) (*next + 1), key)) { if ((*next)->hash == hash && !strcmp((char*)(*next + 1), key)) {
return next; return next;
} }
next = &(*next)->next; next = &(*next)->next;
}
} }
} return NULL;
return NULL;
} }
void map_deinit_(map_base_t *m) { void map_deinit_(map_base_t* m) {
map_node_t *next, *node; map_node_t* next, * node;
int i; int i;
i = m->nbuckets; i = m->nbuckets;
while (i--) { while (i--) {
node = m->buckets[i]; node = m->buckets[i];
while (node) { while (node) {
next = node->next; next = node->next;
free(node); free(node);
node = next; node = next;
}
} }
} free(m->buckets);
free(m->buckets);
} }
void *map_get_(map_base_t *m, const char *key) { void* map_get_(map_base_t* m, const char* key) {
map_node_t **next = map_getref(m, key); map_node_t** next = map_getref(m, key);
return next ? (*next)->value : NULL; return next ? (*next)->value : NULL;
} }
int map_set_(map_base_t *m, const char *key, void *value, int vsize) { int map_set_(map_base_t* m, const char* key, void* value, int vsize) {
int n, err; int n, err;
map_node_t **next, *node; map_node_t** next, * node;
/* Find & replace existing node */ /* Find & replace existing node */
next = map_getref(m, key); next = map_getref(m, key);
if (next) { if (next) {
memcpy((*next)->value, value, vsize); memcpy((*next)->value, value, vsize);
return 0;
}
/* Add new node */
node = map_newnode(key, value, vsize);
if (node == NULL) goto fail;
if (m->nnodes >= m->nbuckets) {
n = (m->nbuckets > 0) ? (m->nbuckets << 1) : 1;
err = map_resize(m, n);
if (err) goto fail;
}
map_addnode(m, node);
m->nnodes++;
return 0; return 0;
} fail:
/* Add new node */ if (node) free(node);
node = map_newnode(key, value, vsize); return -1;
if (node == NULL) goto fail;
if (m->nnodes >= m->nbuckets) {
n = (m->nbuckets > 0) ? (m->nbuckets << 1) : 1;
err = map_resize(m, n);
if (err) goto fail;
}
map_addnode(m, node);
m->nnodes++;
return 0;
fail:
if (node) free(node);
return -1;
} }
void map_remove_(map_base_t *m, const char *key) { void map_remove_(map_base_t* m, const char* key) {
map_node_t *node; map_node_t* node;
map_node_t **next = map_getref(m, key); map_node_t** next = map_getref(m, key);
if (next) { if (next) {
node = *next; node = *next;
*next = (*next)->next; *next = (*next)->next;
free(node); free(node);
m->nnodes--; m->nnodes--;
} }
} }
map_iter_t map_iter_(void) { map_iter_t map_iter_(void) {
map_iter_t iter; map_iter_t iter;
iter.bucketidx = -1; iter.bucketidx = -1;
iter.node = NULL; iter.node = NULL;
return iter; return iter;
} }
const char *map_next_(map_base_t *m, map_iter_t *iter) { const char* map_next_(map_base_t* m, map_iter_t* iter) {
if (iter->node) { if (iter->node) {
iter->node = iter->node->next; iter->node = iter->node->next;
if (iter->node == NULL) goto nextBucket; if (iter->node == NULL) goto nextBucket;
} else { }
else {
nextBucket: nextBucket:
do { do {
if (++iter->bucketidx >= m->nbuckets) { if (++iter->bucketidx >= m->nbuckets) {
return NULL; return NULL;
} }
iter->node = m->buckets[iter->bucketidx]; iter->node = m->buckets[iter->bucketidx];
} while (iter->node == NULL); } while (iter->node == NULL);
} }
return (char*) (iter->node + 1); return (char*)(iter->node + 1);
} }
#endif #endif

@ -12,6 +12,7 @@ static void free_linked_list(node_info *p_list)
{ {
if (!p_list) if (!p_list)
return; return;
node_info* temp; node_info* temp;
while (p_list->next) while (p_list->next)
{ {
@ -23,19 +24,19 @@ static void free_linked_list(node_info *p_list)
/* /*
Author: xerox Author: xerox
Update: 1/20/2020 Update: 5/13/2020
updates linked list. shallow copy linked list.
*/ */
static node_info *copy_linked_list(node_info *p_list) static node_info* shallow_copy_linked_list(node_info* p_list)
{ {
node_info* new_list = malloc(sizeof(node_info)); if (!p_list)
if (!new_list || !p_list)
return NULL; return NULL;
node_info* new_list = malloc(sizeof(node_info));
new_list->next = NULL; new_list->next = NULL;
node_info *copy_list = new_list; node_info* copy_list = new_list;
//TODO update with doubly linked list
while(p_list->next) while(p_list->next)
{ {
p_list = p_list->next; p_list = p_list->next;
@ -54,10 +55,12 @@ static void linked_list_append(node_info* p_head, node_info* new_node)
{ {
if (!p_head || !new_node) if (!p_head || !new_node)
return; return;
node_info* p_head_temp = p_head; node_info* p_head_temp = p_head;
while (p_head->next) while (p_head->next)
p_head_temp = p_head_temp->next; p_head_temp = p_head_temp->next;
p_head->next = NULL; //sanity check
p_head->next = NULL;
p_head_temp->next = p_head; p_head_temp->next = p_head;
} }

@ -3,7 +3,7 @@
#include "../types.h" #include "../types.h"
void free_linked_list(node_info *p_list); void free_linked_list(node_info *p_list);
node_info *copy_linked_list(node_info *p_list); node_info *shallow_copy_linked_list(node_info *p_list);
void linked_list_append(node_info* p_head, node_info* new_node); void linked_list_append(node_info* p_head, node_info* new_node);
bool linked_list_remove(node_info* p_head, node_info* delete_node); bool linked_list_remove(node_info* p_head, node_info* delete_node);
#endif #endif

@ -8,13 +8,31 @@
*/ */
static logic* create_logic(logic_type type, node_info* stmt_list, char* var_name) static logic* create_logic(logic_type type, node_info* stmt_list, char* var_name)
{ {
logic* new_logic = malloc(sizeof(logic)); if (!stmt_list || !var_name)
if (!new_logic || !stmt_list || !var_name)
return NULL; return NULL;
memset(new_logic, NULL, sizeof(*new_logic)); logic* new_logic = malloc(sizeof(logic));
new_logic->type = type; new_logic->type = type;
new_logic->condition_var = var_name; new_logic->condition_var = var_name;
new_logic->stmt_list = stmt_list; new_logic->stmt_list = stmt_list;
return new_logic; return new_logic;
}
/*
Author: xerox
Updated: 1/19/2020
create logic node (logical expression)
*/
static node_info* create_logic_node(logic* logic_expr)
{
if (!logic_expr)
return NULL;
node_info* new_node = malloc(sizeof(node_info));
new_node->operation = LOGIC_OPERATION;
new_node->logical_expr = logic_expr;
new_node->statement_list = NULL;
new_node->next = NULL;
return new_node;
} }

@ -2,4 +2,5 @@
#define LOGIC_H #define LOGIC_H
#include "../types.h" #include "../types.h"
logic* create_logic(logic_type type, node_info* stmt_list, char* var_name); logic* create_logic(logic_type type, node_info* stmt_list, char* var_name);
node_info* create_logic_node(logic* logic_expr);
#endif #endif

@ -2,72 +2,6 @@
#define PARSER_C #define PARSER_C
#include "parser.h" #include "parser.h"
/*
Author: xerox
Updated: 1/19/2020
create variable given name, value, and type.
*/
static node_info* create_variable_const(char* p_name, void* value, variable_type var_type)
{
node_info* new_node = malloc(sizeof(*new_node));
if (!new_node)
return NULL;
memset(new_node, NULL, sizeof(*new_node));
variable* new_var = make_variable(p_name, value, var_type);
if (!new_var)
return NULL;
new_node->assignment = (assignment_operation*)malloc(sizeof(assignment_operation));
if (!new_node->assignment)
return NULL;
memset(new_node->assignment, NULL, sizeof(*new_node->assignment));
new_node->operation = ASSIGNMENT_OPERATION;
new_node->operation_type.assignment = ASSIGN_FROM_CONST;
new_node->assignment->var = new_var;
return new_node;
}
/*
Author: xerox
Updated: 1/20/2020
Move value from one variable to another.
*/
static node_info* move_value(char* to, char* from)
{
node_info* new_node = (node_info *)malloc(sizeof(node_info));
if (!from || !new_node || !to)
return NULL;
memset(new_node, NULL, sizeof(*new_node));
new_node->assignment = (assignment_operation*)malloc(sizeof(*new_node->assignment));
if (!new_node->assignment)
return NULL;
memset(new_node->assignment, NULL, sizeof(*new_node->assignment));
new_node->operation = ASSIGNMENT_OPERATION;
new_node->operation_type.assignment = ASSIGN_FROM_VAR;
new_node->assignment->from = from;
new_node->assignment->to = to;
return new_node;
}
/*
Author: xerox
Updated: 1/19/2020
create logic node (logical expression)
*/
static node_info* create_logic_node(logic* logic_expr)
{
node_info* new_node = malloc(sizeof(node_info));
if (!new_node || !logic_expr)
return NULL;
memset(new_node, NULL, sizeof(*new_node));
new_node->operation = LOGIC_OPERATION;
new_node->logical_expr = logic_expr;
return new_node;
}
/* /*
Author: xerox Author: xerox
Updated: 1/20/2020 Updated: 1/20/2020
@ -89,17 +23,20 @@ static void exec_stmt(node_info *exec_node)
switch (exec_node->operation_type.assignment) switch (exec_node->operation_type.assignment)
{ {
case ASSIGN_FROM_CONST: case ASSIGN_FROM_CONST:
add_var(exec_node->assignment->var->name, exec_node->assignment->var->value, _var_map); add_var(
exec_node->assignment->var->name,
exec_node->assignment->var->value,
_var_map
);
break; break;
case ASSIGN_FROM_VAR: case ASSIGN_FROM_VAR:
add_var(exec_node->assignment->to, add_var(exec_node->assignment->to,
get_value(exec_node->assignment->from, _var_map), _var_map); get_value(exec_node->assignment->from, _var_map),
_var_map
);
break; break;
default: default:
; break;
#if DEON_DEBUGGING
printf("[error] unkown assignment operation: 0x%x", exec_node->operation_type.assignment);
#endif
} }
break; break;
case LOGIC_OPERATION: case LOGIC_OPERATION:
@ -108,10 +45,12 @@ static void exec_stmt(node_info *exec_node)
{ {
case IF_LOGIC: case IF_LOGIC:
{ {
variable_values* p_var_values = get_value(exec_node->logical_expr->condition_var, _var_map); variable_values* p_var_values = get_value(
if (!p_var_values) exec_node->logical_expr->condition_var,
return; _var_map
if (*(unsigned char*)p_var_values->data_ptr) );
if (p_var_values && *(unsigned char*)p_var_values->data_ptr)
exec_stmt(exec_node->logical_expr->stmt_list); exec_stmt(exec_node->logical_expr->stmt_list);
break; break;
} }
@ -120,6 +59,7 @@ static void exec_stmt(node_info *exec_node)
variable_values* p_var_values = get_value(exec_node->logical_expr->condition_var, _var_map); variable_values* p_var_values = get_value(exec_node->logical_expr->condition_var, _var_map);
if (!p_var_values) if (!p_var_values)
return; return;
while (*(unsigned char*)p_var_values->data_ptr) while (*(unsigned char*)p_var_values->data_ptr)
{ {
exec_stmt(exec_node->logical_expr->stmt_list); exec_stmt(exec_node->logical_expr->stmt_list);
@ -128,10 +68,7 @@ static void exec_stmt(node_info *exec_node)
break; break;
} }
default: default:
; break;
#if DEON_DEBUGGING
printf("[error] unknown logic operation\n");
#endif
} }
} }
// execute statement list. // execute statement list.
@ -139,6 +76,7 @@ static void exec_stmt(node_info *exec_node)
{ {
if(exec_node->statement_list) if(exec_node->statement_list)
exec_node = exec_node->statement_list; exec_node = exec_node->statement_list;
while (exec_node->next) while (exec_node->next)
{ {
exec_node = exec_node->next; exec_node = exec_node->next;
@ -153,6 +91,7 @@ static void exec_stmt(node_info *exec_node)
{ {
case PRINT_CONSTANT: case PRINT_CONSTANT:
{ {
// TODO replace with puts to add more modularity
switch (exec_node->print_expr->type) switch (exec_node->print_expr->type)
{ {
case VAR_STRING: case VAR_STRING:
@ -170,10 +109,7 @@ static void exec_stmt(node_info *exec_node)
case VAR_VOID: //TODO add void variable type case VAR_VOID: //TODO add void variable type
break; break;
default: default:
; break;
#if DEON_DEBUGGING
printf("[error] unable to print variable of type: 0x%x\n", exec_node->print_expr->type);
#endif
} }
break; break;
} }
@ -197,18 +133,12 @@ static void exec_stmt(node_info *exec_node)
case VAR_VOID: //TODO add void variable type case VAR_VOID: //TODO add void variable type
break; break;
default: default:
; break;
#if DEON_DEBUGGING
printf("[error] unable to print variable of type: 0x%x\n", p_var->type);
#endif
} }
break; break;
} }
default: default:
; break;
#if DEON_DEBUGGING
printf("[error] unknown print type: 0x%x\n", exec_node->operation_type.print);
#endif
} }
break; break;
} }
@ -240,18 +170,12 @@ static void exec_stmt(node_info *exec_node)
break; break;
} }
default: default:
; break;
#if DEON_DEBUGGING
printf("[error] unknown function operation: 0x%x\n", exec_node->operation_type.func);
#endif
} }
break; break;
} }
default: default:
; break;
#if DEON_DEBUGGING
printf("[error] unknown operation: 0x%x\n", exec_node->operation);
#endif
} }
} }
#endif #endif

@ -2,8 +2,5 @@
#define PARSER_H #define PARSER_H
#include "../types.h" #include "../types.h"
#include "../vars/vars.h" #include "../vars/vars.h"
node_info* create_variable_const(char* p_name, void* value, variable_type var_type);
node_info* move_value(char* to, char* from);
node_info* create_logic_node(logic* logic_expr);
void exec_stmt(node_info *exec_node); void exec_stmt(node_info *exec_node);
#endif #endif

@ -9,20 +9,19 @@
creates a print statement given variable type and value. creates a print statement given variable type and value.
*/ */
static node_info *create_print_statement(variable_type operation, void* value) static node_info* create_print_statement(variable_type operation, void* value)
{ {
node_info* new_print_stmt = (node_info*)malloc(sizeof(node_info)); if (!value)
print* new_print = malloc(sizeof(print));
if (!new_print_stmt || !new_print)
return NULL; return NULL;
memset(new_print_stmt, NULL, sizeof(*new_print_stmt));
memset(new_print, NULL, sizeof(*new_print));
node_info* new_print_stmt = (node_info*)malloc(sizeof(node_info));
print* new_print = malloc(sizeof(print));
new_print_stmt->operation = PRINT_OPERATION; new_print_stmt->operation = PRINT_OPERATION;
new_print_stmt->operation_type.print = PRINT_CONSTANT; new_print_stmt->operation_type.print = PRINT_CONSTANT;
new_print->type = operation; new_print->type = operation;
new_print->value = value; new_print->value = value;
new_print_stmt->print_expr = new_print; new_print_stmt->print_expr = new_print;
new_print_stmt->statement_list = NULL;
new_print_stmt->next = NULL; new_print_stmt->next = NULL;
return new_print_stmt; return new_print_stmt;
} }
@ -35,19 +34,16 @@ static node_info *create_print_statement(variable_type operation, void* value)
*/ */
static node_info* create_print_variable(char* var_name) static node_info* create_print_variable(char* var_name)
{ {
node_info* new_print_stmt = (node_info*)malloc(sizeof(node_info)); if (!var_name)
if (!new_print_stmt || !var_name)
return NULL; return NULL;
memset(new_print_stmt, NULL, sizeof(*new_print_stmt));
node_info* new_print_stmt = (node_info*)malloc(sizeof(node_info));
new_print_stmt->var = malloc(sizeof(variable)); new_print_stmt->var = malloc(sizeof(variable));
if (!new_print_stmt->var)
return NULL;
memset(new_print_stmt->var, NULL, sizeof(*new_print_stmt->var));
new_print_stmt->operation = PRINT_OPERATION; new_print_stmt->operation = PRINT_OPERATION;
new_print_stmt->operation_type.print = PRINT_VARIABLE; new_print_stmt->operation_type.print = PRINT_VARIABLE;
new_print_stmt->var->name = var_name; new_print_stmt->var->name = var_name;
new_print_stmt->statement_list = NULL;
new_print_stmt->next = NULL; new_print_stmt->next = NULL;
return new_print_stmt; return new_print_stmt;
} }

@ -8,7 +8,7 @@
add var to hashmap of variables. add var to hashmap of variables.
*/ */
static void add_var(char *p_name, variable_values *p_values, map_void_t* p_var_map) static void add_var(char* p_name, variable_values* p_values, map_void_t* p_var_map)
{ {
if (!p_name || !p_values) if (!p_name || !p_values)
return; return;
@ -22,7 +22,7 @@ static void add_var(char *p_name, variable_values *p_values, map_void_t* p_var_m
get value given name and hashmap. get value given name and hashmap.
*/ */
static variable_values* get_value(char *p_name, map_void_t* p_var_map) static variable_values* get_value(char* p_name, map_void_t* p_var_map)
{ {
if (!p_name) if (!p_name)
return NULL; return NULL;
@ -35,15 +35,13 @@ static variable_values* get_value(char *p_name, map_void_t* p_var_map)
makes a variable struct and define it. makes a variable struct and define it.
*/ */
static variable* make_variable(char *p_name, void* value, variable_type var_type) static variable* make_variable(char* p_name, void* value, variable_type var_type)
{ {
variable *new_var = malloc(sizeof(variable)); if (!p_name || !value)
variable_values *new_value = malloc(sizeof(variable_values));
if (!new_var || !new_value || !p_name || !value)
return NULL; return NULL;
memset(new_var, NULL, sizeof(*new_var)); variable* new_var = malloc(sizeof(variable));
memset(new_value, NULL, sizeof(*new_value)); variable_values* new_value = malloc(sizeof(variable_values));
new_value->type = var_type; new_value->type = var_type;
new_value->data_ptr = value; new_value->data_ptr = value;
new_var->name = p_name; new_var->name = p_name;
@ -59,23 +57,20 @@ static variable* make_variable(char *p_name, void* value, variable_type var_type
*/ */
static void* alloc_value(void* value, variable_type var_type) static void* alloc_value(void* value, variable_type var_type)
{ {
if (!value)
return NULL;
switch (var_type) switch (var_type)
{ {
case VAR_INT: case VAR_INT:
{ {
int* p_int = (int*)malloc(sizeof(int)); int* p_int = malloc(sizeof(int));
if (!p_int)
return NULL;
memset(p_int, NULL, sizeof(*p_int));
*p_int = *(int*)value; *p_int = *(int*)value;
return p_int; return p_int;
} }
case VAR_SHORT: case VAR_SHORT:
{ {
short* p_short = (short*)malloc(sizeof(short)); short* p_short = malloc(sizeof(short));
if (!p_short)
return NULL;
memset(p_short, NULL, sizeof(*p_short));
*p_short = *(short*)value; *p_short = *(short*)value;
return p_short; return p_short;
} }
@ -91,9 +86,6 @@ static void* alloc_value(void* value, variable_type var_type)
case VAR_DOUBLE: case VAR_DOUBLE:
{ {
double* p_double = malloc(sizeof(double)); double* p_double = malloc(sizeof(double));
if (!p_double)
return NULL;
memset(p_double, NULL, sizeof(*p_double));
*p_double = *(double*)value; *p_double = *(double*)value;
return p_double; return p_double;
} }
@ -101,4 +93,48 @@ static void* alloc_value(void* value, variable_type var_type)
return NULL; return NULL;
} }
} }
/*
Author: xerox
Updated: 1/19/2020
create variable given name, value, and type.
*/
static node_info* create_variable(char* p_name, void* value, variable_type var_type)
{
if (!p_name || !value)
return NULL;
node_info* new_node = malloc(sizeof(node_info));
variable* new_var = make_variable(p_name, value, var_type);
new_node->assignment = malloc(sizeof(assignment_operation));
new_node->operation = ASSIGNMENT_OPERATION;
new_node->operation_type.assignment = ASSIGN_FROM_CONST;
new_node->assignment->var = new_var;
new_node->statement_list = NULL;
new_node->next = NULL;
return new_node;
}
/*
Author: xerox
Updated: 1/20/2020
Move value from one variable to another.
*/
static node_info* move_value(char* to, char* from)
{
if (!from || !to)
return NULL;
node_info* new_node = malloc(sizeof(node_info));
new_node->assignment = malloc(sizeof(*new_node->assignment));
new_node->operation = ASSIGNMENT_OPERATION;
new_node->operation_type.assignment = ASSIGN_FROM_VAR;
new_node->assignment->from = from;
new_node->assignment->to = to;
new_node->statement_list = NULL;
new_node->next = NULL;
return new_node;
}
#endif #endif

@ -5,4 +5,6 @@ void add_var(char* name, variable_values* values, map_void_t* p_var_map);
variable* make_variable(char* p_name, void* value, variable_type var_type); variable* make_variable(char* p_name, void* value, variable_type var_type);
variable_values* get_value(char* p_name, map_void_t* p_var_map); variable_values* get_value(char* p_name, map_void_t* p_var_map);
void* alloc_value(void* value, variable_type var_type); void* alloc_value(void* value, variable_type var_type);
node_info* create_variable(char* p_name, void* value, variable_type var_type);
node_info* move_value(char* to, char* from);
#endif #endif

Loading…
Cancel
Save