master
john doe 5 years ago
parent f717a280f7
commit 46a4c2b589

@ -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) My computer programming language made with LEX/YACC written in C. (interpreted)
* Moved nodeInfo declarations to its own folder/file # Making a program.
* Moved print declarations to its own folder/file
* Moved heads of linked lists/hashmaps to headerfiles
# Example Program.
Everything in this language is based around "Structs". These are not the regular structs that are in C. 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: To make a hello world program it would look like this:
``` ```

@ -1 +0,0 @@
//merge in progress, refer to parser.c

@ -1 +0,0 @@
//merge in progress, refer to parser.c

BIN
deon

Binary file not shown.

@ -1,14 +1,26 @@
%{ %{
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "parser/parser.c" #include "parser/parser.c"
#include "functions/functions.c"
#include "logic/logic.c"
#include "print/print.c"
#include "includes/include.h"
int yylex(void); int yylex(void);
void yyerror(char *); void yyerror(char *);
extern int yylineno; extern int yylineno;
extern int _DEBUG; 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 /* doing these before multiplication and division because
@ -29,7 +41,7 @@ extern int _DEBUG;
int num; int num;
char character; char character;
double Double; double Double;
struct nodeInfo *nPtr; struct node_info *nPtr;
} }
@ -61,37 +73,37 @@ statement:
VARIABLE { $$ = $1; } //this is a real var dec. VARIABLE { $$ = $1; } //this is a real var dec.
| FUNC { $$ = $1; } | FUNC { $$ = $1; }
| IF '[' expr ']' ':' '{' statement_list '}' { $$ = createLogic(1, $3, $7, NULL); } //examples of logic in this language | IF '[' expr ']' ':' '{' statement_list '}' { $$ = create_logic(1, $3, $7, NULL); } //examples of logic in this language
| WHILE '[' expr ']' ':' '{' statement_list '}' { $$ = createLogic(2, $3, $7, NULL); } | WHILE '[' expr ']' ':' '{' statement_list '}' { $$ = create_logic(2, $3, $7, NULL); }
| PRINTF'['print']' { $$ = $3; } | PRINTF'['print']' { $$ = $3; }
| '{' statement_list '}' { $$ = $2; } | '{' statement_list '}' { $$ = $2; }
; ;
statement_list: statement_list:
statement ';' { $$ = createCompoundStatement($1); } statement ';' { $$ = create_compound_statement($1, _temp_statement_head); }
| statement_list statement ';' { $$ = addToCompoundStatement($2); } | statement_list statement ';' { $$ = add_to_compound_statement($2, _temp_statement_head); }
; ;
FUNC: FUNC:
DEF VARNAME ':' '{' statement_list '}' { $$ = createFunction($2, $5); } // defining functions DEF VARNAME ':' '{' statement_list '}' { $$ = create_function($2, $5); } // defining functions
| CALL VARNAME { $$ = createFunctionCall($2); } // calling functions | CALL VARNAME { $$ = create_function_call($2); } // calling functions
; ;
VARIABLE: VARIABLE:
VARNAME '=' STRING { $$ = createVarAssignmentString($1, $3);} VARNAME '=' STRING { $$ = create_var_assignment_string($1, $3);}
| VARNAME '=' expr { $$ = createVarAssignmentInt($1, $3);} | VARNAME '=' expr { $$ = create_var_assignment_int($1, $3);}
| VARNAME '=' CHAR { $$ = createVarAssignmentChar($1, $3);} | VARNAME '=' CHAR { $$ = create_var_assignment_char($1, $3);}
| VARNAME '=' DOUBLEVAR { $$ = createVarAssignmentDouble($1, $3);} | VARNAME '=' DOUBLEVAR { $$ = create_var_assignment_double($1, $3);}
| VARNAME '=' '{' statement_list '}' { $$ = createFunction($1, $4); } // you can also define functions like this | VARNAME '=' '{' statement_list '}' { $$ = create_function($1, $4); } // you can also define functions like this
; ;
expr: expr:
INTEGER { $$ = $1; } 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; } /* addition */
| expr '-' expr { $$ = $1 - $3; } /* subtration */ | expr '-' expr { $$ = $1 - $3; } /* subtration */
| expr '*' expr { $$ = $1 * $3; } /* multiplication */ | expr '*' expr { $$ = $1 * $3; } /* multiplication */
@ -102,12 +114,12 @@ expr:
| '(' expr ')' { $$ = $2; } | '(' expr ')' { $$ = $2; }
; ;
//TODO update this.
print: print:
VARNAME { $$ = createPrintVarNode($1); } VARNAME { $$ = create_print_var_node($1); }
| expr { $$ = createPrintStatement(1, $1, NULL); } | expr { $$ = create_print_statement(1, $1, NULL); }
| STRING { $$ = createPrintStatement(2, 0, $1); } | STRING { $$ = create_print_statement(2, 0, $1); }
| ',' print { $$ = $2; } | ',' print { $$ = $2; }
; ;

@ -1,9 +0,0 @@
i = 10;
def a :
if [ {int} i > 4 ] :
i = {int} i - 1;
call a;
printf [ {int} i ];

@ -0,0 +1,11 @@
def Main : {
if[ 10 > 4 ] : {
printf["Hello world!"];
};
};
call Main;

@ -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!" ];

@ -1,18 +0,0 @@
def AnotherFunction: {
printf[ "another Function!" ];
};
def Main : {
if [ 10 > 4 ]: {
printf[ "hello world" ];
};
};
call Main;

@ -1,11 +0,0 @@
def Main : {
printf["hello world"];
a = 10;
printf[a];
};
call Main;

@ -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 ];

@ -1 +1,74 @@
//merge in progress, refer to parser.c #include <stdlib.h>
#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;
}

