added parameters and cleaned more code

master
xerox 4 years ago
parent fec18fad07
commit c9835bda97

Binary file not shown.

Binary file not shown.

@ -3,13 +3,10 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29519.181
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "deon", "deon\deon.vcxproj", "{B08E2E4F-D8D8-4089-AD3B-6AE42D6F0DBD}"
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "eon", "eon\eon.vcxproj", "{B08E2E4F-D8D8-4089-AD3B-6AE42D6F0DBD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
arm|ARM = arm|ARM
arm|x64 = arm|x64
arm|x86 = arm|x86
Debug|ARM = Debug|ARM
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
@ -18,12 +15,6 @@ Global
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B08E2E4F-D8D8-4089-AD3B-6AE42D6F0DBD}.arm|ARM.ActiveCfg = Debug|ARM
{B08E2E4F-D8D8-4089-AD3B-6AE42D6F0DBD}.arm|ARM.Build.0 = Debug|ARM
{B08E2E4F-D8D8-4089-AD3B-6AE42D6F0DBD}.arm|x64.ActiveCfg = Release|x64
{B08E2E4F-D8D8-4089-AD3B-6AE42D6F0DBD}.arm|x64.Build.0 = Release|x64
{B08E2E4F-D8D8-4089-AD3B-6AE42D6F0DBD}.arm|x86.ActiveCfg = Release|Win32
{B08E2E4F-D8D8-4089-AD3B-6AE42D6F0DBD}.arm|x86.Build.0 = Release|Win32
{B08E2E4F-D8D8-4089-AD3B-6AE42D6F0DBD}.Debug|ARM.ActiveCfg = Debug|ARM
{B08E2E4F-D8D8-4089-AD3B-6AE42D6F0DBD}.Debug|ARM.Build.0 = Debug|ARM
{B08E2E4F-D8D8-4089-AD3B-6AE42D6F0DBD}.Debug|x64.ActiveCfg = Debug|x64

