Debian Codegraph @

terps/tads/tads3/tcjsint.h

raw symbols SCIP
1 /* Copyright (c) 2009 by Michael J. Roberts. All Rights Reserved. */
2 /*
3 Name
4 tcjsint.h - tads 3 compiler - javascript code generator - intermediate
5 parse node classes
6 Function
7
8 Notes
9
10 Modified
11 02/17/09 MJRoberts - Creation
12 */
13
14 #ifndef TCJSINT_H
15 #define TCJSINT_H
16
17 #include "tcjsbase.h"
18 #include "tcpnint.h"
19
20 /* ------------------------------------------------------------------------ */
21 /*
22 * Unary Operator Expression node
23 */
24 class CTPNUnary: public CTPNUnaryBase
25 {
26 public:
27 CTPNUnary(CTcPrsNode *subexpr)
28 : CTPNUnaryBase(subexpr) { }
29
30 /*
31 * Generate code for a unary operator with no side effects. We'll
32 * generate code for the subexpression, then apply the given javascript
33 * operator to the result.
34 *
35 * If 'discard' is true, we won't generate the opcode. We know the
36 * opcode has no side effects, so if the result of the calculation
37 * isn't going to be used, there's no point in calculating it in the
38 * first place.
39 */
40 void gen_unary(const char *op, int discard, int for_condition);
41 };
42
43
44 /* ------------------------------------------------------------------------ */
45 /*
46 * Binary Operator Expression node
47 */
48 class CTPNBin: public CTPNBinBase
49 {
50 public:
51 CTPNBin(CTcPrsNode *lhs, CTcPrsNode *rhs)
52 : CTPNBinBase(lhs, rhs) { }
53
54 /*
55 * Generate code for a binary operator with no side effects. We'll
56 * generate code for the left operand, then for the right operand, then
57 * apply the given javascript operator to the pair.
58 *
59 * If 'discard' is true, we won't generate the opcode. We know that
60 * the opcode has no side effects, so if the result of the calculation
61 * isn't needed, we need not apply the opcode; we simply want to
62 * evaluate the subexpressions for any side effects they might have.
63 */
64 void gen_binary(const char *op, int discard, int for_condition);
65 };
66
67 /* ------------------------------------------------------------------------ */
68 /*
69 * generic statement
70 */
71 class CTPNStm: public CTPNStmBase
72 {
73 public:
74 /* initialize at the tokenizer's current source file position */
75 CTPNStm() : CTPNStmBase() { }
76
77 /* initialize at the given source position */
78 CTPNStm(class CTcTokFileDesc *file, long linenum)
79 : CTPNStmBase(file, linenum) { }
80
81 /*
82 * Generate code for a labeled 'continue'. These are called when this
83 * statement is labeled, and the same label is used in a 'continue'
84 * statement.
85 *
86 * Returns true if successful, false if not. This should return false
87 * if the statement is not suitable for a 'continue'. By default, we
88 * simply return false, since most statements cannot be used as the
89 * target of a 'continue'.
90 */
91 virtual int gen_code_labeled_continue() { return FALSE; }
92 };
93
94 /* ------------------------------------------------------------------------ */
95 /*
96 * Enclosing Statement class: this is the base class for statements that
97 * have special needs for exiting due to break, continue, goto, and return.
98 * Refer to tcpnint.h for details.
99 */
100 class CTPNStmEnclosing: public CTPNStm
101 {
102 public:
103 CTPNStmEnclosing(CTPNStmEnclosing *enclosing)
104 {
105 /* remember the statement that encloses me */
106 enclosing_ = enclosing;
107 }
108
109 /* get the enclosing statement */
110 CTPNStmEnclosing *get_enclosing() const { return enclosing_; }
111
112 /*
113 * Generate code for a 'break' to a given label, or an unlabeled
114 * 'break' if lbl is null. Returns true if the break was fully
115 * processed, false if not. If any work is necessary to unwind the
116 * exception stack to break out of this block, that should be done
117 * here. If this node doesn't actually accomplish the break, it should
118 * recursively invoke the next enclosing statement's routine to do the
119 * work.
120 *
121 * By default, we'll simply invoke the enclosing handler, if there is
122 * one, or return failure (i.e., false) if not.
123 */
124 virtual int gen_code_break(const textchar_t *lbl, size_t lbl_len)
125 {
126 if (enclosing_)
127 return enclosing_->gen_code_break(lbl, lbl_len);
128 else
129 return 0;
130 }
131
132 /*
133 * Generate code for a 'continue' to a given label, or an unlabeled
134 * 'continue' if lbl is null. Same rules as gen_code_break().
135 *
136 * By default, we'll simply invoke the enclosing handler, if there is
137 * one, or return failure (i.e., false) if not.
138 */
139 virtual int gen_code_continue(const textchar_t *lbl, size_t lbl_len)
140 {
141 if (enclosing_)
142 return enclosing_->gen_code_continue(lbl, lbl_len);
143 else
144 return 0;
145 }
146
147 /*
148 * Generate the code necessary to unwind the stack for returning
149 * through this enclosing statement. This should generate any code
150 * necessary (such as a call to a 'finally' block, for a 'try'
151 * statement), then call the next enclosing statement to do the same
152 * thing. By default, this does nothing except invoke the enclosing
153 * statement, since most enclosing statements don't need to generate
154 * any code to handle a 'return' through them.
155 */
156 virtual void gen_code_unwind_for_return()
157 {
158 /* if there's an enclosing statement, ask it to unwind itself */
159 if (enclosing_ != 0)
160 enclosing_->gen_code_unwind_for_return();
161 }
162
163 /*
164 * Determine if we'll generate any code to unwind the stack for
165 * returning through this enclosing statement. This should return true
166 * if any code will be generated by a call to
167 * gen_code_unwind_for_return(). By default, we'll return whatever our
168 * enclosing statement does.
169 */
170 virtual int will_gen_code_unwind_for_return() const
171 {
172 /*
173 * return what the enclosing statement does, or false if we're the
174 * outermost statement
175 */
176 return (enclosing_ != 0
177 ? enclosing_->will_gen_code_unwind_for_return()
178 : FALSE);
179 }
180
181 /*
182 * Generate the code necessary to unwind the stack for executing a
183 * 'goto' to the given label statement. We first determine if the
184 * target label is contained within this statement; if it is, there's
185 * nothing we need to do, because we're transferring control within the
186 * same enclosing statement.
187 *
188 * If control is being transferred outside of this statement (i.e., the
189 * label is not contained within this statement), we must generate the
190 * necessary code to leave the block; for example, a 'try' block must
191 * generate a call to its 'finally' block. We must then invoke this
192 * same function on our enclosing statement to let it do the same work.
193 */
194 void gen_code_unwind_for_goto(class CTPNStmGoto *goto_stm,
195 class CTPNStmLabel *target);
196
197 /*
198 * Generate code for transferring control out of this statement. We
199 * invoke this from gen_code_unwind_for_goto() when we determine that
200 * the 'goto' target is not enclosed within this statement. By default
201 * we do nothing; statements such as 'try' that must do work on
202 * transferring control must override this to generate the appropriate
203 * code.
204 */
205 virtual void gen_code_for_transfer_out() { }
206
207 /*
208 * Check to see if we are allowed to transfer in to this block via
209 * 'goto' statements. If so, simply return TRUE. If we don't allow
210 * this type of transfer, we should log an appropriate error, then
211 * return FALSE. Note that the function should both log an error and
212 * return FALSE if the transfer isn't allowed; the return value allows
213 * the caller to ensure that only one error is displayed for this type
214 * of problem even if the target label is nested within several levels
215 * of blocks that don't allow transfers in.
216 *
217 * By default, we'll simply return TRUE, since most block types allow
218 * this type of transfer.
219 */
220 virtual int check_enter_by_goto(class CTPNStmGoto * /*goto_stm*/,
221 class CTPNStmLabel * /*target*/)
222 { return TRUE; }
223
224 protected:
225 /*
226 * generate code for a break - this can be used as a service routine
227 * by loop/switch statements
228 */
229 int gen_code_break_loop(CTcCodeLabel *code_label,
230 const textchar_t *lbl, size_t lbl_len);
231
232 /*
233 * generate code for a continue - this can be used as a service
234 * routine by loop/switch statements
235 */
236 int gen_code_continue_loop(CTcCodeLabel *code_label,
237 const textchar_t *lbl, size_t lbl_len);
238
239
240 /* the block enclosing this block */
241 CTPNStmEnclosing *enclosing_;
242 };
243
244 #endif /* TCJSINT_H */