Skip to content

Commit fdf4cb5

Browse files
author
Joshua Goller
committed
rpn passes test cases
1 parent d502c57 commit fdf4cb5

File tree

4 files changed

+11
-14
lines changed

4 files changed

+11
-14
lines changed

ch-4/Makefile

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ endif
1313
rpc: clean
1414
$(COMPILE) -I ch-4/rpc/include/ ch-4/rpc/src/*.c -o bin/$@
1515
ifdef RUN_TESTS
16-
printf "4 5 +" | $(VALGRIND) ./bin/$@
17-
@#printf "4 5 + 5 6 + -\n" | $(VALGRIND) ./bin/$@
18-
@#printf "5 SIN" | $(VALGRIND) ./bin/$@
19-
@#printf "b 6 = \n b 10 *\n" | $(VALGRIND) ./bin/$@
20-
@#printf "" | $(VALGRIND) ./bin/$@
16+
printf "4 5 +" | $(VALGRIND) ./bin/$@ | grep "9"
17+
printf "4 5 + 5 6 + -\n" | $(VALGRIND) ./bin/$@ | grep "\-2"
18+
printf "5 SIN" | $(VALGRIND) ./bin/$@ | grep "\-0.95892427"
19+
printf "b 6 = \n b 10 *\n" | $(VALGRIND) ./bin/$@ | grep "60"
20+
printf "" | $(VALGRIND) ./bin/$@
2121
endif
2222

2323
4.11: 4.11-build

ch-4/rpc/include/rpc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ operand pop(void);
9797
operand peek(void);
9898
void duplicate_top(void);
9999
void swap_top(void);
100-
int get_stack_size(void);
100+
int get_stack_top(void);
101101

102102
// vars.c
103103
void assign(operand val1, operand val2);

ch-4/rpc/src/main.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ int main() {
88
char token[MAX_TOKEN_SIZE];
99

1010
while ((type = lex(token, MAX_TOKEN_SIZE))) {
11-
printf("main() | evaluating type %d, token %s\n", type, token);
1211
switch (type) {
1312
case VAL:
1413
op1.type = VAL;
@@ -106,12 +105,14 @@ int main() {
106105
return -1;
107106
}
108107
}
109-
if (get_stack_size() == 0) {
108+
int top = get_stack_top();
109+
if (top == 0) {
110110
operand final = pop();
111111
printf("%8.8g\n", final.dvalue);
112112
return 0;
113+
} else if (top == -1) {
114+
return 0;
113115
} else {
114-
printf("stack size: %d\n", get_stack_size());
115116
printf("Error: invalid expression; quitting.\n");
116117
return -1;
117118
}

ch-4/rpc/src/stack.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ static operand stack[MAX_STACK_SIZE];
77
int push(const operand op) {
88
if (sp < MAX_STACK_SIZE) {
99
stack[++sp] = op;
10-
printf("push() | pushed (%d, %c, %lf)\n", stack[sp].type, stack[sp].cvalue,
11-
stack[sp].dvalue);
1210
return 0;
1311
} else {
1412
printf("Error; stack full.\n");
@@ -26,8 +24,6 @@ operand pop(void) {
2624
printf("Error: stack invalid.\n");
2725
return (operand){GARBAGE, 0, 0.0};
2826
} else {
29-
printf("pop() | popping (%d, %c, %lf)\n", stack[sp].type, stack[sp].cvalue,
30-
stack[sp].dvalue);
3127
return stack[sp--];
3228
}
3329
}
@@ -63,4 +59,4 @@ void swap_top() {
6359
push(second);
6460
}
6561

66-
int get_stack_size() { return sp; }
62+
int get_stack_top() { return sp; }

0 commit comments

Comments
 (0)