-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacker.cc
150 lines (122 loc) · 2.55 KB
/
stacker.cc
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include "stacker.h"
#include "proggen.h"
/*
class StackItem {
enum { Number, Operator, Variable } type;
union {
double number;
char operator;
char* variable;
} value;
};
*/
Stack stack;
template<typename T>
Symbols<T>::Symbols () {
count = 0;
names = new char* [50];
values = new T [50];
}
template<typename T>
int Symbols<T>::lookup (const char* name) {
for (int i = 0; i < count; i++) {
if (!strcmp (name, names[i])) {
return i;
}
}
return -1;
}
template<typename T>
int Symbols<T>::add (const char* name) {
int ret = lookup(name);
if (ret == -1) {
names[count] = strdup (name);
values[count] = (T)0;
ret = count++;
}
return ret;
}
template<typename T>
int Symbols<T>::add (const char* name, T val) {
int i = add(name);
set(i, val);
}
template<typename T>
T Symbols<T>::get (int index) {
return values[index];
}
template<typename T>
void Symbols<T>::set (int index, T val) {
values[index] = val;
}
Symbols<double> variables;
Symbols<pfunc> functions;
Program::Program () {
instructions = new Instruction* [50];
pc = 0;
}
void Program::add(Instruction* i) {
instructions[pc++] = i;
}
void Program::execute () {
for(int i = 0; i < pc; i++) {
instructions[i]->execute ();
}
}
Stack::Stack () : pointer(0) {
items = new double [50];
}
Stack::~Stack () {
delete [] items;
}
Program program;
extern "C" double initProg () {
variables.add("x");
functions.add("sin", sin);
functions.add("cos", cos);
functions.add("tan", tan);
functions.add("exp", exp);
functions.add("log", log);
// functions.add("ln", ln);
}
extern "C" double genEval (double val) {
double ret;
int ix = variables.lookup("x");
variables.set (ix, val);
stack.reset ();
program.execute();
ret = stack.items[0];
printf("ret=%f\n", ret);
return ret;
}
extern "C" void push(double value) {
program.add (new Push(value));
}
extern "C" void pushVariable (char* name) {
int varIndex = variables.lookup (name);
program.add (new PushVariable(varIndex));
}
extern "C" void functionCall (char* name) {
// printf("functionCall name='%s'\n", name);
int i = functions.lookup (name);
pfunc f = functions.get(i);
program.add (new FunctionCall(f));
}
extern "C" void add () {
program.add(new Add());
}
extern "C" void sub () {
program.add(new Sub());
}
extern "C" void mul () {
program.add(new Mul());
}
extern "C" void gendiv () {
program.add(new Div());
}
extern "C" void genpow () {}
extern "C" void neg () {}