Piethon  1.0
A Python-like interpreter using flex and bison.
statement.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <cstdlib>
3 #include "exception.h"
4 #include "number.h"
5 #include "symbolTable.h"
6 #include "functionTable.h"
7 #include "expression.h"
8 #include "function.h"
9 #include "exception.h"
10 #include "statement.h"
11 
12 using std::cout;
13 using std::cerr;
14 using std::endl;
15 
17  return lineNumber;
18 }
19 
21  delete exp;
22 }
23 
25  st[name] = exp->eval(st, ft);
26 }
27 
28 
30  delete expList;
31 }
32 
34  // semi-hack: create const FunctionTable to ensure only accessor
35  // operator[] is used - don't want to get a NULL function*
36  const FunctionTable cft(ft);
37 
38  cft[name]->apply(st, ft, expList);
39 }
40 
41 
43  delete function;
44 }
45 
47  ft[name] = function;
48 }
49 
51  delete condition;
52  delete trueList;
53  delete falseList;
54 }
55 
56 void IfElse::eval(SymbolTable& st, FunctionTable& ft) const {
57  if (condition->eval(st, ft) > 0) {
58  trueList->eval(st, ft);
59  } else {
60  falseList->eval(st, ft);
61  }
62 }
63 
65  delete condition;
66  delete stmtList;
67 }
68 
69 void While::eval(SymbolTable& st, FunctionTable& ft) const {
70  while (condition->eval(st, ft) > 0) {
71  stmtList->eval(st, ft);
72  }
73 }
74 
76  delete exp;
77 }
78 
79 void Print::eval(SymbolTable& st, FunctionTable& ft) const {
80  Number n(exp->eval(st, ft));
81  cout << ">> " << n << endl;
82 }
83 
85  delete exp;
86 }
87 
88 void Return::eval(SymbolTable& st, FunctionTable& ft) const {
89  throw ReturnValue(exp->eval(st, ft), lineNumber);
90 }
91 
92 
94  for (list<Statement*>::const_iterator it = stmtList.begin(); it != stmtList.end(); ++it) {
95  delete *it;
96  }
97 }
98 
100  stmtList.push_back(stmt);
101 }
102 
104  // eval() each statement in statement list
105  list<Statement*>::const_iterator it;
106  try {
107  for (it = stmtList.begin(); it != stmtList.end(); ++it) {
108  (*it)->eval(st, ft);
109  }
110  } catch (ParseException e) {
111  // if we catch an exception indicating a semantic error, print it and bail out
112  cerr << "error (line " << (*it)->getLineNumber() << "): " << e.what() << endl;
113  exit(2);
114  }
115 }
void eval(SymbolTable &, FunctionTable &) const
Definition: statement.cpp:46
void eval(SymbolTable &, FunctionTable &) const
Definition: statement.cpp:79
void eval(SymbolTable &, FunctionTable &) const
Definition: statement.cpp:24
~IfElse()
Definition: statement.cpp:50
void eval(SymbolTable &, FunctionTable &) const
Definition: statement.cpp:103
void eval(SymbolTable &, FunctionTable &) const
Definition: statement.cpp:33
~While()
Definition: statement.cpp:64
void add(Statement *)
Definition: statement.cpp:99
~Print()
Definition: statement.cpp:75
Definition: number.h:21
virtual const char * what() const
Definition: exception.cpp:3
void eval(SymbolTable &, FunctionTable &) const
Definition: statement.cpp:69
virtual int getLineNumber() const
Definition: statement.cpp:16
void eval(SymbolTable &, FunctionTable &) const
Definition: statement.cpp:56
int lineNumber
Definition: statement.h:28
~Return()
Definition: statement.cpp:84
void eval(SymbolTable &, FunctionTable &) const
Definition: statement.cpp:88