Skip to content

Commit bdc734d

Browse files
author
Joshua Goller
committed
overhauling rpc
1 parent 9686dc3 commit bdc734d

File tree

8 files changed

+208
-208
lines changed

8 files changed

+208
-208
lines changed

ch-4/Makefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ rpc: clean
1414
$(COMPILE) -I ch-4/rpc/include/ ch-4/rpc/src/*.c -o bin/$@
1515
ifdef RUN_TESTS
1616
printf "4 5 +\n" | $(VALGRIND) ./bin/$@
17-
printf "4 5 + 5 6 + -\n" | $(VALGRIND) ./bin/$@
18-
printf "a 5 =" | $(VALGRIND) ./bin/$@
19-
printf "b 6 = \n b 10 * SIN\n" | $(VALGRIND) ./bin/$@
20-
printf "" | $(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/$@
2121
endif
2222

2323
4.11: 4.11-build

ch-4/rpc/include/rpc.h

+54-49
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,46 @@
1818
// single-character variables, and strings of characters representing
1919
// mathematical functions (sine, cosine, etc). The determination is returned to
2020
// the switch block in main(), which does the right thing.
21-
#define EXIT 0
22-
#define VAR 1
23-
#define RAW 2
24-
#define GARBAGE 3
25-
#define SIN 4
21+
typedef enum {
22+
EXIT,
23+
VAR,
24+
VAL,
25+
GARBAGE,
26+
SIN,
27+
COS,
28+
TAN,
29+
ASIN,
30+
ACOS,
31+
ATAN,
32+
POW,
33+
EXP,
34+
SQRT,
35+
FLOR,
36+
ASSIGN,
37+
ADD,
38+
SUB,
39+
DIV,
40+
MUL,
41+
MOD,
42+
DISPLAY
43+
} operand_type;
44+
45+
typedef struct {
46+
operand_type type;
47+
char cval;
48+
double dvalue;
49+
} operand;
50+
2651
#define SIN_STR "SIN"
27-
#define COS 5
2852
#define COS_STR "COS"
29-
#define TAN 6
3053
#define TAN_STR "TAN"
31-
#define ASIN 7
3254
#define ASIN_STR "ASIN"
33-
#define ACOS 8
3455
#define ACOS_STR "ACOS"
35-
#define ATAN 9
3656
#define ATAN_STR "ATAN"
37-
#define POW 10
3857
#define POW_STR "POW"
39-
#define EXP 11
4058
#define EXP_STR "EXP"
41-
#define SQRT 12
4259
#define SQRT_STR "SQRT"
43-
#define FLOR 13
4460
#define FLOR_STR "FLOR"
45-
#define ASSIGN 14
46-
#define ADD 15
47-
#define SUB 16
48-
#define DIV 17
49-
#define MUL 18
50-
#define MOD 19
51-
#define DISPLAY 20
52-
53-
// bools
54-
#define true 1
55-
#define false 0
5661

5762
// parser.c
5863
int rpc_getch(void);
@@ -63,38 +68,38 @@ int parse(char s[], int s_size);
6368
// lexer.c
6469
int lex(char symbol[], int len);
6570
int handle_alpha(char oper[], int len);
66-
int handle_numeric(char oper[], int len);
67-
int is_operator(char oper[]);
68-
int handle_operator(char oper[]);
71+
operand_type handle_numeric(char oper[], int len);
72+
int is_operator(char oper);
73+
operand_type handle_operator(char oper[]);
6974

7075
// rpn_math.c
7176
bool double_eq(const double x, const double y);
72-
void rpn_add(double val1[], double val2[]);
73-
void rpn_subtract(double val1[], double val2[]);
74-
void rpn_multiply(double val1[], double val2[]);
75-
void rpn_divide(double val1[], double val2[]);
76-
void rpn_modulus(double val1[], double val2[]);
77-
void rpn_sin(double val1[]);
78-
void rpn_cos(double val1[]);
79-
void rpn_tan(double val1[]);
80-
void rpn_asin(double val1[]);
81-
void rpn_acos(double val1[]);
82-
void rpn_atan(double val1[]);
83-
void rpn_pow(double val1[], double val2[]);
84-
void rpn_exp(double val1[]);
85-
void rpn_sqrt(double val1[]);
86-
void rpn_floor(double val1[]);
77+
void rpn_add(operand val1, operand val2);
78+
void rpn_subtract(operand val1, operand val2);
79+
void rpn_multiply(operand val1, operand val2);
80+
void rpn_divide(operand val1, operand val2);
81+
void rpn_modulus(operand val1, operand val2);
82+
void rpn_sin(operand val1);
83+
void rpn_cos(operand val1);
84+
void rpn_tan(operand val1);
85+
void rpn_asin(operand val1);
86+
void rpn_acos(operand val1);
87+
void rpn_atan(operand val1);
88+
void rpn_pow(operand val1, operand val2);
89+
void rpn_exp(operand val1);
90+
void rpn_sqrt(operand val1);
91+
void rpn_floor(operand val1);
8792

8893
// stack.c
89-
void push(double val[]);
90-
void pop(double ret[]);
91-
void peek(double ret[]);
94+
void push(operand op);
95+
void pop(operand op);
96+
void peek(operand op);
9297
void duplicate_top(void);
9398
void swap_top(void);
9499
int get_stack_size(void);
95100
void display(void);
96101

97102
// vars.c
98-
void assign(double val1[], double val2[]);
99-
void dereference(double var[]);
100-
int validate_var(double var[]);
103+
void assign(operand val1, operand val2);
104+
void dereference(operand op);
105+
int validate_var(operand op);

ch-4/rpc/src/lexer.c

+13-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ this file do.
99
*/
1010

