summaryrefslogtreecommitdiff
path: root/graphics/asymptote/stm.h
blob: 3f8d426014bf3f49a14baec268aff6d87217282a (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
/*****
 * stm.h
 * Andy Hammerlindl 2002/8/30
 *
 * Statements are objects in the language that do something on their
 * own.  Statements are different from declarations in that statements
 * do not modify the environment.  Translation of a statements puts the
 * stack code to run it into the instruction stream.
 *****/

#ifndef STM_H
#define STM_H

#include "types.h"
#include "symbol.h"
#include "dec.h"

namespace trans {
class coenv;
}

namespace absyntax {

using trans::coenv;
using sym::symbol;

class stm : public runnable {
public:
  stm(position pos)
    : runnable(pos) {}

  void prettyprint(ostream &out, Int indent);

  void transAsField(coenv &e, record *) {
    // Ignore the record.
    trans(e);
  }

  void trans(coenv &e) = 0;
};

class emptyStm : public stm {
public:
  emptyStm(position pos)
    : stm(pos) {}

  void prettyprint(ostream &out, Int indent);

  void trans(coenv &) {}
};

// Wrapper around a block to use it as a statement.
class blockStm : public stm {
  block *base;

public:
  blockStm(position pos, block *base)
    : stm(pos), base(base) {}

  void prettyprint(ostream &out, Int indent);

  void trans(coenv &e) {
    return base->trans(e);
  }

  // A block is guaranteed to return iff its last statement is
  // guaranteed to return.
  bool returns() {
    return base->returns();
  }

};

// A statement that consist of a single expression to evaluate.
class expStm : public stm {
  exp *body;

public:
  expStm(position pos, exp *body)
    : stm(pos), body(body) {}

  void prettyprint(ostream &out, Int indent);

  void trans(coenv &e);

  // Should be called when running an expStm at the interactive prompt.
  // The code will "write" the value of the expression at the prompt if
  // possible.
  void interactiveTrans(coenv &e);
};

class ifStm : public stm {
  exp *test;
  stm *onTrue;
  stm *onFalse;

public:
  ifStm(position pos, exp *test, stm* onTrue, stm* onFalse = 0)
    : stm(pos), test(test), onTrue(onTrue), onFalse(onFalse) {}

  void prettyprint(ostream &out, Int indent);

  void trans(coenv &e);

  // An if statement is guaranteed to return iff both its pieces are
  // guaranteed to return.
  bool returns() {
    if (onTrue == 0 || onFalse == 0)
      return false;
    return onTrue->returns() && onFalse->returns();
  }
};

class whileStm : public stm {
  exp *test;
  stm *body;

public:
  whileStm(position pos, exp *test, stm *body)
    : stm(pos), test(test), body(body) {}

  void prettyprint(ostream &out, Int indent);

  void trans(coenv &e);
};

class doStm : public stm {
  stm *body;
  exp *test;

public:
  doStm(position pos, stm *body, exp *test)
    : stm(pos), body(body), test(test) {}

  void prettyprint(ostream &out, Int indent);

  void trans(coenv &e);
};

class forStm : public stm {
  runnable *init;
  exp *test;
  runnable *update;
  stm *body;

public:
  forStm(position pos, runnable *init, exp *test, runnable *update, stm *body)
    : stm(pos), init(init), test(test), update(update), body(body) {}

  void prettyprint(ostream &out, Int indent);

  void trans(coenv &e);
};

class extendedForStm : public stm {
  ty *start;
  symbol var;
  exp *set;

  stm *body;

public:
  extendedForStm(position pos, ty *start, symbol var, exp *set, stm *body)
    : stm(pos), start(start), var(var), set(set), body(body) {}

  void prettyprint(ostream &out, Int indent);

  void trans(coenv &e);
};


class breakStm : public stm {
public:
  breakStm(position pos)
    : stm(pos) {}

  void prettyprint(ostream &out, Int indent);

  void trans(coenv &e);
};

class continueStm : public stm {
public:
  continueStm(position pos)
    : stm(pos) {}

  void prettyprint(ostream &out, Int indent);

  void trans(coenv &e);
};

class returnStm : public stm {
  exp *value;

public:
  returnStm(position pos, exp *value = 0)
    : stm(pos), value(value) {}

  void prettyprint(ostream &out, Int indent);

  void trans(coenv &e);

  // A return statement is, of course, guaranteed to return.
  bool returns() {
    return true;
  }
};


// Used at the start of for loops.
class stmExpList : public stm {
  mem::list<stm *> stms;

public:
  stmExpList(position pos)
    : stm(pos) {}

  // To ensure list deallocates properly.
  virtual ~stmExpList() {}

  void add(stm *s) {
    stms.push_back(s);
  }

  void prettyprint(ostream &out, Int indent);

  void trans(coenv &e);
};

} // namespace absyntax

#endif