@ -9,47 +9,60 @@
extern YYSTYPE yylval;
extern FILE *yyin;
int yylineno;
%}
%%
\/\*.*?\*\/ { ; // comment }
-?[0-9]+ { yylval.int_num = atoi(yytext); return INTEGER; }
\'[ -~]\' { yylval.byte_num = yytext[1]; return CHAR; }
[-()<>=+*/:;,%{}\[\]] { return *yytext; }
(\/\/.*) { ; // single line comment }
[-()<>=+*/:;,%{}\[\]] { return *yytext; }
"if" { return IF; }
"else" { return ELSE; }
"print" { return PRINT_TOKEN; }
"string" { return KEYWORD_S; }
"while" { return WHILE; }
"int" { return KEYWORD_I; }
"def" { return DEF;}
"call" { return CALL; }
"__main" { return MAIN_TOKEN; }
"char" { return KEYWORD_C; }
"int" { return KEYWORD_I; }
"string" { return KEYWORD_S; }
"double" { return KEYWORD_D; }
"->" { return POINT_VALUE; }
"++" { return INCREASE; }
"--" { return DECREASE; }
"<=" { return LESS_THAN_OR_EQUAL; }
">=" { return GREATER_THAN_OR_EQUAL; }
-?[0-9]+ { yylval.int_num = atoi(yytext); return INTEGER; }
\'[ -~]\' { yylval.byte_num = yytext[1]; return EXPR_C; }
\"(\\.|[^"\\])*\" { 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; }
"\n" { yylineno++; }
[ ?\t\n\r]
. printf("invalid character on line %d, '%s'\n", yylineno, yytext);
<<EOF>> { exit(0); }
%%
int yywrap(void) {
int yywrap(void)
{
return 1;
}
int main(int argc, char* argv[])
{
{
_var_map = malloc(sizeof(map_void_t));
map_init(_var_map);
_temp_statement_head = NULL;
_function_list = NULL;
_arg_list = malloc(sizeof(map_void_t));
map_init(_arg_list);
_stmt_head_curser = NULL;
_function_map = NULL;
FILE *fh;
if (argc > 1)
{

@ -49,8 +49,8 @@
<ClCompile Include="y.tab.c" />
</ItemGroup>
<ItemGroup>
<None Include="deon.l" />
<None Include="deon.y" />
<None Include="eon.l" />
<None Include="eon.y" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>

@ -9,10 +9,6 @@
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
<Filter Include="Source Files\vars">
<UniqueIdentifier>{e714c06a-61ed-402f-aba1-b117816214ef}</UniqueIdentifier>
</Filter>
@ -115,10 +111,10 @@
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="deon.l">
<None Include="eon.l">
<Filter>Source Files</Filter>
</None>
<None Include="deon.y">
<None Include="eon.y">
<Filter>Source Files</Filter>
</None>
</ItemGroup>

@ -15,11 +15,8 @@ void yyerror(char *);
extern int yylineno;
%}
/* doing these before multiplication and division because they have a lower presidence */
%left '<' '>'
%left '+' '-'
/* doing this last so they have a higher presidence */
%left '*' '/' '%'
%union {
@ -32,42 +29,61 @@ 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 INCREASE
%token LESS_THAN_OR_EQUAL
%token GREATER_THAN_OR_EQUAL
%token DECREASE
%token PRINT_TOKEN
%token IF
%token END
%token KEYWORD_S
%token KEYWORD_I
%token KEYWORD_C
%token KEYWORD_D
%token QUOTE
%token ELSE
%token WHILE
%token DEF
%token CALL
%token POINT_VALUE
%token <str_ptr> MAIN_TOKEN
%token <int_num> INTEGER
%type <int_num> EXPR_I
%token <str_ptr> STRING VARNAME
%token <byte_num> CHAR
%token <byte_num> EXPR_C
%token <double_num> DOUBLEVAR
%type <double_num> EXPR_D
%token <float_num> FLOAT_NUM
%type <node_ptr> STMT PRINT VARIABLE FUNC STMT_LIST LOGIC
%type <node_ptr> STMT PRINT VARIABLE FUNC STMT_LIST LOGIC MAIN ARGS
%%
PROGRAM:
MAIN '\n'
MAIN { exec_stmt($1); exec_stmt(create_function_call("__main", NULL)); exit(0); }
|
;
MAIN:
MAIN FUNC ';' { exec_stmt($2); }
|
MAIN_TOKEN ':' '{' STMT_LIST '}' ';' { $$ = create_function("__main", NULL, $4); }
| FUNC ';' MAIN { exec_stmt($1); $$ = $3; }
;
FUNC:
DEF VARNAME ':' '{' STMT_LIST '}' { $$ = create_function($2, $5); }
| CALL VARNAME { $$ = create_function_call($2); }
DEF VARNAME '[' ARGS ']' ':' '{' STMT_LIST '}' { $$ = create_function($2, $4, $8); }
| DEF VARNAME ':' '{' STMT_LIST '}' { $$ = create_function($2, NULL, $5); }
| CALL VARNAME { $$ = create_function_call($2, NULL); }
| CALL VARNAME '[' ARGS ']' { $$ = create_function_call($2, $4); }
;
ARGS:
VARNAME ',' ARGS { $$ = append_argument($1); }
| VARNAME { $$ = append_argument($1); }
;
STMT_LIST:
STMT ';' { $$ = create_compound_statement($1, _temp_statement_head); }
STMT ';' { $$ = create_compound_statement($1, _stmt_head_curser); }
| STMT_LIST STMT ';' { $$ = add_to_compound_statement($2, $1); }
;
@ -88,9 +104,8 @@ VARIABLE:
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 '=' EXPR_C { $$ = create_variable($1, alloc_value(&$3, VAR_BYTE), VAR_BYTE);}
| VARNAME '=' '{' STMT_LIST '}' { $$ = create_function($1, NULL, $4); }
| VARNAME '=' VARNAME { $$ = move_value($1, $3); }
;
@ -99,7 +114,7 @@ EXPR_I:
| 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 { if ($1 == 0 || $3 == 0) yyerror("cannot divide by zero!"); $$ = $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; }
@ -110,7 +125,6 @@ EXPR_I:
| DECREASE EXPR_I { $$ = $2 - 1; }
| EXPR_I DECREASE { $$ = $1 - 1; }
| '(' EXPR_I ')' { $$ = $2; }
| EXPR_I { $$ = $1; }
;
EXPR_D:
@ -118,7 +132,7 @@ EXPR_D:
| 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 '/' EXPR_D { if ($1 == 0 || $3 == 0) yyerror("cannot divide by zero!"); $$ = $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; }
@ -128,12 +142,10 @@ EXPR_D:
| DECREASE EXPR_D { $$ = $2 - 1; }
| EXPR_D DECREASE { $$ = $1 - 1; }
| '(' EXPR_D ')' { $$ = $2; }
| EXPR_D { $$ = $1; }
;
PRINT:
VARNAME { $$ = create_print_variable($1); }
| EXPR_I { $$ = create_print_statement(VAR_INT, alloc_value(&$1, VAR_INT)); }
| STRING { $$ = create_print_statement(VAR_STRING, $1); }
;
%%
@ -141,4 +153,5 @@ PRINT:
void yyerror(char *s)
{
fprintf(stderr, "Error on line %d, %s\n", yylineno, s);
exit(0);
}

@ -2,100 +2,98 @@
#define FUNCTIONC_C
#include "functions.h"
/*
Author: xerox
Update: 1/20/2020
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 pnode_info create_function(const char* p_name, const pnode_info args, const pnode_info p_stmts)
{
if (!p_name || !p_stmts)
return NULL;
node_info *new_node = malloc(sizeof(node_info));
pnode_info new_node = malloc(sizeof(node_info));
new_node->function_body = malloc(sizeof(node_info));
new_node->operation = FUNCTION_OPERATION;
new_node->operation_type.func = FUNCTION;
new_node->name = p_name;
new_node->next = NULL;
new_node->function_body->arguments = args;
new_node->function_body->scope_map = _var_map;
new_node->function_body->statement_list = p_stmts;
// causes a new scope to be created
_var_map = malloc(sizeof(map_void_t));
map_init(_var_map);
if (args)
{
// clear argument list
_arg_list = malloc(sizeof(node_info));
_arg_list->next = NULL;
}
return new_node;
}
/*
Author: xerox
Update: 1/20/2020
create a function call given the name
TODO add scope.
*/
static node_info* create_function_call(char* p_name)
static pnode_info create_function_call(const char* p_name, const pnode_info arg_list)
{
if (!p_name)
return NULL;
node_info *new_node = malloc(sizeof(node_info));
const pnode_info new_node = malloc(sizeof(node_info));
new_node->operation = FUNCTION_OPERATION;
new_node->operation_type.func = FUNCTION_CALL;
new_node->name = p_name;
new_node->arguments = arg_list;
// set the current scope for the call
new_node->scope_map = _var_map;
new_node->next = NULL;
if (arg_list)
{
// clear argument list
_arg_list = malloc(sizeof(node_info));
_arg_list->next = NULL;
}
return new_node;
}
/*
Author: xerox
Update: 1/20/2020
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 pnode_info create_compound_statement(const pnode_info new_statement_list, pnode_info statement_head)
{
if (!new_statement_list)
return NULL;
if(!statement_head)
if (!statement_head)
{
const pnode_info new_node = malloc(sizeof(node_info));
statement_head = malloc(sizeof(node_info));
node_info* new_node = malloc(sizeof(node_info));
statement_head->statement_list = malloc(sizeof(node_info));
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
}
else
{
node_info *p_temp_head = statement_head;
pnode_info p_temp_head = statement_head;
//TODO update with doubly linked list
while(p_temp_head->statement_list != NULL)
while (p_temp_head->statement_list != NULL)
p_temp_head = p_temp_head->statement_list;
p_temp_head->statement_list = malloc(sizeof(node_info));
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;
p_temp_head->statement_list->statement_list = NULL;
}
return statement_head;
}
/*
Author: xerox
Update: 1/20/2020
add to compound statement given new statement and head of statement,
*/
static node_info* add_to_compound_statement(node_info* new_statement, node_info* statement_head)
static pnode_info add_to_compound_statement(const pnode_info new_statement, const pnode_info statement_head)
{
if (!new_statement || !statement_head)
return NULL;
node_info* p_head_tmp = statement_head;
pnode_info p_head_tmp = statement_head;
while (p_head_tmp->statement_list)
p_head_tmp = p_head_tmp->statement_list;
@ -106,4 +104,21 @@ static node_info* add_to_compound_statement(node_info* new_statement, node_info*
p_head_tmp->next = new_statement;
return statement_head;
}
static pnode_info append_argument(const char* var_name)
{
if (!var_name)
return NULL;
const pnode_info new_arg = malloc(sizeof(node_info));
new_arg->name = var_name;
new_arg->next = NULL;
pnode_info temp_arg_head = _arg_list;
while (temp_arg_head->next)
temp_arg_head = temp_arg_head->next;
temp_arg_head->next = new_arg;
return _arg_list;
}
#endif

@ -4,8 +4,9 @@
#include "../linked_list/linked_list.h"
#include "../linked_list/linked_list.c"
node_info *create_function(char *p_name, node_info *p_stmts);
node_info *create_function_call(char *p_name);
node_info *create_compound_statement(node_info *new_statement_list, node_info *statement_head);
node_info* add_to_compound_statement(node_info* new_statement, node_info* statement_head);
pnode_info create_function(const char* p_name, const pnode_info p_stmts);
pnode_info create_function_call(const char* p_name, const pnode_info args);
pnode_info create_compound_statement(const pnode_info new_statement_list, pnode_info statement_head);
pnode_info add_to_compound_statement(const pnode_info new_statement, const pnode_info statement_head);
pnode_info append_argument(const char* var_name);
#endif

@ -8,12 +8,12 @@
free's linked list.
*/
static void free_linked_list(node_info *p_list)
static void free_linked_list(pnode_info p_list)
{
if (!p_list)
return;
node_info* temp;
pnode_info temp;
while (p_list->next)
{
temp = p_list;
@ -28,14 +28,14 @@ static void free_linked_list(node_info *p_list)
shallow copy linked list.
*/
static node_info* shallow_copy_linked_list(node_info* p_list)
static pnode_info shallow_copy_linked_list(pnode_info p_list)
{
if (!p_list)
return NULL;
node_info* new_list = malloc(sizeof(node_info));
const pnode_info new_list = malloc(sizeof(node_info));
const pnode_info copy_list = new_list;
new_list->next = NULL;
node_info* copy_list = new_list;
while(p_list->next)
{
@ -51,7 +51,7 @@ static node_info* shallow_copy_linked_list(node_info* p_list)
append to linked list.
*/
static void linked_list_append(node_info* p_head, node_info* new_node)
static void linked_list_append(const pnode_info p_head, const pnode_info new_node)
{
if (!p_head || !new_node)
return;
@ -70,12 +70,12 @@ static void linked_list_append(node_info* p_head, node_info* new_node)
remove link from linked list.
*/
static bool linked_list_remove(node_info* p_head, node_info* delete_node)
static bool linked_list_remove(const pnode_info p_head, const pnode_info delete_node)
{
if (!p_head || !delete_node)
return false;
node_info* p_head_temp = p_head;
pnode_info p_head_temp = p_head;
while (p_head_temp->next)
{
if (p_head_temp->next == delete_node)

@ -2,8 +2,8 @@
#define LINKED_LIST_H
#include "../types.h"
void free_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);
void free_linked_list(const pnode_info p_list);
pnode_info shallow_copy_linked_list(const pnode_info p_list);
void linked_list_append(const pnode_info p_head, const pnode_info new_node);
bool linked_list_remove(const pnode_info p_head, const pnode_info delete_node);
#endif

@ -1,38 +1,27 @@
#include "logic.h"
/*
Author: xerox
Date: 1/19/2020
creates logic given type, statement list to execute, and variable name for condition.
*/
static logic* create_logic(logic_type type, node_info* stmt_list, char* var_name)
static plogic create_logic(const logic_type type, const pnode_info stmt_list, const char* var_name)
{
if (!stmt_list || !var_name)
return NULL;
logic* new_logic = malloc(sizeof(logic));
plogic 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)
static pnode_info create_logic_node(const plogic logic_expr)
{
if (!logic_expr)
return NULL;
node_info* new_node = malloc(sizeof(node_info));
const pnode_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;
new_node->scope_map = _var_map;
return new_node;
}

@ -1,6 +1,6 @@
#ifndef LOGIC_H
#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);
plogic create_logic(const logic_type type, const pnode_info stmt_list, const char* var_name);
pnode_info create_logic_node(const plogic logic_expr);
#endif

@ -2,17 +2,7 @@
#define PARSER_C
#include "parser.h"
/*
Author: xerox
Updated: 1/20/2020
Executes a statment.
@param nodeInfo statement to be executed
@return void.
*/
static void exec_stmt(node_info *exec_node)
static void exec_stmt(pnode_info exec_node)
{
if (!exec_node)
return;
@ -26,13 +16,13 @@ static void exec_stmt(node_info *exec_node)
add_var(
exec_node->assignment->var->name,
exec_node->assignment->var->value,
_var_map
exec_node->scope_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, exec_node->scope_map),
exec_node->scope_map
);
break;
default:
@ -45,9 +35,9 @@ static void exec_stmt(node_info *exec_node)
{
case IF_LOGIC:
{
variable_values* p_var_values = get_value(
const pvariable_values p_var_values = get_value(
exec_node->logical_expr->condition_var,
_var_map
exec_node->scope_map
);
if (p_var_values && *(unsigned char*)p_var_values->data_ptr)
@ -56,14 +46,14 @@ static void exec_stmt(node_info *exec_node)
}
case WHILE_LOGIC:
{
variable_values* p_var_values = get_value(exec_node->logical_expr->condition_var, _var_map);
pvariable_values p_var_values = get_value(exec_node->logical_expr->condition_var, exec_node->scope_map);
if (!p_var_values)
return;
while (*(unsigned char*)p_var_values->data_ptr)
{
exec_stmt(exec_node->logical_expr->stmt_list);
p_var_values = get_value(exec_node->logical_expr->condition_var, _var_map);
p_var_values = get_value(exec_node->logical_expr->condition_var, exec_node->scope_map);
}
break;
}
@ -116,7 +106,7 @@ static void exec_stmt(node_info *exec_node)
}
case PRINT_VARIABLE:
{
variable_values* p_var = get_value(exec_node->var->name, _var_map);
const pvariable_values p_var = get_value(exec_node->var->name, exec_node->scope_map);
switch (p_var->type)
{
case VAR_STRING:
@ -149,25 +139,52 @@ static void exec_stmt(node_info *exec_node)
{
case FUNCTION_CALL:
{
node_info* _stmts = *map_get(&*((map_void_t*)_function_list), exec_node->name);
_stmts = _stmts->statement_list;
while (_stmts->statement_list)
pnode_info _stmts = *map_get(&*((map_void_t*)_function_map), exec_node->name);
if (_stmts)
{
// for each argument set the values in the function to the arguments in the calling function
pnode_info tmp_arg_list = exec_node->arguments;
pnode_info tmp_func_arg_list = _stmts->arguments;
if (tmp_arg_list && tmp_func_arg_list)
{
while (tmp_arg_list->next && tmp_func_arg_list->next)
{
tmp_func_arg_list = tmp_func_arg_list->next;
tmp_arg_list = tmp_arg_list->next;
const pvariable_values values = get_value(
tmp_arg_list->name,
exec_node->scope_map
);
map_set_(
_stmts->scope_map,
tmp_func_arg_list->name,
&values,
sizeof(variable_values*)
);
}
}
_stmts = _stmts->statement_list;
exec_stmt(_stmts);
while (_stmts->statement_list)
{
_stmts = _stmts->statement_list;
exec_stmt(_stmts);
}
break;
}
break;
}
case FUNCTION:
{
if (!_function_list)
if (!_function_map)
{
_function_list = malloc(sizeof(map_void_t));
map_init(_function_list);
map_set(_function_list, exec_node->name, exec_node->function_body);
_function_map = malloc(sizeof(map_void_t));
map_init(_function_map);
map_set(_function_map, exec_node->name, exec_node->function_body);
}
else //else we dont malloc, we just put the function inside of the hashmap.
map_set(_function_list, exec_node->name, exec_node->function_body);
else
map_set(_function_map, exec_node->name, exec_node->function_body);
break;
}
default:

@ -2,5 +2,5 @@
#define PARSER_H
#include "../types.h"
#include "../vars/vars.h"
void exec_stmt(node_info *exec_node);
void exec_stmt(pnode_info exec_node);
#endif

@ -2,20 +2,13 @@
#define PRINT_C
#include "print.h"
/*
Author: xerox
Updated: 1/20/2020
creates a print statement given variable type and value.
*/
static node_info* create_print_statement(variable_type operation, void* value)
static pnode_info create_print_statement(const variable_type operation, const void* value)
{
if (!value)
return NULL;
node_info* new_print_stmt = (node_info*)malloc(sizeof(node_info));
print* new_print = malloc(sizeof(print));
const pnode_info new_print_stmt = (pnode_info)malloc(sizeof(node_info));
const pprint new_print = malloc(sizeof(print));
new_print_stmt->operation = PRINT_OPERATION;
new_print_stmt->operation_type.print = PRINT_CONSTANT;
new_print->type = operation;
@ -23,21 +16,16 @@ static node_info* create_print_statement(variable_type operation, void* value)
new_print_stmt->print_expr = new_print;
new_print_stmt->statement_list = NULL;
new_print_stmt->next = NULL;
new_print_stmt->scope_map = _var_map;
return new_print_stmt;
}
/*
Author: xerox
Updated: 1/19/2020
create print variable from variable name.
*/
static node_info* create_print_variable(char* var_name)
static pnode_info create_print_variable(const char* var_name)
{
if (!var_name)
return NULL;
node_info* new_print_stmt = (node_info*)malloc(sizeof(node_info));
const pnode_info new_print_stmt = malloc(sizeof(node_info));
new_print_stmt->var = malloc(sizeof(variable));
new_print_stmt->operation = PRINT_OPERATION;
new_print_stmt->operation_type.print = PRINT_VARIABLE;
@ -45,6 +33,7 @@ static node_info* create_print_variable(char* var_name)
new_print_stmt->statement_list = NULL;
new_print_stmt->next = NULL;
new_print_stmt->scope_map = _var_map;
return new_print_stmt;
}
#endif

@ -1,6 +1,6 @@
#ifndef PRINT_H
#define PRINT_H
#include "../types.h"
node_info* create_print_statement(variable_type operation, void* value);
node_info* create_print_variable(char* var_name);
pnode_info create_print_statement(const variable_type operation, const void* value);
pnode_info create_print_variable(const char* var_name);
#endif

@ -8,7 +8,6 @@
#define DEON_DEBUGGING 0
bool _DEON_DEBUG;
//--- different variable values
typedef enum _variable_type
{
VAR_INT,
@ -17,36 +16,38 @@ typedef enum _variable_type
VAR_DOUBLE,
VAR_STRING,
VAR_VOID
} variable_type;
} variable_type, *pvariable_type;
typedef enum _eval_type
{
DECREASE_EQUALS,
INCREASE_EQUALS
} eval_type, *peval_type;
//--- different function operations
typedef enum _function_type
{
FUNCTION,
FUNCTION_CALL,
} function_type;
} function_type, *pfunction_type;
//--- different print operations
typedef enum _print_type
{
PRINT_VARIABLE,
PRINT_CONSTANT
} print_type;
} print_type, *pprint_type;
//--- different logic types
typedef enum _logic_type
{
IF_LOGIC,
WHILE_LOGIC
} logic_type;
} logic_type, *plogic_type;
typedef enum _assignment_type
{
ASSIGN_FROM_VAR,
ASSIGN_FROM_CONST
} assignment_type;
} assignment_type, *passignment_type;
//--- all operations in the language
typedef enum _operation
{
ASSIGNMENT_OPERATION,
@ -54,53 +55,44 @@ typedef enum _operation
EXEC_STMT_LIST_OPERATION,
PRINT_OPERATION,
FUNCTION_OPERATION,
};
} operation, *poperation;
//--- variable value details
typedef struct _variable_values
typedef struct _variable_values
{
variable_type type;
void* data_ptr;
} variable_values;
} variable_values, *pvariable_values;
//--- variable details
typedef struct _variable
{
char *name;
variable_values *value;
struct variable *next;
} variable;
char* name;
variable_values* value;
struct variable* next;
} variable, *pvariable;
//--- print data
typedef struct _print
typedef struct _print
{
enum _variable_type type;
void* value;
} print;
} print, *pprint;
//--- assignment data
typedef struct _assignment_operation
{
char* from;
char* to;
variable* var;
}assignment_operation;
} assignment_operation, *passignment_operation;
//--- logic operation data
typedef struct _logic
{
logic_type type;
char* condition_var;
struct node_info* stmt_list;
} logic;
} logic, *plogic;
//--- struct that brings it all together
typedef struct node_info
typedef struct node_info
{
//--- node operation.
enum _operation operation;
//--- sub operations.
union _operation_type
{
logic_type logic;
@ -109,20 +101,21 @@ typedef struct node_info
assignment_type assignment;
} operation_type;
char *name;
char* name;
print* print_expr;
logic* logical_expr;
assignment_operation* assignment;
variable* var;
struct node_info *next;
struct node_info *function_body;
struct node_info *statement_list;
struct node_info *new_scope;
struct map_void_t *var_map;
} node_info;
//--- globals
struct node_info* next;
struct node_info* function_body;
struct node_info* statement_list;
struct node_info* arguments;
struct map_void_t* scope_map;
} node_info, *pnode_info;
//--- globals used for parsing
map_void_t* _var_map;
node_info* _temp_statement_head;
map_void_t* _function_list;
pnode_info _stmt_head_curser;
pnode_info _arg_list;
map_void_t* _function_map;
#endif

@ -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(const char* p_name, const pvariable_values p_values, map_void_t* p_var_map)
{
if (!p_name || !p_values)
return;
@ -22,11 +22,11 @@ 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 pvariable_values get_value(const char* p_name, const map_void_t* p_var_map)
{
if (!p_name)
return NULL;
return *map_get(&*((map_void_t* ) p_var_map), p_name);
return *map_get(&*((map_void_t* )p_var_map), p_name);
}
/*
@ -35,13 +35,14 @@ 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 pvariable make_variable(const char* p_name, const void* value, const variable_type var_type)
{
if (!p_name || !value)
return NULL;
variable* new_var = malloc(sizeof(variable));
variable_values* new_value = malloc(sizeof(variable_values));
const pvariable new_var = malloc(sizeof(variable));
const pvariable_values new_value = malloc(sizeof(variable_values));
new_value->type = var_type;
new_value->data_ptr = value;
new_var->name = p_name;
@ -55,7 +56,7 @@ static variable* make_variable(char* p_name, void* value, variable_type var_type
allocate memory for values that are 8 bytes or less.
*/
static void* alloc_value(void* value, variable_type var_type)
static void* alloc_value(const void* value, const variable_type var_type)
{
if (!value)
return NULL;
@ -99,18 +100,20 @@ static void* alloc_value(void* value, variable_type var_type)
create variable given name, value, and type.
*/
static node_info* create_variable(char* p_name, void* value, variable_type var_type)
static pnode_info create_variable(const char* p_name, const void* value, const 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);
const pnode_info new_node = malloc(sizeof(node_info));
const pvariable 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->scope_map = _var_map;
new_node->next = NULL;
return new_node;
}
@ -121,18 +124,19 @@ static node_info* create_variable(char* p_name, void* value, variable_type var_t
Move value from one variable to another.
*/
static node_info* move_value(char* to, char* from)
static node_info* move_value(const char* to, const char* from)
{
if (!from || !to)
return NULL;
node_info* new_node = malloc(sizeof(node_info));
new_node->assignment = malloc(sizeof(*new_node->assignment));
const pnode_info new_node = malloc(sizeof(node_info));
new_node->assignment = malloc(sizeof(assignment_operation));
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->scope_map = _var_map;
new_node->next = NULL;
return new_node;
}

@ -1,10 +1,10 @@
#ifndef VARS_H
#define VARS_H
#include "../types.h"
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);
void add_var(const char* name, const pvariable_values values, map_void_t* p_var_map);
pvariable make_variable(char* p_name, void* value, variable_type var_type);
pvariable_values get_value(const char* p_name, map_void_t* p_var_map);
void* alloc_value(const void* value, const variable_type var_type);
pnode_info create_variable(const char* p_name, const void* value, variable_type var_type);
pnode_info move_value(const char* to, const char* from);
#endif

Loading…
Cancel
Save