Finished cleaning, found/documented issues.

master
xerox 5 years ago
parent 14240512e5
commit 929e257a8e

@ -1,24 +1,7 @@
# Deon programming language V0.9.2 # Deon programming language V0.9.2.1
My computer programming language made with LEX/YACC written in C. (interpreted) My computer programming language made with LEX/YACC written in C. (interpreted)
V0.9.2 Update notes:
* major cleaning of the project
* issues found and documented
V0.9.1 Update notes:
* Prep for doubly linked lists
* Prep for VOID data type
* Prep for merge/clean
V0.8
* Prep for TCP/IP implementation
* Prep for VOID data type
# Hello World. # Hello World.
``` ```
def Main : { def Main : {
printf[ "hello world" ]; printf[ "hello world" ];
@ -26,12 +9,9 @@ def Main : {
call Main; call Main;
``` ```
Requirements to making a program: Requirements to making a program:
1.) a struct/function. 1.) a struct/function.
2.) a call statement. 2.) a call statement.
# Variables # Variables
Variables are stored in a hashmap for quick and easy access. Variables also support type juggling. When you define a variable its type will be assumed when parsed. Variables are stored in a hashmap for quick and easy access. Variables also support type juggling. When you define a variable its type will be assumed when parsed.
@ -42,34 +22,6 @@ mystring = "cool string";
mychar = 'c'; // char mychar = 'c'; // char
myfunction = { printf[ "hello world" ]; }; (statement list) myfunction = { printf[ "hello world" ]; }; (statement list)
``` ```
Variables are scoped to the struct that they are defined in. This is so everything is not global. An example of this would look:
```
def MyDog : {
age = -153000;
name = "i dont have a name";
printfMyName = {
printf[ name ];
};
};
def Main : {
someOtherAge = 2;
myRealDogsName = "cat"; // because variable naming is the hardest thing in computer science.
printfMyName = {
printf[ someOtherAge ];
};
call MyDog::printfMyName;
/*
When you do not use the scope resolution modifier then it will only check local functions.
The local function hashmap is stored in the head of your functions linked list.
/*
call printfMyName;
};
call Main;
```
# Logic # Logic
Logic is first evaluated and if deemed valid then a list of statements will be executed. Logic is first evaluated and if deemed valid then a list of statements will be executed.
``` ```
@ -82,18 +34,14 @@ if [ {int} newInt > 99 ]: {
printf [ "newInt is bigger than 99" ]; printf [ "newInt is bigger than 99" ];
}; };
``` ```
# Functions # Functions
Functions can be declared inside of a struct and accessed by anyone who uses the scope resolution operator. Do not mistake structs as functions just because you can interact with them in the same way. Functions do not have "scope" like structs do. Functions can interact with all elements in the parenting struct including other functions, and those functions local variables. Functions can be declared inside of a struct and accessed by anyone who uses the scope resolution operator. Do not mistake structs as functions just because you can interact with them in the same way. Functions do not have "scope" like structs do. Functions can interact with all elements in the parenting struct including other functions, and those functions local variables.
``` ```
def NewStruct : { def NewStruct : {
someFunction = { someFunction = {
printf[ "hello world" ]; printf[ "hello world" ];
}; };
}; };
call NewStruct; call NewStruct;
``` ```

@ -43,43 +43,38 @@ int yywrap(void) {
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[])
{
FILE *fh; FILE *fh;
if (argc > 1)
if (argc > 1) { {
if (strcmp(argv[1],"help") == 0 || strcmp(argv[1],"-help") == 0 if (strcmp(argv[1],"help") == 0 || strcmp(argv[1],"-help") == 0
|| strcmp(argv[1],"--help") == 0 || strcmp(argv[1],"h") == 0 || strcmp(argv[1],"--help") == 0 || strcmp(argv[1],"h") == 0
|| strcmp(argv[1],"-h") == 0) { || strcmp(argv[1],"-h") == 0)
{
printf("[ HELP ] put a file/path as the first flag.\n"); printf("[ HELP ] put a file/path as the first flag.\n");
printf("[ HELP ] -h, -help, --help, help, h: Prints this.\n"); printf("[ HELP ] -h, -help, --help, help, h: Prints this.\n");
printf("[ HELP ] -d, d, -debug, debug: Enter REPL mode.\n"); printf("[ HELP ] -d, d, -debug, debug: Enter REPL mode.\n");
}
} else if (strcmp(argv[1],"-debug") == 0 || strcmp(argv[1],"-d") == 0 else if (strcmp(argv[1],"-debug") == 0 || strcmp(argv[1],"-d") == 0
|| strcmp(argv[1],"d") == 0 || strcmp(argv[1],"debug") == 0) { || strcmp(argv[1],"d") == 0 || strcmp(argv[1],"debug") == 0)
{
_DEBUG = 1; _DEBUG = 1;
printf(">>> "); printf(">>> ");
yyparse(); yyparse();
}
} else if (fh = fopen(argv[1], "r")){ else if (fh = fopen(argv[1], "r"))
{
yyin = fh; yyin = fh;
yyparse(); yyparse();
fclose(fh); fclose(fh);
}
} else { else
{
printf("[ ERROR ] please enter a correct command. try --help\n"); printf("[ ERROR ] please enter a correct command. try --help\n");
fclose(fh);
} }
} else
} else {
printf("[ ERROR ] missing input file. Try --help?\n"); printf("[ ERROR ] missing input file. Try --help?\n");
}
return 0; return 0;
} }

