Skip to content

Commit

Permalink
Fixed converting in expr.c, added debug option for tokstack.c
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriFurda committed Nov 30, 2017
1 parent 4272efa commit 876abc3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
9 changes: 8 additions & 1 deletion expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ int expr_generateInstruction(tokStack_t *tokStack, char terminal, token_t token)
{
// --- Converting types ---
tokenType_t topType = tokStack_Top(tokStack);
if(topType == TOK_integer && topType == TOK_decimal && topType == TOK_string) // No need to convert identifier (int and dec) and string
if((topType == TOK_integer || topType == TOK_decimal || topType == TOK_string) && terminal != 'i' && terminal != TERM_string) // No need to convert identifier (int and dec) and string && no converting when reducing i to E or str to E
{
int retVal;
retVal = expr_convertTypes(tokStack, terminal);
Expand Down Expand Up @@ -725,6 +725,10 @@ int expr_generateInstruction(tokStack_t *tokStack, char terminal, token_t token)
case 'i':
add_instruction(PUSHS, &token, NULL, NULL);
break;

// String
case TERM_string:
break; // No need to add instructuon (already done while loading token)

// Logic operators
case TERM_equal:
Expand Down Expand Up @@ -766,6 +770,9 @@ int expr_generateInstruction(tokStack_t *tokStack, char terminal, token_t token)
else
add_instruction(GTEQS, NULL, NULL, NULL); // Nonexisting instruction, but ilist does some magic
break;
default:
expr_error("generateInstructuon: Unexpected terminal");
return EXPR_RETURN_ERROR_INTERNAL;
}

return EXPR_RETURN_SUCC;
Expand Down
4 changes: 2 additions & 2 deletions expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
#define EXPR_SUCCESS 1 /// Intarnal return value for success
#define EXPR_TRUE 1 /// Intarnal return value for true
#define EXPR_FALSE 0 /// Intarnal return value for false
#define TOK_FAIL TOK_endOfFile /// Representing error return with type tokenType_t
#define TOK_BOOLEAN 100 /// Boolean data type for tokStack
#define TOK_FAIL TOK_endOfFile /// Representing error return with type tokenType_t (@warning If you change this here, you must change it also in tokstack.h!)
#define TOK_BOOLEAN 100 /// Boolean data type for tokStack (@warning If you change this here, you must change it also in tokstack.h!)
#define RESULT_ASSIGNMENT 'R' /// Representing result assignment with type char (in expr_convertTypes())
// External return values (@todo This is already defined somewhere for sure)
#define EXPR_RETURN_SUCC 0
Expand Down
32 changes: 32 additions & 0 deletions tokstack.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
* @todo
*/

//#define TOKSTACKDEBUG
#include "tokstack.h"



int tokStack_Init(tokStack_t *stack)
{
if(stack == NULL) // If the stack is not allocated
Expand Down Expand Up @@ -34,6 +37,11 @@ int tokStack_Push(tokStack_t *stack, tokenType_t tokenType)
// Push token to the stack
stack->tokArr[stack->top] = tokenType;

// Debug
#ifdef TOKSTACKDEBUG
tokStack_Info(stack);
#endif

// Return success
return SUCCESS;
}
Expand All @@ -49,6 +57,11 @@ tokenType_t tokStack_Pop(tokStack_t *stack)
// Decrease top of the stack
(stack->top)--;

// Debug
#ifdef TOKSTACKDEBUG
tokStack_Info(stack);
#endif

// Return pointer to the token on top of the stack
return topType;
}
Expand Down Expand Up @@ -87,3 +100,22 @@ void tokStack_Error(char* msg)
{
fprintf(stderr, "[ERROR] %s\n", msg);
}

#ifdef TOKSTACKDEBUG
void tokStack_Info(tokStack_t *stack)
{
printf("[DBG] tokStack=");
for(int i = 0; i <= stack->top; i++)
{
switch(stack->tokArr[i])
{
case TOK_integer: printf("int "); break;
case TOK_decimal: printf("dec "); break;
case TOK_string: printf("str "); break;
case TOK_BOOLEAN: printf("bool "); break;
default: printf("WTF "); break;
}
}
printf("\n");
}
#endif
5 changes: 5 additions & 0 deletions tokstack.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define TRUE 1
#define FALSE 0
#define TOK_FAIL TOK_endOfFile /// Representing error return with type tokenType_t
#define TOK_BOOLEAN 100 /// Boolean data type for tokStack


// --- Strucutres ---
Expand All @@ -43,4 +44,8 @@ int tokStack_Empty(tokStack_t *stack);
int tokStack_Full(tokStack_t *stack);
void tokStack_Error(char* msg);

#ifdef TOKSTACKDEBUG
void tokStack_Info(tokStack_t *stack);
#endif

#endif

0 comments on commit 876abc3

Please sign in to comment.