1111
// lex: get next operator or numeric operand
12-
int lex(char symbol[], int token_len) {
12+
operand_type lex(char symbol[], int token_len) {
1313
int len;
1414

1515
while ((len = parse(symbol, token_len))) {
@@ -23,10 +23,12 @@ int lex(char symbol[], int token_len) {
2323
if (isalpha(symbol[0])) {
2424
return handle_alpha(symbol, len);
2525
} else if (isdigit(symbol[0]) || symbol[0] == '.') {
26-
// uncomment this line to test ungets();
27-
// this will push all floats back to the
28-
// buffer before pushing them onto the
29-
// stack, i.e. "4 +" will evaluate to 8
26+
/*
27+
uncomment the line below line to test ungets();
28+
this will push all floats back to the
29+
buffer before pushing them onto the
30+
stack, i.e. "4 +" will evaluate to 8
31+
*/
3032
// ungets(symbol, len);
3133
return RAW;
3234
} else {
@@ -38,7 +40,7 @@ int lex(char symbol[], int token_len) {
3840
return DISPLAY;
3941
}
4042

41-
int handle_alpha(char operator[], int len) {
43+
operand_type handle_alpha(char operator[], int len) {
4244
/*
4345
If we hit an alpha char, there are only two valid expressions:
4446
1) it's one character long, and therefore a variable
@@ -87,13 +89,13 @@ int handle_alpha(char operator[], int len) {
8789
return GARBAGE;
8890
}
8991

90-
int is_operator(char oper[]) {
91-
return ((oper[0] == '+') || (oper[0] == '-') || (oper[0] == '/') ||
92-
(oper[0] == '*') || (oper[0] == '%') || (oper[0] == '='));
92+
int is_operator(char op) {
93+
return ((op == '+') || (op == '-') || (op == '/') || (op == '*') ||
94+
(op == '%') || (op == '='));
9395
}
9496

95-
int handle_operator(char operator[]) {
96-
switch (operator[0]) {
97+
operand_type handle_operator(char operator) {
98+
switch (operator) {
9799
case '+':
98100
return ADD;
99101
case '-':

ch-4/rpc/src/main.c

+20-16
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22

33
// Reverse Polish calculator
44
int main() {
5-
double op1[2];
6-
double op2[2];
7-
int type;
5+
operand op1;
6+
operand op2;
7+
operand_type type;
88
char token[MAX_TOKEN_SIZE];
99

10-
while ((type = lex(token, MAX_TOKEN_SIZE))) {
10+
while ((type = lex(token, MAX_TOKEN_SIZE)) != EXIT) {
1111
switch (type) {
12-
case RAW:
13-
op1[0] = atof(token);
14-
op1[1] = (double)RAW;
12+
case VAL:
13+
op1.type = VAL;
14+
op1.dvalue = atof(token);
15+
op1.cvalue = 0;
1516
push(op1);
1617
break;
1718
case VAR:
18-
op1[0] = token[0];
19-
op1[1] = (double)VAR;
19+
op1.type = VAR;
20+
op1.cvalue = token;
21+
op1.dvalue = 0.0;
2022
push(op1);
2123
break;
2224
case ASSIGN:
@@ -90,16 +92,18 @@ int main() {
9092
pop(op1);
9193
rpn_floor(op1);
9294
break;
93-
case DISPLAY:
94-
display();
95-
break;
9695
case GARBAGE:
9796
default:
9897
printf("Error: invalid expression %s.\n", token);
99-
break;
98+
return -1;
10099
}
101100
}
102-
103-
printf("Quitting, bye!\n"); // TODO: Never actually called?
104-
return 0;
101+
if (get_stack_size() == 1) {
102+
operand final = pop();
103+
printf("%8.8g\n", final.dvalue);
104+
return 0;
105+
} else {
106+
printf("Error: invalid expression; quitting.\n");
107+
return -1;
108+
}
105109
}

ch-4/rpc/src/parser.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#include "rpc.h"
22

33
static char buf[PARSE_BUFFER_SIZE]; // buffer for ungetch
4+
// Uncomment below for 4-8
45
// static int buffered = false;
5-
// static int bufval = 0; // this answers 4-8
6+
// static int bufval = 0;
67
static int bufp = 0;
78

89
int rpc_getch() {

0 commit comments

Comments
 (0)