Skip to content

Commit 62e9805

Browse files
author
Joshua Goller
committed
rpc works for single-line expressions
1 parent bdc734d commit 62e9805

File tree

8 files changed

+96
-105
lines changed

8 files changed

+96
-105
lines changed

ch-4/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ 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 +\n" | $(VALGRIND) ./bin/$@
16+
printf "4 5 +" | $(VALGRIND) ./bin/$@
1717
@#printf "4 5 + 5 6 + -\n" | $(VALGRIND) ./bin/$@
1818
@#printf "5 SIN" | $(VALGRIND) ./bin/$@
1919
@#printf "b 6 = \n b 10 *\n" | $(VALGRIND) ./bin/$@

ch-4/rpc/include/rpc.h

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#pragma clang diagnostic ignored "-Wpadded"
23

34
#include <ctype.h>
45
#include <math.h>
@@ -39,12 +40,11 @@ typedef enum {
3940
DIV,
4041
MUL,
4142
MOD,
42-
DISPLAY
4343
} operand_type;
4444

4545
typedef struct {
4646
operand_type type;
47-
char cval;
47+
char cvalue;
4848
double dvalue;
4949
} operand;
5050

@@ -66,11 +66,11 @@ void ungets(char s[], int len);
6666
int parse(char s[], int s_size);
6767

6868
// lexer.c
69-
int lex(char symbol[], int len);
70-
int handle_alpha(char oper[], int len);
69+
operand_type lex(char symbol[], int len);
70+
operand_type handle_alpha(char oper[], int len);
7171
operand_type handle_numeric(char oper[], int len);
7272
int is_operator(char oper);
73-
operand_type handle_operator(char oper[]);
73+
operand_type handle_operator(char oper);
7474

7575
// rpn_math.c
7676
bool double_eq(const double x, const double y);
@@ -91,15 +91,14 @@ void rpn_sqrt(operand val1);
9191
void rpn_floor(operand val1);
9292

9393
// stack.c
94-
void push(operand op);
95-
void pop(operand op);
96-
void peek(operand op);
94+
int push(operand op);
95+
operand pop(void);
96+
operand peek(void);
9797
void duplicate_top(void);
9898
void swap_top(void);
9999
int get_stack_size(void);
100-
void display(void);
101100

102101
// vars.c
103102
void assign(operand val1, operand val2);
104-
void dereference(operand op);
103+
operand dereference(operand op);
105104
int validate_var(operand op);

