1 // Copyright (c) 1996 James Clark
2 // See the file copying.txt for copying permission.
4 #ifndef Expression_INCLUDED
5 #define Expression_INCLUDED 1
17 #ifdef DSSSL_NAMESPACE
18 namespace DSSSL_NAMESPACE {
25 const Identifier *ident;
31 boxedFlags = assignedFlag|sharedFlag
33 static bool flagsBoxed(unsigned f) { return (f & boxedFlags) == boxedFlags; }
34 bool boxed() const { return flagsBoxed(flags); }
36 unsigned reboundCount;
39 class BoundVarList : public Vector<BoundVar> {
42 BoundVarList(const Vector<const Identifier *> &);
43 BoundVarList(const Vector<const Identifier *> &, size_t, unsigned flags = 0);
44 void append(const Identifier *, unsigned flags);
45 void mark(const Identifier *, unsigned flags);
47 void remove(const Vector<const Identifier *> &);
48 void rebind(const Vector<const Identifier *> &);
49 void unbind(const Vector<const Identifier *> &);
50 BoundVar *find(const Identifier *);
56 Environment(const BoundVarList &frameVars,
57 const BoundVarList &closureVars);
58 void boundVars(BoundVarList &) const;
59 bool lookup(const Identifier *var,
60 bool &isFrame, int &index, unsigned &flags) const;
61 void augmentFrame(const BoundVarList &,
64 struct FrameVarList : public Resource {
66 const BoundVarList *vars;
67 ConstPtr<FrameVarList> next;
69 ConstPtr<FrameVarList> frameVarList_;
70 const BoundVarList *closureVars_;
75 Expression(const Location &);
76 virtual ~Expression() { }
77 virtual InsnPtr compile(Interpreter &, const Environment &, int,
80 InsnPtr optimizeCompile(Owner<Expression> &, Interpreter &, const Environment &, int,
82 virtual void markBoundVars(BoundVarList &vars, bool);
83 virtual void optimize(Interpreter &, const Environment &, Owner<Expression> &);
84 virtual ELObj *constantValue() const;
85 virtual bool canEval(bool maybeCall) const = 0;
86 virtual const Identifier *keyword() const;
87 const Location &location() const;
90 InsnPtr compilePushVars(Interpreter &interp,
91 const Environment &env, int stackPos,
92 const BoundVarList &vars, size_t varIndex,
98 class ConstantExpression : public Expression {
100 ConstantExpression(ELObj *, const Location &);
101 InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
102 void optimize(Interpreter &, const Environment &, Owner<Expression> &);
103 bool canEval(bool maybeCall) const;
104 const Identifier *keyword() const;
106 ELObj *obj_; // must be permanent
109 class ResolvedConstantExpression : public Expression {
111 ResolvedConstantExpression(ELObj *, const Location &);
112 InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
113 bool canEval(bool maybeCall) const;
114 ELObj *constantValue() const;
119 class CallExpression : public Expression {
121 CallExpression(Owner<Expression> &, NCVector<Owner<Expression> > &,
122 const Location &loc);
123 InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
124 void markBoundVars(BoundVarList &vars, bool);
126 bool canEval(bool maybeCall) const;
128 Owner<Expression> op_;
129 NCVector<Owner<Expression> > args_;
132 class VariableExpression : public Expression {
134 VariableExpression(const Identifier *, const Location &loc);
135 InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
136 void optimize(Interpreter &, const Environment &, Owner<Expression> &);
137 void markBoundVars(BoundVarList &vars, bool);
138 bool canEval(bool maybeCall) const;
140 const Identifier *ident_;
144 class IfExpression : public Expression {
146 IfExpression(Owner<Expression> &,
150 InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
151 void markBoundVars(BoundVarList &vars, bool);
152 bool canEval(bool maybeCall) const;
153 void optimize(Interpreter &, const Environment &, Owner<Expression> &);
155 Owner<Expression> test_;
156 Owner<Expression> consequent_;
157 Owner<Expression> alternate_;
160 class OrExpression : public Expression {
162 OrExpression(Owner<Expression> &,
165 InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
166 void markBoundVars(BoundVarList &vars, bool);
167 bool canEval(bool maybeCall) const;
168 void optimize(Interpreter &, const Environment &, Owner<Expression> &);
170 Owner<Expression> test1_;
171 Owner<Expression> test2_;
174 class CondFailExpression : public Expression {
176 CondFailExpression(const Location &);
177 InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
178 bool canEval(bool maybeCall) const;
181 class CaseExpression : public Expression {
184 Vector<ELObj *> datums;
185 Owner<Expression> expr;
187 CaseExpression(Owner<Expression> &,
191 InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
192 void markBoundVars(BoundVarList &vars, bool);
193 bool canEval(bool maybeCall) const;
194 void optimize(Interpreter &, const Environment &, Owner<Expression> &);
196 Owner<Expression> key_;
197 NCVector<Case> cases_;
198 Vector<unsigned> nResolved_;
199 Owner<Expression> else_;
202 class LambdaExpression : public Expression {
204 LambdaExpression(Vector<const Identifier *> &vars,
205 NCVector<Owner<Expression> > &inits,
209 Owner<Expression> &body,
211 InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
212 void markBoundVars(BoundVarList &vars, bool);
213 bool canEval(bool maybeCall) const;
215 Vector<const Identifier *> formals_;
216 NCVector<Owner<Expression> > inits_;
218 Owner<Expression> body_;
221 class LetExpression : public Expression {
223 LetExpression(Vector<const Identifier *> &vars,
224 NCVector<Owner<Expression> > &inits,
225 Owner<Expression> &body,
227 InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
228 void markBoundVars(BoundVarList &vars, bool);
229 bool canEval(bool maybeCall) const;
231 InsnPtr compileInits(Interpreter &interp, const Environment &env,
232 const BoundVarList &initVars,
233 size_t initIndex, int stackPos, const InsnPtr &next);
234 Vector<const Identifier *> vars_;
235 NCVector<Owner<Expression> > inits_;
236 Owner<Expression> body_;
239 class LetStarExpression : public LetExpression {
241 LetStarExpression(Vector<const Identifier *> &vars,
242 NCVector<Owner<Expression> > &inits,
243 Owner<Expression> &body,
245 InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
247 InsnPtr compileInits(Interpreter &interp, const Environment &env,
248 const BoundVarList &initVars,
249 size_t initIndex, int stackPos, const InsnPtr &next);
252 class LetrecExpression : public Expression {
254 LetrecExpression(Vector<const Identifier *> &vars,
255 NCVector<Owner<Expression> > &inits,
256 Owner<Expression> &body,
258 InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
259 void markBoundVars(BoundVarList &vars, bool);
260 bool canEval(bool maybeCall) const;
262 InsnPtr compileInits(Interpreter &interp, const Environment &env,
263 size_t initIndex, int stackPos, const InsnPtr &next);
264 Vector<const Identifier *> vars_;
265 NCVector<Owner<Expression> > inits_;
266 Owner<Expression> body_;
269 class QuasiquoteExpression : public Expression {
276 QuasiquoteExpression(NCVector<Owner<Expression> > &,
277 Vector<PackedBoolean> &spliced,
280 InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
281 void markBoundVars(BoundVarList &vars, bool);
282 bool canEval(bool maybeCall) const;
283 void optimize(Interpreter &, const Environment &, Owner<Expression> &);
285 NCVector<Owner<Expression> > members_;
286 Vector<PackedBoolean> spliced_;
290 class SequenceExpression : public Expression {
292 SequenceExpression(NCVector<Owner<Expression> > &,
294 InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
295 void markBoundVars(BoundVarList &vars, bool);
296 bool canEval(bool maybeCall) const;
297 void optimize(Interpreter &, const Environment &, Owner<Expression> &);
299 NCVector<Owner<Expression> > sequence_;
302 class AssignmentExpression : public Expression {
304 AssignmentExpression(const Identifier *,
307 InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
308 void markBoundVars(BoundVarList &vars, bool);
309 bool canEval(bool maybeCall) const;
311 const Identifier *var_;
312 Owner<Expression> value_;
315 class ProcessingMode;
317 class WithModeExpression : public Expression {
319 WithModeExpression(const ProcessingMode *, Owner<Expression> &,
321 InsnPtr compile(Interpreter &, const Environment &, int,
323 void markBoundVars(BoundVarList &vars, bool);
324 bool canEval(bool maybeCall) const;
326 const ProcessingMode *mode_;
327 Owner<Expression> expr_;
330 class StyleExpression : public Expression {
332 StyleExpression(Vector<const Identifier *> &,
333 NCVector<Owner<Expression> > &,
334 const Location &loc);
335 InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
336 void markBoundVars(BoundVarList &vars, bool);
337 bool canEval(bool maybeCall) const;
339 virtual void unknownStyleKeyword(const Identifier *ident, Interpreter &interp,
340 const Location &loc) const;
341 virtual bool maybeStyleKeyword(const Identifier *ident) const;
342 Vector<const Identifier *> keys_;
343 NCVector<Owner<Expression> > exprs_;
348 class MakeExpression : public StyleExpression {
350 MakeExpression(const Identifier *,
351 Vector<const Identifier *> &,
352 NCVector<Owner<Expression> > &,
353 const Location &loc);
354 InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
356 InsnPtr compileNonInheritedCs(Interpreter &interp, const Environment &env,
357 int stackPos, const InsnPtr &next);
358 FlowObj *applyConstNonInheritedCs(FlowObj *, Interpreter &, const Environment &);
359 void unknownStyleKeyword(const Identifier *ident, Interpreter &interp,
360 const Location &loc) const;
361 bool maybeStyleKeyword(const Identifier *ident) const;
363 const Identifier *foc_;
367 const Location &Expression::location() const
373 InsnPtr Expression::optimizeCompile(Owner<Expression> &expr, Interpreter &interp,
374 const Environment &env, int stackPos,
377 expr->optimize(interp, env, expr);
378 return expr->compile(interp, env, stackPos, next);
381 #ifdef DSSSL_NAMESPACE
385 #endif /* not Expression_INCLUDED */