Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 876abc3

Browse files
committed
Fixed converting in expr.c, added debug option for tokstack.c
1 parent 4272efa commit 876abc3

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

expr.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ int expr_generateInstruction(tokStack_t *tokStack, char terminal, token_t token)
686686
{
687687
// --- Converting types ---
688688
tokenType_t topType = tokStack_Top(tokStack);
689-
if(topType == TOK_integer && topType == TOK_decimal && topType == TOK_string) // No need to convert identifier (int and dec) and string
689+
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
690690
{
691691
int retVal;
692692
retVal = expr_convertTypes(tokStack, terminal);
@@ -725,6 +725,10 @@ int expr_generateInstruction(tokStack_t *tokStack, char terminal, token_t token)
725725
case 'i':
726726
add_instruction(PUSHS, &token, NULL, NULL);
727727
break;
728+
729+
// String
730+
case TERM_string:
731+
break; // No need to add instructuon (already done while loading token)
728732

729733
// Logic operators
730734
case TERM_equal:
@@ -766,6 +770,9 @@ int expr_generateInstruction(tokStack_t *tokStack, char terminal, token_t token)
766770
else
767771
add_instruction(GTEQS, NULL, NULL, NULL); // Nonexisting instruction, but ilist does some magic
768772
break;
773+
default:
774+
expr_error("generateInstructuon: Unexpected terminal");
775+
return EXPR_RETURN_ERROR_INTERNAL;
769776
}
770777

771778
return EXPR_RETURN_SUCC;

expr.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
#define EXPR_SUCCESS 1 /// Intarnal return value for success
4141
#define EXPR_TRUE 1 /// Intarnal return value for true
4242
#define EXPR_FALSE 0 /// Intarnal return value for false
43-
#define TOK_FAIL TOK_endOfFile /// Representing error return with type tokenType_t
44-
#define TOK_BOOLEAN 100 /// Boolean data type for tokStack
43+
#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!)
44+
#define TOK_BOOLEAN 100 /// Boolean data type for tokStack (@warning If you change this here, you must change it also in tokstack.h!)
4545
#define RESULT_ASSIGNMENT 'R' /// Representing result assignment with type char (in expr_convertTypes())
4646
// External return values (@todo This is already defined somewhere for sure)
4747
#define EXPR_RETURN_SUCC 0

tokstack.c

+32
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
* @todo
66
*/
77

8+
//#define TOKSTACKDEBUG
89
#include "tokstack.h"
910

11+
12+
1013
int tokStack_Init(tokStack_t *stack)
1114
{
1215
if(stack == NULL) // If the stack is not allocated
@@ -34,6 +37,11 @@ int tokStack_Push(tokStack_t *stack, tokenType_t tokenType)
3437
// Push token to the stack
3538
stack->tokArr[stack->top] = tokenType;
3639

40+
// Debug
41+
#ifdef TOKSTACKDEBUG
42+
tokStack_Info(stack);
43+
#endif
44+
3745
// Return success
3846
return SUCCESS;
3947
}
@@ -49,6 +57,11 @@ tokenType_t tokStack_Pop(tokStack_t *stack)
4957
// Decrease top of the stack
5058
(stack->top)--;
5159

60+
// Debug
61+
#ifdef TOKSTACKDEBUG
62+
tokStack_Info(stack);
63+
#endif
64+
5265
// Return pointer to the token on top of the stack
5366
return topType;
5467
}
@@ -87,3 +100,22 @@ void tokStack_Error(char* msg)
87100
{
88101
fprintf(stderr, "[ERROR] %s\n", msg);
89102
}
103+
104+
#ifdef TOKSTACKDEBUG
105+
void tokStack_Info(tokStack_t *stack)
106+
{
107+
printf("[DBG] tokStack=");
108+
for(int i = 0; i <= stack->top; i++)
109+
{
110+
switch(stack->tokArr[i])
111+
{
112+
case TOK_integer: printf("int "); break;
113+
case TOK_decimal: printf("dec "); break;
114+
case TOK_string: printf("str "); break;
115+
case TOK_BOOLEAN: printf("bool "); break;
116+
default: printf("WTF "); break;
117+
}
118+
}
119+
printf("\n");
120+
}
121+
#endif

tokstack.h

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define TRUE 1
2626
#define FALSE 0
2727
#define TOK_FAIL TOK_endOfFile /// Representing error return with type tokenType_t
28+
#define TOK_BOOLEAN 100 /// Boolean data type for tokStack
2829

2930

3031
// --- Strucutres ---
@@ -43,4 +44,8 @@ int tokStack_Empty(tokStack_t *stack);
4344
int tokStack_Full(tokStack_t *stack);
4445
void tokStack_Error(char* msg);
4546

47+
#ifdef TOKSTACKDEBUG
48+
void tokStack_Info(tokStack_t *stack);
49+
#endif
50+
4651
#endif

0 commit comments

Comments
 (0)