ch-4/rpc/src/lexer.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ operand_type lex(char symbol[], int token_len) {
1313
int len;
1414

1515
while ((len = parse(symbol, token_len))) {
16+
// printf("lex() | parsed symbol: %s (%d)\n", symbol, len);
1617
if (len == -1) {
1718
return EXIT;
1819
}
19-
if (is_operator(symbol)) {
20-
return handle_operator(symbol);
20+
if (is_operator(symbol[0])) {
21+
return handle_operator(symbol[0]);
2122
}
2223

2324
if (isalpha(symbol[0])) {
@@ -30,14 +31,14 @@ operand_type lex(char symbol[], int token_len) {
3031
stack, i.e. "4 +" will evaluate to 8
3132
*/
3233
// ungets(symbol, len);
33-
return RAW;
34+
return VAL;
3435
} else {
3536
return GARBAGE;
3637
}
3738
}
3839

3940
// Loop breaks on display.
40-
return DISPLAY;
41+
return EXIT;
4142
}
4243

4344
operand_type handle_alpha(char operator[], int len) {
@@ -95,6 +96,7 @@ int is_operator(char op) {
9596
}
9697

9798
operand_type handle_operator(char operator) {
99+
// printf("handle_operator() | handling %c\n", operator);
98100
switch (operator) {
99101
case '+':
100102
return ADD;

ch-4/rpc/src/main.c

+30-27
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ int main() {
77
operand_type type;
88
char token[MAX_TOKEN_SIZE];
99

10-
while ((type = lex(token, MAX_TOKEN_SIZE)) != EXIT) {
10+
while ((type = lex(token, MAX_TOKEN_SIZE))) {
11+
printf("main() | evaluating type %d, token %s\n", type, token);
1112
switch (type) {
1213
case VAL:
1314
op1.type = VAL;
@@ -17,92 +18,94 @@ int main() {
1718
break;
1819
case VAR:
1920
op1.type = VAR;
20-
op1.cvalue = token;
21+
op1.cvalue = token[0];
2122
op1.dvalue = 0.0;
2223
push(op1);
2324
break;
2425
case ASSIGN:
25-
pop(op2);
26-
pop(op1);
26+
op2 = pop();
27+
op1 = pop();
2728
assign(op1, op2);
2829
break;
2930
case ADD:
30-
pop(op2);
31-
pop(op1);
31+
op2 = pop();
32+
op1 = pop();
3233
rpn_add(op1, op2);
3334
break;
3435
case MUL:
35-
pop(op2);
36-
pop(op1);
36+
op2 = pop();
37+
op1 = pop();
3738
rpn_multiply(op1, op2);
3839
break;
3940
case SUB:
40-
pop(op2);
41-
pop(op1);
41+
op2 = pop();
42+
op1 = pop();
4243
rpn_subtract(op1, op2);
4344
break;
4445
case DIV:
45-
pop(op2);
46-
pop(op1);
46+
op2 = pop();
47+
op1 = pop();
4748
rpn_divide(op1, op2);
4849
break;
4950
case MOD:
50-
pop(op2);
51-
pop(op1);
51+
op2 = pop();
52+
op1 = pop();
5253
rpn_modulus(op1, op2);
5354
break;
5455
case SIN:
55-
pop(op1);
56+
op1 = pop();
5657
rpn_sin(op1);
5758
break;
5859
case COS:
59-
pop(op1);
60+
op1 = pop();
6061
rpn_cos(op1);
6162
break;
6263
case TAN:
63-
pop(op1);
64+
op1 = pop();
6465
rpn_tan(op1);
6566
break;
6667
case ASIN:
67-
pop(op1);
68+
op1 = pop();
6869
rpn_asin(op1);
6970
break;
7071
case ACOS:
71-
pop(op1);
72+
op1 = pop();
7273
rpn_acos(op1);
7374
break;
7475
case ATAN:
75-
pop(op1);
76+
op1 = pop();
7677
rpn_atan(op1);
7778
break;
7879
case POW:
79-
pop(op2);
80-
pop(op1);
80+
op2 = pop();
81+
op1 = pop();
8182
rpn_pow(op1, op2);
8283
break;
8384
case EXP:
84-
pop(op1);
85+
op1 = pop();
8586
rpn_exp(op1);
8687
break;
8788
case SQRT:
88-
pop(op1);
89+
op1 = pop();
8990
rpn_sqrt(op1);
9091
break;
9192
case FLOR:
92-
pop(op1);
93+
op1 = pop();
9394
rpn_floor(op1);
9495
break;
96+
case EXIT:
97+
break;
9598
case GARBAGE:
96-
default:
9799
printf("Error: invalid expression %s.\n", token);
98100
return -1;
99101
}
100102
}
101-
if (get_stack_size() == 1) {
103+
if (get_stack_size() == 0) {
102104
operand final = pop();
103105
printf("%8.8g\n", final.dvalue);
104106
return 0;
105107
} else {
108+
printf("stack size: %d\n", get_stack_size());
106109
printf("Error: invalid expression; quitting.\n");
107110
return -1;
108111
}

ch-4/rpc/src/parser.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ int parse(char s[], int s_size) {
7878
}
7979
s[i] = '\0';
8080

81-
if (c == EOF) {
81+
if (c == EOF && i == 0) {
8282
return -1;
8383
} else if (c == '\n') {
8484
// \n indicates we're at the end of the expression,

ch-4/rpc/src/rpn_math.c

+19-17
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ void rpn_add(operand val1, operand val2) {
1717
if (val2.type == VAR) {
1818
dereference(val2);
1919
}
20-
val1.dval = val1.dval + val2.dval;
20+
printf("rpn_add() | %lf + %lf\n", val1.dvalue, val2.dvalue);
21+
val1.dvalue = val1.dvalue + val2.dvalue;
2122
val1.type = VAL;
23+
printf("rpn_add() | called (new dvalue %lf).\n", val1.dvalue);
2224
push(val1);
2325
}
2426

@@ -29,7 +31,7 @@ void rpn_subtract(operand val1, operand val2) {
2931
if (val2.type == VAR) {
3032
dereference(val2);
3133
}
32-
val1.dval = val1.dval - val2.dval;
34+
val1.dvalue = val1.dvalue - val2.dvalue;
3335
val1.type = VAL;
3436
push(val1);
3537
}
@@ -41,7 +43,7 @@ void rpn_multiply(operand val1, operand val2) {
4143
if (val2.type == VAR) {
4244
dereference(val2);
4345
}
44-
val1.dval = val1.dval * val2.dval;
46+
val1.dvalue = val1.dvalue * val2.dvalue;
4547
val1.type = VAL;
4648
push(val1);
4749
}
@@ -53,11 +55,11 @@ void rpn_divide(operand val1, operand val2) {
5355
if (val2.type == VAR) {
5456
dereference(val2);
5557
}
56-
if (double_eq(val2.dval, 0.0)) {
58+
if (double_eq(val2.dvalue, 0.0)) {
5759
printf("Error: division by zero.\n");
5860
return;
5961
}
60-
val1.dval = val1.dval / val2.dval;
62+
val1.dvalue = val1.dvalue / val2.dvalue;
6163
val1.type = VAL;
6264
push(val1);
6365
}
@@ -69,12 +71,12 @@ void rpn_modulus(operand val1, operand val2) {
6971
if (val2.type == VAR) {
7072
dereference(val2);
7173
}
72-
if (double_eq(val2.dval, 0.0)) {
74+
if (double_eq(val2.dvalue, 0.0)) {
7375
printf("Error: modulo by zero.\n");
7476
return;
7577
}
7678

77-
val1.dval = (int)val1.dval % (int)val2.dval;
79+
val1.dvalue = (int)val1.dvalue % (int)val2.dvalue;
7880
val1.type = VAL;
7981
push(val1);
8082
}
@@ -84,7 +86,7 @@ void rpn_sin(operand val1) {
8486
dereference(val1);
8587
}
8688

87-
val1.dval = sin(val1.dval);
89+
val1.dvalue = sin(val1.dvalue);
8890
val1.type = VAL;
8991
push(val1);
9092
}
@@ -94,7 +96,7 @@ void rpn_cos(operand val1) {
9496
dereference(val1);
9597
}
9698

97-
val1.dval = cos(val1.dval);
99+
val1.dvalue = cos(val1.dvalue);
98100
val1.type = VAL;
99101
push(val1);
100102
}
@@ -104,7 +106,7 @@ void rpn_tan(operand val1) {
104106
dereference(val1);
105107
}
106108

107-
val1.dval = tan(val1.dval);
109+
val1.dvalue = tan(val1.dvalue);
108110
val1.type = VAL;
109111
push(val1);
110112
}
@@ -114,7 +116,7 @@ void rpn_asin(operand val1) {
114116
dereference(val1);
115117
}
116118

117-
val1.dval = asin(val1.dval);
119+
val1.dvalue = asin(val1.dvalue);
118120
val1.type = VAL;
119121
push(val1);
120122
}
@@ -124,7 +126,7 @@ void rpn_acos(operand val1) {
124126
dereference(val1);
125127
}
126128

127-
val1.dval = acos(val1.dval);
129+
val1.dvalue = acos(val1.dvalue);
128130
val1.type = VAL;
129131
push(val1);
130132
}
@@ -134,7 +136,7 @@ void rpn_atan(operand val1) {
134136
dereference(val1);
135137
}
136138

137-
val1.dval = atan(val1.dval);
139+
val1.dvalue = atan(val1.dvalue);
138140
val1.type = VAL;
139141
push(val1);
140142
}
@@ -147,7 +149,7 @@ void rpn_pow(operand val1, operand val2) {
147149
dereference(val2);
148150
}
149151

150-
val1.dval = pow(val1.dval, val2.dval);
152+
val1.dvalue = pow(val1.dvalue, val2.dvalue);
151153
val1.type = VAL;
152154
push(val1);
153155
}
@@ -157,7 +159,7 @@ void rpn_exp(operand val1) {
157159
dereference(val1);
158160
}
159161

160-
val1.dval = exp(val1.dval);
162+
val1.dvalue = exp(val1.dvalue);
161163
val1.type = VAL;
162164
push(val1);
163165
}
@@ -167,7 +169,7 @@ void rpn_sqrt(operand val1) {
167169
dereference(val1);
168170
}
169171

170-
val1.dval = sqrt(val1.dval);
172+
val1.dvalue = sqrt(val1.dvalue);
171173
val1.type = VAL;
172174
push(val1);
173175
}
@@ -177,7 +179,7 @@ void rpn_floor(operand val1) {
177179
dereference(val1);
178180
}
179181

180-
val1.dval = floor(val1.dval);
182+
val1.dvalue = floor(val1.dvalue);
181183
val1.type = VAL;
182184
push(val1);
183185
}

0 commit comments

Comments
 (0)