@ -126,8 +126,6 @@ print:
%% %%
void yyerror(char *s) { void yyerror(char *s) {
fprintf(stderr, "Error on line %d, %s\n", yylineno, s); fprintf(stderr, "Error on line %d, %s\n", yylineno, s);
} }

@ -13,7 +13,6 @@
* @return nodeInfo * to head * @return nodeInfo * to head
*/ */
node_info *create_function(char *pName, node_info *pStmts) { node_info *create_function(char *pName, node_info *pStmts) {
node_info *newNode = malloc(sizeof(node_info)); node_info *newNode = malloc(sizeof(node_info));
newNode->type = FUNCTION; newNode->type = FUNCTION;
newNode->name = pName; newNode->name = pName;
@ -21,7 +20,6 @@ node_info *create_function(char *pName, node_info *pStmts) {
newNode->_function_body->next = copy_linked_list(pStmts); newNode->_function_body->next = copy_linked_list(pStmts);
_free_linked_list(pStmts); _free_linked_list(pStmts);
return newNode; return newNode;
} }
/** /**
@ -31,7 +29,6 @@ node_info *create_function(char *pName, node_info *pStmts) {
* @return a node_info pointer to the head of the function * @return a node_info pointer to the head of the function
*/ */
node_info *create_function_call(char *pName) { node_info *create_function_call(char *pName) {
node_info *newNode = malloc(sizeof(node_info)); node_info *newNode = malloc(sizeof(node_info));
newNode->type = FUNCTION_CALL; newNode->type = FUNCTION_CALL;
newNode->name = pName; newNode->name = pName;
@ -49,17 +46,13 @@ node_info *create_compound_statement(node_info *new_statement_list, node_info *s
if(statement_head == NULL) { if(statement_head == NULL) {
statement_head = malloc(sizeof(node_info)); statement_head = malloc(sizeof(node_info));
node_info *newNode = malloc(sizeof(node_info)); node_info *newNode = malloc(sizeof(node_info));
statement_head->opperation = 5; statement_head->opperation = 5;
statement_head->next = new_statement_list; statement_head->next = new_statement_list;
statement_head->next->next = NULL; statement_head->next->next = NULL;
statement_head->statement_list = NULL; statement_head->statement_list = NULL;
} else { } else {
node_info *p = statement_head;
node_info *p = statement_head; //make a temp of list head, so we can traverse it.
//TODO update with doubly linked list //TODO update with doubly linked list
while(p->statement_list != NULL) while(p->statement_list != NULL)
p = p->statement_list; p = p->statement_list;
@ -70,5 +63,4 @@ node_info *create_compound_statement(node_info *new_statement_list, node_info *s
} }
return statement_head; return statement_head;
} }

@ -1,5 +1,8 @@
#ifndef FUNCTIONS
#define FUNCTIONS
#include "../includes/include.h" #include "../includes/include.h"
node_info *create_function(char *pName, node_info *pStmts); node_info *create_function(char *pName, node_info *pStmts);
node_info *create_function_call(char *pName); node_info *create_function_call(char *pName);
node_info *create_compound_statement(node_info *new_statement_list, node_info *statement_head); node_info *create_compound_statement(node_info *new_statement_list, node_info *statement_head);
#endif

@ -9,44 +9,34 @@
* VariableType is a enum of varible types that will be inside of my language. * VariableType is a enum of varible types that will be inside of my language.
*/ */
typedef enum variable_type { typedef enum variable_type {
VAR_STRING, VAR_STRING,
VAR_INT, VAR_INT,
VAR_CHAR, VAR_CHAR,
VAR_DOUBLE, VAR_DOUBLE,
VAR_VOID VAR_VOID
} variable_type; } variable_type;
/** /**
* VariableValues is a struct that holds all possible variable values * VariableValues is a struct that holds all possible variable values
*/ */
typedef struct variable_values { typedef struct variable_values {
variable_type type; variable_type type;
char _char; char _char;
char *_string; char *_string;
int _int; int _int;
double _double; double _double;
void* _void_pointer; //TODO create a concept of "void" void* _void_pointer; //TODO create a concept of "void"
} variable_values; } variable_values;
/** /**
* Variables is a struct that has all the information needed for a variables * Variables is a struct that has all the information needed for a variables
*/ */
typedef struct variable{ typedef struct variable{
char *name; char *name;
variable_values *value; variable_values *value;
struct Variable *next; struct Variable *next;
} variable; } variable;
typedef struct print { typedef struct print {
int opperation; int opperation;
@ -56,20 +46,16 @@ typedef struct print {
} print; } print;
typedef struct logic { typedef struct logic {
int opperation; int opperation;
int expr; int expr;
struct node_info *if_true; struct node_info *if_true;
struct node_info *else_false; struct node_info *else_false;
struct node_info *while_true; struct node_info *while_true;
} logic; } logic;
typedef struct node_info { typedef struct node_info {
int opperation;
int opperation;
enum type{ enum type{
ASSIGNMENT, ASSIGNMENT,
@ -80,8 +66,6 @@ typedef struct node_info {
FUNCTION_CALL FUNCTION_CALL
}type; }type;
char *name; char *name;
variable *var; variable *var;
print printExpression; print printExpression;
@ -91,8 +75,5 @@ typedef struct node_info {
struct node_info *statement_list; struct node_info *statement_list;
struct node_info *local_list; struct node_info *local_list;
struct map_void_t *_var_list; struct map_void_t *_var_list;
} node_info; } node_info;
#endif #endif

@ -1,5 +1,4 @@
#include <stdlib.h> #include <stdlib.h>
#include "linked_list.h" #include "linked_list.h"
/** /**
@ -7,18 +6,13 @@
* @param head of linked list to be freed * @param head of linked list to be freed
* @return void * @return void
*/ */
void _free_linked_list(node_info *pList) { void _free_linked_list(node_info *p_list) {
node_info *temp; node_info *temp;
while(p_list->next != NULL) {
while(pList->next != NULL) { temp = p_list;
p_list = p_list->next;
temp = pList;
pList = pList->next;
free(temp); free(temp);
} }
} }
/** /**
@ -26,19 +20,14 @@ void _free_linked_list(node_info *pList) {
* @param nodeInfo list to be copied * @param nodeInfo list to be copied
* @return copied list * @return copied list
*/ */
node_info *copy_linked_list(node_info *pList) { node_info *copy_linked_list(node_info *p_list) {
node_info *new_list = malloc(sizeof(node_info));
node_info *newList = malloc(sizeof(node_info)); new_list->next = NULL;
newList->next = NULL; node_info *copy_list = new_list;
node_info *copy_list = newList; while(p_list->next != NULL) {
while(pList->next != NULL) { p_list = p_list->next;
copy_list->next = p_list;
pList = pList->next;
copy_list->next = pList;
copy_list = copy_list->next; copy_list = copy_list->next;
} }
return new_list;
return newList;
} }

@ -1,4 +1,7 @@
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include "../includes/include.h" #include "../includes/include.h"
void _free_linked_list(node_info *pList); void _free_linked_list(node_info *pList);
node_info *copy_linked_list(node_info *pList); node_info *copy_linked_list(node_info *pList);
#endif

@ -1,39 +1,30 @@
#include "../includes/include.h" #include "../includes/include.h"
node_info *create_logic(int opperation, int expr, node_info *statement_list, node_info *else_false) { /*
* WARNING! do not use this function, it is old and outdated, and is missing alot of new features!
*/
node_info *create_logic(int opperation, int expr, node_info *statement_list, node_info *else_false)
{
node_info *n = malloc(sizeof(node_info)); node_info *n = malloc(sizeof(node_info));
switch (opperation)
switch (opperation) { {
//if statement case 1: //if statement
case 1:
//logical expression, see node_info for opperation info
n->opperation = 4; n->opperation = 4;
logic a; logic a;
a.opperation = 1; a.opperation = 1;
a.expr = expr; a.expr = expr;
while (statement_list->statement_list)
while (statement_list->statement_list != NULL) {
statement_list = statement_list->statement_list; 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)); node_info *newNode = malloc(sizeof(node_info));
newNode->opperation = 5; newNode->opperation = 5;
newNode->next = copy_linked_list(statement_list); newNode->next = copy_linked_list(statement_list);
_free_linked_list(statement_list); _free_linked_list(statement_list);
a.if_true = newNode; a.if_true = newNode;
//this removes the last statement list in statementHead
statement_list = NULL; statement_list = NULL;
n->logicalExpression = a; n->logicalExpression = a;
return n; return n;
break;
//while loop case 2: //while loop
case 2:
/*n->opperation = 4; /*n->opperation = 4;
logic b; logic b;
b.opperation = 2; b.opperation = 2;
@ -43,10 +34,8 @@ node_info *create_logic(int opperation, int expr, node_info *statement_list, nod
n->logicalExpression = b; n->logicalExpression = b;
return n;*/ return n;*/
break; break;
default: default:
printf("check your yacc file for logical statements\n"); printf("check your yacc file for logical statements\n");
} }
} }

@ -1 +1,6 @@
node_info *createLogic(int opperation, int expr, node_info *statement_list, node_info *else_false); #ifndef LOGIC_H
#define LOGIC_H
node_info *createLogic(int opperation, int expr, node_info *statement_list, node_info *else_false);
#endif

@ -1,12 +0,0 @@
#include <stdlib.h>
#include "socket.h"
int main() {
Socket news = create_lsocket(5001, 5);
printf("Created socket!\n");
sleep(10);
return 0;
}

@ -1,134 +0,0 @@
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <strings.h>
/**
*
* socket struct for building sockets...
* This will hold both listening sockets and connection sockets
* TCP AND UDP!
*
*/
typedef struct Socket {
int socketfd; //socket id
int client_socketfd; // this is the int you get when you call accept()
int port; // port information
struct sockaddr_in address; // sockaddr_in struct
struct Socket *next; // next in the linked list
} Socket;
//head of the linked list of sockets
struct Socket *_socket_head = NULL;
/**
* This is used by a thread to listen for incoming
* connections
*
*/
void doprocessing (int sock) {
int n;
char buffer[256];
bzero(buffer,256);
while(1) {
n = read(sock,buffer,255);
if (n < 0) {
perror("ERROR reading from socket");
exit(1);
}
printf("Here is the message: %s\n",buffer);
n = write(sock,"I got your message",18);
if (n < 0) {
perror("ERROR writing to socket");
exit(1);
}
}
}
/**
*
* This function creates a listening function...
* @params takes a an int for a port.
* @return An active listening socket for incoming connections
*
*/
Socket create_lsocket(int _port, int _connection_amount) {
Socket *newSocket = malloc(sizeof(Socket));
//sockaddr_in struct stuff (making a new sockaddr_in and giving it details)
struct sockaddr_in address; // make a struct of type sockaddr_in
int addr_size = sizeof(address); // size of addess in decimal form
// filling our struct with info
address.sin_family = AF_INET; //ipv4....
address.sin_addr.s_addr = INADDR_ANY; // any local address I.E :0.0.0.0
address.sin_port = htons(_port); // htons takes a decimal number and converts it to network
//this is a socket handler, like a file handler...
int socketfd = socket(AF_INET, SOCK_STREAM, 0);
// if the socks returns 0 then exit
if (socketfd == 0) {
printf("SOCKET FAILED!\n");
exit(1);
}
//lets bind that socket now
int bindRes = bind(socketfd, (struct sockaddr *)&address, sizeof(address));
if(bindRes < 0) {
printf("BIND FAILED! WITH CODE: %d\n", bindRes);
exit(1);
}
listen(socketfd, 5);
while(1) {
int newsockfd = accept(socketfd, (struct sockaddr *) &address, &addr_size);
if (newsockfd < 0) {
printf("ERROR LISTENING!\n");
exit(1);
}
int pid = fork();
if (pid == 0) {
printf("NEW CLIENT!\n");
doprocessing(newsockfd);
exit(0);
}
}
newSocket->socketfd = socketfd;
newSocket->port = _port;
newSocket->address = address;
newSocket->next = NULL;
// if the socket head is empty we are going to malloc the head and then start
// the linked list of sockets
if (_socket_head == NULL) {
_socket_head = malloc(sizeof(Socket));
_socket_head->next = newSocket;
}
}

@ -1,17 +0,0 @@
socket = l_SOCKET("0.0.0.0", 90); //creates a server socket
client = C_SOCKET("127.0.0.1", 90); // creates a client
socket.template = {
"HEADER-DATA" : varToSaveData : call responce;,
"SOMEOTHER-HEADER" : varToSaveData : call responce;
}
client.plan = {
}

@ -1,87 +0,0 @@
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <strings.h>
void doprocessing (int sock) {
int n;
char buffer[256];
bzero(buffer,256);
while(1) {
n = read(sock,buffer,255);
if (n < 0) {
perror("ERROR reading from socket");
exit(1);
}
printf("Here is the message: %s\n",buffer);
n = write(sock,"I got your message",18);
if (n < 0) {
perror("ERROR writing to socket");
exit(1);
}
}
}
int main() {
int port = 5001;
struct sockaddr_in address;
int clilen = sizeof(address);
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
int socketHandler = socket(AF_INET, SOCK_STREAM, 0);
if (socketHandler == 0) {
printf("SOCKET FAILED!\n");
return 1;
} else {
printf("SETUP SOCKET!\n");
}
int bindRes = bind(socketHandler, (struct sockaddr *)&address, sizeof(address));
if(bindRes < 0) {
printf("BIND FAILED! CODE: %d\n", bindRes);
} else {
printf("BINDED SOCKET!\n");
}
listen(socketHandler, 5);
while(1) {
int newsockfd = accept(socketHandler, (struct sockaddr *) &address, &clilen);
if (newsockfd < 0) {
printf("ERROR LISTENING!\n");
exit(1);
}
int pid = fork();
if (pid == 0) {
printf("NEW CLIENT!\n");
doprocessing(newsockfd);
exit(0);
}
}
return 0;
}

@ -5,53 +5,44 @@
//hashmaps/link list heads //hashmaps/link list heads
extern map_void_t *_var_map; extern map_void_t *_var_map;
extern node_info *_temp_statement_head; extern node_info *_temp_statement_head;
//struct variable *_var_head = NULL; //struct variable *_var_head = NULL;
extern map_void_t *_function_list; extern map_void_t *_function_list;
node_info* create_var_assignment_string(char *pName, char *pString) { node_info* create_var_assignment_string(char *p_name, char *value) {
node_info *new_node = malloc(sizeof(node_info));
node_info *newNode = malloc(sizeof(node_info)); variable *new_var = make_variable_string(p_name, value);
variable *newVar = make_variable_string(pName, pString);
newNode->type = ASSIGNMENT; newNode->type = ASSIGNMENT;
newNode->opperation = 1; newNode->opperation = 1;
newNode->var = newVar; newNode->var = new_var;
return newNode; return new_node;
} }
node_info* create_var_assignment_int(char *pName, int number) { node_info* create_var_assignment_int(char *p_name, int value) {
node_info *new_node = malloc(sizeof(node_info));
node_info *newNode = malloc(sizeof(node_info)); variable *new_var = make_variable_int(p_name, value);
variable *newVar = make_variable_int(pName, number);
newNode->type = ASSIGNMENT; newNode->type = ASSIGNMENT;
newNode->opperation = 1; newNode->opperation = 1;
newNode->var = newVar; newNode->var = new_var;
return newNode; return new_node;
} }
node_info* create_var_assignment_char(char *pName, char Char) { node_info* create_var_assignment_char(char *p_name, char value) {
node_info *new_node = malloc(sizeof(node_info));
node_info *newNode = malloc(sizeof(node_info)); variable *new_var = make_variable_char(p_name, value);
variable *newVar = make_variable_char(pName, Char);
newNode->type = ASSIGNMENT; newNode->type = ASSIGNMENT;
newNode->opperation = 1; newNode->opperation = 1;
newNode->var = newVar; newNode->var = new_var;
return newNode; return new_node;
} }
node_info* create_var_assignment_double(char *pName, double Double) { node_info* create_var_assignment_double(char *p_name, double value) {
node_info *new_node = malloc(sizeof(node_info));
node_info *newNode = malloc(sizeof(node_info)); variable *new_var = make_variable_double(p_name, value);
variable *newVar = make_variable_double(pName, Double);
//new node stuff //new node stuff
newNode->type = ASSIGNMENT; newNode->type = ASSIGNMENT;
newNode->opperation = 1; newNode->opperation = 1;
newNode->var = newVar; newNode->var = newVar;
return newNode; return new_node;
} }
@ -74,27 +65,20 @@ node_info* add_to_compound_statement(node_info *new_statement, node_info *statem
} else { } else {
node_info *p = statement_head; //make a temp of list head, so we can traverse it. node_info *p = statement_head; //make a temp of list head, so we can traverse it.
//TODO replace with double linked list end //TODO replace with double linked list end
while (p->statement_list != NULL) { while (p->statement_list)
p = p->statement_list; p = p->statement_list;
}
//TODO replace with double linked list end //TODO replace with double linked list end
while (p->next != NULL) { while (p->next)
p = p->next; p = p->next;
}
p->next = malloc(sizeof(node_info)); p->next = malloc(sizeof(node_info));
p->next = new_statement; p->next = new_statement;
p->next->next = NULL; p->next->next = NULL;
} }
return statement_head; return statement_head;
} }
/* /*
@ -105,21 +89,18 @@ Executes a statment.
*/ */
//TODO return bool or int detailing the status of execution. //TODO return bool or int detailing the status of execution.
void ex(node_info *n) { void ex(node_info *n)
{
switch(n->opperation){ switch(n->opperation)
{
case 1: case 1:
add_var(n->var->name, n->var->value, _var_map); add_var(n->var->name, n->var->value, _var_map);
break; break;
//TODO expression stuff //TODO expression stuff
case 2: case 2:
break; break;
//print //print
case 3: case 3:
switch(n->printExpression.opperation) { switch(n->printExpression.opperation) {
case 1: case 1:
@ -128,102 +109,64 @@ void ex(node_info *n) {
case 2: case 2:
printf("%s\n", n->printExpression.string); printf("%s\n", n->printExpression.string);
break; break;
case 3: case 3:
printf("%s%lf\n", n->printExpression.string, n->printExpression.value); printf("%s%lf\n", n->printExpression.string, n->printExpression.value);
break; break;
default: default:
printf("{ERROR} CHECK YACC FILE FOR INCORRECT PRINT\n"); printf("{ERROR} CHECK YACC FILE FOR INCORRECT PRINT\n");
} }
break; break;
//logic (if, while, for, etc) //logic (if, while, for, etc)
case 4: case 4:
switch(n->logicalExpression.opperation) { switch(n->logicalExpression.opperation) {
case 1: case 1:
if (n->logicalExpression.expr == 1)
if (n->logicalExpression.expr == 1) {
ex(n->logicalExpression.if_true); ex(n->logicalExpression.if_true);
}
break; break;
case 2: case 2:
while (n->logicalExpression.expr == 1)
while (n->logicalExpression.expr == 1) {
ex(n->logicalExpression.while_true); ex(n->logicalExpression.while_true);
}
break; break;
default: default:
printf("{ERROR} SOMETHING WRONG WITH LOGICAL EXPRESSION CHECK YACC\n"); printf("{ERROR} SOMETHING WRONG WITH LOGICAL EXPRESSION CHECK YACC\n");
exit(1);
} }
break; break;
case 5: case 5:
while(n->next != NULL) { while(n->next != NULL)
{
n = n->next; n = n->next;
ex(n); ex(n);
} }
break; break;
case 6: case 6:
; ;
variable_values *value = get_value(n->name, _var_map); variable_values *value = get_value(n->name, _var_map);
switch(value->type)
switch(value->type) { {
case VAR_STRING:
case VAR_STRING:
printf("%s\n", value->_string); printf("%s\n", value->_string);
break; break;
case VAR_INT: case VAR_INT:
printf("%d\n", value->_int); printf("%d\n", value->_int);
break; break;
case VAR_CHAR: case VAR_CHAR:
printf("%c\n", value->_char); printf("%c\n", value->_char);
break; break;
case VAR_DOUBLE: case VAR_DOUBLE:
printf("%f\n", value->_double); printf("%f\n", value->_double);
break; break;
case VAR_VOID: //TODO add void variable type case VAR_VOID: //TODO add void variable type
break; break;
default: default:
printf("{ERROR} NO SUCH VARIABLE TYPE CHECK YACC!\n"); printf("{ERROR} NO SUCH VARIABLE TYPE CHECK YACC!\n");
exit(1);
} }
break; break;
//TODO update default to case 7 for functions //TODO update default to case 7 for functions
default: default:
; ;
switch(n->type)
switch(n->type) { {
case FUNCTION_CALL: case FUNCTION_CALL:
; ;
//gets the statements of a function with the provided name //gets the statements of a function with the provided name
@ -233,33 +176,26 @@ void ex(node_info *n) {
ex(_stmts); ex(_stmts);
} }
break; break;
case FUNCTION: case FUNCTION:
; ;
//if the list of functions already defined is null then we malloc space for it. //if the list of functions already defined is null then we malloc space for it.
if (_function_list == NULL) { if (!_function_list)
{
_function_list = malloc(sizeof(map_void_t)); _function_list = malloc(sizeof(map_void_t));
n->_var_list = malloc(sizeof(node_info)); n->_var_list = malloc(sizeof(node_info));
n->next = NULL; n->next = NULL;
n->local_list = NULL; n->local_list = NULL;
map_init(&*((map_void_t *) _function_list)); map_init(&*((map_void_t *) _function_list));
map_init(&*((map_void_t *) n->_var_list)); map_init(&*((map_void_t *) n->_var_list));
//put the function in the hashmap //put the function in 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);
} else { //else we dont malloc, we just put the function inside of the hashmap. } 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: default:
; ;
//TODO add error here and close //TODO add error here and close
} }
} }
} }

@ -1,3 +1,5 @@
#ifndef PARSER_H
#define PARSER_H
#include "../includes/include.h" #include "../includes/include.h"
node_info* create_var_assignment_string(char *pName, char *pString); node_info* create_var_assignment_string(char *pName, char *pString);
@ -8,4 +10,5 @@ node_info* create_print_var_node(char *pVarname);
node_info* create_print_statement(int opperation, int value, char *string); 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* 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); node_info* add_to_compound_statement(node_info *new_statement, node_info *statement_head);
void ex(node_info *n); void ex(node_info *n);
#endif

@ -6,13 +6,12 @@
* @param pVarName name of variable to print * @param pVarName name of variable to print
* @return node_info print statement * @return node_info print statement
*/ */
node_info *create_print_var_node(char *pVarName) { node_info *create_print_var_node(char *p_var_name)
{
node_info *newNode = malloc(sizeof(node_info)); node_info *new_node = malloc(sizeof(node_info));
newNode->opperation = 6; new_node->opperation = 6;
newNode->name = pVarName; new_node->name = p_var_name;
return newNode; return new_node;
} }
/** /**
@ -23,47 +22,33 @@ node_info *create_print_var_node(char *pVarName) {
* *
*/ */
node_info *create_print_statement(int opperation, int value, char *string) { node_info *create_print_statement(int opperation, int value, char *string)
{
node_info *n = malloc(sizeof(node_info)); node_info *n = malloc(sizeof(node_info));
print a; print a;
switch(opperation) { switch(opperation)
{
case 1: case 1:
n->opperation = 3;//this needs to not be a magic number lol n->opperation = 3;//this needs to not be a magic number lol
a.value = value; a.value = value;
a.opperation = 1; a.opperation = 1;
n->printExpression = a; n->printExpression = a;
return n; return n;
break;
//else we are printing a string //else we are printing a string
case 2: case 2:
n->opperation = 3; n->opperation = 3;
a.string = string; a.string = string;
a.opperation = 2; a.opperation = 2;
n->printExpression = a; n->printExpression = a;
return n; return n;
break;
case 3: case 3:
n->opperation = 3; n->opperation = 3;
a.string = string; a.string = string;
a.opperation = 3; a.opperation = 3;
a.value = value; a.value = value;
n->printExpression = a; n->printExpression = a;
return n; return n;
break;
default: default:
printf("{ERROR} PRINT STATEMENT ERROR IN YACC\n"); printf("{ERROR} PRINT STATEMENT ERROR IN YACC\n");
} }
} }

@ -1,2 +1,5 @@
#ifndef PRINT_H
#define PRINT_H
node_info *create_print_var_node(char *pVarName); node_info *create_print_var_node(char *pVarName);
node_info *create_print_statement(int opperation, int value, char *string); node_info *create_print_statement(int opperation, int value, char *string);
#endif

@ -13,14 +13,14 @@
* @param string for var name * @param string for var name
* *
*/ */
void add_var(char *pName, variable_values *pValues, map_void_t* pVarMap) { void add_var(char *p_name, variable_values *p_values, map_void_t* p_var_map)
{
//if variable map is null //if variable map is null
if(pVarMap == NULL) { if(p_var_map == NULL) {
pVarMap = malloc(sizeof(map_void_t)); p_var_map = malloc(sizeof(map_void_t));
map_init(&*((map_void_t *) pVarMap)); map_init(&*((map_void_t *) p_var_map));
} }
map_set(&*((map_void_t* ) pVarMap), pName, &*pValues); map_set(&*((map_void_t* ) p_var_map), p_name, &*p_values);
} }
/** /**
@ -32,10 +32,9 @@ void add_var(char *pName, variable_values *pValues, map_void_t* pVarMap) {
* @param [char *string] Variable name. * @param [char *string] Variable name.
* *
*/ */
variable_values *get_value(char *pName, map_void_t* pVarMap) { variable_values *get_value(char *pName, map_void_t* pVarMap)
{
return *map_get(&*((map_void_t* ) pVarMap), pName); return *map_get(&*((map_void_t* ) pVarMap), pName);
} }
/** /**
@ -45,16 +44,15 @@ variable_values *get_value(char *pName, map_void_t* pVarMap) {
* @return the head of the linked list which just points to all of the vars * @return the head of the linked list which just points to all of the vars
* *
*/ */
variable* make_variable_int(char *pName, int value) { variable* make_variable_int(char *p_name, int value)
{
variable *p = malloc(sizeof(variable)); variable *p = malloc(sizeof(variable));
variable_values *int_value = malloc(sizeof(variable_values)); variable_values *int_value = malloc(sizeof(variable_values));
int_value->type = VAR_INT; int_value->type = VAR_INT;
int_value->_int = value; int_value->_int = value;
p->name = pName; p->name = p_name;
p->value = int_value; p->value = int_value;
return p; return p;
} }
@ -63,16 +61,15 @@ variable* make_variable_int(char *pName, int value) {
* @param name of the varible. * @param name of the varible.
* @param double data to be stored in the variable. * @param double data to be stored in the variable.
*/ */
variable* make_variable_double(char *pName, double value) { variable* make_variable_double(char *p_name, double value)
{
variable *new_var = malloc(sizeof(variable)); variable *new_var = malloc(sizeof(variable));
variable_values *double_value = malloc(sizeof(variable_values)); variable_values *double_value = malloc(sizeof(variable_values));
double_value->type = VAR_DOUBLE; double_value->type = VAR_DOUBLE;
double_value->_double = value; double_value->_double = value;
new_var->name = pName; new_var->name = p_name;
new_var->value = double_value; new_var->value = double_value;
return new_var; return new_var;
} }
/** /**
@ -81,16 +78,15 @@ variable* make_variable_double(char *pName, double value) {
* @param name of the variable. * @param name of the variable.
* @param char value to be stored. * @param char value to be stored.
*/ */
variable* make_variable_char(char *pName, char value) { variable* make_variable_char(char *p_name, char value)
{
variable *p = malloc(sizeof(variable)); variable *p = malloc(sizeof(variable));
variable_values *char_value = malloc(sizeof(variable_values)); variable_values *char_value = malloc(sizeof(variable_values));
char_value->type = VAR_CHAR; char_value->type = VAR_CHAR;
char_value->_char = value; char_value->_char = value;
p->name = pName; p->name = p_name;
p->value = char_value; p->value = char_value;
return p; return p;
} }
/** /**
@ -99,14 +95,13 @@ variable* make_variable_char(char *pName, char value) {
* @param name of the variable. * @param name of the variable.
* @param value to be stored. * @param value to be stored.
*/ */
variable* make_variable_string(char *pName, char *value) { variable* make_variable_string(char *p_name, char *value)
{
variable *p = malloc(sizeof(variable)); variable *p = malloc(sizeof(variable));
variable_values *string_value = malloc(sizeof(variable_values)); variable_values *string_value = malloc(sizeof(variable_values));
string_value->type = VAR_STRING; string_value->type = VAR_STRING;
string_value->_string = value; string_value->_string = value;
p->name = pName; p->name = p_name;
p->value = string_value; p->value = string_value;
return p; return p;
} }

@ -1,6 +1,9 @@
#ifndef VARS_H
#define VARS_H
void add_var(char *name, variable_values *values, map_void_t* pVarMap); void add_var(char *name, variable_values *values, map_void_t* pVarMap);
variable_values *get_value(char *pName, 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_int(char *pName, int value);
variable* make_variable_double(char *pName, double value); variable* make_variable_double(char *pName, double value);
variable* make_variable_char(char *pName, char value); variable* make_variable_char(char *pName, char value);
variable* make_variable_string(char *pName, char *value); variable* make_variable_string(char *pName, char *value);
#endif
Loading…
Cancel
Save