-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoOperator.c
77 lines (67 loc) · 1.44 KB
/
doOperator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "doOperator.h"
#include "tokenStack.h"
#include "lexical.h"
static int op_quit(struct tokenStack *stack);
static int op_print(struct tokenStack *stack);
static int op_dump(struct tokenStack *stack);
static int op_add(struct tokenStack *stack);
static struct operator_struct {
char *name;
int (*fn_ptr)(struct tokenStack *);
} ops[] = {
{"quit", op_quit},
{"print", op_print},
{"dump", op_dump},
{"+", op_add},
{(char *)NULL, (int(*)(struct tokenStack *)) NULL}
};
/* YOU WRITE THIS */
static int popInt(struct tokenStack *s)
{
return 0;
}
/* YOU WRITE THIS */
static void pushInt(struct tokenStack *s, int v)
{
}
int doOperator(struct tokenStack *stack, char *o)
{
struct operator_struct *op = ops;
for(op=ops;op->name != (char *)NULL; op++) {
if(!strcmp(op->name, o))
return op->fn_ptr(stack);
}
return(-1);
}
/*ARGSUSED*/
static int op_quit(struct tokenStack *stack)
{
printf("[quit]\n");
exit(0);
/*NOTREACHED*/
}
static int op_print(struct tokenStack *stack)
{
struct lexToken *t = popTokenStack(stack);
printToken(stdout, t);
freeToken(t);
return(0);
}
static int op_dump(struct tokenStack *stack)
{
struct lexToken *t = popTokenStack(stack);
dumpToken(stdout, t);
freeToken(t);
return(0);
}
static int op_add(struct tokenStack *stack)
{
int v1, v2;
v1 = popInt(stack);
v2 = popInt(stack);
pushInt(stack, v1+v2);
return(0);
}