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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
//************************** interpreter.cpp ***********************
#include <cctype>
#include "interpreter.h"
Iterator list :: begin()
{
}
Iterator list :: end()
{
}
Iterator list :: push_front()
{
}
double Statement::findValue(char *id) {
IdNode tmp(id);
list::iterator i = find(idList.begin(),idList.end(),tmp);
if (i != idList.end())
return i->value;
else issueError("Unknown variable");
return 0; // this statement will never be reached;
}
void Statement::processNode(char* id ,double e) {
IdNode tmp(id,e);
list::iterator i = find(idList.begin(),idList.end(),tmp);
if (i != idList.end())
i->value = e;
else idList.push_front(tmp);
}
// readId() reads strings of letters and digits that start with
// a letter, and stores them in array passed to it as an actual
// parameter.
// Examples of identifiers are: var1, x, pqr123xyz, aName, etc.
void Statement::readId(char *id) {
int i = 0;
if (isspace(ch))
cin >> ch; // skip blanks;
if (isalpha(ch)) {
while (isalnum(ch)) {
id[i++] = ch;
cin.get(ch); // don't skip blanks;
}
id[i] = '\0';
}
else issueError("Identifier expected");
}
double Statement::factor() {
double var, minus = 1.0;
static char id[200];
cin >> ch;
while (ch == '+' || ch == '-') { // take all '+'s and '-'s.
if (ch == '-')
minus *= -1.0;
cin >> ch;
}
if (isdigit(ch) || ch == '.') { // Factor can be a number
cin.putback(ch);
cin >> var >> ch;
}
else if (ch == '(') { // or a parenthesized expression,
var = expression();
if (ch == ')')
cin >> ch;
else issueError("Right paren left out");
}
else {
readId(id); // or an identifier.
if (isspace(ch))
cin >> ch;
var = findValue(id);
}
return minus * var;
}
double Statement::term() {
double f = factor();
while (true) {
switch (ch) {
case '*' : f *= factor(); break;
case '/' : f /= factor(); break;
default : return f;
}
}
}
double Statement::expression() {
double t = term();
while (true) {
switch (ch) {
case '+' : t += term(); break;
case '-' : t -= term(); break;
default : return t;
}
}
}
void Statement::getStatement() {
char id[20], command[20];
double e;
cout << "Enter a statement: ";
cin >> ch;
readId(id);
strcpy(command,id);
for (int i = 0; i < strlen(command); i++)
command[i] = toupper(command[i]);
if (strcmp(command,"STATUS") == 0)
cout << *this;
else if (strcmp(command,"PRINT") == 0) {
readId(id);
cout << id << " = " << findValue(id) << endl;
}
else if (strcmp(command,"END") == 0)
exit(0);
else {
if (isspace(ch))
cin >> ch;
if (ch == '=') {
e = expression();
if (ch != ';')
issueError("There are some extras in the statement");
else processNode(id,e);
}
else issueError("'=' is missing");
}
}
//************************** interpreter.h ***********************
#ifndef INTERPRETER
#define INTERPRETER
#include <iostream>
//#include <list>
//#include <algorithm> // find()
using namespace std;
class IdNode {
public:
IdNode(char *s = "", double e = 0) {
id = strdup(s);
value = e;
}
bool operator== (const IdNode& node) const {
return strcmp(id,node.id) == 0;
}
IdNode *end;
IdNode *begin;
IdNode *push_front;
private:
char *id;
double value;
friend class Statement;
friend ostream& operator<< (ostream& out, const IdNode& r) {
out << r.id << " = " << r.value << endl;
return out;
}
};
class Iterator
{
public:
IdNode *node;
Iterator &operator ++ (int);
IdNode &operator * ();
IdNode *operator -> ();
bool operator != (bool);
};
class list
{
IdNode *head, *tail;
public:
typedef Iterator iterator;
typedef Iterator const_iterator;
Iterator begin ();
Iterator end ();
Iterator push_front ();
};
class Statement {
public:
Statement() {
}
void getStatement();
private:
IdNode idList;
char ch;
double factor();
double term();
double expression();
void readId(char*);
void issueError(char *s) {
cerr << s << endl; exit(1);
}
double findValue(char*);
void processNode(char*, double);
friend ostream& operator<< (ostream& out, const Statement& s) {
list::const_iterator i = s.idList.begin();
for ( ; i != s.idList.end(); i++)
out << *i;
out << endl;
return out;
}
};
#endif
|