Skip to content

Commit 0caeb52

Browse files
author
Joshua Goller
committed
continued const correcting
1 parent 12e7ba3 commit 0caeb52

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

ch-4/rpc/include/rpc.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ void ungets(const char* const s, const int len);
6767
int parse(char* const s, const int s_size);
6868

6969
// lexer.c
70-
operand_type lex(char symbol[], int len);
71-
operand_type handle_alpha(char oper[], int len);
72-
operand_type handle_numeric(char oper[], int len);
73-
int is_operator(char oper);
74-
operand_type handle_operator(char oper);
70+
operand_type lex(char* const symbol, const int len);
71+
operand_type handle_alpha(const char* const oper, const int len);
72+
operand_type handle_numeric(const char* const oper, const int len);
73+
int is_operator(const char oper);
74+
operand_type handle_operator(const char oper);
7575

7676
// rpn_math.c
7777
bool double_eq(const double x, const double y);
@@ -92,14 +92,14 @@ void rpn_sqrt(operand val1);
9292
void rpn_floor(operand val1);
9393

9494
// stack.c
95-
int push(operand op);
95+
int push(const operand op);
9696
operand pop(void);
9797
operand peek(void);
9898
void duplicate_top(void);
9999
void swap_top(void);
100100
int get_stack_top(void);
101101

102102
// vars.c
103-
void assign(operand val1, operand val2);
104-
operand dereference(operand op);
105-
int validate_var(operand op);
103+
void assign(const operand val1, const operand val2);
104+
operand dereference(const operand op);
105+
int validate_var(const operand op);

ch-4/rpc/src/lexer.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ this file do.
99
*/
1010

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

1515
while ((len = parse(symbol, token_len))) {
16-
// printf("lex() | parsed symbol: %s (%d)\n", symbol, len);
1716
if (len == -1) {
1817
return EXIT;
1918
}
@@ -41,7 +40,7 @@ operand_type lex(char symbol[], int token_len) {
4140
return CONTINUE;
4241
}
4342

44-
operand_type handle_alpha(char operator[], int len) {
43+
operand_type handle_alpha(const char* const operator, const int len) {
4544
/*
4645
If we hit an alpha char, there are only two valid expressions:
4746
1) it's one character long, and therefore a variable
@@ -90,12 +89,12 @@ operand_type handle_alpha(char operator[], int len) {
9089
return GARBAGE;
9190
}
9291

93-
int is_operator(char op) {
92+
int is_operator(const char op) {
9493
return ((op == '+') || (op == '-') || (op == '/') || (op == '*') ||
9594
(op == '%') || (op == '='));
9695
}
9796

98-
operand_type handle_operator(char operator) {
97+
operand_type handle_operator(const char operator) {
9998
// printf("handle_operator() | handling %c\n", operator);
10099
switch (operator) {
101100
case '+':

ch-4/rpc/src/vars.c

+13-14
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,45 @@
55
// A-Z at offsets 26-51
66
static double vars_table[MAX_STACK_SIZE];
77

8-
void assign(operand val1, operand val2) {
8+
void assign(const operand val1, const operand val2) {
99
if (!validate_var(val1)) {
1010
return;
1111
}
1212

13-
if (val2.type == VAR) {
14-
val2 = dereference(val2);
15-
}
13+
const operand val2_ready = (val2.type == VAR) ? dereference(val2) : val2;
1614

1715
if (val1.cvalue >= 'a' && val1.cvalue <= 'z') {
18-
vars_table[val1.cvalue - 'a'] = val2.dvalue;
16+
vars_table[val1.cvalue - 'a'] = val2_ready.dvalue;
1917
} else if (val1.cvalue >= 'A' && val1.cvalue <= 'Z') {
2018
// upper case var storage starts at vars_table[26]
21-
vars_table[val1.cvalue - 'A' + 26] = val2.dvalue;
19+
vars_table[val1.cvalue - 'A' + 26] = val2_ready.dvalue;
2220
}
2321

2422
// C style assignment returns the value assigned
25-
push(val2);
23+
push(val2_ready);
2624
}
2725

28-
operand dereference(operand op) {
26+
operand dereference(const operand op) {
2927
if (!validate_var(op)) {
3028
return (operand){GARBAGE, 0, 0.0};
3129
}
3230

31+
operand new;
3332
if (op.cvalue >= 'a' && op.cvalue <= 'z') {
34-
op.dvalue = vars_table[op.cvalue - 'a'];
33+
new.dvalue = vars_table[op.cvalue - 'a'];
3534
} else if (op.cvalue >= 'A' && op.cvalue <= 'Z') {
36-
op.dvalue = vars_table[op.cvalue - 'A' + 26];
35+
new.dvalue = vars_table[op.cvalue - 'A' + 26];
3736
} else {
3837
printf("Error: Invalid lvalue for variable assignment.\n");
3938
return (operand){GARBAGE, 0, 0.0};
4039
}
4140

42-
op.type = VAL;
43-
op.cvalue = 0;
44-
return op;
41+
new.type = VAL;
42+
new.cvalue = 0;
43+
return new;
4544
}
4645

47-
int validate_var(operand var) {
46+
int validate_var(const operand var) {
4847
if (var.type != VAR) {
4948
printf("validate_var() | Error: operand is not a VAR.\n");
5049
return 0;

0 commit comments

Comments
 (0)