@ -1 +1,5 @@
//merge in progress, refer to parser.c #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);

@ -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 <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "map.h" #include "map.h"

@ -0,0 +1,99 @@
#ifndef INCLUDE_H
#define INCLUDE_H
#include <stdlib.h>
#include <stdio.h>
#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

@ -0,0 +1,44 @@
#include <stdlib.h>
#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;
}

@ -0,0 +1,4 @@
#include "../includes/include.h"
void _free_linked_list(node_info *pList);
node_info *copy_linked_list(node_info *pList);

@ -1,6 +0,0 @@
typedef struct link {
} link;

@ -1 +1,52 @@
//merge in progress, refer to parser.c #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");
}
}

@ -1 +1 @@
//merge in progress, refer to parser.c node_info *createLogic(int opperation, int expr, node_info *statement_list, node_info *else_false);

@ -1 +0,0 @@
//merge in progress, refer to parser.c

@ -1 +0,0 @@
# Folder contains all data related to networking

Binary file not shown.

Binary file not shown.

@ -1,131 +1,17 @@
#include <stdio.h> #include <stdio.h>
#include "parser.h" #include <stdlib.h>
#include "../vars/vars.h" #include "../vars/vars.c"
#include "../hashmap/map.h"
//TODO put in header //hashmaps/link list heads
void ex(); 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 node_info* create_var_assignment_string(char *pName, char *pString) {
typedef struct print {
int opperation; node_info *newNode = malloc(sizeof(node_info));
double value; variable *newVar = make_variable_string(pName, pString);
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);
newNode->type = ASSIGNMENT; newNode->type = ASSIGNMENT;
newNode->opperation = 1; newNode->opperation = 1;
newNode->var = newVar; 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)); node_info *newNode = malloc(sizeof(node_info));
Variable *newVar = makeVariableInt(name, number); variable *newVar = make_variable_int(pName, number);
newNode->type = ASSIGNMENT; newNode->type = ASSIGNMENT;
newNode->opperation = 1; newNode->opperation = 1;
newNode->var = newVar; 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)); node_info *newNode = malloc(sizeof(node_info));
Variable *newVar = makeVariableChar(name, Char); variable *newVar = make_variable_char(pName, Char);
newNode->type = ASSIGNMENT; newNode->type = ASSIGNMENT;
newNode->opperation = 1; newNode->opperation = 1;
newNode->var = newVar; newNode->var = newVar;
@ -155,216 +41,59 @@ nodeInfo* createVarAssignmentChar(char *name, char Char) {
} }
nodeInfo* createVarAssignmentDouble(char *name, double Double) { node_info* create_var_assignment_double(char *pName, double Double) {
nodeInfo *newNode = malloc(sizeof(nodeInfo));
Variable *newVar = makeVariableDouble(name, Double);
node_info *newNode = malloc(sizeof(node_info));
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 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. Adds statement to linked list.
@param nodeInfo link to be added. @param nodeInfo link to be added.
@return head of linked list. @return head of linked list.
*/ */
node_info* add_to_compound_statement(node_info *new_statement, node_info *statement_head) {
nodeInfo *addToCompoundStatement(nodeInfo *n) {
if(statementHead == NULL) { if(statement_head == NULL) {
statementHead = malloc(sizeof(nodeInfo)); statement_head = malloc(sizeof(node_info));
statementHead->opperation = 5; statement_head->opperation = 5;
statementHead->next = n; statement_head->next = new_statement;
statementHead->next->next = NULL; statement_head->next->next = NULL;
statementHead->statement_list = NULL; statement_head->statement_list = NULL;
} else { } 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) { while (p->statement_list != NULL) {
p = p->statement_list; p = p->statement_list;
} }
//TODO replace with double linked list end
while (p->next != NULL) { while (p->next != NULL) {
p = p->next; p = p->next;
} }
p->next = malloc(sizeof(nodeInfo)); p->next = malloc(sizeof(node_info));
p->next = n; p->next = new_statement;
p->next->next = NULL; 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. //TODO return bool or int detailing the status of execution.
void ex(nodeInfo *n) { void ex(node_info *n) {
switch(n->opperation){ switch(n->opperation){
case 1: case 1:
addVar(n->var->name, n->var->value); add_var(n->var->name, n->var->value, _var_map);
break; break;
//TODO expression stuff //TODO expression stuff
case 2: case 2:
@ -405,7 +134,7 @@ void ex(nodeInfo *n) {
break; break;
default: 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: default:
printf("something wrong with logical expression check yacc\n"); printf("{ERROR} SOMETHING WRONG WITH LOGICAL EXPRESSION CHECK YACC\n");
exit(1);
} }
break; break;
@ -446,89 +176,90 @@ void ex(nodeInfo *n) {
ex(n); ex(n);
} }
statementHead = NULL;
break; break;
case 6: case 6:
; ;
VariableValues *value = getValue(n->name); variable_values *value = get_value(n->name, _var_map);
switch(value->type) { switch(value->type) {
case STRINGVAR: case VAR_STRING:
printf("%s\n", value->String); printf("%s\n", value->_string);
break; break;
case INT: case VAR_INT:
printf("%d\n", value->Int); printf("%d\n", value->_int);
break; break;
case CHARVAR: case VAR_CHAR:
printf("%c\n", value->Char); printf("%c\n", value->_char);
break; 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; break;
default: default:
printf("no such variable type called , keep coding!\n"); printf("{ERROR} NO SUCH VARIABLE TYPE CHECK YACC!\n");
exit(1);
} }
break; break;
//TODO update default to case 7 for functions
default: default:
; ;
//TODO add error/exit here
switch(n->type) {
case FUNCTION_CALL: switch(n->type) {
;
nodeInfo *_stmts = *map_get(&*((map_void_t* ) _function_list), n->name);
while (_stmts->next != NULL) {
_stmts = _stmts->next;
ex(_stmts);
}
break;
case FUNCTION: case FUNCTION_CALL:
;
if (_function_list == NULL) { //gets the statements of a function with the provided name
_function_list = malloc(sizeof(map_void_t)); node_info *_stmts = *map_get(&*((map_void_t* ) _function_list), n->name);
n->local_var_list = malloc(sizeof(map_void_t)); while (_stmts->next != NULL) { //loop over all the statements and execute them
_stmts = _stmts->next;
ex(_stmts);
}
break;
n->next = NULL; case FUNCTION:
n->local_list = NULL; ;
map_init(&*((map_void_t *) _function_list)); //if the list of functions already defined is null then we malloc space for it.
map_init(&*((map_void_t *) n->local_var_list)); if (_function_list == NULL) {
map_set(&*((map_void_t* ) _function_list), n->name, &*n->_function_body);
} else { _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;
} default:
break; ;
//TODO add error here and close
default: }
;
//TODO add error here and close
}
} }
} }

@ -1,24 +1,11 @@
typedef struct assignment assignment; #include "../includes/include.h"
typedef struct expression expression;
typedef struct print print;
typedef struct logic logic;
typedef struct nodeInfo nodeInfo;
nodeInfo *node(int opperation, char opper, char var, int l, int r); node_info* create_var_assignment_string(char *pName, char *pString);
nodeInfo *createFunctionCall(char *_name); node_info* create_var_assignment_int(char *pName, int number);
nodeInfo *createFunction(char *_name, nodeInfo *_stmts); node_info* create_var_assignment_char(char *pName, char Char);
nodeInfo *createFunctionExpression(int lvalue, int rvalue, char opper); node_info* create_var_assignment_double(char *pName, double Double);
nodeInfo *createVarAssignmentString(char *name, char *string); node_info* create_print_var_node(char *pVarname);
nodeInfo *createVarAssignmentInt(char *name, int number); node_info* create_print_statement(int opperation, int value, char *string);
nodeInfo *createVarAssignmentChar(char *name, char Char); node_info* create_logic(int opperation, int expr, node_info *statement_list, node_info *else_false);
nodeInfo *createVarAssignmentDouble(char *name, double Double); node_info* add_to_compound_statement(node_info *new_statement, node_info *statement_head);
nodeInfo *createPrintVarNode(char *varname); void ex(node_info *n);
//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);

@ -1 +1,69 @@
//merge in progress refer to parser.c #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");
}
}

@ -1 +1,2 @@
//merge in progress refer to parser.c node_info *create_print_var_node(char *pVarName);
node_info *create_print_statement(int opperation, int value, char *string);

@ -1,3 +0,0 @@
# Info
This folder contains all data related to varibles.

@ -1,53 +1,10 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#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 * Version: 0.2
* *
* adds var to hashmap if its not already set * adds var to hashmap if its not already set
@ -56,34 +13,28 @@ struct map_void_t *varMap = NULL;
* @param string for var name * @param string for var name
* *
*/ */
void add_var(char *pName, variable_values *pValues, map_void_t* pVarMap) {
void addVar(char *name, VariableValues *values) {
if(varMap == NULL) { //if variable map is null
if(pVarMap == NULL) {
varMap = malloc(sizeof(map_void_t)); pVarMap = malloc(sizeof(map_void_t));
map_init(&*((map_void_t *) varMap)); map_init(&*((map_void_t *) pVarMap));
} }
map_set(&*((map_void_t* ) pVarMap), pName, &*pValues);
map_set(&*((map_void_t* ) varMap), name, &*values);
} }
/** /**
* returns variables values from hashmap * returns variables values from hashmap
* *
* @see [VariableValues] * @see [variable_values]
* @see [char *string] * @see [char *string]
* @return [VariableValues] containing variables data * @return [variable_values] containing variables data
* @param [char *string] Variable name. * @param [char *string] Variable name.
* *
*/ */
variable_values *get_value(char *pName, map_void_t* pVarMap) {
VariableValues *getValue(char *name) {
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 * @return the head of the linked list which just points to all of the vars
* *
*/ */
variable* make_variable_int(char *pName, int value) {
Variable* makeVariableInt(char *name, int Int) {
Variable *p = malloc(sizeof(Variable)); variable *p = malloc(sizeof(variable));
VariableValues *IntegerValue = malloc(sizeof(VariableValues)); variable_values *int_value = malloc(sizeof(variable_values));
IntegerValue->type = INT; int_value->type = VAR_INT;
IntegerValue->Int = Int; int_value->_int = value;
p->name = name; p->name = pName;
p->value = IntegerValue; p->value = int_value;
return p; 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)); variable *new_var = malloc(sizeof(variable));
VariableValues *DoubleValue= malloc(sizeof(VariableValues)); variable_values *double_value = malloc(sizeof(variable_values));
DoubleValue->type = DOUBLE; double_value->type = VAR_DOUBLE;
DoubleValue->Double = Double; double_value->_double = value;
p->name = name; new_var->name = pName;
p->value = DoubleValue; new_var->value = double_value;
return p; 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)); variable *p = malloc(sizeof(variable));
VariableValues *CharacterValue = malloc(sizeof(VariableValues)); variable_values *char_value = malloc(sizeof(variable_values));
CharacterValue->type = CHARVAR; char_value->type = VAR_CHAR;
CharacterValue->Char = Char; char_value->_char = value;
p->name = name; p->name = pName;
p->value = CharacterValue; p->value = char_value;
return p; 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)); variable *p = malloc(sizeof(variable));
VariableValues *StringValue = malloc(sizeof(VariableValues)); variable_values *string_value = malloc(sizeof(variable_values));
StringValue->type = STRINGVAR; string_value->type = VAR_STRING;
StringValue->String = String; string_value->_string = value;
p->name = name; p->name = pName;
p->value = StringValue; p->value = string_value;
return p; return p;
} }

@ -1,9 +1,6 @@
#include "vars.c" void add_var(char *name, variable_values *values, map_void_t* pVarMap);
variable_values *get_value(char *pName, map_void_t* pVarMap);
typedef enum VariableType VariableType; variable* make_variable_int(char *pName, int value);
typedef struct VariableValues VariableValues; variable* make_variable_double(char *pName, double value);
typedef struct Variable Variable; variable* make_variable_char(char *pName, char value);
variable* make_variable_string(char *pName, char *value);
//vars functions
void addVar(char *name, VariableValues *values);
VariableValues *getValue(char *name);
Loading…
Cancel
Save