added floating point arithmetic

master
xerox 5 years ago
parent fb95f8058c
commit 943c1c4d80

@ -6,7 +6,6 @@
#include "y.tab.h"
void yyerror(char *);
extern YYSTYPE yylval;
extern FILE *yyin;
int yylineno;
@ -28,6 +27,8 @@
"char" { return KEYWORD_C; }
"++" { return INCREASE; }
"--" { 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; }
[a-zA-Z0-9_]+ { yylval.str_ptr = strdup(yytext); return VARNAME; }
-?[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)
{
_DEON_DEBUG = 1;
printf(">>> ");
yyparse();
}
else if (fh = fopen(argv[1], "r"))

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

@ -13,7 +13,6 @@
int yylex(void);
void yyerror(char *);
extern int yylineno;
%}
/* doing these before multiplication and division because they have a lower presidence */
@ -33,14 +32,21 @@ extern int yylineno;
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
%type <int_num> EXPR_I
%token <str_ptr> STRING VARNAME
%token <byte_num> CHAR
%token <double_num> DOUBLEVAR
%type <double_num> EXPR_D
%token <float_num> FLOAT_NUM
%type <int_num> EXPR
%type <node_ptr> STMT PRINT VARIABLE FUNC STMT_LIST LOGIC
%%
@ -51,7 +57,7 @@ PROGRAM:
;
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)); }
| WHILE '[' VARNAME ']' ':' '{' STMT_LIST '}' { $$ = create_logic_node(create_logic(WHILE_LOGIC, $7, $3)); }
;
VARIABLE:
VARNAME '=' STRING { $$ = create_variable_const($1, (void *) $3, VAR_STRING);}
| VARNAME '=' EXPR { $$ = create_variable_const($1, alloc_value(&$3, VAR_INT), VAR_INT);}
| VARNAME '=' CHAR { $$ = create_variable_const($1, alloc_value(&$3, VAR_BYTE), VAR_BYTE);}
| VARNAME '=' DOUBLEVAR { $$ = create_variable_const($1, alloc_value(&$3, VAR_DOUBLE), VAR_DOUBLE);}
VARNAME '=' STRING { $$ = create_variable($1, (void *) $3, VAR_STRING);}
| VARNAME '=' EXPR_I { $$ = create_variable($1, alloc_value(&$3, VAR_INT), VAR_INT);}
| VARNAME '=' EXPR_D { $$ = create_variable($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 '=' VARNAME { $$ = move_value($1, $3); }
;
EXPR:
EXPR_I:
INTEGER { $$ = $1; }
| EXPR '+' EXPR { $$ = $1 + $3; }
| EXPR '-' EXPR { $$ = $1 - $3; }
| EXPR '*' EXPR { $$ = $1 * $3; }
| EXPR '/' EXPR { $$ = $1 / $3; }
| EXPR '%' EXPR { $$ = $1 % $3; }
| EXPR '>' EXPR { if ($1 > $3) { $$ = 1; } else { $$ = 0;}}
| EXPR '<' EXPR { if ($1 < $3) { $$ = 1; } else { $$ = 0;}}
| INCREASE EXPR { $$ = $2 + 1; }
| EXPR INCREASE { $$ = $1 + 1; }
| DECREASE EXPR { $$ = $2 - 1; }
| EXPR DECREASE { $$ = $1 - 1; }
| '(' EXPR ')' { $$ = $2; }
| EXPR_I '+' EXPR_I { $$ = $1 + $3; }
| EXPR_I '-' EXPR_I { $$ = $1 - $3; }
| EXPR_I '*' EXPR_I { $$ = $1 * $3; }
| EXPR_I '/' EXPR_I { $$ = $1 / $3; }
| EXPR_I '%' EXPR_I { $$ = $1 % $3; }
| EXPR_I LESS_THAN_OR_EQUAL EXPR_I { $$ = $1 <= $3; }
| EXPR_I GREATER_THAN_OR_EQUAL EXPR_I { $$ = $1 >= $3; }
| EXPR_I '>' EXPR_I { $$ = $1 > $3; }
| EXPR_I '<' EXPR_I { $$ = $1 < $3; }
| INCREASE EXPR_I { $$ = $2 + 1; }
| EXPR_I INCREASE { $$ = $1 + 1; }
| 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:
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); }
;
%%
void yyerror(char *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
*/
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 || !new_node)
if (!p_name || !p_stmts)
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));
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_type.func = FUNCTION;
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
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 (!new_node || !p_name)
if (!p_name)
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_type.func = FUNCTION_CALL;
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.
*/
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)
return NULL;
@ -64,20 +58,12 @@ static node_info *create_compound_statement(node_info *new_statement_list, node_
{
statement_head = 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));
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->statement_list->operation = EXEC_STMT_LIST_OPERATION;
statement_head->statement_list->next = new_statement_list;
statement_head->next = NULL;
statement_head->statement_list->statement_list = NULL;
}
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->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->statement_list->operation = EXEC_STMT_LIST_OPERATION;
p_temp_head->statement_list->next = new_statement_list;
statement_head->next = NULL;
p_temp_head->statement_list->statement_list = NULL;
}
return statement_head;
@ -112,6 +95,7 @@ static node_info* add_to_compound_statement(node_info* new_statement, node_info*
return NULL;
node_info* p_head_tmp = statement_head;
while (p_head_tmp->statement_list)
p_head_tmp = p_head_tmp->statement_list;

@ -6,185 +6,186 @@
#include "map.h"
struct map_node_t {
unsigned hash;
void *value;
map_node_t *next;
/* char key[]; */
/* char value[]; */
unsigned hash;
void* value;
map_node_t* next;
/* char key[]; */
/* char value[]; */
};
static unsigned map_hash(const char *str) {
unsigned hash = 5381;
while (*str) {
hash = ((hash << 5) + hash) ^ *str++;
}
return hash;
static unsigned map_hash(const char* str) {
unsigned hash = 5381;
while (*str) {
hash = ((hash << 5) + hash) ^ *str++;
}
return hash;
}
static map_node_t *map_newnode(const char *key, void *value, int vsize) {
map_node_t *node;
int ksize = strlen(key) + 1;
int voffset = ksize + ((sizeof(void*) - ksize) % sizeof(void*));
node = malloc(sizeof(*node) + voffset + vsize);
if (!node) return NULL;
memcpy(node + 1, key, ksize);
node->hash = map_hash(key);
node->value = ((char*) (node + 1)) + voffset;
memcpy(node->value, value, vsize);
return node;
static map_node_t* map_newnode(const char* key, void* value, int vsize) {
map_node_t* node;
int ksize = strlen(key) + 1;
int voffset = ksize + ((sizeof(void*) - ksize) % sizeof(void*));
node = malloc(sizeof(*node) + voffset + vsize);
if (!node) return NULL;
memcpy(node + 1, key, ksize);
node->hash = map_hash(key);
node->value = ((char*)(node + 1)) + voffset;
memcpy(node->value, value, vsize);
return node;
}
static int map_bucketidx(map_base_t *m, unsigned hash) {
/* 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 */
return hash & (m->nbuckets - 1);
static int map_bucketidx(map_base_t* m, unsigned hash) {
/* 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 */
return hash & (m->nbuckets - 1);
}
static void map_addnode(map_base_t *m, map_node_t *node) {
int n = map_bucketidx(m, node->hash);
node->next = m->buckets[n];
m->buckets[n] = node;
static void map_addnode(map_base_t* m, map_node_t* node) {
int n = map_bucketidx(m, node->hash);
node->next = m->buckets[n];
m->buckets[n] = node;
}
static int map_resize(map_base_t *m, int nbuckets) {
map_node_t *nodes, *node, *next;
map_node_t **buckets;
int i;
/* Chain all nodes together */
nodes = NULL;
i = m->nbuckets;
while (i--) {
node = (m->buckets)[i];
while (node) {
next = node->next;
node->next = nodes;
nodes = node;
node = next;
static int map_resize(map_base_t* m, int nbuckets) {
map_node_t* nodes, * node, * next;
map_node_t** buckets;
int i;
/* Chain all nodes together */
nodes = NULL;
i = m->nbuckets;
while (i--) {
node = (m->buckets)[i];
while (node) {
next = node->next;
node->next = nodes;
nodes = node;
node = next;
}
}
/* Reset buckets */
buckets = realloc(m->buckets, sizeof(*m->buckets) * nbuckets);
if (buckets != NULL) {
m->buckets = buckets;
m->nbuckets = nbuckets;
}
}
/* Reset buckets */
buckets = realloc(m->buckets, sizeof(*m->buckets) * nbuckets);
if (buckets != NULL) {
m->buckets = buckets;
m->nbuckets = nbuckets;
}
if (m->buckets) {
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;
if (m->buckets) {
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 (buckets == NULL) ? -1 : 0;
/* Return error code if realloc() failed */
return (buckets == NULL) ? -1 : 0;
}
static map_node_t **map_getref(map_base_t *m, const char *key) {
unsigned hash = map_hash(key);
map_node_t **next;
if (m->nbuckets > 0) {
next = &m->buckets[map_bucketidx(m, hash)];
while (*next) {
if ((*next)->hash == hash && !strcmp((char*) (*next + 1), key)) {
return next;
}
next = &(*next)->next;
static map_node_t** map_getref(map_base_t* m, const char* key) {
unsigned hash = map_hash(key);
map_node_t** next;
if (m->nbuckets > 0) {
next = &m->buckets[map_bucketidx(m, hash)];
while (*next) {
if ((*next)->hash == hash && !strcmp((char*)(*next + 1), key)) {
return next;
}
next = &(*next)->next;
}
}
}
return NULL;
return NULL;
}
void map_deinit_(map_base_t *m) {
map_node_t *next, *node;
int i;
i = m->nbuckets;
while (i--) {
node = m->buckets[i];
while (node) {
next = node->next;
free(node);
node = next;
void map_deinit_(map_base_t* m) {
map_node_t* next, * node;
int i;
i = m->nbuckets;
while (i--) {
node = m->buckets[i];
while (node) {
next = node->next;
free(node);
node = next;
}
}
}
free(m->buckets);
free(m->buckets);
}
void *map_get_(map_base_t *m, const char *key) {
map_node_t **next = map_getref(m, key);
return next ? (*next)->value : NULL;
void* map_get_(map_base_t* m, const char* key) {
map_node_t** next = map_getref(m, key);
return next ? (*next)->value : NULL;
}
int map_set_(map_base_t *m, const char *key, void *value, int vsize) {
int n, err;
map_node_t **next, *node;
/* Find & replace existing node */
next = map_getref(m, key);
if (next) {
memcpy((*next)->value, value, vsize);
int map_set_(map_base_t* m, const char* key, void* value, int vsize) {
int n, err;
map_node_t** next, * node;
/* Find & replace existing node */
next = map_getref(m, key);
if (next) {
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;
}
/* 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;
fail:
if (node) free(node);
return -1;
fail:
if (node) free(node);
return -1;
}
void map_remove_(map_base_t *m, const char *key) {
map_node_t *node;
map_node_t **next = map_getref(m, key);
if (next) {
node = *next;
*next = (*next)->next;
free(node);
m->nnodes--;
}
void map_remove_(map_base_t* m, const char* key) {
map_node_t* node;
map_node_t** next = map_getref(m, key);
if (next) {
node = *next;
*next = (*next)->next;
free(node);
m->nnodes--;
}
}
map_iter_t map_iter_(void) {
map_iter_t iter;
iter.bucketidx = -1;
iter.node = NULL;
return iter;
map_iter_t iter;
iter.bucketidx = -1;
iter.node = NULL;
return iter;
}
const char *map_next_(map_base_t *m, map_iter_t *iter) {
if (iter->node) {
iter->node = iter->node->next;
if (iter->node == NULL) goto nextBucket;
} else {
const char* map_next_(map_base_t* m, map_iter_t* iter) {
if (iter->node) {
iter->node = iter->node->next;
if (iter->node == NULL) goto nextBucket;
}
else {
nextBucket:
do {
if (++iter->bucketidx >= m->nbuckets) {
return NULL;
}
iter->node = m->buckets[iter->bucketidx];
} while (iter->node == NULL);
}
return (char*) (iter->node + 1);
do {
if (++iter->bucketidx >= m->nbuckets) {
return NULL;
}
iter->node = m->buckets[iter->bucketidx];
} while (iter->node == NULL);
}
return (char*)(iter->node + 1);
}
#endif

@ -12,6 +12,7 @@ static void free_linked_list(node_info *p_list)
{
if (!p_list)
return;
node_info* temp;
while (p_list->next)
{
@ -23,19 +24,19 @@ static void free_linked_list(node_info *p_list)
/*
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 (!new_list || !p_list)
if (!p_list)
return NULL;
node_info* new_list = malloc(sizeof(node_info));
new_list->next = NULL;
node_info *copy_list = new_list;
//TODO update with doubly linked list
node_info* copy_list = new_list;
while(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)
return;
node_info* p_head_temp = p_head;
while (p_head->next)
p_head_temp = p_head_temp->next;
p_head->next = NULL; //sanity check
p_head->next = NULL;
p_head_temp->next = p_head;
}

@ -3,7 +3,7 @@
#include "../types.h"
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);
bool linked_list_remove(node_info* p_head, node_info* delete_node);
#endif

@ -8,13 +8,31 @@
*/
static logic* create_logic(logic_type type, node_info* stmt_list, char* var_name)
{
logic* new_logic = malloc(sizeof(logic));
if (!new_logic || !stmt_list || !var_name)
if (!stmt_list || !var_name)
return NULL;
memset(new_logic, NULL, sizeof(*new_logic));
logic* new_logic = malloc(sizeof(logic));
new_logic->type = type;
new_logic->condition_var = var_name;
new_logic->stmt_list = stmt_list;
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
#include "../types.h"
logic* create_logic(logic_type type, node_info* stmt_list, char* var_name);
node_info* create_logic_node(logic* logic_expr);
#endif

@ -2,72 +2,6 @@
#define PARSER_C
#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
Updated: 1/20/2020
@ -89,17 +23,20 @@ static void exec_stmt(node_info *exec_node)
switch (exec_node->operation_type.assignment)
{
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;
case ASSIGN_FROM_VAR:
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;
default:
;
#if DEON_DEBUGGING
printf("[error] unkown assignment operation: 0x%x", exec_node->operation_type.assignment);
#endif
break;
}
break;
case LOGIC_OPERATION:
@ -108,10 +45,12 @@ static void exec_stmt(node_info *exec_node)
{
case IF_LOGIC:
{
variable_values* p_var_values = get_value(exec_node->logical_expr->condition_var, _var_map);
if (!p_var_values)
return;
if (*(unsigned char*)p_var_values->data_ptr)
variable_values* p_var_values = get_value(
exec_node->logical_expr->condition_var,
_var_map
);
if (p_var_values && *(unsigned char*)p_var_values->data_ptr)
exec_stmt(exec_node->logical_expr->stmt_list);
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);
if (!p_var_values)
return;
while (*(unsigned char*)p_var_values->data_ptr)
{
exec_stmt(exec_node->logical_expr->stmt_list);
@ -128,10 +68,7 @@ static void exec_stmt(node_info *exec_node)
break;
}
default:
;
#if DEON_DEBUGGING
printf("[error] unknown logic operation\n");
#endif
break;
}
}
// execute statement list.
@ -139,6 +76,7 @@ static void exec_stmt(node_info *exec_node)
{
if(exec_node->statement_list)
exec_node = exec_node->statement_list;
while (exec_node->next)
{
exec_node = exec_node->next;
@ -153,6 +91,7 @@ static void exec_stmt(node_info *exec_node)
{
case PRINT_CONSTANT:
{
// TODO replace with puts to add more modularity
switch (exec_node->print_expr->type)
{
case VAR_STRING:
@ -170,10 +109,7 @@ static void exec_stmt(node_info *exec_node)
case VAR_VOID: //TODO add void variable type
break;
default:
;
#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
break;
default:
;
#if DEON_DEBUGGING
printf("[error] unable to print variable of type: 0x%x\n", p_var->type);
#endif
break;
}
break;
}
default:
;
#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;
}
default:
;
#if DEON_DEBUGGING
printf("[error] unknown function operation: 0x%x\n", exec_node->operation_type.func);
#endif
break;
}
break;
}
default:
;
#if DEON_DEBUGGING
printf("[error] unknown operation: 0x%x\n", exec_node->operation);
#endif
break;
}
}
#endif

@ -2,8 +2,5 @@
#define PARSER_H
#include "../types.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);
#endif

@ -9,20 +9,19 @@
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));
print* new_print = malloc(sizeof(print));
if (!new_print_stmt || !new_print)
if (!value)
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_type.print = PRINT_CONSTANT;
new_print->type = operation;
new_print->value = value;
new_print_stmt->print_expr = new_print;
new_print_stmt->statement_list = NULL;
new_print_stmt->next = NULL;
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)
{
node_info* new_print_stmt = (node_info*)malloc(sizeof(node_info));
if (!new_print_stmt || !var_name)
if (!var_name)
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));
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_type.print = PRINT_VARIABLE;
new_print_stmt->var->name = var_name;
new_print_stmt->statement_list = NULL;
new_print_stmt->next = NULL;
return new_print_stmt;
}

@ -8,7 +8,7 @@
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)
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.
*/
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)
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.
*/
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));
variable_values *new_value = malloc(sizeof(variable_values));
if (!new_var || !new_value || !p_name || !value)
if (!p_name || !value)
return NULL;
memset(new_var, NULL, sizeof(*new_var));
memset(new_value, NULL, sizeof(*new_value));
variable* new_var = malloc(sizeof(variable));
variable_values* new_value = malloc(sizeof(variable_values));
new_value->type = var_type;
new_value->data_ptr = value;
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)
{
if (!value)
return NULL;
switch (var_type)
{
case VAR_INT:
{
int* p_int = (int*)malloc(sizeof(int));
if (!p_int)
return NULL;
memset(p_int, NULL, sizeof(*p_int));
int* p_int = malloc(sizeof(int));
*p_int = *(int*)value;
return p_int;
}
case VAR_SHORT:
{
short* p_short = (short*)malloc(sizeof(short));
if (!p_short)
return NULL;
memset(p_short, NULL, sizeof(*p_short));
short* p_short = malloc(sizeof(short));
*p_short = *(short*)value;
return p_short;
}
@ -91,9 +86,6 @@ static void* alloc_value(void* value, variable_type var_type)
case VAR_DOUBLE:
{
double* p_double = malloc(sizeof(double));
if (!p_double)
return NULL;
memset(p_double, NULL, sizeof(*p_double));
*p_double = *(double*)value;
return p_double;
}
@ -101,4 +93,48 @@ static void* alloc_value(void* value, variable_type var_type)
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

@ -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_values* get_value(char* p_name, map_void_t* p_var_map);
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

Loading…
Cancel
Save