From 5bf810a4a4b4e31466cf6cc8f165950e95ed8270 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Tue, 22 Mar 2016 19:03:36 +0100 Subject: [PATCH] some code cleanups --- include/Makefile.am | 10 +- include/assign.h | 2 +- include/bbtree.h | 36 +-- include/block.h | 19 +- include/cast.h | 6 +- include/evalCond.h | 4 +- include/evalExpr.h | 2 +- include/expValue.h | 19 +- include/ident.h | 30 +-- include/identList.h | 24 +- include/statement.h | 58 ++--- include/stmtQueue.h | 16 +- include/tepal.h | 12 +- include/variable.h | 4 +- src/assign.c | 13 +- src/bbtree.c | 237 +++++++++----------- src/block.c | 36 ++- src/cast.c | 16 +- src/evalCond.c | 109 +++++---- src/evalExpr.c | 113 ++++------ src/expValue.c | 97 ++++---- src/ident.c | 103 ++++----- src/identList.c | 178 +++++++-------- src/statement.c | 416 +++++++++++++++++----------------- src/stmtQueue.c | 84 ++++--- src/tepal.c | 39 ++-- src/tepal_pars.y | 269 ++++++++++------------ src/tepal_scan.l | 536 +++++++++++++++++++++----------------------- src/variable.c | 27 ++- 29 files changed, 1203 insertions(+), 1312 deletions(-) diff --git a/include/Makefile.am b/include/Makefile.am index 98c02d3..fc4820c 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1,6 +1,6 @@ -nobase_include_HEADERS = tepal.h helper.h \ - evalExpr.h evalCond.h expValue.h \ - ident.h identList.h bbtree.h \ - variable.h assign.h cast.h \ - statement.h stmtQueue.h block.h +nobase_include_HEADERS = tepal.h helper.h \ + evalExpr.h evalCond.h expValue.h \ + ident.h identList.h bbtree.h \ + variable.h assign.h cast.h \ + statement.h stmtQueue.h block.h diff --git a/include/assign.h b/include/assign.h index 2904e7a..b8b0b54 100644 --- a/include/assign.h +++ b/include/assign.h @@ -4,6 +4,6 @@ #include #include -s_expVal * assign (s_ident *, s_expVal *); +s_expVal * assign(s_ident *, s_expVal *); #endif /* _ASSIGN_H_ */ diff --git a/include/bbtree.h b/include/bbtree.h index a1a6542..2f30fa1 100644 --- a/include/bbtree.h +++ b/include/bbtree.h @@ -15,10 +15,10 @@ struct bbTreeNode } s_bbTreeNode; -#define BBTREE_LEFT_HEIGHT(t) ((t)->left ? (t)->left->height : -1) -#define BBTREE_RIGHT_HEIGHT(t) ((t)->right ? (t)->right->height : -1) -#define BBTREE_AVL(t) (BBTREE_RIGHT_HEIGHT ((t)) - \ - BBTREE_LEFT_HEIGHT ((t))) +#define BBTREE_LEFT_HEIGHT(t) ((t)->left ? (t)->left->height : -1) +#define BBTREE_RIGHT_HEIGHT(t) ((t)->right ? (t)->right->height : -1) +#define BBTREE_AVL(t) (BBTREE_RIGHT_HEIGHT ((t)) - \ + BBTREE_LEFT_HEIGHT ((t))) /* @@ -27,13 +27,13 @@ struct bbTreeNode * with bbTreeNew. * * params: - * left: pointer to left value - * right: pointer to right value + * left: pointer to left value + * right: pointer to right value * * returns: - * 0: if values equal - * 1: if right greater left - * -1: if left greater right + * 0: if values equal + * 1: if right greater left + * -1: if left greater right */ typedef int (* t_bbTreeCmp) (void *, void *); @@ -47,20 +47,20 @@ struct bbTree /* constructor, destructor */ -s_bbTree * bbTreeNew (t_bbTreeCmp); -void bbTreeFree (s_bbTree *); +s_bbTree * bbTreeNew(t_bbTreeCmp); +void bbTreeFree(s_bbTree *); /* data manipulation */ -void * bbTreeInsert (s_bbTree *, void *); -void * bbTreeSeek (s_bbTree *, void *); -void * bbTreeRemove (s_bbTree *, void *); +void * bbTreeInsert(s_bbTree *, void *); +void * bbTreeSeek(s_bbTree *, void *); +void * bbTreeRemove(s_bbTree *, void *); /* analysation */ -void * bbTreeMin (s_bbTree *); -void * bbTreeMax (s_bbTree *); -int bbTreeSize (s_bbTree *); +void * bbTreeMin(s_bbTree *); +void * bbTreeMax(s_bbTree *); +int bbTreeSize(s_bbTree *); /* bbTree to other Datastructure */ -void ** bbTreeInOrder (s_bbTree *, void **); +void ** bbTreeInOrder(s_bbTree *, void **); #endif /* _BBTREE_H_ */ diff --git a/include/block.h b/include/block.h index 830aa5a..ce1cf7f 100644 --- a/include/block.h +++ b/include/block.h @@ -6,14 +6,15 @@ typedef struct block s_block; #include #include -s_block * blockNew (s_stmtQueue *); -void blockFree (s_block *); -void blockSetNonLocalId (s_block *, s_ident *); -s_block * blockPush (s_block **, s_block *); -s_block * blockPop (s_block **); -s_block * blockPrev (s_block *); -s_stmtQueue * blockStmts (s_block *); -s_identList * blockIdl (s_block *); +s_block * blockNew(s_stmtQueue *); +void blockFree(s_block *); +void blockSetNonLocalId(s_block *, s_ident *); +s_block * blockPush(s_block **, s_block *); +s_block * blockPop(s_block **); +s_block * blockPrev(s_block *); +s_stmtQueue * blockStmts(s_block *); +s_identList * blockIdl(s_block *); + +s_identList * blockGetIdl(s_block *); -s_identList * blockGetIdl (s_block *); #endif /* _BLOCK_H_ */ diff --git a/include/cast.h b/include/cast.h index ab13e53..28813fa 100644 --- a/include/cast.h +++ b/include/cast.h @@ -3,8 +3,8 @@ #include "expValue.h" -s_expVal * castExprToInt (s_expVal *); -s_expVal * castExprToFloat (s_expVal *); -s_expVal * castExprToString (s_expVal *); +s_expVal * castExprToInt(s_expVal *); +s_expVal * castExprToFloat(s_expVal *); +s_expVal * castExprToString(s_expVal *); #endif /* _CAST_H_ */ diff --git a/include/evalCond.h b/include/evalCond.h index 6b748d2..efa5404 100644 --- a/include/evalCond.h +++ b/include/evalCond.h @@ -3,7 +3,7 @@ #include "expValue.h" -int evalCondExpr (s_expVal *); -int evalComp (int, s_expVal *, s_expVal *); +int evalCondExpr(s_expVal *); +int evalComp(int, s_expVal *, s_expVal *); #endif /* _EVALCOND_H_ */ diff --git a/include/evalExpr.h b/include/evalExpr.h index 1b5031d..c561bc6 100644 --- a/include/evalExpr.h +++ b/include/evalExpr.h @@ -10,6 +10,6 @@ #define OVER '/' #define MODULO '%' -s_expVal * evalExpr (int, s_expVal *, s_expVal *); +s_expVal * evalExpr(int, s_expVal *, s_expVal *); #endif /* _EVAL_EXPR_H_ */ diff --git a/include/expValue.h b/include/expValue.h index 92c742f..9f22630 100644 --- a/include/expValue.h +++ b/include/expValue.h @@ -10,22 +10,21 @@ typedef struct expVal s_expVal; /* Constructoren / Destructoren */ -s_expVal * expValueIntNew (long); -s_expVal * expValueFloatNew (double); -s_expVal * expValueStringNew (char *); +s_expVal * expValueIntNew(long); +s_expVal * expValueFloatNew(double); +s_expVal * expValueStringNew(char *); -s_expVal * expValueClone (s_expVal *); +s_expVal * expValueClone(s_expVal *); -void expValueFree (s_expVal *); +void expValueFree(s_expVal *); /* Accessors */ -long expValueInt (s_expVal *); -double expValueFloat (s_expVal *); -char * expValueString (s_expVal *); +long expValueInt(s_expVal *); +double expValueFloat(s_expVal *); +char * expValueString(s_expVal *); /* analyse expValue */ -int expValueGetType (s_expVal *); - +int expValueGetType(s_expVal *); #endif /* _EXP_VALUE_H_ */ diff --git a/include/ident.h b/include/ident.h index b83bb94..bffdc1f 100644 --- a/include/ident.h +++ b/include/ident.h @@ -12,24 +12,24 @@ typedef struct ident s_ident; #include /* identifier constructors/destructors */ -s_ident * identNew (int, const char *); -s_ident * identUndefNew (int, const char *); -s_ident * identExpNew (int, const char *, s_expVal *); -s_ident * identIdlNew (int, const char *, s_identList *); -void identFree (s_ident *); +s_ident * identNew(int, const char *); +s_ident * identUndefNew(int, const char *); +s_ident * identExpNew(int, const char *, s_expVal *); +s_ident * identIdlNew(int, const char *, s_identList *); +void identFree(s_ident *); /* analyse ident */ -int identIsQueued (s_ident *); -void identEnqueue (s_ident *); -void identDequeue (s_ident *); -int identGetType (s_ident *); -char * identGetKey (s_ident *); -int identGetIdx (s_ident *); +int identIsQueued(s_ident *); +void identEnqueue(s_ident *); +void identDequeue(s_ident *); +int identGetType(s_ident *); +char * identGetKey(s_ident *); +int identGetIdx(s_ident *); /* identifier to value */ -s_expVal * identExp (s_ident *); -s_identList * identIdl (s_ident *); -s_ident * identSetExp (s_ident *, s_expVal *); -s_ident * identSetIdl (s_ident *, s_identList *); +s_expVal * identExp(s_ident *); +s_identList * identIdl(s_ident *); +s_ident * identSetExp(s_ident *, s_expVal *); +s_ident * identSetIdl(s_ident *, s_identList *); #endif /* _IDENT_H_ */ diff --git a/include/identList.h b/include/identList.h index b2d0702..03f2b96 100644 --- a/include/identList.h +++ b/include/identList.h @@ -7,23 +7,23 @@ typedef struct identList s_identList; #include /* constructor/destructor for new identList */ -s_identList * identListNew (void); -void identListFree (s_identList *); +s_identList * identListNew(void); +void identListFree(s_identList *); /* insertions or deletion into a list */ -s_ident * identListPutVal (s_identList *, s_ident *); -s_ident * identListPutExpByIdx (s_identList *, int, s_expVal *); -s_ident * identListPutIdlByIdx (s_identList *, int, s_identList *); -s_ident * identListPutExpByKey (s_identList *, const char *, s_expVal *); -s_ident * identListPutIdlByKey (s_identList *, const char *, s_identList *); -void identListRemoveByIdx (s_identList *, int); -void identListRemoveByKey (s_identList *, const char *); +s_ident * identListPutVal(s_identList *, s_ident *); +s_ident * identListPutExpByIdx(s_identList *, int, s_expVal *); +s_ident * identListPutIdlByIdx(s_identList *, int, s_identList *); +s_ident * identListPutExpByKey(s_identList *, const char *, s_expVal *); +s_ident * identListPutIdlByKey(s_identList *, const char *, s_identList *); +void identListRemoveByIdx(s_identList *, int); +void identListRemoveByKey(s_identList *, const char *); /* seeking in list */ -s_ident * identListSeekIdx (s_identList *, int); -s_ident * identListSeekKey (s_identList *, const char *); +s_ident * identListSeekIdx(s_identList *, int); +s_ident * identListSeekKey(s_identList *, const char *); /* identList to other DataStructures */ -s_ident ** identListToArray (s_identList *); +s_ident ** identListToArray(s_identList *); #endif /* _IDENT_LIST_H_ */ diff --git a/include/statement.h b/include/statement.h index f7ad656..d706ac9 100644 --- a/include/statement.h +++ b/include/statement.h @@ -1,15 +1,15 @@ #ifndef _STMT_H_ #define _STMT_H_ -#define STMT_CONST 0 -#define STMT_BLOCK 1 +#define STMT_CONST 0 +#define STMT_BLOCK 1 -#define STMT_PRINT 10 -#define STMT_IF 11 -#define STMT_FOREACH 12 -#define STMT_REPEAT 13 -#define STMT_ASSIGN 14 -#define STMT_UNSET 15 +#define STMT_PRINT 10 +#define STMT_IF 11 +#define STMT_FOREACH 12 +#define STMT_REPEAT 13 +#define STMT_ASSIGN 14 +#define STMT_UNSET 15 #define STMT_EVAL_PLUS 20 #define STMT_EVAL_MINUS 21 @@ -26,24 +26,24 @@ #define STMT_CAST_FLOAT 41 #define STMT_CAST_STRING 42 -#define STMT_COMP_EQ 50 -#define STMT_COMP_NE 51 -#define STMT_COMP_LT 52 -#define STMT_COMP_GT 53 -#define STMT_COMP_LE 54 -#define STMT_COMP_GE 55 +#define STMT_COMP_EQ 50 +#define STMT_COMP_NE 51 +#define STMT_COMP_LT 52 +#define STMT_COMP_GT 53 +#define STMT_COMP_LE 54 +#define STMT_COMP_GE 55 -#define STMT_COND_EXPR 60 -#define STMT_COND_AND 61 -#define STMT_COND_OR 62 -#define STMT_COND_NEG 63 +#define STMT_COND_EXPR 60 +#define STMT_COND_AND 61 +#define STMT_COND_OR 62 +#define STMT_COND_NEG 63 -#define STYP_NONE 0 -#define STYP_COND 1 -#define STYP_EVAL 2 -#define STYP_IDVAL 3 +#define STYP_NONE 0 +#define STYP_COND 1 +#define STYP_EVAL 2 +#define STYP_IDVAL 3 /* * Deklaration und Definition @@ -52,7 +52,7 @@ union stmtType { int cond; struct expVal * eVal; - struct ident * idVal; + struct ident * idVal; }; typedef struct stmt s_stmt; @@ -64,11 +64,11 @@ typedef union stmtType u_stmtType; /* * Interface */ -s_stmt * stmtNew (int, s_stmtQueue *, int, u_stmtType); -void stmtFree (s_stmt *); -s_stmtQueue * stmtGetArgs (s_stmt *); -u_stmtType stmtGetVal (s_stmt *); -int stmtGetConstTyp (s_stmt *); -u_stmtType stmtDo (s_stmt *, s_block *); +s_stmt * stmtNew(int, s_stmtQueue *, int, u_stmtType); +void stmtFree(s_stmt *); +s_stmtQueue * stmtGetArgs(s_stmt *); +u_stmtType stmtGetVal(s_stmt *); +int stmtGetConstTyp(s_stmt *); +u_stmtType stmtDo(s_stmt *, s_block *); #endif /* _STMT_H_ */ diff --git a/include/stmtQueue.h b/include/stmtQueue.h index f0c07de..1c60911 100644 --- a/include/stmtQueue.h +++ b/include/stmtQueue.h @@ -7,16 +7,16 @@ typedef struct stmtQueue s_stmtQueue; #include -s_stmtQueue * stmtQueueNew (void); -void stmtQueueFree (s_stmtQueue *); +s_stmtQueue * stmtQueueNew(void); +void stmtQueueFree(s_stmtQueue *); -void stmtQueueEnqueue (s_stmtQueue *, s_stmt *); -s_stmt * stmtQueueDequeue (s_stmtQueue *); -s_stmtQueue * stmtQueueConcat (s_stmtQueue *, s_stmtQueue *); +void stmtQueueEnqueue(s_stmtQueue *, s_stmt *); +s_stmt * stmtQueueDequeue(s_stmtQueue *); +s_stmtQueue * stmtQueueConcat(s_stmtQueue *, s_stmtQueue *); -s_stmt * stmtQueueGet (s_stmtQueue *, unsigned int); -void stmtQueueDo (s_stmtQueue *); +s_stmt * stmtQueueGet(s_stmtQueue *, unsigned int); +void stmtQueueDo(s_stmtQueue *); -unsigned int stmtQueueGetSize (s_stmtQueue *); +unsigned int stmtQueueGetSize(s_stmtQueue *); #endif /* _STMT_QUEUE_H_ */ diff --git a/include/tepal.h b/include/tepal.h index df29302..47f710f 100644 --- a/include/tepal.h +++ b/include/tepal.h @@ -3,12 +3,12 @@ #include "expValue.h" -#define ERR_UNDEF_VAR 0 -#define ERR_NO_INDEX 1 -#define ERR_STRING_OPERATOR 2 -#define ERR_FLOAT_OPERATOR 3 +#define ERR_UNDEF_VAR 0 +#define ERR_NO_INDEX 1 +#define ERR_STRING_OPERATOR 2 +#define ERR_FLOAT_OPERATOR 3 -void exitError (int, ...); -void printExpr (s_expVal *); +void exitError(int, ...); +void printExpr(s_expVal *); #endif /* __HTMLPARS_H__ */ diff --git a/include/variable.h b/include/variable.h index fd43a7f..62e600c 100644 --- a/include/variable.h +++ b/include/variable.h @@ -5,7 +5,7 @@ #include #include -s_ident * getVariable (s_identList *, char *); -s_ident * getArray (s_ident *, s_expVal *); +s_ident * getVariable(s_identList *, char *); +s_ident * getArray(s_ident *, s_expVal *); #endif /* _VARIABLE_H_ */ diff --git a/src/assign.c b/src/assign.c index 6f8a4ce..acbf1e0 100644 --- a/src/assign.c +++ b/src/assign.c @@ -8,17 +8,16 @@ #include s_expVal * -assign (s_ident * to, s_expVal * from) -{ - if (identGetType (to) == ID_TYP_IDL) - { - char * key = identGetKey (to); - exitError (ERR_NO_INDEX, key); +assign(s_ident * to, s_expVal * from) +{ + if (identGetType (to) == ID_TYP_IDL) { + char * key = identGetKey(to); + exitError(ERR_NO_INDEX, key); } /* !!!IMPORTANT!!! work here */ /*identListPutVal (block, to);*/ - identSetExp (to, from); + identSetExp(to, from); return from; } diff --git a/src/bbtree.c b/src/bbtree.c index e22dd23..96b7fca 100644 --- a/src/bbtree.c +++ b/src/bbtree.c @@ -8,15 +8,14 @@ #include #include - /* * statisch Funktionen */ static s_bbTreeNode * -bbTreeNodeNew (void * value) +bbTreeNodeNew(void * value) { - s_bbTreeNode * new = (s_bbTreeNode *) malloc (sizeof (s_bbTreeNode)); + s_bbTreeNode * new = (s_bbTreeNode*)malloc(sizeof(s_bbTreeNode)); new->value = value; new->left = NULL; @@ -28,20 +27,18 @@ bbTreeNodeNew (void * value) static void -bbTreeNodeFree (s_bbTreeNode * t) +bbTreeNodeFree(s_bbTreeNode * t) { if (t == NULL) return; - while (t->left != NULL) - { - bbTreeNodeFree (t->left); + while (t->left != NULL) { + bbTreeNodeFree(t->left); t->left = NULL; } - while (t->right != NULL) - { - bbTreeNodeFree (t->right); + while (t->right != NULL) { + bbTreeNodeFree(t->right); t->right = NULL; } @@ -50,11 +47,11 @@ bbTreeNodeFree (s_bbTreeNode * t) static void -bbTreeNodeRotLeft (s_bbTreeNode * node) +bbTreeNodeRotLeft(s_bbTreeNode * node) { s_bbTreeNode * left = node->left; s_bbTreeNode * right = node->right; - void * value = node->value; + void * value = node->value; node->right = right->right; node->left = right; @@ -64,18 +61,18 @@ bbTreeNodeRotLeft (s_bbTreeNode * node) node->value = right->value; right->value = value; - node->right->height = - 1 + MAX (BBTREE_LEFT_HEIGHT (node->left), - BBTREE_RIGHT_HEIGHT (node->left)); + node->right->height = + 1 + MAX(BBTREE_LEFT_HEIGHT(node->left), + BBTREE_RIGHT_HEIGHT(node->left)); } static void -bbTreeNodeRotRight (s_bbTreeNode * node) +bbTreeNodeRotRight(s_bbTreeNode * node) { s_bbTreeNode * left = node->left; s_bbTreeNode * right = node->right; - void * value = node->value; + void * value = node->value; node->left = left->left; node->right = left; @@ -85,29 +82,27 @@ bbTreeNodeRotRight (s_bbTreeNode * node) node->value = left->value; left->value = value; - node->right->height = - 1 + MAX (BBTREE_LEFT_HEIGHT (node->right), - BBTREE_RIGHT_HEIGHT (node->right)); + node->right->height = + 1 + MAX(BBTREE_LEFT_HEIGHT(node->right), + BBTREE_RIGHT_HEIGHT(node->right)); } static void -bbTreeNodeBalance (s_bbTreeNode * node) +bbTreeNodeBalance(s_bbTreeNode * node) { - if (BBTREE_AVL (node) == -2) + if (BBTREE_AVL(node) == -2) { /* left is to long */ - { - if (BBTREE_AVL (node->left) == 1) - bbTreeNodeRotLeft (node->left); - bbTreeNodeRotRight (node); + if (BBTREE_AVL(node->left) == 1) + bbTreeNodeRotLeft(node->left); + bbTreeNodeRotRight(node); } - if (BBTREE_AVL (node) == -2) + if (BBTREE_AVL(node) == -2) { /* right is to long */ - { - if (BBTREE_AVL (node->right) == 1) - bbTreeNodeRotRight (node->right); - bbTreeNodeRotLeft (node); + if (BBTREE_AVL(node->right) == 1) + bbTreeNodeRotRight(node->right); + bbTreeNodeRotLeft(node); } } @@ -117,57 +112,57 @@ bbTreeNodeBalance (s_bbTreeNode * node) */ static s_bbTreeNode * -bbTreeNodeInsert (s_bbTreeNode ** _node, t_bbTreeCmp cmp, void * value) +bbTreeNodeInsert(s_bbTreeNode ** _node, t_bbTreeCmp cmp, void * value) { s_bbTreeNode * node = *_node; s_bbTreeNode * ret; - if (node == NULL) + if (node == NULL) { /* This happens only when bbTree::root is NULL * on bbTreeInsert call */ - { - *_node = bbTreeNodeNew (value); + *_node = bbTreeNodeNew(value); return NULL; } - if (cmp (value, node->value) == 0) + if (cmp(value, node->value) == 0) { /* The key is already in the tree. * In this case a node containing the old value is returned. */ - { - ret = bbTreeNodeNew (node->value); + ret = bbTreeNodeNew(node->value); node->value = value; return ret; } - if (cmp (value, node->value) < 0) - if (node->left == NULL) - { - node->left = bbTreeNodeNew (value); + if (cmp(value, node->value) < 0) { + if (node->left == NULL) { + node->left = bbTreeNodeNew(value); ret = NULL; - if (BBTREE_AVL (node) == -2 || BBTREE_AVL (node) == 2) - bbTreeNodeBalance (node); + if (BBTREE_AVL(node) == -2 || BBTREE_AVL(node) == 2) + bbTreeNodeBalance(node); - node->height = 1 + MAX ( - BBTREE_LEFT_HEIGHT (node), BBTREE_RIGHT_HEIGHT (node)); + node->height = 1 + MAX( + BBTREE_LEFT_HEIGHT(node), BBTREE_RIGHT_HEIGHT(node)); } - else - ret = bbTreeNodeInsert (&node->left, cmp, value); + else { + ret = bbTreeNodeInsert(&node->left, cmp, value); + } + } - if (cmp (value, node->value) > 0) - if (node->right == NULL) - { - node->right = bbTreeNodeNew (value); + if (cmp(value, node->value) > 0) { + if (node->right == NULL) { + node->right = bbTreeNodeNew(value); ret = NULL; - if (BBTREE_AVL (node) == -2 || BBTREE_AVL (node) == 2) - bbTreeNodeBalance (node); + if (BBTREE_AVL(node) == -2 || BBTREE_AVL(node) == 2) + bbTreeNodeBalance(node); - node->height = 1 + MAX ( - BBTREE_LEFT_HEIGHT (node), BBTREE_RIGHT_HEIGHT (node)); + node->height = 1 + MAX( + BBTREE_LEFT_HEIGHT(node), BBTREE_RIGHT_HEIGHT(node)); + } + else { + ret = bbTreeNodeInsert(&node->right, cmp, value); } - else - ret = bbTreeNodeInsert (&node->right, cmp, value); + } /* if we come here a new node was inserted a returned node * containing {NULL, NULL} as value reflects this fact. */ @@ -176,18 +171,18 @@ bbTreeNodeInsert (s_bbTreeNode ** _node, t_bbTreeCmp cmp, void * value) static s_bbTreeNode * -bbTreeNodeSeek (s_bbTreeNode * node, t_bbTreeCmp cmp, void * value) +bbTreeNodeSeek(s_bbTreeNode * node, t_bbTreeCmp cmp, void * value) { if (node == NULL) return NULL; - if (cmp (value, node->value) == 0) + if (cmp(value, node->value) == 0) return node; - if (cmp (value, node->value) < 0) + if (cmp(value, node->value) < 0) return bbTreeNodeSeek (node->left, cmp, value); - if (cmp (value, node->value) > 0) + if (cmp(value, node->value) > 0) return bbTreeNodeSeek (node->right, cmp, value); return NULL; @@ -195,35 +190,35 @@ bbTreeNodeSeek (s_bbTreeNode * node, t_bbTreeCmp cmp, void * value) static s_bbTreeNode * -bbTreeNodeMax (s_bbTreeNode * node) +bbTreeNodeMax(s_bbTreeNode * node) { if (node != NULL && node->right != NULL) - return bbTreeNodeMax (node->right); + return bbTreeNodeMax(node->right); return node; } static s_bbTreeNode * -bbTreeNodeMin (s_bbTreeNode * node) +bbTreeNodeMin(s_bbTreeNode * node) { if (node != NULL && node->left != NULL) - return bbTreeNodeMin (node->left); + return bbTreeNodeMin(node->left); return node; } static int -bbTreeNodeSize (s_bbTreeNode * node) +bbTreeNodeSize(s_bbTreeNode * node) { int size = 0; if (node == NULL) return 0; - size += bbTreeNodeSize (node->left); - size += bbTreeNodeSize (node->right); + size += bbTreeNodeSize(node->left); + size += bbTreeNodeSize(node->right); return size + 1; } @@ -235,7 +230,7 @@ bbTreeNodeSize (s_bbTreeNode * node) */ static s_bbTreeNode * -bbTreeNodeRemove (s_bbTreeNode ** _node, t_bbTreeCmp cmp, void * value) +bbTreeNodeRemove(s_bbTreeNode ** _node, t_bbTreeCmp cmp, void * value) { s_bbTreeNode * ret = NULL; s_bbTreeNode * node = *_node; @@ -243,49 +238,43 @@ bbTreeNodeRemove (s_bbTreeNode ** _node, t_bbTreeCmp cmp, void * value) if (node == NULL) return NULL; - if (cmp (value, node->value) == 0) + if (cmp (value, node->value) == 0) { /* found the element left */ - { - if (node->left == NULL && node->right == NULL) + if (node->left == NULL && node->right == NULL) { /* found a leaf */ - { - ret = bbTreeNodeNew (node->value); - free (node); + ret = bbTreeNodeNew(node->value); + free(node); *_node = NULL; } - else if (node->left != NULL && node->left != NULL) + else if (node->left != NULL && node->left != NULL) { /* left & right subtree exists use either max(left) or min(right) */ - { s_bbTreeNode * maxLeft; - maxLeft = bbTreeNodeMax (node->left); + maxLeft = bbTreeNodeMax(node->left); node->value = maxLeft->value; - ret = bbTreeNodeRemove (&node->left, cmp, maxLeft->value); + ret = bbTreeNodeRemove(&node->left, cmp, maxLeft->value); - if (BBTREE_AVL (node) == -2 || BBTREE_AVL (node) == 2) + if (BBTREE_AVL(node) == -2 || BBTREE_AVL(node) == 2) bbTreeNodeBalance (node); - node->height = 1 + MAX ( - BBTREE_LEFT_HEIGHT (node), BBTREE_RIGHT_HEIGHT (node)); + node->height = 1 + MAX( + BBTREE_LEFT_HEIGHT(node), BBTREE_RIGHT_HEIGHT(node)); } - else if ((node->left == NULL) != (node->right == NULL) /* ^^ */) + else if ((node->left == NULL) != (node->right == NULL) /* ^^ */) { /* there is only one subtree */ /* This subtree must be a leaf, because the tree is balanced */ - { - ret = bbTreeNodeNew (node->value); + ret = bbTreeNodeNew(node->value); - if (node->left != NULL) + if (node->left != NULL) { /* found left node */ - { node->value = node->left->value; - free (node->left); + free(node->left); node->left = NULL; } - else + else { /* found right node */ - { node->value = node->right->value; - free (node->right); + free(node->right); node->right = NULL; } } @@ -293,36 +282,36 @@ bbTreeNodeRemove (s_bbTreeNode ** _node, t_bbTreeCmp cmp, void * value) return ret; } - if (cmp (value, node->value) < 0) - ret = bbTreeNodeRemove (&node->left, cmp, value); + if (cmp(value, node->value) < 0) + ret = bbTreeNodeRemove(&node->left, cmp, value); - if (cmp (value, node->value) > 0) - ret = bbTreeNodeRemove (&node->right, cmp, value); + if (cmp(value, node->value) > 0) + ret = bbTreeNodeRemove(&node->right, cmp, value); return ret; } static void -bbTreeNodeInOrder (const s_bbTreeNode * node, void *** ret) +bbTreeNodeInOrder(const s_bbTreeNode * node, void *** ret) { if (node != NULL && node->left != NULL) - bbTreeNodeInOrder (node->left, ret); + bbTreeNodeInOrder(node->left, ret); if (node != NULL) **ret = node->value; (*ret)++; if (node != NULL && node->right != NULL) - bbTreeNodeInOrder (node->right, ret); + bbTreeNodeInOrder(node->right, ret); } /* * Interface (non-static functions) */ s_bbTree * -bbTreeNew (t_bbTreeCmp cmp) +bbTreeNew(t_bbTreeCmp cmp) { - s_bbTree * new = (s_bbTree *) malloc (sizeof (s_bbTree)); + s_bbTree * new = (s_bbTree*)malloc(sizeof(s_bbTree)); new->root = NULL; new->cmp = cmp; @@ -331,22 +320,21 @@ bbTreeNew (t_bbTreeCmp cmp) } void -bbTreeFree (s_bbTree * t) +bbTreeFree(s_bbTree * t) { - bbTreeNodeFree (t->root); + bbTreeNodeFree(t->root); - free (t); + free(t); } void * -bbTreeInsert (s_bbTree * t, void * value) +bbTreeInsert(s_bbTree * t, void * value) { - s_bbTreeNode * oldNode = bbTreeNodeInsert (&t->root, t->cmp, value); - - if (oldNode != NULL) - { + s_bbTreeNode * oldNode = bbTreeNodeInsert(&t->root, t->cmp, value); + + if (oldNode != NULL) { value = oldNode->value; - free (oldNode); + free(oldNode); return value; } @@ -355,22 +343,21 @@ bbTreeInsert (s_bbTree * t, void * value) } void * -bbTreeSeek (s_bbTree * t, void * value) +bbTreeSeek(s_bbTree * t, void * value) { - s_bbTreeNode * seek = bbTreeNodeSeek (t->root, t->cmp, value); + s_bbTreeNode * seek = bbTreeNodeSeek(t->root, t->cmp, value); return (seek != NULL) ? seek->value : NULL; } void * -bbTreeRemove (s_bbTree * t, void * value) +bbTreeRemove(s_bbTree * t, void * value) { - s_bbTreeNode * oldNode = bbTreeNodeRemove (&t->root, t->cmp, value); + s_bbTreeNode * oldNode = bbTreeNodeRemove(&t->root, t->cmp, value); - if (oldNode != NULL) - { + if (oldNode != NULL) { value = oldNode->value; - free (oldNode); + free(oldNode); return value; } @@ -379,33 +366,33 @@ bbTreeRemove (s_bbTree * t, void * value) } void * -bbTreeMax (s_bbTree * t) +bbTreeMax(s_bbTree * t) { - s_bbTreeNode * max = bbTreeNodeMax (t->root); + s_bbTreeNode * max = bbTreeNodeMax(t->root); return max == NULL ? max : max->value; } void * -bbTreeMin (s_bbTree * t) +bbTreeMin(s_bbTree * t) { - s_bbTreeNode * max = bbTreeNodeMin (t->root); + s_bbTreeNode * max = bbTreeNodeMin(t->root); return max == NULL ? max : max->value; } int -bbTreeSize (s_bbTree * t) +bbTreeSize(s_bbTree * t) { - return bbTreeNodeSize (t->root); + return bbTreeNodeSize(t->root); } void ** -bbTreeInOrder (s_bbTree * t, void ** buffer) +bbTreeInOrder(s_bbTree * t, void ** buffer) { void ** tmp = buffer; - bbTreeNodeInOrder (t->root, &tmp); + bbTreeNodeInOrder(t->root, &tmp); return buffer; } diff --git a/src/block.c b/src/block.c index 3bb7d13..879adde 100644 --- a/src/block.c +++ b/src/block.c @@ -12,26 +12,27 @@ struct block /* a stack of used blocks. */ }; -s_identList * -blockGetIdl (s_block * block) + s_identList * +blockGetIdl(s_block * block) { return block->idl; } + s_block * -blockNew (s_stmtQueue * stmts) +blockNew(s_stmtQueue * stmts) { s_block * new = (s_block *) malloc (sizeof (s_block)); new->stmts = stmts; new->prev = NULL; new->idl = identListNew (); /* !!!FIXME: i guess idl should know about - its block! (Give the block as arg) */ + its block! (Give the block as arg) */ return new; } void -blockFree (s_block * bs) +blockFree(s_block * bs) { if (bs->prev != NULL) blockFree (bs->prev); @@ -39,50 +40,47 @@ blockFree (s_block * bs) if (bs->stmts != NULL) stmtQueueFree (bs->stmts); - identListFree (bs->idl); + identListFree(bs->idl); free (bs); } void -blockSetNonLocalId (s_block * bs, s_ident * id) +blockSetNonLocalId(s_block * bs, s_ident * id) { - s_identListPutVal (bs->idl, id); + s_identListPutVal(bs->idl, id); } s_block * -blockPush (s_block ** bs, s_block * new) +blockPush(s_block ** bs, s_block * new) { - new->prev = *bs; - - *bs = new; - + new->prev = *bs; + *bs = new; return *bs; } s_block * -blockPop (s_block ** bs) +blockPop(s_block ** bs) { s_block * ret = *bs; - *bs = (*bs)->prev; - + *bs = (*bs)->prev; return ret; } s_block * -blockPrev (s_block * bs) +blockPrev(s_block * bs) { return bs->prev; } s_stmtQueue * -blockStmts (s_block * bs) +blockStmts(s_block * bs) { return bs->stmts; } s_identList * -blockIdl (s_block * bs) +blockIdl(s_block * bs) { return bs->idl; } diff --git a/src/cast.c b/src/cast.c index 7b94b2c..db1441a 100644 --- a/src/cast.c +++ b/src/cast.c @@ -7,24 +7,24 @@ #include s_expVal * -castExprToInt (s_expVal * eVal) +castExprToInt(s_expVal * eVal) { - return expValueIntNew (expValueInt (eVal)); + return expValueIntNew(expValueInt (eVal)); } s_expVal * -castExprToFloat (s_expVal * eVal) +castExprToFloat(s_expVal * eVal) { - return expValueFloatNew (expValueFloat (eVal)); + return expValueFloatNew(expValueFloat (eVal)); } s_expVal * -castExprToString (s_expVal * eVal) +castExprToString(s_expVal * eVal) { - char * val = expValueString (eVal); - s_expVal * ret = expValueStringNew (val); + char * val = expValueString(eVal); + s_expVal * ret = expValueStringNew(val); - free (val); + free(val); return ret; } diff --git a/src/evalCond.c b/src/evalCond.c index 7b174b0..8d22279 100644 --- a/src/evalCond.c +++ b/src/evalCond.c @@ -12,60 +12,57 @@ */ static int -evalIntComp (int op, s_expVal * _op1, s_expVal * _op2) +evalIntComp(int op, s_expVal * _op1, s_expVal * _op2) { - long op1 = expValueInt (_op1); - long op2 = expValueInt (_op2); - - switch (op) - { - case EQ: return (op1 == op2); - case NE: return (op1 != op2); - case LT: return (op1 < op2); - case GT: return (op1 > op2); - case LE: return (op1 <= op2); - case GE: return (op1 >= op2); + long op1 = expValueInt(_op1); + long op2 = expValueInt(_op2); + + switch (op) { + case EQ: return (op1 == op2); + case NE: return (op1 != op2); + case LT: return (op1 < op2); + case GT: return (op1 > op2); + case LE: return (op1 <= op2); + case GE: return (op1 >= op2); } } static int -evalFloatComp (int op, s_expVal * _op1, s_expVal * _op2) +evalFloatComp(int op, s_expVal * _op1, s_expVal * _op2) { - double op1 = expValueFloat (_op1); - double op2 = expValueFloat (_op2); - - switch (op) - { - case EQ: return (op1 == op2); - case NE: return (op1 != op2); - case LT: return (op1 < op2); - case GT: return (op1 > op2); - case LE: return (op1 <= op2); - case GE: return (op1 >= op2); + double op1 = expValueFloat(_op1); + double op2 = expValueFloat(_op2); + + switch (op) { + case EQ: return (op1 == op2); + case NE: return (op1 != op2); + case LT: return (op1 < op2); + case GT: return (op1 > op2); + case LE: return (op1 <= op2); + case GE: return (op1 >= op2); } } static int -evalStringComp (int op, s_expVal * _op1, s_expVal * _op2) +evalStringComp(int op, s_expVal * _op1, s_expVal * _op2) { - char * op1 = expValueString (_op1); - char * op2 = expValueString (_op2); + char * op1 = expValueString(_op1); + char * op2 = expValueString(_op2); long ret; - switch (op) - { - case EQ: ret = (! strcmp (op1, op2)); - case NE: ret = strcmp (op1, op2); - case LT: ret = (strcmp (op1, op2) < 0) ? 1 : 0; - case GT: ret = (strcmp (op1, op2) > 0) ? 1 : 0; - case LE: ret = (strcmp (op1, op2) <= 0) ? 1 : 0; - case GE: ret = (strcmp (op1, op2) >= 0) ? 1 : 0; + switch (op) { + case EQ: ret = (! strcmp(op1, op2)); + case NE: ret = strcmp(op1, op2); + case LT: ret = (strcmp(op1, op2) < 0) ? 1 : 0; + case GT: ret = (strcmp(op1, op2) > 0) ? 1 : 0; + case LE: ret = (strcmp(op1, op2) <= 0) ? 1 : 0; + case GE: ret = (strcmp(op1, op2) >= 0) ? 1 : 0; } - free (op1); - free (op2); + free(op1); + free(op2); return ret; } @@ -76,38 +73,34 @@ evalStringComp (int op, s_expVal * _op1, s_expVal * _op2) * public functions (interface) */ int -evalCondExpr (s_expVal * _op) +evalCondExpr(s_expVal * _op) { - switch (expValueGetType (_op)) - { - case EXP_TYP_INT: return expValueInt (_op); - case EXP_TYP_FLOAT: return expValueInt (_op); + switch (expValueGetType(_op)) { + case EXP_TYP_INT: + return expValueInt(_op); + case EXP_TYP_FLOAT: + return expValueInt(_op); case EXP_TYP_STRING: - { - char * op = expValueString (_op); - long ret = strlen (op); - - free (op); - - return ret; - } + { + char * op = expValueString(_op); + long ret = strlen(op); + free(op); + return ret; + } } return 0; } int -evalComp (int op, s_expVal * op1, s_expVal * op2) +evalComp(int op, s_expVal * op1, s_expVal * op2) { - switch (expValueGetType (op1)) - { + switch (expValueGetType(op1)) { case EXP_TYP_INT: - return evalIntComp (op, op1, op2); - + return evalIntComp(op, op1, op2); case EXP_TYP_FLOAT: - return evalFloatComp (op, op1, op2); - + return evalFloatComp(op, op1, op2); case EXP_TYP_STRING: - return evalStringComp (op, op1, op2); + return evalStringComp(op, op1, op2); } } diff --git a/src/evalExpr.c b/src/evalExpr.c index 07391f3..f5d543d 100644 --- a/src/evalExpr.c +++ b/src/evalExpr.c @@ -15,21 +15,21 @@ static inline s_expVal * -stringPlus (s_expVal * _op1, s_expVal * _op2) +stringPlus(s_expVal * _op1, s_expVal * _op2) { s_expVal * ret; - char * op1 = expValueString (_op1); - char * op2 = expValueString (_op2); - char * retVal = (char *) malloc ( - sizeof (char) * (strlen (op1) + strlen (op2) + 1)); + char * op1 = expValueString (_op1); + char * op2 = expValueString (_op2); + char * retVal = + (char *)malloc(sizeof(char) * (strlen(op1) + strlen(op2) + 1)); - strcpy (retVal, op1); - strcat (retVal, op2); - ret = expValueStringNew (retVal); + strcpy(retVal, op1); + strcat(retVal, op2); + ret = expValueStringNew(retVal); - free (retVal); - free (op2); - free (op1); + free(retVal); + free(op2); + free(op1); return ret; } @@ -37,83 +37,74 @@ stringPlus (s_expVal * _op1, s_expVal * _op2) static inline s_expVal * -stringNeg (s_expVal * _op) +stringNeg(s_expVal * _op) { s_expVal * ret; int i; - char * op = expValueString (_op); - int len = strlen (op) - 1; + char * op = expValueString(_op); + int len = strlen(op) - 1; - for (i=0; i<=(len/2); i++) - { + for (i=0; i<=(len/2); i++) { char tmp = op[i]; op[i] = op[len-i]; op[len-i] = tmp; } - ret = expValueStringNew (op); - free (op); + ret = expValueStringNew(op); + free(op); return ret; } static inline s_expVal * -evalIntExp (int op, s_expVal * _op1, s_expVal * _op2) +evalIntExp(int op, s_expVal * _op1, s_expVal * _op2) { - long op1 = expValueInt (_op1); - long op2 = (_op2 != NULL) ? expValueInt (_op2) : 0; + long op1 = expValueInt(_op1); + long op2 = (_op2 != NULL) ? expValueInt(_op2) : 0; - if (op == NEG) return expValueIntNew (- op1); + if (op == NEG) return expValueIntNew(- op1); - switch (expValueGetType (_op2)) - { + switch (expValueGetType(_op2)) { case EXP_TYP_INT: case EXP_TYP_FLOAT: - switch (op) - { - case PLUS: return expValueIntNew (op1 + op2); - case MINUS: return expValueIntNew (op1 - op2); - case TIMES: return expValueIntNew (op1 * op2); - case OVER: return expValueIntNew (op1 / op2); - case MODULO: return expValueIntNew (op1 % op2); + switch (op) { + case PLUS: return expValueIntNew(op1 + op2); + case MINUS: return expValueIntNew(op1 - op2); + case TIMES: return expValueIntNew(op1 * op2); + case OVER: return expValueIntNew(op1 / op2); + case MODULO: return expValueIntNew(op1 % op2); } - case EXP_TYP_STRING: - if (op == PLUS) - return stringPlus (_op1, _op2); - exitError (ERR_STRING_OPERATOR, op); + if (op == PLUS) return stringPlus(_op1, _op2); + exitError(ERR_STRING_OPERATOR, op); } } static inline s_expVal * -evalFloatExp (int op, s_expVal * _op1, s_expVal * _op2) +evalFloatExp(int op, s_expVal * _op1, s_expVal * _op2) { double op1 = expValueFloat (_op1); - double op2 = (_op2 != NULL) ? expValueFloat (_op2) : 0.0; + double op2 = (_op2 != NULL) ? expValueFloat(_op2) : 0.0; - if (op == NEG) return expValueFloatNew (- op1); + if (op == NEG) return expValueFloatNew(- op1); - switch (expValueGetType (_op2)) - { + switch (expValueGetType(_op2)) { case EXP_TYP_INT: case EXP_TYP_FLOAT: - switch (op) - { - case MODULO: exitError (ERR_FLOAT_OPERATOR, op); - case PLUS: return expValueFloatNew (op1 + op2); - case MINUS: return expValueFloatNew (op1 - op2); - case TIMES: return expValueFloatNew (op1 * op2); - case OVER: return expValueFloatNew (op1 / op2); + switch (op) { + case MODULO: exitError(ERR_FLOAT_OPERATOR, op); + case PLUS: return expValueFloatNew(op1 + op2); + case MINUS: return expValueFloatNew(op1 - op2); + case TIMES: return expValueFloatNew(op1 * op2); + case OVER: return expValueFloatNew(op1 / op2); } - case EXP_TYP_STRING: - if (op == PLUS) - return stringPlus (_op1, _op2); - exitError (ERR_STRING_OPERATOR, op); + if (op == PLUS) return stringPlus(_op1, _op2); + exitError(ERR_STRING_OPERATOR, op); } } @@ -123,23 +114,17 @@ evalFloatExp (int op, s_expVal * _op1, s_expVal * _op2) * public functions (interface) */ s_expVal * -evalExpr (int op, s_expVal * op1, s_expVal * op2) +evalExpr(int op, s_expVal * op1, s_expVal * op2) { - switch (expValueGetType (op1)) - { + switch (expValueGetType(op1)) { case EXP_TYP_INT: - return evalIntExp (op, op1, op2); - + return evalIntExp(op, op1, op2); case EXP_TYP_FLOAT: - if (op != MODULO) - return evalFloatExp (op, op1, op2); + if (op != MODULO) return evalFloatExp(op, op1, op2); exitError (ERR_FLOAT_OPERATOR, op); - case EXP_TYP_STRING: - if (op == PLUS) - return stringPlus (op1, op2); - if (op == NEG) - return stringNeg (op1); - exitError (ERR_STRING_OPERATOR, op); + if (op == PLUS) return stringPlus(op1, op2); + if (op == NEG) return stringNeg(op1); + exitError(ERR_STRING_OPERATOR, op); } } diff --git a/src/expValue.c b/src/expValue.c index 1d5ebf2..696701a 100644 --- a/src/expValue.c +++ b/src/expValue.c @@ -22,9 +22,9 @@ struct expVal * Constructoren / Destructoren */ s_expVal * -expValueIntNew (long val) +expValueIntNew(long val) { - s_expVal * ret = (s_expVal *) malloc (sizeof (s_expVal)); + s_expVal * ret = (s_expVal *)malloc(sizeof(s_expVal)); ret->type = EXP_TYP_INT; ret->val.iVal = val; @@ -33,9 +33,9 @@ expValueIntNew (long val) } s_expVal * -expValueFloatNew (double val) +expValueFloatNew(double val) { - s_expVal * ret = (s_expVal *) malloc (sizeof (s_expVal)); + s_expVal * ret = (s_expVal *)malloc(sizeof(s_expVal)); ret->type = EXP_TYP_FLOAT; ret->val.fVal = val; @@ -44,33 +44,34 @@ expValueFloatNew (double val) } s_expVal * -expValueStringNew (char * val) +expValueStringNew(char * val) { - s_expVal * ret = (s_expVal *) malloc (sizeof (s_expVal)); + s_expVal * ret = (s_expVal *)malloc(sizeof(s_expVal)); ret->type = EXP_TYP_STRING; - ret->val.cPtr = (char *) malloc (sizeof (char) * (strlen (val) + 1)); - strcpy (ret->val.cPtr, val); + ret->val.cPtr = (char *)malloc(sizeof(char) * (strlen(val) + 1)); + strcpy(ret->val.cPtr, val); return ret; } - s_expVal * -expValueClone (s_expVal * eVal) + s_expVal * +expValueClone(s_expVal * eVal) { - s_expVal * ret = (s_expVal *) malloc (sizeof (s_expVal)); + s_expVal * ret = (s_expVal *)malloc(sizeof(s_expVal)); ret->type = eVal->type; - switch (eVal->type) - { - case EXP_TYP_INT: ret->val.iVal = eVal->val.iVal; break; - case EXP_TYP_FLOAT: ret->val.fVal = eVal->val.fVal; break; - case EXP_TYP_STRING: + switch (eVal->type) { + case EXP_TYP_INT: + ret->val.iVal = eVal->val.iVal; break; + case EXP_TYP_FLOAT: + ret->val.fVal = eVal->val.fVal; break; + case EXP_TYP_STRING: { - ret->val.cPtr = (char *) malloc ( - sizeof (char) * (strlen (eVal->val.cPtr) + 1)); - strcpy (ret->val.cPtr, eVal->val.cPtr); + ret->val.cPtr = (char *)malloc( + sizeof(char) * (strlen(eVal->val.cPtr) + 1)); + strcpy(ret->val.cPtr, eVal->val.cPtr); } } @@ -78,11 +79,10 @@ expValueClone (s_expVal * eVal) } void -expValueFree (s_expVal * eVal) +expValueFree(s_expVal * eVal) { - if (eVal->type == EXP_TYP_STRING) - free (eVal->val.cPtr); - free (eVal); + if (eVal->type == EXP_TYP_STRING) free(eVal->val.cPtr); + free(eVal); } @@ -90,50 +90,45 @@ expValueFree (s_expVal * eVal) * Accessors */ long -expValueInt (s_expVal * eVal) +expValueInt(s_expVal * eVal) { - switch (eVal->type) - { - case EXP_TYP_INT: return eVal->val.iVal; - case EXP_TYP_FLOAT: return (long) eVal->val.fVal; - case EXP_TYP_STRING: return atoi (eVal->val.cPtr); + switch (eVal->type) { + case EXP_TYP_INT: return eVal->val.iVal; + case EXP_TYP_FLOAT: return (long)eVal->val.fVal; + case EXP_TYP_STRING: return atoi(eVal->val.cPtr); } } double -expValueFloat (s_expVal * eVal) +expValueFloat(s_expVal * eVal) { - switch (eVal->type) - { - case EXP_TYP_INT: return (double) eVal->val.iVal; - case EXP_TYP_FLOAT: return eVal->val.fVal; - case EXP_TYP_STRING: return atof (eVal->val.cPtr); + switch (eVal->type) { + case EXP_TYP_INT: return (double) eVal->val.iVal; + case EXP_TYP_FLOAT: return eVal->val.fVal; + case EXP_TYP_STRING: return atof(eVal->val.cPtr); } } char * -expValueString (s_expVal * eVal) +expValueString(s_expVal * eVal) { char buf[40]; char * ret = NULL; - switch (eVal->type) - { + switch (eVal->type) { case EXP_TYP_INT: - sprintf (buf, "%d", eVal->val.iVal); - ret = (char *) malloc (sizeof (char) * (strlen (buf) + 1)); - strcpy (ret, buf); + sprintf(buf, "%d", eVal->val.iVal); + ret = (char*)malloc(sizeof(char) * (strlen(buf) + 1)); + strcpy(ret, buf); break; - - case EXP_TYP_FLOAT: - sprintf (buf, "%f", eVal->val.fVal); - ret = (char *) malloc (sizeof (char) * (strlen (buf) + 1)); - strcpy (ret, buf); + case EXP_TYP_FLOAT: + sprintf(buf, "%f", eVal->val.fVal); + ret = (char*)malloc(sizeof(char) * (strlen(buf) + 1)); + strcpy(ret, buf); break; - case EXP_TYP_STRING: - ret = (char *) malloc (sizeof (char) * (strlen (eVal->val.cPtr) + 1)); - strcpy (ret, eVal->val.cPtr); + ret = (char*)malloc(sizeof(char) * (strlen(eVal->val.cPtr) + 1)); + strcpy(ret, eVal->val.cPtr); break; } @@ -141,10 +136,10 @@ expValueString (s_expVal * eVal) } /* - * analyse expValue + * analyse expValue */ int -expValueGetType (s_expVal * eVal) +expValueGetType(s_expVal * eVal) { return eVal->type; } diff --git a/src/ident.c b/src/ident.c index 0287a64..8fe46c8 100644 --- a/src/ident.c +++ b/src/ident.c @@ -14,7 +14,7 @@ */ union identTypes { - s_expVal * base; + s_expVal * base; s_identList * idl; }; @@ -41,19 +41,18 @@ struct ident * Contructors / Destructors for ident */ s_ident * -identNew (int idx, const char * key) +identNew(int idx, const char * key) { - int keyLen = (key == NULL) ? 2 : 2 + strlen (key); - s_ident * ident = (s_ident *) malloc (sizeof (s_ident)); + int keyLen = (key == NULL) ? 2 : 2 + strlen(key); + s_ident * ident = (s_ident*)malloc(sizeof(s_ident)); ident->idx = idx; - ident->key = (char *) malloc (sizeof (char) * keyLen); + ident->key = (char*)malloc(sizeof(char) * keyLen); ident->key[0] = ID_TYP_UNDEF; ident->key[1] = '\0'; - if (key != NULL) - strcpy (ident->key + 1, key); + if (key != NULL) strcpy(ident->key + 1, key); ident->queued = 0; @@ -61,37 +60,37 @@ identNew (int idx, const char * key) } s_ident * -identUndefNew (int idx, const char * key) +identUndefNew(int idx, const char * key) { #ifdef DEBUG2 - printf ("[!DEBUG!] identUndefNew: %d/%s\n", idx, key); + printf("[!DEBUG!] identUndefNew: %d/%s\n", idx, key); #endif - return identNew (idx, key); + return identNew(idx, key); } s_ident * -identExpNew (int idx, const char * key, s_expVal * val) +identExpNew(int idx, const char * key, s_expVal * val) { - s_ident * ident = identNew (idx, key); + s_ident * ident = identNew(idx, key); #ifdef DEBUG2 - printf ("[!DEBUG!] identExpNew: %d/%s\n", idx, key); + printf("[!DEBUG!] identExpNew: %d/%s\n", idx, key); #endif ident->key[0] = ID_TYP_EXP; - ident->val.base = expValueClone (val); + ident->val.base = expValueClone(val); return ident; } s_ident * -identIdlNew (int idx, const char * key, s_identList * val) +identIdlNew(int idx, const char * key, s_identList * val) { - s_ident * ident = identNew (idx, key); + s_ident * ident = identNew(idx, key); #ifdef DEBUG2 - printf ("[!DEBUG!] identIdlNew: %d/%s\n", idx, key); + printf("[!DEBUG!] identIdlNew: %d/%s\n", idx, key); #endif ident->key[0] = ID_TYP_IDL; @@ -101,117 +100,107 @@ identIdlNew (int idx, const char * key, s_identList * val) } void -identFree (s_ident * id) +identFree(s_ident * id) { #ifdef DEBUG2 - printf ("[!DEBUG!] identFree: %d/%s\n", id->idx, id->key); + printf("[!DEBUG!] identFree: %d/%s\n", id->idx, id->key); #endif - switch (identGetType (id)) - { - case ID_TYP_EXP: expValueFree (id->val.base); break; - case ID_TYP_IDL: identListFree (id->val.idl); break; + switch (identGetType(id)) { + case ID_TYP_EXP: expValueFree (id->val.base); break; + case ID_TYP_IDL: identListFree (id->val.idl); break; } - free (id->key); - free (id); + free(id->key); + free(id); } /* * analyse ident */ int -identIsQueued (s_ident * id) +identIsQueued(s_ident * id) { return id->queued; } void -identEnqueue (s_ident * id) +identEnqueue(s_ident * id) { - id->queued ++; + id->queued++; } void -identDequeue (s_ident * id) +identDequeue(s_ident * id) { - id->queued --; + id->queued--; } int -identGetType (s_ident * id) +identGetType(s_ident * id) { return id->key[0]; } char * -identGetKey (s_ident * id) +identGetKey(s_ident * id) { return id->key + 1; } int -identGetIdx (s_ident * id) +identGetIdx(s_ident * id) { return id->idx; } /* identifier to value */ s_expVal * -identExp (s_ident * id) +identExp(s_ident * id) { s_expVal * ret = NULL; - if (id == NULL) - exitError (ERR_UNDEF_VAR, identGetKey (id)); + if (id == NULL) exitError (ERR_UNDEF_VAR, identGetKey (id)); - if (identGetType (id) == ID_TYP_EXP) - { - ret = expValueClone (id->val.base); - } + if (identGetType (id) == ID_TYP_EXP) ret = expValueClone(id->val.base); return ret; } s_identList * -identIdl (s_ident * id) +identIdl(s_ident * id) { s_identList * ret = NULL; - if (identGetType (id) == ID_TYP_IDL) - { - ret = id->val.idl; - } + if (identGetType(id) == ID_TYP_IDL) ret = id->val.idl; return ret; } s_ident * -identSetExp (s_ident * id, s_expVal * val) +identSetExp(s_ident * id, s_expVal * val) { - switch (identGetType (id)) - { - case ID_TYP_EXP: expValueFree (id->val.base); break; - case ID_TYP_IDL: identListFree (id->val.idl); break; + switch (identGetType(id)) { + case ID_TYP_EXP: expValueFree(id->val.base); break; + case ID_TYP_IDL: identListFree(id->val.idl); break; } id->key[0] = ID_TYP_EXP; - id->val.base = expValueClone (val); + id->val.base = expValueClone(val); return id; } s_ident * -identSetIdl (s_ident * id, s_identList * val) +identSetIdl(s_ident * id, s_identList * val) { - switch (identGetType (id)) - { - case ID_TYP_EXP: expValueFree (id->val.base); - case ID_TYP_IDL: identListFree (id->val.idl); + switch (identGetType (id)) { + case ID_TYP_EXP: expValueFree (id->val.base); + case ID_TYP_IDL: identListFree (id->val.idl); } id->key[0] = ID_TYP_IDL; id->val.idl = val; - + return id; } diff --git a/src/identList.c b/src/identList.c index 3c87480..b2d617e 100644 --- a/src/identList.c +++ b/src/identList.c @@ -21,20 +21,20 @@ struct identList */ static int -idHashCmp (void * _a, void * _b) +idHashCmp(void * _a, void * _b) { - s_ident * a = (s_ident *) _a; - s_ident * b = (s_ident *) _b; + s_ident * a = (s_ident *)_a; + s_ident * b = (s_ident *)_b; - return strcmp (identGetKey (a), identGetKey (b)); + return strcmp(identGetKey (a), identGetKey (b)); } static int -idIdxCmp (void * _a, void * _b) +idIdxCmp(void * _a, void * _b) { - s_ident * a = (s_ident *) _a; - s_ident * b = (s_ident *) _b; + s_ident * a = (s_ident *)_a; + s_ident * b = (s_ident *)_b; return identGetIdx (a) - identGetIdx (b); } @@ -48,140 +48,134 @@ idIdxCmp (void * _a, void * _b) * Contructors / Destructors for identList */ s_identList * -identListNew (void) +identListNew(void) { - s_identList * ret = (s_identList *) malloc (sizeof (s_identList)); + s_identList * ret = (s_identList *)malloc(sizeof(s_identList)); - ret->idIdx = bbTreeNew (idIdxCmp); - ret->idHash = bbTreeNew (idHashCmp); + ret->idIdx = bbTreeNew(idIdxCmp); + ret->idHash = bbTreeNew(idHashCmp); return ret; } void -identListFree (s_identList * l) +identListFree(s_identList * l) { s_ident ** idArray, ** _run; - for (_run = idArray = (s_ident **) identListToArray (l); + for ( + _run = idArray = (s_ident **)identListToArray(l); *_run != NULL; - _run++) + _run++ ) { - identDequeue (*_run); - if (! identIsQueued (*_run)) - identFree (*_run); + identDequeue(*_run); + if (! identIsQueued(*_run)) identFree(*_run); } - - free (idArray); - bbTreeFree (l->idIdx); - bbTreeFree (l->idHash); + free(idArray); - free (l); + bbTreeFree(l->idIdx); + bbTreeFree(l->idHash); + + free(l); } /* * insertions or deletion into a identList */ s_ident * -identListPutVal (s_identList * l, s_ident * val) +identListPutVal(s_identList * l, s_ident * val) { s_ident * oldVal; - int idx = identGetIdx (val); - char * key = identGetKey (val); + int idx = identGetIdx(val); + char * key = identGetKey(val); - identEnqueue (val); + identEnqueue(val); - if ((idx < 0 && key[0] == '\0') || idx != -1 && key[0] != '\0') + if ((idx < 0 && key[0] == '\0') || idx != -1 && key[0] != '\0') { /* calling error: either key or idx must be valid. But not both. */ return NULL; + } - if (idx >= 0) - oldVal = (s_ident *) bbTreeInsert (l->idIdx, val); - else - oldVal = (s_ident *) bbTreeInsert (l->idHash, val); - - if (oldVal != NULL) - /* - * these are few lines with a lot behind them. The not obvious question - * here is: What happens if oldval points to the same address than val? - * well, this could only happen if val was was formally queued, because - * else oldVal would be NULL. Knowing this makes clear that the queue - * state is not changed at all, because first it was increased above with - * identEnqueue and the it will be decreased in this if block again. But as - * it was formally queued it will not be freed (Good)! - */ - { - identDequeue (oldVal); - if (! identIsQueued (oldVal)) - identFree (oldVal); + if (idx >= 0) oldVal = (s_ident*)bbTreeInsert(l->idIdx, val); + else oldVal = (s_ident*)bbTreeInsert(l->idHash, val); + + if (oldVal != NULL) { + /* + * these are few lines with a lot behind them. The not obvious + * question here is: What happens if oldval points to the same address + * than val? well, this could only happen if val was was formally + * queued, because else oldVal would be NULL. Knowing this makes clear + * that the queue state is not changed at all, because first it was + * increased above with identEnqueue and the it will be decreased in + * this if block again. But as it was formally queued it will not be + * freed (Good)! + */ + identDequeue(oldVal); + if (! identIsQueued(oldVal)) identFree(oldVal); } return val; } s_ident * -identListPutExpByIdx (s_identList * l, int idx, s_expVal * val) +identListPutExpByIdx(s_identList * l, int idx, s_expVal * val) { - return identListPutVal (l, identExpNew (idx, NULL, val)); + return identListPutVal(l, identExpNew(idx, NULL, val)); } s_ident * -identListPutIdlByIdx (s_identList * l, int idx, s_identList * val) +identListPutIdlByIdx(s_identList * l, int idx, s_identList * val) { - return identListPutVal (l, identIdlNew (idx, NULL, val)); + return identListPutVal(l, identIdlNew(idx, NULL, val)); } s_ident * -identListPutExpByKey (s_identList * l, const char * key, s_expVal * val) +identListPutExpByKey(s_identList * l, const char * key, s_expVal * val) { - return identListPutVal (l, identExpNew (-1, key, val)); + return identListPutVal(l, identExpNew(-1, key, val)); } s_ident * -identListPutIdlByKey (s_identList * l, const char * key, s_identList * val) +identListPutIdlByKey(s_identList * l, const char * key, s_identList * val) { - return identListPutVal (l, identIdlNew (-1, key, val)); + return identListPutVal(l, identIdlNew(-1, key, val)); } void -identListRemoveByIdx (s_identList * l, int idx) +identListRemoveByIdx(s_identList * l, int idx) { - s_ident * seek = (s_ident *) identNew (idx, NULL); - s_ident * val = bbTreeRemove (l->idIdx, seek); + s_ident * seek = (s_ident*)identNew(idx, NULL); + s_ident * val = bbTreeRemove(l->idIdx, seek); #ifdef DEBUG2 - printf ("[!DEBUG!] seek (remove) identNew: %d/%s\n", idx, ""); + printf("[!DEBUG!] seek (remove) identNew: %d/%s\n", idx, ""); #endif - identFree (seek); + identFree(seek); - if (val != NULL) - { - identDequeue (val); - if (! identIsQueued (val)) - identFree (val); + if (val != NULL) { + identDequeue(val); + if (! identIsQueued(val)) identFree(val); } } void -identListRemoveByKey (s_identList * l, const char * key) +identListRemoveByKey(s_identList * l, const char * key) { - s_ident * seek = (s_ident *) identNew (-1, key); - s_ident * val = bbTreeRemove (l->idIdx, seek); + s_ident * seek = (s_ident*)identNew(-1, key); + s_ident * val = bbTreeRemove(l->idIdx, seek); #ifdef DEBUG2 - printf ("[!DEBUG!] seek (remove) identNew: %d/%s\n", -1, key); + printf("[!DEBUG!] seek (remove) identNew: %d/%s\n", -1, key); #endif - identFree (seek); + identFree(seek); - if (val != NULL) - { - identDequeue (val); - if (! identIsQueued (val)) - identFree (val); + if (val != NULL) { + identDequeue(val); + if (! identIsQueued(val)) identFree(val); } } @@ -189,50 +183,50 @@ identListRemoveByKey (s_identList * l, const char * key) * seeking in identList */ s_ident * -identListSeekIdx (s_identList * l, int idx) +identListSeekIdx(s_identList * l, int idx) { - s_ident * seek = (s_ident *) identNew (idx, NULL); - s_ident * val = (s_ident *) bbTreeSeek (l->idIdx, seek); + s_ident * seek = (s_ident*)identNew(idx, NULL); + s_ident * val = (s_ident*)bbTreeSeek(l->idIdx, seek); #ifdef DEBUG2 - printf ("[!DEBUG!] seek identNew: %d/%s\n", idx, ""); + printf("[!DEBUG!] seek identNew: %d/%s\n", idx, ""); #endif - identFree (seek); + identFree(seek); return val; } s_ident * -identListSeekKey (s_identList * l, const char * key) +identListSeekKey(s_identList * l, const char * key) { - s_ident * seek = (s_ident *) identNew (-1, key); - s_ident * val = (s_ident *) bbTreeSeek (l->idHash, seek); + s_ident * seek = (s_ident*)identNew(-1, key); + s_ident * val = (s_ident*)bbTreeSeek(l->idHash, seek); #ifdef DEBUG2 - printf ("[!DEBUG!] seek identNew: %d/%s\n", -1, key); + printf("[!DEBUG!] seek identNew: %d/%s\n", -1, key); #endif - identFree (seek); + identFree(seek); return val; } /* - * identList to other DataStructures + * identList to other DataStructures */ s_ident ** -identListToArray (s_identList * l) +identListToArray(s_identList * l) { - int idIdxLen = bbTreeSize (l->idIdx); - int idHashLen = bbTreeSize (l->idHash); + int idIdxLen = bbTreeSize(l->idIdx); + int idHashLen = bbTreeSize(l->idHash); int retLen = idIdxLen + idHashLen + 1; - s_ident ** ret = (s_ident **) malloc (sizeof (s_ident *) * retLen); + s_ident ** ret = (s_ident**)malloc(sizeof(s_ident*) * retLen); - memset (ret, 0, sizeof (s_ident **) * retLen); + memset(ret, 0, sizeof(s_ident**) * retLen); - bbTreeInOrder (l->idIdx, (void **) ret); - bbTreeInOrder (l->idHash, (void **) &(ret[idIdxLen])); + bbTreeInOrder(l->idIdx, (void**) ret); + bbTreeInOrder(l->idHash, (void**) &(ret[idIdxLen])); return ret; } diff --git a/src/statement.c b/src/statement.c index bb33d71..6cc7528 100644 --- a/src/statement.c +++ b/src/statement.c @@ -32,343 +32,331 @@ struct stmt static inline u_stmtType -stmtEval (s_stmt * stmt, s_block * actBlock, int op) +stmtEval(s_stmt * stmt, s_block * actBlock, int op) { s_stmtQueue * args = stmt->sQueue; - s_expVal * op1 = stmtDo (stmtQueueGet (args, 0), actBlock).eVal, - * op2 = stmtDo (stmtQueueGet (args, 1), actBlock).eVal, - * ret; - - ret = evalExpr (op, op1, op2); + s_expVal * op1 = stmtDo(stmtQueueGet(args, 0), actBlock).eVal, + * op2 = stmtDo(stmtQueueGet(args, 1), actBlock).eVal, + * ret; - if (op1 != NULL) expValueFree (op1); - if (op2 != NULL) expValueFree (op2); + ret = evalExpr(op, op1, op2); - return (u_stmtType) ret; + if (op1 != NULL) expValueFree(op1); + if (op2 != NULL) expValueFree(op2); + + return (u_stmtType)ret; } static inline u_stmtType -stmtIdentVar (s_stmt * stmt, s_block * actBlock) +stmtIdentVar(s_stmt * stmt, s_block * actBlock) { s_ident * ret = NULL; s_stmtQueue * args = stmt->sQueue; - s_expVal * _op = stmtDo (stmtQueueGet (args, 0), actBlock).eVal; - char * op = expValueString (_op); + s_expVal * _op = stmtDo(stmtQueueGet(args, 0), actBlock).eVal; + char * op = expValueString(_op); s_block * run = actBlock; - while (ret == NULL && run != NULL) - { - ret = identListSeekKey (blockIdl (run), op); - run = blockPrev (run); + while (ret == NULL && run != NULL) { + ret = identListSeekKey(blockIdl(run), op); + run = blockPrev(run); } - if (ret == NULL) - ret = identListPutVal (blockIdl (actBlock), identUndefNew (-1, op)); + if (ret == NULL) { + ret = identListPutVal(blockIdl(actBlock), identUndefNew(-1, op)); + } - expValueFree (_op); - free (op); + expValueFree(_op); + free(op); - return (u_stmtType) ret; + return (u_stmtType)ret; } static inline u_stmtType -stmtIdentArray (s_stmt * stmt, s_block * actBlock) +stmtIdentArray(s_stmt * stmt, s_block * actBlock) { s_stmtQueue * args = stmt->sQueue; - s_ident * op1 = stmtDo (stmtQueueGet (args, 0), actBlock).idVal; - s_expVal * op2 = stmtDo (stmtQueueGet (args, 1), actBlock).eVal; + s_ident * op1 = stmtDo(stmtQueueGet(args, 0), actBlock).idVal; + s_expVal * op2 = stmtDo(stmtQueueGet(args, 1), actBlock).eVal; s_ident * ret; - ret = getArray (op1, op2); + ret = getArray(op1, op2); - if (op2 != NULL) expValueFree (op2); + if (op2 != NULL) expValueFree(op2); - return (u_stmtType) ret; + return (u_stmtType)ret; } static inline u_stmtType -stmtIdentVal (s_stmt * stmt, s_block * actBlock) +stmtIdentVal(s_stmt * stmt, s_block * actBlock) { s_stmtQueue * args = stmt->sQueue; - s_ident * op1 = stmtDo (stmtQueueGet (args, 0), actBlock).idVal; + s_ident * op1 = stmtDo(stmtQueueGet(args, 0), actBlock).idVal; - return (u_stmtType) identExp (op1); + return (u_stmtType)identExp(op1); } static inline u_stmtType -stmtEvalComp (s_stmt * stmt, s_block * actBlock, int op) +stmtEvalComp(s_stmt * stmt, s_block * actBlock, int op) { s_stmtQueue * args = stmt->sQueue; - s_expVal * op1 = stmtDo (stmtQueueGet (args, 0), actBlock).eVal, - * op2 = stmtDo (stmtQueueGet (args, 1), actBlock).eVal; - int ret; - - ret = evalComp (op, op1, op2); + s_expVal * op1 = stmtDo(stmtQueueGet(args, 0), actBlock).eVal, + * op2 = stmtDo(stmtQueueGet(args, 1), actBlock).eVal; + int ret; + + ret = evalComp(op, op1, op2); - if (op1 != NULL) expValueFree (op1); - if (op2 != NULL) expValueFree (op2); + if (op1 != NULL) expValueFree(op1); + if (op2 != NULL) expValueFree(op2); - return (u_stmtType) ret; + return (u_stmtType)ret; } static inline u_stmtType -stmtAssign (s_stmt * stmt, s_block * actBlock) +stmtAssign(s_stmt * stmt, s_block * actBlock) { s_stmtQueue * args = stmt->sQueue; - s_ident * op1 = stmtDo (stmtQueueGet (args, 0), actBlock).idVal; - s_expVal * op2 = stmtDo (stmtQueueGet (args, 1), actBlock).eVal, - * ret; + s_ident * op1 = stmtDo(stmtQueueGet(args, 0), actBlock).idVal; + s_expVal * op2 = stmtDo(stmtQueueGet(args, 1), actBlock).eVal, + * ret; - if (op1 == NULL) - op1 = getVariable (NULL, NULL); + if (op1 == NULL) op1 = getVariable(NULL, NULL); - ret = assign (op1, op2); + ret = assign(op1, op2); - if (op2 != NULL) expValueFree (op2); + if (op2 != NULL) expValueFree(op2); - return (u_stmtType) ret; + return (u_stmtType)ret; } static inline u_stmtType -stmtPrint (s_stmt * stmt, s_block * actBlock) +stmtPrint(s_stmt * stmt, s_block * actBlock) { s_stmtQueue * args = stmt->sQueue; - s_expVal * op = stmtDo (stmtQueueGet (args, 0), actBlock).eVal; + s_expVal * op = stmtDo(stmtQueueGet(args, 0), actBlock).eVal; - if (op != NULL) - { - printExpr (op); - expValueFree (op); + if (op != NULL) { + printExpr(op); + expValueFree(op); } - return (u_stmtType) 0; + return (u_stmtType)0; } static inline u_stmtType -stmtEvalCondExpr (s_stmt * stmt, s_block * actBlock) +stmtEvalCondExpr(s_stmt * stmt, s_block * actBlock) { s_stmtQueue * args = stmt->sQueue; - s_expVal * op = stmtDo (stmtQueueGet (args, 0), actBlock).eVal; - int ret; + s_expVal * op = stmtDo(stmtQueueGet(args, 0), actBlock).eVal; + int ret; - ret = evalCondExpr (op); + ret = evalCondExpr(op); - if (op != NULL) expValueFree (op); + if (op != NULL) expValueFree(op); - return (u_stmtType) ret; + return (u_stmtType)ret; } static inline u_stmtType -stmtEvalCond (s_stmt * stmt, s_block * actBlock, int op) +stmtEvalCond(s_stmt * stmt, s_block * actBlock, int op) { s_stmtQueue * args = stmt->sQueue; - int op1 = stmtDo (stmtQueueGet (args, 0), actBlock).cond; + int op1 = stmtDo(stmtQueueGet(args, 0), actBlock).cond; - switch (op) - { + switch (op) { case LOGAND: { - int op2 = stmtDo (stmtQueueGet (args, 1), actBlock).cond; - return (u_stmtType) (op1 && op2); + int op2 = stmtDo(stmtQueueGet(args, 1), actBlock).cond; + return (u_stmtType)(op1 && op2); } - case LOGOR: { - int op2 = stmtDo (stmtQueueGet (args, 1), actBlock).cond; - return (u_stmtType) (op1 || op2); + int op2 = stmtDo(stmtQueueGet(args, 1), actBlock).cond; + return (u_stmtType)(op1 || op2); } - case LOGNEG: - return (u_stmtType) (! op1); + return (u_stmtType)(! op1); } } static inline u_stmtType -stmtCastInt (s_stmt * stmt, s_block * actBlock) +stmtCastInt(s_stmt * stmt, s_block * actBlock) { s_stmtQueue * args = stmt->sQueue; - s_expVal * op = stmtDo (stmtQueueGet (args, 0), actBlock).eVal, - * ret; + s_expVal * op = stmtDo(stmtQueueGet(args, 0), actBlock).eVal, + * ret; - ret = castExprToInt (op); + ret = castExprToInt(op); - if (op != NULL) expValueFree (op); + if (op != NULL) expValueFree(op); - return (u_stmtType) ret; + return (u_stmtType)ret; } static inline u_stmtType -stmtCastFloat (s_stmt * stmt, s_block * actBlock) +stmtCastFloat(s_stmt * stmt, s_block * actBlock) { s_stmtQueue * args = stmt->sQueue; s_expVal * op = stmtDo (stmtQueueGet (args, 0), actBlock).eVal, * ret; - ret = castExprToFloat (op); + ret = castExprToFloat(op); - if (op != NULL) expValueFree (op); + if (op != NULL) expValueFree(op); - return (u_stmtType) ret; + return (u_stmtType)ret; } static inline u_stmtType -stmtCastString (s_stmt * stmt, s_block * actBlock) +stmtCastString(s_stmt * stmt, s_block * actBlock) { s_stmtQueue * args = stmt->sQueue; - s_expVal * op = stmtDo (stmtQueueGet (args, 0), actBlock).eVal, - * ret; + s_expVal * op = stmtDo(stmtQueueGet (args, 0), actBlock).eVal, + * ret; - ret = castExprToString (op); + ret = castExprToString(op); - if (op != NULL) expValueFree (op); + if (op != NULL) expValueFree(op); - return (u_stmtType) ret; + return (u_stmtType)ret; } static inline u_stmtType -stmtBlock (s_stmt * stmt, s_block * actBlock) +stmtBlock(s_stmt * stmt, s_block * actBlock) { unsigned int i; - s_stmtQueue * gIds = stmtQueueGet (stmt->sQueue, 0)->sQueue; - s_stmtQueue * args = stmtQueueGet (stmt->sQueue, 1)->sQueue; + s_stmtQueue * gIds = stmtQueueGet(stmt->sQueue, 0)->sQueue; + s_stmtQueue * args = stmtQueueGet(stmt->sQueue, 1)->sQueue; s_block * gBlock = NULL; - gBlock = actBlock = blockPush (&actBlock, blockNew (args)); + gBlock = actBlock = blockPush(&actBlock, blockNew (args)); /* find the global block */ - while (blockPrev (gBlock) != NULL) - gBlock = blockPrev (gBlock); - - if (gIds != NULL) - { - for (i = 0; i < stmtQueueGetSize (gIds); i++) - { - s_expVal * tmp = stmtDo (stmtQueueGet (gIds, i), actBlock).eVal; + while (blockPrev(gBlock) != NULL) + gBlock = blockPrev(gBlock); + + if (gIds != NULL) { + for (i=0; i < stmtQueueGetSize(gIds); i++) { + s_expVal * tmp = stmtDo(stmtQueueGet (gIds, i), actBlock).eVal; char * _id; s_ident * id; - if (tmp == NULL) - exitError (0); + if (tmp == NULL) exitError (0); - _id = expValueString (tmp); - id = identListSeekKey (blockGetIdl (gBlock), _id); - expValueFree (tmp); - free (_id); + _id = expValueString(tmp); + id = identListSeekKey(blockGetIdl (gBlock), _id); + expValueFree(tmp); + free(_id); - if (id == NULL) - exitError (0); + if (id == NULL) exitError (0); - blockSetNonLocalId (gBlock, id); + blockSetNonLocalId(gBlock, id); } } - blockDo (actBlock); - blockFree (actBlock); + blockDo(actBlock); + blockFree(actBlock); - return (u_stmtType) 0; + return (u_stmtType)0; } static inline u_stmtType -stmtIf (s_stmt * stmt, s_block * actBlock) +stmtIf(s_stmt * stmt, s_block * actBlock) { s_stmtQueue * args = stmt->sQueue; - int cond = stmtDo (stmtQueueGet (args, 0), actBlock).cond; + int cond = stmtDo(stmtQueueGet (args, 0), actBlock).cond; s_stmt * _do; - if (cond) - _do = stmtQueueGet (args, 1); - else - _do = stmtQueueGet (args, 2); + if (cond) _do = stmtQueueGet(args, 1); + else _do = stmtQueueGet(args, 2); - if (_do != NULL) - stmtDo (_do, actBlock); + if (_do != NULL) stmtDo(_do, actBlock); - return (u_stmtType) 0; + return (u_stmtType)0; } static inline u_stmtType -stmtForeach (s_stmt * stmt, s_block * actBlock) +stmtForeach(s_stmt * stmt, s_block * actBlock) { s_stmtQueue * args = stmt->sQueue; - s_expVal * _id = stmtDo (stmtQueueGet (args, 0), actBlock).eVal, - * _key = stmtDo (stmtQueueGet (args, 1), actBlock).eVal, - * _val = stmtDo (stmtQueueGet (args, 2), actBlock).eVal; - char * id = expValueString (_id); - char * key = expValueString (_key); - char * val = expValueString (_val); - - printf ("[DEBUG]found foreach statement: id=%s, key=%s, val=%s\n", + s_expVal * _id = stmtDo(stmtQueueGet (args, 0), actBlock).eVal, + * _key = stmtDo(stmtQueueGet (args, 1), actBlock).eVal, + * _val = stmtDo(stmtQueueGet (args, 2), actBlock).eVal; + char * id = expValueString(_id); + char * key = expValueString(_key); + char * val = expValueString(_val); + + printf("[DEBUG]found foreach statement: id=%s, key=%s, val=%s\n", id, key, val); - free (id); - free (key); - free (val); + free(id); + free(key); + free(val); - expValueFree (_id); - expValueFree (_key); - expValueFree (_val); + expValueFree(_id); + expValueFree(_key); + expValueFree(_val); - return (u_stmtType) 0; + return (u_stmtType)0; } static inline u_stmtType -stmtRepeat (s_stmt * stmt, s_block * actBlock) +stmtRepeat(s_stmt * stmt, s_block * actBlock) { s_stmtQueue * args = stmt->sQueue; - s_expVal * _id = stmtDo (stmtQueueGet (args, 0), actBlock).eVal, - * _count = stmtDo (stmtQueueGet (args, 1), actBlock).eVal; - char * id = expValueString (_id); - int count = expValueInt (_count); + s_expVal * _id = stmtDo(stmtQueueGet (args, 0), actBlock).eVal, + * _count = stmtDo(stmtQueueGet (args, 1), actBlock).eVal; + char * id = expValueString(_id); + int count = expValueInt(_count); - printf ("[DEBUG]found repeat statement: id=%s, count=%d\n", id, count); + printf("[DEBUG]found repeat statement: id=%s, count=%d\n", id, count); - free (id); + free(id); - expValueFree (_id); - expValueFree (_count); + expValueFree(_id); + expValueFree(_count); - return (u_stmtType) 0; + return (u_stmtType)0; } /* * Interface */ s_stmt * -stmtNew (int id, s_stmtQueue * queue, int conTyp, u_stmtType con) +stmtNew(int id, s_stmtQueue * queue, int conTyp, u_stmtType con) { - s_stmt * new = (s_stmt *) malloc (sizeof (s_stmt)); + s_stmt * new = (s_stmt *)malloc(sizeof(s_stmt)); new->sId = id; new->sQueue = queue; @@ -379,82 +367,102 @@ stmtNew (int id, s_stmtQueue * queue, int conTyp, u_stmtType con) } s_stmtQueue * -stmtGetArgs (s_stmt * stmt) +stmtGetArgs(s_stmt * stmt) { return stmt->sQueue; } u_stmtType -stmtGetVal (s_stmt * stmt) +stmtGetVal(s_stmt * stmt) { return stmt->sConst; } int -stmtGetConstTyp (s_stmt * stmt) +stmtGetConstTyp(s_stmt * stmt) { return stmt->sConstTyp; } void -stmtFree (s_stmt * stmt) +stmtFree(s_stmt * stmt) { - if (stmt == NULL) - return; - - switch (stmt->sConstTyp) - { - case STYP_NONE: stmtQueueFree (stmt->sQueue); break; - case STYP_EVAL: expValueFree (stmt->sConst.eVal); break; - case STYP_IDVAL: identFree (stmt->sConst.idVal); break; + if (stmt == NULL) return; + + switch(stmt->sConstTyp) { + case STYP_NONE: stmtQueueFree (stmt->sQueue); break; + case STYP_EVAL: expValueFree (stmt->sConst.eVal); break; + case STYP_IDVAL: identFree (stmt->sConst.idVal); break; } - free (stmt); + free(stmt); } u_stmtType -stmtDo (s_stmt * stmt, s_block * actBlock) +stmtDo(s_stmt * stmt, s_block * actBlock) { - if (stmt == NULL) - return (u_stmtType) 0; - - switch (stmt->sId) - { - case STMT_CONST: return (u_stmtType) expValueClone (stmt->sConst.eVal); - case STMT_BLOCK: return stmtBlock (stmt, actBlock); - - case STMT_PRINT: return stmtPrint (stmt, actBlock); - case STMT_IF: return stmtIf (stmt, actBlock); - case STMT_FOREACH: return stmtForeach (stmt, actBlock); - case STMT_REPEAT: return stmtRepeat (stmt, actBlock); - case STMT_ASSIGN: return stmtAssign (stmt, actBlock); - case STMT_UNSET: return (u_stmtType) 0; - - case STMT_EVAL_PLUS: return stmtEval (stmt, actBlock, PLUS); - case STMT_EVAL_MINUS: return stmtEval (stmt, actBlock, MINUS); - case STMT_EVAL_TIMES: return stmtEval (stmt, actBlock, TIMES); - case STMT_EVAL_OVER: return stmtEval (stmt, actBlock, OVER); - case STMT_EVAL_MODULO: return stmtEval (stmt, actBlock, MODULO); - case STMT_EVAL_NEG: return stmtEval (stmt, actBlock, NEG); - - case STMT_IDENT_VAR: return stmtIdentVar (stmt, actBlock); - case STMT_IDENT_ARRAY: return stmtIdentArray (stmt, actBlock); - case STMT_IDENT_VAL: return stmtIdentVal (stmt, actBlock); - - case STMT_CAST_INT: return stmtCastInt (stmt, actBlock); - case STMT_CAST_FLOAT: return stmtCastFloat (stmt, actBlock); - case STMT_CAST_STRING: return stmtCastString (stmt, actBlock); - - case STMT_COMP_EQ: return stmtEvalComp (stmt, actBlock, EQ); - case STMT_COMP_NE: return stmtEvalComp (stmt, actBlock, NE); - case STMT_COMP_LT: return stmtEvalComp (stmt, actBlock, LT); - case STMT_COMP_GT: return stmtEvalComp (stmt, actBlock, GT); - case STMT_COMP_LE: return stmtEvalComp (stmt, actBlock, LE); - case STMT_COMP_GE: return stmtEvalComp (stmt, actBlock, GE); - - case STMT_COND_EXPR: return stmtEvalCondExpr (stmt, actBlock); - case STMT_COND_AND: return stmtEvalCond (stmt, actBlock, LOGAND); - case STMT_COND_OR: return stmtEvalCond (stmt, actBlock, LOGOR); - case STMT_COND_NEG: return stmtEvalCond (stmt, actBlock, LOGNEG); + if (stmt == NULL) return (u_stmtType)0; + + switch (stmt->sId) { + case STMT_CONST: + return (u_stmtType)expValueClone(stmt->sConst.eVal); + case STMT_BLOCK: + return stmtBlock(stmt, actBlock); + case STMT_PRINT: + return stmtPrint(stmt, actBlock); + case STMT_IF: + return stmtIf(stmt, actBlock); + case STMT_FOREACH: + return stmtForeach(stmt, actBlock); + case STMT_REPEAT: + return stmtRepeat(stmt, actBlock); + case STMT_ASSIGN: + return stmtAssign(stmt, actBlock); + case STMT_UNSET: + return (u_stmtType)0; + case STMT_EVAL_PLUS: + return stmtEval(stmt, actBlock, PLUS); + case STMT_EVAL_MINUS: + return stmtEval(stmt, actBlock, MINUS); + case STMT_EVAL_TIMES: + return stmtEval(stmt, actBlock, TIMES); + case STMT_EVAL_OVER: + return stmtEval(stmt, actBlock, OVER); + case STMT_EVAL_MODULO: + return stmtEval(stmt, actBlock, MODULO); + case STMT_EVAL_NEG: + return stmtEval(stmt, actBlock, NEG); + case STMT_IDENT_VAR: + return stmtIdentVar(stmt, actBlock); + case STMT_IDENT_ARRAY: + return stmtIdentArray(stmt, actBlock); + case STMT_IDENT_VAL: + return stmtIdentVal(stmt, actBlock); + case STMT_CAST_INT: + return stmtCastInt(stmt, actBlock); + case STMT_CAST_FLOAT: + return stmtCastFloat(stmt, actBlock); + case STMT_CAST_STRING: + return stmtCastString(stmt, actBlock); + case STMT_COMP_EQ: + return stmtEvalComp(stmt, actBlock, EQ); + case STMT_COMP_NE: + return stmtEvalComp(stmt, actBlock, NE); + case STMT_COMP_LT: + return stmtEvalComp(stmt, actBlock, LT); + case STMT_COMP_GT: + return stmtEvalComp(stmt, actBlock, GT); + case STMT_COMP_LE: + return stmtEvalComp(stmt, actBlock, LE); + case STMT_COMP_GE: + return stmtEvalComp(stmt, actBlock, GE); + case STMT_COND_EXPR: + return stmtEvalCondExpr(stmt, actBlock); + case STMT_COND_AND: + return stmtEvalCond(stmt, actBlock, LOGAND); + case STMT_COND_OR: + return stmtEvalCond(stmt, actBlock, LOGOR); + case STMT_COND_NEG: + return stmtEvalCond(stmt, actBlock, LOGNEG); } } diff --git a/src/stmtQueue.c b/src/stmtQueue.c index 289d335..293b208 100644 --- a/src/stmtQueue.c +++ b/src/stmtQueue.c @@ -8,26 +8,26 @@ #include /* give the queue an initial size for 10 statements */ -#define QUEUE_GROW_SIZE 10 +#define QUEUE_GROW_SIZE 10 struct stmtQueue { - s_stmt ** stmts; /* a dynamically growing array of statements. */ + s_stmt ** stmts; /* a dynamically growing array of statements. */ - unsigned int size; /* the actual size of the array in statements */ - unsigned int maxIdx; /* the maximum index used in the array. */ + unsigned int size; /* the actual size of the array in statements */ + unsigned int maxIdx; /* the maximum index used in the array. */ }; s_stmtQueue * -stmtQueueNew (void) +stmtQueueNew(void) { - s_stmtQueue * new = (s_stmtQueue *) malloc (sizeof (s_stmtQueue)); + s_stmtQueue * new = (s_stmtQueue*)malloc(sizeof(s_stmtQueue)); new->size = QUEUE_GROW_SIZE; new->maxIdx = 0; - new->stmts = (s_stmt **) calloc (sizeof (s_stmt *), new->size); - memset (new->stmts, 0, sizeof (s_stmt *) * new->size); + new->stmts = (s_stmt**)calloc(sizeof(s_stmt*), new->size); + memset(new->stmts, 0, sizeof(s_stmt *) * new->size); return new; } @@ -36,92 +36,88 @@ stmtQueueNew (void) * This concat does not change a or b, as opposed to the c strcat function */ s_stmtQueue * -stmtQueueConcat (s_stmtQueue * a, s_stmtQueue * b) +stmtQueueConcat(s_stmtQueue * a, s_stmtQueue * b) { - s_stmtQueue * new = (s_stmtQueue *) malloc (sizeof (s_stmtQueue)); + s_stmtQueue * new = (s_stmtQueue*)malloc(sizeof(s_stmtQueue)); new->size = a->size + b->size; new->maxIdx = a->maxIdx + b->maxIdx; - new->stmts = (s_stmt **) calloc (sizeof (s_stmt *), new->size); - memset (new->stmts, 0, sizeof (s_stmt *) * new->size); + new->stmts = (s_stmt**)calloc(sizeof(s_stmt*), new->size); + memset(new->stmts, 0, sizeof(s_stmt*) * new->size); - memcpy (new->stmts, a->stmts, sizeof (s_stmt *) * a->maxIdx); - memcpy (&(new->stmts[a->maxIdx]), b->stmts, sizeof (s_stmt *) * b->maxIdx); + memcpy(new->stmts, a->stmts, sizeof(s_stmt*) * a->maxIdx); + memcpy(&(new->stmts[a->maxIdx]), b->stmts, sizeof(s_stmt*) * b->maxIdx); return new; } void -stmtQueueFree (s_stmtQueue * sQueue) +stmtQueueFree(s_stmtQueue * sQueue) { unsigned int i; - if (sQueue == NULL) - return; + if (sQueue == NULL) return; /* freeing the queue is the final step. All stmts should be freed too! */ - for (i = 0; i < sQueue->maxIdx; i++) - stmtFree (sQueue->stmts [i]); + for (i=0; i < sQueue->maxIdx; i++) + stmtFree(sQueue->stmts[i]); - free (sQueue->stmts); - free (sQueue); + free(sQueue->stmts); + free(sQueue); } void -stmtQueueEnqueue (s_stmtQueue * sQueue, s_stmt * stmt) +stmtQueueEnqueue(s_stmtQueue * sQueue, s_stmt * stmt) { if (sQueue->maxIdx >= sQueue->size) { s_stmt ** stmtsOld = sQueue->stmts; unsigned int newSize = sQueue->size + QUEUE_GROW_SIZE; - sQueue->stmts = (s_stmt **) calloc (sizeof (s_stmt *), newSize); - memcpy (sQueue->stmts, stmtsOld, sizeof (s_stmt **) * sQueue->size); - free (stmtsOld); /* free the old queue but keep the statements */ - memset ( - &(sQueue->stmts[sQueue->size]), - 0, - sizeof (s_stmt **) * QUEUE_GROW_SIZE); - sQueue->size = newSize;; + sQueue->stmts = (s_stmt**)calloc(sizeof(s_stmt*), newSize); + memcpy (sQueue->stmts, stmtsOld, sizeof(s_stmt**) * sQueue->size); + free(stmtsOld); /* free the old queue but keep the statements */ + memset( + &(sQueue->stmts[sQueue->size]), + 0, + sizeof(s_stmt**) * QUEUE_GROW_SIZE); + sQueue->size = newSize; } - sQueue->stmts[sQueue->maxIdx ++] = stmt; + sQueue->stmts[sQueue->maxIdx++] = stmt; } s_stmt * -stmtQueueDequeue (s_stmtQueue * sQueue) +stmtQueueDequeue(s_stmtQueue * sQueue) { s_stmt * ret = sQueue->stmts[sQueue->maxIdx - 1]; - sQueue->stmts[-- sQueue->maxIdx] = NULL; + sQueue->stmts[--sQueue->maxIdx] = NULL; return ret; } s_stmt * -stmtQueueGet (s_stmtQueue * sQueue, unsigned int idx) +stmtQueueGet(s_stmtQueue * sQueue, unsigned int idx) { - if (idx < sQueue->size) - return sQueue->stmts [idx]; - else - return NULL; + if (idx < sQueue->size) return sQueue->stmts[idx]; + else return NULL; } void -stmtQueueDo (s_stmtQueue * sQueue) +stmtQueueDo(s_stmtQueue * sQueue) { int i; - if (sQueue == NULL) - return; + if (sQueue == NULL) return; for (i = 0; i < sQueue->maxIdx; i++) - stmtDo (sQueue->stmts[i], NULL /* !!!FIXME: give me a sane value!!! */); + stmtDo(sQueue->stmts[i], NULL /* !!!FIXME: give me a sane value!!! */); } - unsigned +unsigned int -stmtQueueGetSize (s_stmtQueue * sQueue) +stmtQueueGetSize(s_stmtQueue * sQueue) { return sQueue->size; } diff --git a/src/tepal.c b/src/tepal.c index 501ec19..e058770 100644 --- a/src/tepal.c +++ b/src/tepal.c @@ -18,47 +18,44 @@ char * tepalErrMsg[] = }; void -exitError (int errNum, ...) +exitError(int errNum, ...) { va_list ap; - va_start (ap, errNum); + va_start(ap, errNum); - switch (errNum) - { + switch (errNum) { case ERR_UNDEF_VAR: - fprintf (stderr, tepalErrMsg[errNum], va_arg(ap, char *)); + fprintf(stderr, tepalErrMsg[errNum], va_arg(ap, char *)); break; - case ERR_NO_INDEX: - fprintf (stderr, tepalErrMsg[errNum], va_arg(ap, char *)); + fprintf(stderr, tepalErrMsg[errNum], va_arg(ap, char *)); break; - case ERR_STRING_OPERATOR: - fprintf (stderr, tepalErrMsg[errNum], va_arg(ap, int)); + fprintf(stderr, tepalErrMsg[errNum], va_arg(ap, int)); break; - case ERR_FLOAT_OPERATOR: - fprintf (stderr, tepalErrMsg[errNum], va_arg(ap, int)); + fprintf(stderr, tepalErrMsg[errNum], va_arg(ap, int)); break; } - va_end (ap); - exit (errNum); + va_end(ap); + exit(errNum); } void printExpr (s_expVal * val) { - switch (expValueGetType (val)) - { - case EXP_TYP_INT: printf ("%d", expValueInt (val)); break; - case EXP_TYP_FLOAT: printf ("%f", expValueFloat (val)); break; - case EXP_TYP_STRING: + switch (expValueGetType (val)) { + case EXP_TYP_INT: + printf("%d", expValueInt (val)); break; + case EXP_TYP_FLOAT: + printf("%f", expValueFloat (val)); break; + case EXP_TYP_STRING: { - char * v = expValueString (val); - printf (v); - free (v); + char * v = expValueString(val); + printf(v); + free(v); } } } diff --git a/src/tepal_pars.y b/src/tepal_pars.y index 7afcacd..9d6a20d 100644 --- a/src/tepal_pars.y +++ b/src/tepal_pars.y @@ -18,25 +18,25 @@ extern int yylex (void); - int + int yywrap () -{ - return 1; +{ + return 1; } - void + void yyerror (char const * s) { fprintf(stderr, "%s\n", s); } -/* +/* * globale Variablen */ -extern FILE * yyin; -extern s_block * _globalBlock_; +extern FILE * yyin; +extern s_block * _globalBlock_; -s_stmtQueue * astRoot; +s_stmtQueue * astRoot; /* * statische Funktionen (zum lokalen gebrauch) @@ -59,9 +59,9 @@ enqGlobId (s_stmtQueue * sQueue, char * id) { s_stmtQueue * ret = (sQueue == NULL) ? stmtQueueNew () : sQueue; s_stmt * val = stmtNew ( - STMT_CONST, - NULL, - STYP_EVAL, + STMT_CONST, + NULL, + STYP_EVAL, (u_stmtType) expValueStringNew (id)); stmtQueueEnqueue (ret, val); @@ -117,9 +117,8 @@ genStringStmt (s_stmt * stmt, char * string) s_expVal * eVal = NULL; char * new = NULL; - - if (stmt != NULL) - { + + if (stmt != NULL) { char * old = expValueString (stmtGetVal (stmt).eVal); size_t len = strlen (old) + strlen (string) + 1; @@ -130,8 +129,7 @@ genStringStmt (s_stmt * stmt, char * string) stmtFree (stmt); free (old); } - else - { + else { new = (char *)calloc (sizeof (char), strlen (string) + 1); strcpy (new, string); } @@ -187,7 +185,7 @@ genRepStmt (char * _ident, long _count, s_stmt * stmt) u_stmtType _co = (u_stmtType) expValueIntNew (_count); s_stmt * ident = stmtNew (STMT_CONST, NULL, STYP_EVAL, _id); s_stmt * count = stmtNew (STMT_CONST, NULL, STYP_EVAL, _co); - + s_stmtQueue * sQueue = stmtQueueNew (); free (_ident); @@ -366,24 +364,24 @@ genCastStringStmt (s_stmt * stmt) %} -%token STMT_END ';' REPEAT COUNT FOREACH AS IF ELSE BLOCK_END UNSET -%token ICAST FCAST SCAST GLOBAL PARENT /* for explicit casts */ -%token IDENT -%token HTML -%token INT -%token FLOAT -%token STRING -%token EOL - -%left LOGAND LOGOR -%left LOGNEG -%nonassoc NE EQ LT GT LE GE - -%right '=' -%left '+' '-' -%left '*' '/' '%' -%left NEG -%nonassoc '(' ')' +%token STMT_END ';' REPEAT COUNT FOREACH AS IF ELSE BLOCK_END UNSET +%token ICAST FCAST SCAST GLOBAL PARENT /* for explicit casts */ +%token IDENT +%token HTML +%token INT +%token FLOAT +%token STRING +%token EOL + +%left LOGAND LOGOR +%left LOGNEG +%nonassoc NE EQ LT GT LE GE + +%right '=' +%left '+' '-' +%left '*' '/' '%' +%left NEG +%nonassoc '(' ')' %union { long iVal; @@ -396,22 +394,22 @@ genCastStringStmt (s_stmt * stmt) /* Es wird wohl darauf hinauslaufen das alles erstmal ein stmt wird und in den AST wandert, und erst dann ausgewertet wird. Anders wird es wohl nicht gehen. */ -%type assig; -%type ident; -%type cond; -%type comp; -%type expr; -%type html; -%type block_stmt; -%type simple_stmt; -%type if_stmt; -%type rep_stmt; -%type fore_stmt; -%type stmt; -%type stmt_queue; -%type block_queue; -%type glob_decl; -%type gdecl_block; +%type assig; +%type ident; +%type cond; +%type comp; +%type expr; +%type html; +%type block_stmt; +%type simple_stmt; +%type if_stmt; +%type rep_stmt; +%type fore_stmt; +%type stmt; +%type stmt_queue; +%type block_queue; +%type glob_decl; +%type gdecl_block; %expect 1 @@ -419,41 +417,33 @@ genCastStringStmt (s_stmt * stmt) %% -script : stmt_queue { astRoot = $1; } -; +script : stmt_queue { astRoot = $1; }; -stmt_queue : stmt { $$ = enqStmt (NULL, $1); } - | stmt_queue stmt { $$ = enqStmt ($1, $2); } -; +stmt_queue : stmt { $$ = enqStmt (NULL, $1); } + | stmt_queue stmt { $$ = enqStmt ($1, $2); }; -stmt : simple_stmt - | block_stmt - | if_stmt - | rep_stmt - | fore_stmt - | stmt_end stmt { $$ = $2; } -; +stmt : simple_stmt + | block_stmt + | if_stmt + | rep_stmt + | fore_stmt + | stmt_end stmt { $$ = $2; }; -simple_stmt : html stmt_end { $$ = genPrintStmt ($1); } - | expr stmt_end { $$ = genPrintStmt ($1); } - | assig stmt_end -; +simple_stmt : html stmt_end { $$ = genPrintStmt ($1); } + | expr stmt_end { $$ = genPrintStmt ($1); } + | assig stmt_end; -html : HTML { $$ = genStringStmt (NULL, $1); } - | html HTML { $$ = genStringStmt ($1, $2); } -; +html : HTML { $$ = genStringStmt (NULL, $1); } + | html HTML { $$ = genStringStmt ($1, $2); }; -stmt_end : STMT_END - | ';' -; +stmt_end : STMT_END + | ';'; -block_stmt : ':' BLOCK_END { $$ = genBlockStmt (NULL); } - | ':' block_queue BLOCK_END { $$ = genBlockStmt ($2); } -; +block_stmt : ':' BLOCK_END { $$ = genBlockStmt (NULL); } + | ':' block_queue BLOCK_END { $$ = genBlockStmt ($2); }; -block_queue : stmt_queue { $$ = genBlockQue (NULL, $1); } - | gdecl_block stmt_queue { $$ = genBlockQue ($1, $2); } -; +block_queue : stmt_queue { $$ = genBlockQue (NULL, $1); } + | gdecl_block stmt_queue { $$ = genBlockQue ($1, $2); }; /* * Here follows the definitions for the "globals definition block" @@ -462,77 +452,64 @@ block_queue : stmt_queue { $$ = genBlockQue (NULL, $1); } * block a newline should not be interpreted as statmend-end. * ------ */ -gdecl_block : GLOBAL ':' glob_decl BLOCK_END - { $$ = $3; } -; +gdecl_block : GLOBAL ':' glob_decl BLOCK_END { $$ = $3; }; -glob_decl : IDENT { $$ = enqGlobId (NULL, $1); } - | glob_decl ',' IDENT { $$ = enqGlobId ($1, $3); } -; +glob_decl : IDENT { $$ = enqGlobId (NULL, $1); } + | glob_decl ',' IDENT { $$ = enqGlobId ($1, $3); }; /* * ------ */ /* here is 1 shift/reduce conflict, but thats ok. */ -if_stmt : IF '(' cond ')' stmt { $$ = genIfStmt ($3, $5, NULL); } - | IF '(' cond ')' stmt ELSE stmt - { $$ = genIfStmt ($3, $5, $7); } -; - -rep_stmt : REPEAT '(' IDENT COUNT '=' INT ')' stmt - { $$ = genRepStmt ($3, $6, $8); } -; - -fore_stmt : FOREACH '(' IDENT AS IDENT ',' IDENT ')' stmt - { $$ = genForeStmt ($3, $5, $7, $9); } -; - -cond : expr { $$ = genCondExprStmt ($1); } - | comp { $$ = $1; } - | '(' comp ')' { $$ = $2; } - | '!' cond %prec LOGNEG { $$ = genOpStmt (LOGNEG, $2, NULL); } - | '(' '!' cond ')' %prec LOGNEG - { $$ = genOpStmt (LOGNEG, $3, NULL); } - | cond LOGAND cond { $$ = genOpStmt (LOGAND, $1, $3); } - | '(' cond LOGAND cond ')' { $$ = genOpStmt (LOGAND, $2, $4); } - | cond LOGOR cond { $$ = genOpStmt (LOGOR, $1, $3); } - | '(' cond LOGOR cond ')' { $$ = genOpStmt (LOGOR, $2, $4); } -; - -comp : expr EQ expr { $$ = genOpStmt (EQ, $1, $3); } - | expr NE expr { $$ = genOpStmt (NE, $1, $3); } - | expr LT expr { $$ = genOpStmt (LT, $1, $3); } - | expr GT expr { $$ = genOpStmt (GT, $1, $3); } - | expr LE expr { $$ = genOpStmt (LE, $1, $3); } - | expr GE expr { $$ = genOpStmt (GE, $1, $3); } -; - -assig : ident '=' expr { $$ = genAssignStmt ($1, $3); } - | ident '=' assig { $$ = genAssignStmt ($1, $3); } -; - -ident : IDENT { $$ = genIdentVarStmt ($1); } - | ident '[' expr ']' { $$ = genIdentArrStmt ($1, $3); } -; - -expr : ident { $$ = genIdentValStmt ($1); } - | INT { $$ = genIntStmt ($1); } - | FLOAT { $$ = genFloatStmt ($1); } - | STRING { $$ = genStringStmt (NULL, $1); } - | EOL { $$ = genStringStmt (NULL, $1); } - - | '(' ICAST ')' expr { $$ = genCastIntStmt ($4); } - | '(' FCAST ')' expr { $$ = genCastFloatStmt ($4); } - | '(' SCAST ')' expr { $$ = genCastStringStmt ($4); } - | '(' expr ')' { $$ = $2; } - - | '-' expr %prec NEG { $$ = genOpStmt (NEG, $2, NULL); } - | expr '+' expr { $$ = genOpStmt (PLUS, $1, $3); } - | expr '-' expr { $$ = genOpStmt (MINUS, $1, $3); } - | expr '*' expr { $$ = genOpStmt (TIMES, $1, $3); } - | expr '/' expr { $$ = genOpStmt (OVER, $1, $3); } - | expr '%' expr { $$ = genOpStmt (MODULO, $1, $3); } -; +if_stmt : IF '(' cond ')' stmt { $$ = genIfStmt ($3, $5, NULL); } + | IF '(' cond ')' stmt ELSE stmt { $$ = genIfStmt ($3, $5, $7); }; + +rep_stmt : REPEAT '(' IDENT COUNT '=' INT ')' stmt { + $$ = genRepStmt($3, $6, $8); }; + +fore_stmt : FOREACH '(' IDENT AS IDENT ',' IDENT ')' stmt { + $$ = genForeStmt($3, $5, $7, $9); }; + +cond : expr { $$ = genCondExprStmt($1); } + | comp { $$ = $1; } + | '(' comp ')' { $$ = $2; } + | '!' cond %prec LOGNEG { $$ = genOpStmt(LOGNEG, $2, NULL); } + | '(' '!' cond ')' %prec LOGNEG { $$ = genOpStmt(LOGNEG, $3, NULL); } + | cond LOGAND cond { $$ = genOpStmt(LOGAND, $1, $3); } + | '(' cond LOGAND cond ')' { $$ = genOpStmt(LOGAND, $2, $4); } + | cond LOGOR cond { $$ = genOpStmt(LOGOR, $1, $3); } + | '(' cond LOGOR cond ')' { $$ = genOpStmt(LOGOR, $2, $4); }; + +comp : expr EQ expr { $$ = genOpStmt(EQ, $1, $3); } + | expr NE expr { $$ = genOpStmt(NE, $1, $3); } + | expr LT expr { $$ = genOpStmt(LT, $1, $3); } + | expr GT expr { $$ = genOpStmt(GT, $1, $3); } + | expr LE expr { $$ = genOpStmt(LE, $1, $3); } + | expr GE expr { $$ = genOpStmt(GE, $1, $3); }; + +assig : ident '=' expr { $$ = genAssignStmt ($1, $3); } + | ident '=' assig { $$ = genAssignStmt ($1, $3); }; + +ident : IDENT { $$ = genIdentVarStmt ($1); } + | ident '[' expr ']' { $$ = genIdentArrStmt ($1, $3); }; + +expr : ident { $$ = genIdentValStmt ($1); } + | INT { $$ = genIntStmt ($1); } + | FLOAT { $$ = genFloatStmt ($1); } + | STRING { $$ = genStringStmt (NULL, $1); } + | EOL { $$ = genStringStmt (NULL, $1); } + + | '(' ICAST ')' expr { $$ = genCastIntStmt ($4); } + | '(' FCAST ')' expr { $$ = genCastFloatStmt ($4); } + | '(' SCAST ')' expr { $$ = genCastStringStmt ($4); } + | '(' expr ')' { $$ = $2; } + + | '-' expr %prec NEG { $$ = genOpStmt (NEG, $2, NULL); } + | expr '+' expr { $$ = genOpStmt (PLUS, $1, $3); } + | expr '-' expr { $$ = genOpStmt (MINUS, $1, $3); } + | expr '*' expr { $$ = genOpStmt (TIMES, $1, $3); } + | expr '/' expr { $$ = genOpStmt (OVER, $1, $3); } + | expr '%' expr { $$ = genOpStmt (MODULO, $1, $3); }; %% @@ -548,8 +525,8 @@ main (int argc, void * argv []) yyin = stdin; } } - /* - * normally we would read from stdin if no file is given but for + /* + * normally we would read from stdin if no file is given but for * demonstration purpose we read a file from a standard location. */ else diff --git a/src/tepal_scan.l b/src/tepal_scan.l index 8000069..fb0f540 100644 --- a/src/tepal_scan.l +++ b/src/tepal_scan.l @@ -6,290 +6,264 @@ #include %} -%x TOK +%x TOK -DIGITS [0-9]+ -INT {DIGITS} -FLOAT {DIGITS}(.{DIGITS})? -STRING ('[^']*')|(\"[^\"]*\") -IDENT [a-zA-Z][_0-9a-zA-Z]*|_[0-9a-zA-Z]+ -OPERATOR [+\-*/%=,!:\[\]\(\);] +DIGITS [0-9]+ +INT {DIGITS} +FLOAT {DIGITS}(.{DIGITS})? +STRING ('[^']*')|(\"[^\"]*\") +IDENT [a-zA-Z][_0-9a-zA-Z]*|_[0-9a-zA-Z]+ +OPERATOR [+\-*/%=,!:\[\]\(\);] -ICAST int -FCAST float -SCAST string +ICAST int +FCAST float +SCAST string -S_TOK [ \t]*<# -E_TOK #> -TEXT (\<[^#].*)|([^\<]*) +S_TOK [ \t]*<# +E_TOK #> +TEXT (\<[^#].*)|([^\<]*) %% -{TEXT} { - char tok[2]; - size_t len = strlen (yytext); - - yylval.cPtr = (char *) malloc (len + 2); - memset (yylval.cPtr, 0, len + 2); - memcpy (yylval.cPtr, yytext, len); - - tok[0] = input (); - tok[1] = input (); - - if (tok[1] == '#' || tok[0] == EOF) - { - unput ('#'); - unput ('<'); - } - else - { - if (tok[1] != EOF) - unput (tok[1]); - yylval.cPtr[len] = tok[0]; - } - - #ifdef DEBUG - printf ("[TOKEN] HTML\n"); - #endif - - return HTML; - } - -{S_TOK} { - BEGIN (TOK); - #ifdef DEBUG - printf ("[TOKEN] STMT_END {[ \\t]*<#}\n"); - #endif - return STMT_END; - } - -{E_TOK}\n? { - BEGIN (INITIAL); - - #ifdef DEBUG - printf ("[TOKEN] STMT_END {#>\\n?}\n"); - #endif - - return STMT_END; - } - -{OPERATOR} { - #ifdef DEBUG - printf ("[TOKEN] %c\n", yytext[0]); - #endif - yylval.iVal = yytext[0]; - return yytext[0]; - } - -== { - #ifdef DEBUG - printf ("[TOKEN] EQ\n"); - #endif - - return EQ; - } -!= { - #ifdef DEBUG - printf ("[TOKEN] NE\n"); - #endif - - return NE; - } -\< { - #ifdef DEBUG - printf ("[TOKEN] EQ\n"); - #endif - - return LT; - } -\> { - #ifdef DEBUG - printf ("[TOKEN] EQ\n"); - #endif - - return GT; - } -\<= { - #ifdef DEBUG - printf ("[TOKEN] EQ\n"); - #endif - - return LE; - } -\>= { - #ifdef DEBUG - printf ("[TOKEN] EQ\n"); - #endif - - return GE; - } - -&& { - #ifdef DEBUG - printf ("[TOKEN] LOGAND\n"); - #endif - - return LOGAND; - } -\|\| { - #ifdef DEBUG - printf ("[TOKEN] LOGOR\n"); - #endif - - return LOGOR; - } - -{INT} { - yylval.iVal = atol (yytext); - - #ifdef DEBUG - printf ("[TOKEN] INT\n"); - #endif - - return INT; - } - -{FLOAT} { - yylval.fVal = atof (yytext); - - #ifdef DEBUG - printf ("[TOKEN] FLOAT\n"); - #endif - - return FLOAT; - } - -{STRING} { - yylval.cPtr = (char *) malloc (strlen (yytext) - 1); - memset (yylval.cPtr, 0, strlen (yytext) - 1); - memcpy (yylval.cPtr, yytext + 1, strlen (yytext) - 2); - - #ifdef DEBUG - printf ("[TOKEN] STRING\n"); - #endif - - return STRING; - } - -repeat { - #ifdef DEBUG - printf ("[TOKEN] REPEAT\n"); - #endif - - return REPEAT; - } -count { - #ifdef DEBUG - printf ("[TOKEN] COUNT\n"); - #endif - - return COUNT; - } - -foreach { - #ifdef DEBUG - printf ("[TOKEN] FOREACH\n"); - #endif - - return FOREACH; - } -as { - #ifdef DEBUG - printf ("[TOKEN] AS\n"); - #endif - - return AS; - } - -if { - #ifdef DEBUG - printf ("[TOKEN] IF\n"); - #endif - - return IF; - } -else { - #ifdef DEBUG - printf ("[TOKEN] ELSE\n"); - #endif - - return ELSE; - } - -end { - #ifdef DEBUG - printf ("[TOKEN] BLOCK_END\n"); - #endif - - return BLOCK_END; - } - -unset { - #ifdef DEBUG - printf ("[TOKEN] UNSET\n"); - #endif - - return UNSET; - } - -eol { - yylval.cPtr = (char *) calloc (sizeof (char), 2); - yylval.cPtr[0] = '\n'; - yylval.cPtr[1] = '\0'; - #ifdef DEBUG - printf ("[TOKEN] EOL\n"); - #endif - - return EOL; - } - -parent { - #ifdef DEBUG - printf ("[TOKEN] PARENT\n"); - #endif - - return PARENT; - } - -global { - #ifdef DEBUG - printf ("[TOKEN] GLOBAL\n"); - #endif - - return GLOBAL; - } - -{ICAST} { - #ifdef DEBUG - printf ("[TOKEN] ICAST\n"); - #endif - - return ICAST; - } -{FCAST} { - #ifdef DEBUG - printf ("[TOKEN] FCAST\n"); - #endif - - return FCAST; - } -{SCAST} { - #ifdef DEBUG - printf ("[TOKEN] SCAST\n"); - #endif - - return SCAST; - } - -{IDENT} { - yylval.cPtr = (char *) malloc (strlen (yytext) + 1); - strcpy (yylval.cPtr, yytext); - - #ifdef DEBUG - printf ("[TOKEN] IDENT\n"); - #endif - - return IDENT; - } - -[ \t\r\n] { /* eat tokens */ } +{TEXT} { + char tok[2]; + size_t len = strlen(yytext); + + yylval.cPtr = (char *)malloc(len + 2); + memset (yylval.cPtr, 0, len + 2); + memcpy (yylval.cPtr, yytext, len); + + tok[0] = input(); + tok[1] = input(); + + if (tok[1] == '#' || tok[0] == EOF) { + unput('#'); + unput('<'); + } + else { + if (tok[1] != EOF) + unput(tok[1]); + yylval.cPtr[len] = tok[0]; + } + +#ifdef DEBUG + fputs("[TOKEN] HTML", stderr); +#endif + return HTML; +} + +{S_TOK} { + BEGIN(TOK); +#ifdef DEBUG + fputs("[TOKEN] STMT_END {[ \\t]*<#}", stderr); +#endif + return STMT_END; +} + +{E_TOK}\n? { + BEGIN(INITIAL); +#ifdef DEBUG + fputs("[TOKEN] STMT_END {#>\\n?}", stderr); +#endif + return STMT_END; +} + +{OPERATOR} { +#ifdef DEBUG + fprintf(stderr, "[TOKEN] %c\n", yytext[0]); +#endif + yylval.iVal = yytext[0]; + return yytext[0]; +} + +== { +#ifdef DEBUG + fputs("[TOKEN] EQ", stderr); +#endif + return EQ; +} + +!= { +#ifdef DEBUG + fputs("[TOKEN] NE", stderr); +#endif + return NE; +} + +\< { +#ifdef DEBUG + fputs("[TOKEN] LT", stderr); +#endif + return LT; +} + +\> { +#ifdef DEBUG + fputs("[TOKEN] GT", stderr); +#endif + return GT; +} + +\<= { +#ifdef DEBUG + fputs("[TOKEN] LE", stderr); +#endif + return LE; +} + +\>= { +#ifdef DEBUG + fputs("[TOKEN] GE", stderr); +#endif + return GE; +} + +&& { +#ifdef DEBUG + fputs("[TOKEN] LOGAND", stderr); +#endif + return LOGAND; +} +\|\| { +#ifdef DEBUG + fputs("[TOKEN] LOGOR", stderr); +#endif + return LOGOR; +} + +{INT} { + yylval.iVal = atol (yytext); +#ifdef DEBUG + fputs("[TOKEN] INT", stderr); +#endif + return INT; +} + +{FLOAT} { + yylval.fVal = atof(yytext); +#ifdef DEBUG + fputs("[TOKEN] FLOAT", stderr); +#endif + return FLOAT; +} + +{STRING} { + yylval.cPtr = (char *)malloc(strlen (yytext) - 1); + memset (yylval.cPtr, 0, strlen(yytext) - 1); + memcpy (yylval.cPtr, yytext + 1, strlen(yytext) - 2); +#ifdef DEBUG + fputs("[TOKEN] STRING", stderr); +#endif + return STRING; +} + +repeat { +#ifdef DEBUG + fputs("[TOKEN] REPEAT", stderr); +#endif + return REPEAT; +} + +count { +#ifdef DEBUG + fputs("[TOKEN] COUNT", stderr); +#endif + return COUNT; +} + +foreach { +#ifdef DEBUG + fputs("[TOKEN] FOREACH", stderr); +#endif + return FOREACH; +} + +as { +#ifdef DEBUG + fputs("[TOKEN] AS", stderr); +#endif + return AS; +} + +if { +#ifdef DEBUG + fputs("[TOKEN] IF", stderr); +#endif + return IF; +} + +else { +#ifdef DEBUG + fputs("[TOKEN] ELSE", stderr); +#endif + return ELSE; +} + +end { +#ifdef DEBUG + fputs("[TOKEN] BLOCK_END", stderr); +#endif + return BLOCK_END; +} + +unset { +#ifdef DEBUG + fputs("[TOKEN] UNSET", stderr); +#endif + return UNSET; +} + +eol { + yylval.cPtr = (char *)calloc(sizeof (char), 2); + yylval.cPtr[0] = '\n'; + yylval.cPtr[1] = '\0'; +#ifdef DEBUG + fputs("[TOKEN] EOL", stderr); +#endif + return EOL; +} + +parent { +#ifdef DEBUG + fputs("[TOKEN] PARENT", stderr); +#endif + return PARENT; +} + +global { +#ifdef DEBUG + fputs("[TOKEN] GLOBAL", stderr); +#endif + return GLOBAL; +} + +{ICAST} { +#ifdef DEBUG + fputs("[TOKEN] ICAST", stderr); +#endif + return ICAST; +} + +{FCAST} { +#ifdef DEBUG + fputs("[TOKEN] FCAST", stderr); +#endif + return FCAST; +} +{SCAST} { +#ifdef DEBUG + fputs("[TOKEN] SCAST", stderr); +#endif + return SCAST; +} + +{IDENT} { + yylval.cPtr = (char *)malloc(strlen(yytext) + 1); + strcpy(yylval.cPtr, yytext); +#ifdef DEBUG + fputs("[TOKEN] IDENT", stderr); +#endif + return IDENT; +} + +[ \t\r\n] { /* eat tokens */ } diff --git a/src/variable.c b/src/variable.c index a339eb1..25e50ce 100644 --- a/src/variable.c +++ b/src/variable.c @@ -8,47 +8,46 @@ s_ident * -getVariable (s_identList * iList, char * id) +getVariable(s_identList * iList, char * id) { - return identListSeekKey (iList, id); + return identListSeekKey(iList, id); } s_ident * -getArray (s_ident * var, s_expVal * eVal) -{ +getArray(s_ident * var, s_expVal * eVal) +{ s_ident * ret = NULL; /* generate new idl if ident isn't, discard prev val */ if (identGetType (var) != ID_TYP_IDL) - identSetIdl (var, identListNew ()); + identSetIdl (var, identListNew()); /* now seek or generate the actual ident */ - switch (expValueGetType (eVal)) + switch (expValueGetType(eVal)) { case EXP_TYP_INT: { - int idx = expValueInt (eVal); + int idx = expValueInt(eVal); - ret = identListSeekIdx (identIdl (var), idx); + ret = identListSeekIdx(identIdl(var), idx); if (ret == NULL) - ret = identListPutVal ( + ret = identListPutVal( identIdl (var), identUndefNew (idx, NULL)); break; } - case EXP_TYP_STRING: { - char * key = expValueString (eVal); + char * key = expValueString(eVal); - ret = identListSeekKey (identIdl (var), key); + ret = identListSeekKey(identIdl(var), key); if (ret == NULL) - ret = identListPutVal ( + ret = identListPutVal( identIdl (var), identUndefNew (-1, key)); - free (key); + free(key); } }