diff --git a/README.md b/README.md old mode 100644 new mode 100755 index eb6e038..6d9bdcc --- a/README.md +++ b/README.md @@ -1,12 +1,7 @@ -# Deon programming language V0.9.1 update notes +# Deon programming language V0.8 (Structs, Scope) My computer programming language made with LEX/YACC written in C. (interpreted) -* Moved nodeInfo declarations to its own folder/file -* Moved print declarations to its own folder/file -* Moved heads of linked lists/hashmaps to headerfiles - - -# Example Program. +# Making a program. Everything in this language is based around "Structs". These are not the regular structs that are in C. To make a hello world program it would look like this: ``` diff --git a/assign/assign.c b/assign/assign.c deleted file mode 100644 index 70d866c..0000000 --- a/assign/assign.c +++ /dev/null @@ -1 +0,0 @@ -//merge in progress, refer to parser.c \ No newline at end of file diff --git a/assign/assign.h b/assign/assign.h deleted file mode 100644 index 70d866c..0000000 --- a/assign/assign.h +++ /dev/null @@ -1 +0,0 @@ -//merge in progress, refer to parser.c \ No newline at end of file diff --git a/deon b/deon deleted file mode 100644 index fb21d0c..0000000 Binary files a/deon and /dev/null differ diff --git a/deon.l b/deon.l old mode 100644 new mode 100755 diff --git a/deon.y b/deon.y old mode 100644 new mode 100755 index 04aa282..3e7da2e --- a/deon.y +++ b/deon.y @@ -1,14 +1,26 @@ %{ - #include #include #include + #include "parser/parser.c" +#include "functions/functions.c" +#include "logic/logic.c" +#include "print/print.c" +#include "includes/include.h" + int yylex(void); void yyerror(char *); + extern int yylineno; extern int _DEBUG; +//hashmaps/link list heads +map_void_t *_var_map = NULL; +node_info *_temp_statement_head = NULL; +//struct variable *_var_head = NULL; +map_void_t *_function_list = NULL; + %} /* doing these before multiplication and division because @@ -29,7 +41,7 @@ extern int _DEBUG; int num; char character; double Double; - struct nodeInfo *nPtr; + struct node_info *nPtr; } @@ -61,37 +73,37 @@ statement: VARIABLE { $$ = $1; } //this is a real var dec. | FUNC { $$ = $1; } - | IF '[' expr ']' ':' '{' statement_list '}' { $$ = createLogic(1, $3, $7, NULL); } //examples of logic in this language - | WHILE '[' expr ']' ':' '{' statement_list '}' { $$ = createLogic(2, $3, $7, NULL); } + | IF '[' expr ']' ':' '{' statement_list '}' { $$ = create_logic(1, $3, $7, NULL); } //examples of logic in this language + | WHILE '[' expr ']' ':' '{' statement_list '}' { $$ = create_logic(2, $3, $7, NULL); } | PRINTF'['print']' { $$ = $3; } | '{' statement_list '}' { $$ = $2; } ; statement_list: - statement ';' { $$ = createCompoundStatement($1); } - | statement_list statement ';' { $$ = addToCompoundStatement($2); } + statement ';' { $$ = create_compound_statement($1, _temp_statement_head); } + | statement_list statement ';' { $$ = add_to_compound_statement($2, _temp_statement_head); } ; FUNC: - DEF VARNAME ':' '{' statement_list '}' { $$ = createFunction($2, $5); } // defining functions - | CALL VARNAME { $$ = createFunctionCall($2); } // calling functions + DEF VARNAME ':' '{' statement_list '}' { $$ = create_function($2, $5); } // defining functions + | CALL VARNAME { $$ = create_function_call($2); } // calling functions ; VARIABLE: - VARNAME '=' STRING { $$ = createVarAssignmentString($1, $3);} - | VARNAME '=' expr { $$ = createVarAssignmentInt($1, $3);} - | VARNAME '=' CHAR { $$ = createVarAssignmentChar($1, $3);} - | VARNAME '=' DOUBLEVAR { $$ = createVarAssignmentDouble($1, $3);} - | VARNAME '=' '{' statement_list '}' { $$ = createFunction($1, $4); } // you can also define functions like this + VARNAME '=' STRING { $$ = create_var_assignment_string($1, $3);} + | VARNAME '=' expr { $$ = create_var_assignment_int($1, $3);} + | VARNAME '=' CHAR { $$ = create_var_assignment_char($1, $3);} + | VARNAME '=' DOUBLEVAR { $$ = create_var_assignment_double($1, $3);} + | VARNAME '=' '{' statement_list '}' { $$ = create_function($1, $4); } // you can also define functions like this ; expr: INTEGER { $$ = $1; } - | '{' KEYWORD_I '}' VARNAME { $$ = getValue($4)->Int; } + | '{' KEYWORD_I '}' VARNAME { $$ = get_value($4, _var_map)->_int; } | expr '+' expr { $$ = $1 + $3; } /* addition */ | expr '-' expr { $$ = $1 - $3; } /* subtration */ | expr '*' expr { $$ = $1 * $3; } /* multiplication */ @@ -102,12 +114,12 @@ expr: | '(' expr ')' { $$ = $2; } ; - +//TODO update this. print: - VARNAME { $$ = createPrintVarNode($1); } - | expr { $$ = createPrintStatement(1, $1, NULL); } - | STRING { $$ = createPrintStatement(2, 0, $1); } + VARNAME { $$ = create_print_var_node($1); } + | expr { $$ = create_print_statement(1, $1, NULL); } + | STRING { $$ = create_print_statement(2, 0, $1); } | ',' print { $$ = $2; } ; diff --git a/examples/functions.deon b/examples/functions.deon deleted file mode 100644 index fc642a5..0000000 --- a/examples/functions.deon +++ /dev/null @@ -1,9 +0,0 @@ -i = 10; - -def a : - if [ {int} i > 4 ] : - i = {int} i - 1; - -call a; - -printf [ {int} i ]; \ No newline at end of file diff --git a/examples/hello_world.dn b/examples/hello_world.dn new file mode 100755 index 0000000..cc694fd --- /dev/null +++ b/examples/hello_world.dn @@ -0,0 +1,11 @@ +def Main : { + + if[ 10 > 4 ] : { + + printf["Hello world!"]; + + }; + +}; + +call Main; \ No newline at end of file diff --git a/examples/logic.deon b/examples/logic.deon deleted file mode 100644 index f793b78..0000000 --- a/examples/logic.deon +++ /dev/null @@ -1,13 +0,0 @@ -if [ 10 > 9 ]: - - printf [ "ten is bigger than 9" ];, - new_int = 10; - -if [ {int} new_int > 9 ]: - - printf [ "new_int is bigger than 9!" ];, - another_int = 100; - -if [ {int} another_int > 1000 ]: - - printf [ "this wont print!" ]; \ No newline at end of file diff --git a/examples/newfunctions.deon b/examples/newfunctions.deon deleted file mode 100644 index b2b2645..0000000 --- a/examples/newfunctions.deon +++ /dev/null @@ -1,18 +0,0 @@ -def AnotherFunction: { - - printf[ "another Function!" ]; - -}; - - -def Main : { - - if [ 10 > 4 ]: { - - printf[ "hello world" ]; - - }; - -}; - -call Main; diff --git a/examples/test.deon b/examples/test.deon deleted file mode 100644 index 6d790f7..0000000 --- a/examples/test.deon +++ /dev/null @@ -1,11 +0,0 @@ -def Main : { - - - printf["hello world"]; - a = 10; - printf[a]; - - -}; - -call Main; \ No newline at end of file diff --git a/examples/variables.deon b/examples/variables.deon deleted file mode 100644 index 1c3cec9..0000000 --- a/examples/variables.deon +++ /dev/null @@ -1,14 +0,0 @@ -myint = 10; -mystring = "anything you want????"; -mychar = 'a'; -another_char = '!'; - -printf [ mystring ]; -printf [ {int} myint ]; -printf [ another_char ]; - -myint = "my int is not a string!"; -mychar = 109; - -printf [ myint ]; -printf [ {int} mychar ]; \ No newline at end of file diff --git a/functions/functions.c b/functions/functions.c old mode 100644 new mode 100755 index 70d866c..80eb30a --- a/functions/functions.c +++ b/functions/functions.c @@ -1 +1,74 @@ -//merge in progress, refer to parser.c \ No newline at end of file +#include + +#include "functions.h" +#include "../linked_list/linked_list.h" +#include "../linked_list/linked_list.c" + + +/** + * Creates a function. + * + * @param char * to the name of the function + * @param nodeInfo * to statement list + * @return nodeInfo * to head + */ +node_info *create_function(char *pName, node_info *pStmts) { + + node_info *newNode = malloc(sizeof(node_info)); + newNode->type = FUNCTION; + newNode->name = pName; + newNode->_function_body = malloc(sizeof(node_info)); + newNode->_function_body->next = copy_linked_list(pStmts); + _free_linked_list(pStmts); + return newNode; + +} + +/** + * Creates a function + * + * @param name of the function + * @return a node_info pointer to the head of the function + */ +node_info *create_function_call(char *pName) { + + node_info *newNode = malloc(sizeof(node_info)); + newNode->type = FUNCTION_CALL; + newNode->name = pName; + newNode->next = NULL; + return newNode; +} + +/** + * Creates a linked list inside of a linked list + * + * @param new_statement_list to be appended to list + * @param statement_head of linked list to append to + */ +node_info *create_compound_statement(node_info *new_statement_list, node_info *statement_head) { + + if(statement_head == NULL) { + statement_head = malloc(sizeof(node_info)); + + node_info *newNode = malloc(sizeof(node_info)); + + statement_head->opperation = 5; + statement_head->next = new_statement_list; + statement_head->next->next = NULL; + statement_head->statement_list = NULL; + + } else { + + node_info *p = statement_head; //make a temp of list head, so we can traverse it. + //TODO update with doubly linked list + while(p->statement_list != NULL) + p = p->statement_list; + + p->next = malloc(sizeof(node_info)); + p->next = new_statement_list; + p->next->next = NULL; + + } + return statement_head; + +} \ No newline at end of file diff --git a/functions/functions.h b/functions/functions.h old mode 100644 new mode 100755 index 70d866c..154e059 --- a/functions/functions.h +++ b/functions/functions.h @@ -1 +1,5 @@ -//merge in progress, refer to parser.c \ No newline at end of file +#include "../includes/include.h" + +node_info *create_function(char *pName, node_info *pStmts); +node_info *create_function_call(char *pName); +node_info *create_compound_statement(node_info *new_statement_list, node_info *statement_head); \ No newline at end of file diff --git a/hashmap/map.c b/hashmap/map.c old mode 100644 new mode 100755 index 3f0b0b0..429c28d --- a/hashmap/map.c +++ b/hashmap/map.c @@ -1,10 +1,3 @@ -/** - * Copyright (c) 2014 rxi - * - * This library is free software; you can redistribute it and/or modify it - * under the terms of the MIT license. See LICENSE for details. - */ - #include #include #include "map.h" diff --git a/hashmap/map.h b/hashmap/map.h old mode 100644 new mode 100755 diff --git a/includes/include.h b/includes/include.h new file mode 100644 index 0000000..f9eb56c --- /dev/null +++ b/includes/include.h @@ -0,0 +1,99 @@ +#ifndef INCLUDE_H +#define INCLUDE_H +#include +#include + +#include "../hashmap/map.h" + +/** + * VariableType is a enum of type struct that holds the varible types that will + * be inside of my language. + */ +typedef enum variable_type { + + VAR_STRING, + VAR_INT, + VAR_CHAR, + VAR_DOUBLE, + VAR_VOID + +} variable_type; + +/** + * VariableValues is a struct that holds all possible variable values + */ +typedef struct variable_values { + + variable_type type; + char _char; + char *_string; + int _int; + double _double; + void* _void_pointer; + + +} variable_values; + + +/** + * Variables is a struct that has all the information needed for a variables + */ +typedef struct variable{ + + char *name; + variable_values *value; + struct Variable *next; + +} variable; + + + +typedef struct print { + + int opperation; + double value; + char *string; + +} print; + +typedef struct logic { + + int opperation; + int expr; + struct node_info *if_true; + struct node_info *else_false; + struct node_info *while_true; + +} logic; + + +typedef struct node_info { + + int opperation; + + enum type{ + + ASSIGNMENT, + EXPRESSION, + STATEMENT, + LOGIC, + FUNCTION, + FUNCTION_CALL + + }type; + + + char *name; + variable *var; + print printExpression; + logic logicalExpression; + struct node_info *next; + struct node_info *_function_body; + struct node_info *statement_list; + struct node_info *local_list; + struct map_void_t *_var_list; + + +} node_info; + +#endif \ No newline at end of file diff --git a/linked_list/linked_list.c b/linked_list/linked_list.c new file mode 100644 index 0000000..c7e24d2 --- /dev/null +++ b/linked_list/linked_list.c @@ -0,0 +1,44 @@ +#include + +#include "linked_list.h" + +/** + * free's a linked list + * @param head of linked list to be freed + * @return void + */ +void _free_linked_list(node_info *pList) { + + node_info *temp; + + while(pList->next != NULL) { + + temp = pList; + pList = pList->next; + free(temp); + + } + +} + +/** + * returns a copy of list + * @param nodeInfo list to be copied + * @return copied list + */ +node_info *copy_linked_list(node_info *pList) { + + node_info *newList = malloc(sizeof(node_info)); + newList->next = NULL; + node_info *copy_list = newList; + while(pList->next != NULL) { + + pList = pList->next; + copy_list->next = pList; + copy_list = copy_list->next; + + } + + return newList; + +} \ No newline at end of file diff --git a/linked_list/linked_list.h b/linked_list/linked_list.h new file mode 100644 index 0000000..df73f0e --- /dev/null +++ b/linked_list/linked_list.h @@ -0,0 +1,4 @@ +#include "../includes/include.h" + +void _free_linked_list(node_info *pList); +node_info *copy_linked_list(node_info *pList); \ No newline at end of file diff --git a/linkedlist/linked.c b/linkedlist/linked.c deleted file mode 100644 index 790cb6b..0000000 --- a/linkedlist/linked.c +++ /dev/null @@ -1,6 +0,0 @@ -typedef struct link { - - - - -} link; \ No newline at end of file diff --git a/linkedlist/linked.h b/linkedlist/linked.h deleted file mode 100644 index e69de29..0000000 diff --git a/logic/logic.c b/logic/logic.c index 70d866c..7ea9f77 100644 --- a/logic/logic.c +++ b/logic/logic.c @@ -1 +1,52 @@ -//merge in progress, refer to parser.c \ No newline at end of file +#include "../includes/include.h" + +node_info *create_logic(int opperation, int expr, node_info *statement_list, node_info *else_false) { + + node_info *n = malloc(sizeof(node_info)); + + switch (opperation) { + //if statement + case 1: + //logical expression, see node_info for opperation info + n->opperation = 4; + logic a; + a.opperation = 1; + a.expr = expr; + + while (statement_list->statement_list != NULL) { + + statement_list = statement_list->statement_list; + + } + + //now that we have the statement list, we need to make the head opperation 5 + node_info *newNode = malloc(sizeof(node_info)); + newNode->opperation = 5; + newNode->next = copy_linked_list(statement_list); + _free_linked_list(statement_list); + a.if_true = newNode; + + //this removes the last statement list in statementHead + statement_list = NULL; + n->logicalExpression = a; + return n; + break; + //while loop + case 2: + + /*n->opperation = 4; + logic b; + b.opperation = 2; + b.expr = expr; + b.while_true = if_true; + + n->logicalExpression = b; + return n;*/ + break; + + default: + printf("check your yacc file for logical statements\n"); + + } + +} \ No newline at end of file diff --git a/logic/logic.h b/logic/logic.h index 70d866c..9f84c1b 100644 --- a/logic/logic.h +++ b/logic/logic.h @@ -1 +1 @@ -//merge in progress, refer to parser.c \ No newline at end of file +node_info *createLogic(int opperation, int expr, node_info *statement_list, node_info *else_false); \ No newline at end of file diff --git a/logic/main.c b/logic/main.c deleted file mode 100644 index 70d866c..0000000 --- a/logic/main.c +++ /dev/null @@ -1 +0,0 @@ -//merge in progress, refer to parser.c \ No newline at end of file diff --git a/make.sh b/make.sh old mode 100644 new mode 100755 diff --git a/networking/README.md b/networking/README.md deleted file mode 100644 index 8a3bd6c..0000000 --- a/networking/README.md +++ /dev/null @@ -1 +0,0 @@ -# Folder contains all data related to networking \ No newline at end of file diff --git a/networking/a.out b/networking/a.out deleted file mode 100644 index 677a51f..0000000 Binary files a/networking/a.out and /dev/null differ diff --git a/networking/main.c b/networking/main.c old mode 100644 new mode 100755 diff --git a/networking/socket.h b/networking/socket.h old mode 100644 new mode 100755 diff --git a/networking/socket.h.gch b/networking/socket.h.gch deleted file mode 100644 index 8fe9c25..0000000 Binary files a/networking/socket.h.gch and /dev/null differ diff --git a/networking/template.deon b/networking/template.dn old mode 100644 new mode 100755 similarity index 100% rename from networking/template.deon rename to networking/template.dn diff --git a/networking/test.c b/networking/test.c old mode 100644 new mode 100755 diff --git a/parser/parser.c b/parser/parser.c old mode 100644 new mode 100755 index ca31956..0e86ba8 --- a/parser/parser.c +++ b/parser/parser.c @@ -1,131 +1,17 @@ #include -#include "parser.h" -#include "../vars/vars.h" -#include "../hashmap/map.h" +#include +#include "../vars/vars.c" -//TODO put in header -void ex(); +//hashmaps/link list heads +extern map_void_t *_var_map; +extern node_info *_temp_statement_head; +//struct variable *_var_head = NULL; +extern map_void_t *_function_list; -//TODO make a seperate file for this -typedef struct print { +node_info* create_var_assignment_string(char *pName, char *pString) { - int opperation; - double value; - char *string; - - -} print; - -//TODO make a seperate file for this -typedef struct logic { - - int opperation; - int expr; - struct nodeInfo *if_true; - struct nodeInfo *else_false; - struct nodeInfo *while_true; - -} logic; - -//TODO make a seperate file for this. -typedef struct nodeInfo { - - int opperation; - - enum type{ - - ASSIGNMENT, - EXPRESSION, - STATEMENT, - LOGIC, - FUNCTION, - FUNCTION_CALL - - }type; - - - char *name; - Variable *var; - print printExpression; - logic logicalExpression; - struct nodeInfo *next; - struct nodeInfo *_function_body; - struct nodeInfo *statement_list; - struct nodeInfo *local_list; - struct map_void_t *local_var_list; - - -} nodeInfo; - - -//linked list heads. (move to a header file.) - -//TODO put in header. -struct nodeInfo *statementHead = NULL; -struct Variable *head = NULL; -struct map_void_t *_function_list = NULL; - - -nodeInfo *createFunctionCall(char *_name) { - - nodeInfo *newNode = malloc(sizeof(nodeInfo)); - newNode->type = FUNCTION_CALL; - newNode->name = _name; - newNode->next = NULL; - return newNode; - -} - - -/** - * free's a list's links. - * @param list of linked list to be free'ed - * @return void - */ -void freeLinkedList(nodeInfo *list) { - - statementHead = NULL; - -} - -/** - * returns a copy of list - * @param nodeInfo list to be copied - * @return copied list - */ -nodeInfo *copyLinkedList(nodeInfo *list) { - - nodeInfo *newList = malloc(sizeof(nodeInfo)); - newList->next = NULL; - nodeInfo *copy_list = newList; - while(list->next != NULL) { - - list = list->next; - copy_list->next = list; - copy_list = copy_list->next; - - } - - return newList; - -} - -nodeInfo *createFunction(char *_name, nodeInfo *_stmts) { - - nodeInfo *newNode = malloc(sizeof(nodeInfo)); - newNode->type = FUNCTION; - newNode->name = _name; - newNode->_function_body = malloc(sizeof(nodeInfo)); - newNode->_function_body->next = copyLinkedList(_stmts); - freeLinkedList(_stmts); - return newNode; - -} - -nodeInfo* createVarAssignmentString(char *name, char *string) { - - nodeInfo *newNode = malloc(sizeof(nodeInfo)); - Variable *newVar = makeVariableString(name, string); + node_info *newNode = malloc(sizeof(node_info)); + variable *newVar = make_variable_string(pName, pString); newNode->type = ASSIGNMENT; newNode->opperation = 1; newNode->var = newVar; @@ -133,10 +19,10 @@ nodeInfo* createVarAssignmentString(char *name, char *string) { } -nodeInfo* createVarAssignmentInt(char *name, int number) { +node_info* create_var_assignment_int(char *pName, int number) { - nodeInfo *newNode = malloc(sizeof(nodeInfo)); - Variable *newVar = makeVariableInt(name, number); + node_info *newNode = malloc(sizeof(node_info)); + variable *newVar = make_variable_int(pName, number); newNode->type = ASSIGNMENT; newNode->opperation = 1; newNode->var = newVar; @@ -144,10 +30,10 @@ nodeInfo* createVarAssignmentInt(char *name, int number) { } -nodeInfo* createVarAssignmentChar(char *name, char Char) { +node_info* create_var_assignment_char(char *pName, char Char) { - nodeInfo *newNode = malloc(sizeof(nodeInfo)); - Variable *newVar = makeVariableChar(name, Char); + node_info *newNode = malloc(sizeof(node_info)); + variable *newVar = make_variable_char(pName, Char); newNode->type = ASSIGNMENT; newNode->opperation = 1; newNode->var = newVar; @@ -155,216 +41,59 @@ nodeInfo* createVarAssignmentChar(char *name, char Char) { } -nodeInfo* createVarAssignmentDouble(char *name, double Double) { - - nodeInfo *newNode = malloc(sizeof(nodeInfo)); - - Variable *newVar = makeVariableDouble(name, Double); - +node_info* create_var_assignment_double(char *pName, double Double) { + node_info *newNode = malloc(sizeof(node_info)); + variable *newVar = make_variable_double(pName, Double); + //new node stuff newNode->type = ASSIGNMENT; newNode->opperation = 1; newNode->var = newVar; - return newNode; - } -nodeInfo *createPrintVarNode(char *varname) { - - nodeInfo *newNode = malloc(sizeof(nodeInfo)); - newNode->opperation = 6; - newNode->name = varname; - return newNode; - -} - - -/** - * - * @params opperation value, see print struct for info - * @params int value to be printed - * @params string to be printed - * - * - */ - -nodeInfo *createPrintStatement(int opperation, int value, char *string) { - - nodeInfo *n = malloc(sizeof(nodeInfo)); - print a; - //if we are going to print a varibles - //or we are going to print an int - switch(opperation) { - - - - case 1: - - n->opperation = 3;//this needs to not be a magic number lol - a.value = value; - a.opperation = 1; - n->printExpression = a; - return n; - break; - - //else we are printing a string - case 2: - - n->opperation = 3; - a.string = string; - a.opperation = 2; - n->printExpression = a; - return n; - break; - - case 3: - - n->opperation = 3; - a.string = string; - a.opperation = 3; - a.value = value; - n->printExpression = a; - return n; - break; - - - - default: - - printf("Something wrong with your print statement\n"); - - } - -} - - -nodeInfo *createLogic(int opperation, int expr, nodeInfo *statement_list, nodeInfo *else_false) { - nodeInfo *n = malloc(sizeof(nodeInfo)); - - switch (opperation) { - //if statement - case 1: - //logical expression, see nodeInfo for opperation info - n->opperation = 4; - logic a; - a.opperation = 1; - a.expr = expr; - - while (statement_list->statement_list != NULL) { - - statement_list = statement_list->statement_list; - - } - - //now that we have the statement list, we need to make the head opperation 5 - nodeInfo *newNode = malloc(sizeof(nodeInfo)); - newNode->opperation = 5; - newNode->next = copyLinkedList(statement_list); - freeLinkedList(statement_list); - a.if_true = newNode; - - //this removes the last statement list in statementHead - statement_list = NULL; - n->logicalExpression = a; - return n; - break; - //while loop - case 2: - - /*n->opperation = 4; - logic b; - b.opperation = 2; - b.expr = expr; - b.while_true = if_true; - - n->logicalExpression = b; - return n;*/ - break; - - default: - printf("check your yacc file for logical statements\n"); - - } - -} - - - -nodeInfo *createCompoundStatement(nodeInfo *n) { - - if(statementHead == NULL) { - statementHead = malloc(sizeof(nodeInfo)); - - nodeInfo *newNode = malloc(sizeof(nodeInfo)); - - statementHead->opperation = 5; - statementHead->next = n; - statementHead->next->next = NULL; - statementHead->statement_list = NULL; - - } else { - - nodeInfo *p = statementHead; - //tereverse the list for the end - while(p->statement_list != NULL) { - - p = p->statement_list; - - } - - p->next = malloc(sizeof(nodeInfo)); - p->next = n; - p->next->next = NULL; - - } - return statementHead; - - -} - /* Adds statement to linked list. @param nodeInfo link to be added. @return head of linked list. */ - -nodeInfo *addToCompoundStatement(nodeInfo *n) { +node_info* add_to_compound_statement(node_info *new_statement, node_info *statement_head) { - if(statementHead == NULL) { + if(statement_head == NULL) { - statementHead = malloc(sizeof(nodeInfo)); - statementHead->opperation = 5; - statementHead->next = n; - statementHead->next->next = NULL; - statementHead->statement_list = NULL; + statement_head = malloc(sizeof(node_info)); + statement_head->opperation = 5; + statement_head->next = new_statement; + statement_head->next->next = NULL; + statement_head->statement_list = NULL; } else { - nodeInfo *p = statementHead; + node_info *p = statement_head; //make a temp of list head, so we can traverse it. + + //TODO replace with double linked list end while (p->statement_list != NULL) { p = p->statement_list; } - + //TODO replace with double linked list end while (p->next != NULL) { p = p->next; } - p->next = malloc(sizeof(nodeInfo)); - p->next = n; + p->next = malloc(sizeof(node_info)); + p->next = new_statement; p->next->next = NULL; } - return statementHead; - + return statement_head; } @@ -376,12 +105,12 @@ Executes a statment. */ //TODO return bool or int detailing the status of execution. -void ex(nodeInfo *n) { +void ex(node_info *n) { switch(n->opperation){ case 1: - addVar(n->var->name, n->var->value); + add_var(n->var->name, n->var->value, _var_map); break; //TODO expression stuff case 2: @@ -405,7 +134,7 @@ void ex(nodeInfo *n) { break; default: - printf("something wrong with your if statement, check yacc file\n"); + printf("{ERROR} CHECK YACC FILE FOR INCORRECT PRINT\n"); } @@ -434,8 +163,9 @@ void ex(nodeInfo *n) { default: - printf("something wrong with logical expression check yacc\n"); - + printf("{ERROR} SOMETHING WRONG WITH LOGICAL EXPRESSION CHECK YACC\n"); + exit(1); + } break; @@ -446,89 +176,90 @@ void ex(nodeInfo *n) { ex(n); } - statementHead = NULL; break; case 6: ; - VariableValues *value = getValue(n->name); + variable_values *value = get_value(n->name, _var_map); switch(value->type) { - case STRINGVAR: + case VAR_STRING: - printf("%s\n", value->String); + printf("%s\n", value->_string); break; - case INT: + case VAR_INT: - printf("%d\n", value->Int); + printf("%d\n", value->_int); break; - case CHARVAR: + case VAR_CHAR: - printf("%c\n", value->Char); + printf("%c\n", value->_char); break; - case DOUBLE: + case VAR_DOUBLE: - printf("%f\n", value->Double); + printf("%f\n", value->_double); + break; + + case VAR_VOID: //TODO add void variable type break; default: - printf("no such variable type called , keep coding!\n"); + printf("{ERROR} NO SUCH VARIABLE TYPE CHECK YACC!\n"); + exit(1); } break; - + + //TODO update default to case 7 for functions default: ; - //TODO add error/exit here - - switch(n->type) { - case FUNCTION_CALL: - ; - nodeInfo *_stmts = *map_get(&*((map_void_t* ) _function_list), n->name); - - while (_stmts->next != NULL) { - - _stmts = _stmts->next; - ex(_stmts); - - - } - break; + switch(n->type) { - case FUNCTION: - - if (_function_list == NULL) { - _function_list = malloc(sizeof(map_void_t)); - n->local_var_list = malloc(sizeof(map_void_t)); + case FUNCTION_CALL: + ; + //gets the statements of a function with the provided name + node_info *_stmts = *map_get(&*((map_void_t* ) _function_list), n->name); + while (_stmts->next != NULL) { //loop over all the statements and execute them + _stmts = _stmts->next; + ex(_stmts); + } + break; - n->next = NULL; - n->local_list = NULL; - map_init(&*((map_void_t *) _function_list)); - map_init(&*((map_void_t *) n->local_var_list)); - map_set(&*((map_void_t* ) _function_list), n->name, &*n->_function_body); - } else { + case FUNCTION: + ; + //if the list of functions already defined is null then we malloc space for it. + if (_function_list == NULL) { + + _function_list = malloc(sizeof(map_void_t)); + n->_var_list = malloc(sizeof(node_info)); + + n->next = NULL; + n->local_list = NULL; + map_init(&*((map_void_t *) _function_list)); + map_init(&*((map_void_t *) n->_var_list)); + //put the function in the hashmap + map_set(&*((map_void_t* ) _function_list), n->name, &*n->_function_body); + } else { //else we dont malloc, we just put the function inside of the hashmap. - map_set(&*((map_void_t* ) _function_list), n->name, &*n->_function_body); + map_set(&*((map_void_t* ) _function_list), n->name, &*n->_function_body); + + } + break; - } - break; - - default: - ; - //TODO add error here and close - - } - - + default: + ; + //TODO add error here and close + } + } -} +} \ No newline at end of file diff --git a/parser/parser.h b/parser/parser.h old mode 100644 new mode 100755 index a80baa9..90c509c --- a/parser/parser.h +++ b/parser/parser.h @@ -1,24 +1,11 @@ -typedef struct assignment assignment; -typedef struct expression expression; -typedef struct print print; -typedef struct logic logic; -typedef struct nodeInfo nodeInfo; +#include "../includes/include.h" -nodeInfo *node(int opperation, char opper, char var, int l, int r); -nodeInfo *createFunctionCall(char *_name); -nodeInfo *createFunction(char *_name, nodeInfo *_stmts); -nodeInfo *createFunctionExpression(int lvalue, int rvalue, char opper); -nodeInfo *createVarAssignmentString(char *name, char *string); -nodeInfo *createVarAssignmentInt(char *name, int number); -nodeInfo *createVarAssignmentChar(char *name, char Char); -nodeInfo *createVarAssignmentDouble(char *name, double Double); -nodeInfo *createPrintVarNode(char *varname); - -//TODO this doesnt belong here, make another folder/file for print -nodeInfo *createPrintStatement(int opperation, int value, char *string); - -//TODO this doesnt belong here, make another folder/file for logic -nodeInfo *createLogic(int opperationm, int expr, nodeInfo *if_true, nodeInfo *else_false); -nodeInfo *createCompoundStatement(nodeInfo *n); -void ex(nodeInfo *n); -int evalExpression(expression *n); \ No newline at end of file +node_info* create_var_assignment_string(char *pName, char *pString); +node_info* create_var_assignment_int(char *pName, int number); +node_info* create_var_assignment_char(char *pName, char Char); +node_info* create_var_assignment_double(char *pName, double Double); +node_info* create_print_var_node(char *pVarname); +node_info* create_print_statement(int opperation, int value, char *string); +node_info* create_logic(int opperation, int expr, node_info *statement_list, node_info *else_false); +node_info* add_to_compound_statement(node_info *new_statement, node_info *statement_head); +void ex(node_info *n); \ No newline at end of file diff --git a/print/print.c b/print/print.c index ccdf0e8..1cdc3c7 100644 --- a/print/print.c +++ b/print/print.c @@ -1 +1,69 @@ -//merge in progress refer to parser.c \ No newline at end of file +#include "../includes/include.h" + +/** + * Creates a print node + * + * @param pVarName name of variable to print + * @return node_info print statement + */ +node_info *create_print_var_node(char *pVarName) { + + node_info *newNode = malloc(sizeof(node_info)); + newNode->opperation = 6; + newNode->name = pVarName; + return newNode; + +} + +/** + * + * @params opperation value, see print struct for info + * @params int value to be printed + * @params string to be printed + * + */ + +node_info *create_print_statement(int opperation, int value, char *string) { + + node_info *n = malloc(sizeof(node_info)); + print a; + switch(opperation) { + + case 1: + + n->opperation = 3;//this needs to not be a magic number lol + a.value = value; + a.opperation = 1; + n->printExpression = a; + return n; + break; + + //else we are printing a string + case 2: + + n->opperation = 3; + a.string = string; + a.opperation = 2; + n->printExpression = a; + return n; + break; + + case 3: + + n->opperation = 3; + a.string = string; + a.opperation = 3; + a.value = value; + n->printExpression = a; + return n; + break; + + + + default: + + printf("{ERROR} PRINT STATEMENT ERROR IN YACC\n"); + + } + +} \ No newline at end of file diff --git a/print/print.h b/print/print.h index ccdf0e8..0f8c9dc 100644 --- a/print/print.h +++ b/print/print.h @@ -1 +1,2 @@ -//merge in progress refer to parser.c \ No newline at end of file +node_info *create_print_var_node(char *pVarName); +node_info *create_print_statement(int opperation, int value, char *string); \ No newline at end of file diff --git a/vars/README.md b/vars/README.md deleted file mode 100644 index 5cd6a14..0000000 --- a/vars/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Info - -This folder contains all data related to varibles. \ No newline at end of file diff --git a/vars/vars.c b/vars/vars.c old mode 100644 new mode 100755 index aa1e338..9d3b858 --- a/vars/vars.c +++ b/vars/vars.c @@ -1,53 +1,10 @@ #include #include #include -#include "../hashmap/map.h" - -/** - * VariableType is a enum of type struct that holds the varible types that will - * be inside of my language. - */ -typedef enum VariableType{ - - STRINGVAR, - INT, - CHARVAR, - DOUBLE - -} VariableType; - -/** - * VariableValues is a struct that holds all possible variable values - */ -typedef struct VariableValues{ - - VariableType type; - char Char; - char *String; - int Int; - double Double; - - -} VariableValues; - -/** - * Variables is a struct that has all the information needed for a variables - */ -typedef struct Variable{ - - char *name; - VariableValues *value; - struct Variable *next; - -} Variable; - - -//TODO this needs to be handled somewhere else. hashmap of all variables -struct map_void_t *varMap = NULL; +#include "../includes/include.h" /** - * * Version: 0.2 * * adds var to hashmap if its not already set @@ -56,34 +13,28 @@ struct map_void_t *varMap = NULL; * @param string for var name * */ - -void addVar(char *name, VariableValues *values) { +void add_var(char *pName, variable_values *pValues, map_void_t* pVarMap) { - if(varMap == NULL) { - - varMap = malloc(sizeof(map_void_t)); - map_init(&*((map_void_t *) varMap)); - - + //if variable map is null + if(pVarMap == NULL) { + pVarMap = malloc(sizeof(map_void_t)); + map_init(&*((map_void_t *) pVarMap)); } - - map_set(&*((map_void_t* ) varMap), name, &*values); - + map_set(&*((map_void_t* ) pVarMap), pName, &*pValues); } /** * returns variables values from hashmap * - * @see [VariableValues] + * @see [variable_values] * @see [char *string] - * @return [VariableValues] containing variables data + * @return [variable_values] containing variables data * @param [char *string] Variable name. * */ - -VariableValues *getValue(char *name) { +variable_values *get_value(char *pName, map_void_t* pVarMap) { - return *map_get(&*((map_void_t* ) varMap), name); + return *map_get(&*((map_void_t* ) pVarMap), pName); } @@ -94,51 +45,68 @@ VariableValues *getValue(char *name) { * @return the head of the linked list which just points to all of the vars * */ - -Variable* makeVariableInt(char *name, int Int) { +variable* make_variable_int(char *pName, int value) { - Variable *p = malloc(sizeof(Variable)); - VariableValues *IntegerValue = malloc(sizeof(VariableValues)); - IntegerValue->type = INT; - IntegerValue->Int = Int; - p->name = name; - p->value = IntegerValue; + variable *p = malloc(sizeof(variable)); + variable_values *int_value = malloc(sizeof(variable_values)); + int_value->type = VAR_INT; + int_value->_int = value; + p->name = pName; + p->value = int_value; return p; } -Variable* makeVariableDouble(char *name, double Double) { + +/** + * makes a variable of type double. + * @param name of the varible. + * @param double data to be stored in the variable. + */ +variable* make_variable_double(char *pName, double value) { - Variable *p = malloc(sizeof(Variable)); - VariableValues *DoubleValue= malloc(sizeof(VariableValues)); - DoubleValue->type = DOUBLE; - DoubleValue->Double = Double; - p->name = name; - p->value = DoubleValue; - return p; + variable *new_var = malloc(sizeof(variable)); + variable_values *double_value = malloc(sizeof(variable_values)); + double_value->type = VAR_DOUBLE; + double_value->_double = value; + new_var->name = pName; + new_var->value = double_value; + return new_var; } -Variable* makeVariableChar(char *name, char Char) { +/** + * makes a char variable. + * + * @param name of the variable. + * @param char value to be stored. + */ +variable* make_variable_char(char *pName, char value) { - Variable *p = malloc(sizeof(Variable)); - VariableValues *CharacterValue = malloc(sizeof(VariableValues)); - CharacterValue->type = CHARVAR; - CharacterValue->Char = Char; - p->name = name; - p->value = CharacterValue; + variable *p = malloc(sizeof(variable)); + variable_values *char_value = malloc(sizeof(variable_values)); + char_value->type = VAR_CHAR; + char_value->_char = value; + p->name = pName; + p->value = char_value; return p; } -Variable* makeVariableString(char *name, char *String){ +/** + * makes a variable of type string. + * + * @param name of the variable. + * @param value to be stored. + */ +variable* make_variable_string(char *pName, char *value) { - Variable *p = malloc(sizeof(Variable)); - VariableValues *StringValue = malloc(sizeof(VariableValues)); - StringValue->type = STRINGVAR; - StringValue->String = String; - p->name = name; - p->value = StringValue; + variable *p = malloc(sizeof(variable)); + variable_values *string_value = malloc(sizeof(variable_values)); + string_value->type = VAR_STRING; + string_value->_string = value; + p->name = pName; + p->value = string_value; return p; } diff --git a/vars/vars.h b/vars/vars.h old mode 100644 new mode 100755 index 950b156..75bc7fc --- a/vars/vars.h +++ b/vars/vars.h @@ -1,9 +1,6 @@ -#include "vars.c" - -typedef enum VariableType VariableType; -typedef struct VariableValues VariableValues; -typedef struct Variable Variable; - -//vars functions -void addVar(char *name, VariableValues *values); -VariableValues *getValue(char *name); \ No newline at end of file +void add_var(char *name, variable_values *values, map_void_t* pVarMap); +variable_values *get_value(char *pName, map_void_t* pVarMap); +variable* make_variable_int(char *pName, int value); +variable* make_variable_double(char *pName, double value); +variable* make_variable_char(char *pName, char value); +variable* make_variable_string(char *pName, char *value); \ No newline at end of file