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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  
     | 
    
      // Copyright (c) 1996 James Clark
// See the file copying.txt for copying permission.
#ifndef Expression_INCLUDED
#define Expression_INCLUDED 1
#include "ELObj.h"
#include <OpenSP/Owner.h>
#include <OpenSP/Vector.h>
#include <OpenSP/NCVector.h>
#include <OpenSP/Resource.h>
#include <OpenSP/Ptr.h>
#include "Insn.h"
#include <OpenSP/Named.h>
#include <OpenSP/Location.h>
#ifdef DSSSL_NAMESPACE
namespace DSSSL_NAMESPACE {
#endif
class Interpreter;
class Identifier;
struct BoundVar {
  const Identifier *ident;
  enum {
    usedFlag = 01,
    assignedFlag = 02,
    sharedFlag = 04,
    uninitFlag = 010,
    boxedFlags = assignedFlag|sharedFlag
  };
  static bool flagsBoxed(unsigned f) { return (f & boxedFlags) == boxedFlags; }
  bool boxed() const { return flagsBoxed(flags); }
  unsigned flags;
  unsigned reboundCount;
};
class BoundVarList : public Vector<BoundVar> {
public:
  BoundVarList() { }
  BoundVarList(const Vector<const Identifier *> &);
  BoundVarList(const Vector<const Identifier *> &, size_t, unsigned flags = 0);
  void append(const Identifier *, unsigned flags);
  void mark(const Identifier *, unsigned flags);
  void removeUnused();
  void remove(const Vector<const Identifier *> &);
  void rebind(const Vector<const Identifier *> &);
  void unbind(const Vector<const Identifier *> &);
  BoundVar *find(const Identifier *);
};
class Environment {
public:
  Environment();
  Environment(const BoundVarList &frameVars,
	      const BoundVarList &closureVars);
  void boundVars(BoundVarList &) const;
  bool lookup(const Identifier *var,
	      bool &isFrame, int &index, unsigned &flags) const;
  void augmentFrame(const BoundVarList &,
		    int stackPos);
private:
  struct FrameVarList : public Resource {
    int stackPos;
    const BoundVarList *vars;
    ConstPtr<FrameVarList> next;
  };
  ConstPtr<FrameVarList> frameVarList_;
  const BoundVarList *closureVars_;
};
class Expression {
public:
  Expression(const Location &);
  virtual ~Expression() { }
  virtual InsnPtr compile(Interpreter &, const Environment &, int,
			  const InsnPtr &) = 0;
  static
    InsnPtr optimizeCompile(Owner<Expression> &, Interpreter &, const Environment &, int,
                            const InsnPtr &);
  virtual void markBoundVars(BoundVarList &vars, bool);
  virtual void optimize(Interpreter &, const Environment &, Owner<Expression> &);
  virtual ELObj *constantValue() const;
  virtual bool canEval(bool maybeCall) const = 0;
  virtual const Identifier *keyword() const;
  const Location &location() const;
protected:
  static
  InsnPtr compilePushVars(Interpreter &interp,
			  const Environment &env, int stackPos,
			  const BoundVarList &vars, size_t varIndex,
			  const InsnPtr &next);
private:
  Location loc_;
};
class ConstantExpression : public Expression {
public:
  ConstantExpression(ELObj *, const Location &);
  InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
  void optimize(Interpreter &, const Environment &, Owner<Expression> &);
  bool canEval(bool maybeCall) const;
  const Identifier *keyword() const;
private:
  ELObj *obj_;			// must be permanent
};
class ResolvedConstantExpression : public Expression {
public:
  ResolvedConstantExpression(ELObj *, const Location &);
  InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
  bool canEval(bool maybeCall) const;
  ELObj *constantValue() const;
private:
  ELObj *obj_;
};
class CallExpression : public Expression {
public:
  CallExpression(Owner<Expression> &, NCVector<Owner<Expression> > &,
		 const Location &loc);
  InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
  void markBoundVars(BoundVarList &vars, bool);
  int nArgs();
  bool canEval(bool maybeCall) const;
private:
  Owner<Expression> op_;
  NCVector<Owner<Expression> > args_;
};
class VariableExpression : public Expression {
public:
  VariableExpression(const Identifier *, const Location &loc);
  InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
  void optimize(Interpreter &, const Environment &, Owner<Expression> &);
  void markBoundVars(BoundVarList &vars, bool);
  bool canEval(bool maybeCall) const;
private:
  const Identifier *ident_;
  bool isTop_;
};
class IfExpression : public Expression {
public:
  IfExpression(Owner<Expression> &,
	       Owner<Expression> &,
	       Owner<Expression> &,
	       const Location &);
  InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
  void markBoundVars(BoundVarList &vars, bool);
  bool canEval(bool maybeCall) const;
  void optimize(Interpreter &, const Environment &, Owner<Expression> &);
private:
  Owner<Expression> test_;
  Owner<Expression> consequent_;
  Owner<Expression> alternate_;
};
class OrExpression : public Expression {
public:
  OrExpression(Owner<Expression> &,
	       Owner<Expression> &,
	       const Location &);
  InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
  void markBoundVars(BoundVarList &vars, bool);
  bool canEval(bool maybeCall) const;
  void optimize(Interpreter &, const Environment &, Owner<Expression> &);
private:
  Owner<Expression> test1_;
  Owner<Expression> test2_;
};
class CondFailExpression : public Expression {
public:
  CondFailExpression(const Location &);
  InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
  bool canEval(bool maybeCall) const;
};
class CaseExpression : public Expression {
public:
  struct Case {
    Vector<ELObj *> datums;
    Owner<Expression> expr;
  };
  CaseExpression(Owner<Expression> &,
		 NCVector<Case> &,
		 Owner<Expression> &,
		 const Location &);
  InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
  void markBoundVars(BoundVarList &vars, bool);
  bool canEval(bool maybeCall) const;
  void optimize(Interpreter &, const Environment &, Owner<Expression> &);
private:
  Owner<Expression> key_;
  NCVector<Case> cases_;
  Vector<unsigned> nResolved_;
  Owner<Expression> else_;
};
class LambdaExpression : public Expression {
public:
  LambdaExpression(Vector<const Identifier *> &vars,
		   NCVector<Owner<Expression> > &inits,
		   int nOptional,
		   bool hasRest,
		   int nKey,
		   Owner<Expression> &body,
		   const Location &);
  InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
  void markBoundVars(BoundVarList &vars, bool);
  bool canEval(bool maybeCall) const;
private:
  Vector<const Identifier *> formals_;
  NCVector<Owner<Expression> > inits_;
  Signature sig_;
  Owner<Expression> body_;
};
class LetExpression : public Expression {
public:
  LetExpression(Vector<const Identifier *> &vars,
		NCVector<Owner<Expression> > &inits,
		Owner<Expression> &body,
		const Location &);
  InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
  void markBoundVars(BoundVarList &vars, bool);
  bool canEval(bool maybeCall) const;
protected:
  InsnPtr compileInits(Interpreter &interp, const Environment &env,
		       const BoundVarList &initVars,
		       size_t initIndex, int stackPos, const InsnPtr &next);
  Vector<const Identifier *> vars_;
  NCVector<Owner<Expression> > inits_;
  Owner<Expression> body_;
};
class LetStarExpression : public LetExpression {
public:
  LetStarExpression(Vector<const Identifier *> &vars,
		    NCVector<Owner<Expression> > &inits,
		    Owner<Expression> &body,
		    const Location &);
  InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
private:
  InsnPtr compileInits(Interpreter &interp, const Environment &env,
		       const BoundVarList &initVars,
		       size_t initIndex, int stackPos, const InsnPtr &next);
};
class LetrecExpression : public Expression {
public:
  LetrecExpression(Vector<const Identifier *> &vars,
		   NCVector<Owner<Expression> > &inits,
		   Owner<Expression> &body,
		   const Location &);
  InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
  void markBoundVars(BoundVarList &vars, bool);
  bool canEval(bool maybeCall) const;
private:
  InsnPtr compileInits(Interpreter &interp, const Environment &env,
		       size_t initIndex, int stackPos, const InsnPtr &next);
  Vector<const Identifier *> vars_;
  NCVector<Owner<Expression> > inits_;
  Owner<Expression> body_;
};
class QuasiquoteExpression : public Expression {
public:
  enum Type {
    listType,
    improperType,
    vectorType
  };
  QuasiquoteExpression(NCVector<Owner<Expression> > &,
		       Vector<PackedBoolean> &spliced,
		       Type type,
		       const Location &);
  InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
  void markBoundVars(BoundVarList &vars, bool);
  bool canEval(bool maybeCall) const;
  void optimize(Interpreter &, const Environment &, Owner<Expression> &);
private:
  NCVector<Owner<Expression> > members_;
  Vector<PackedBoolean> spliced_;
  Type type_;
};
class SequenceExpression : public Expression {
public:
  SequenceExpression(NCVector<Owner<Expression> > &,
		     const Location &);
  InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
  void markBoundVars(BoundVarList &vars, bool);
  bool canEval(bool maybeCall) const;
  void optimize(Interpreter &, const Environment &, Owner<Expression> &);
private:
  NCVector<Owner<Expression> > sequence_;
};
class AssignmentExpression : public Expression {
public:
  AssignmentExpression(const Identifier *,
                       Owner<Expression> &,
		       const Location &);
  InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
  void markBoundVars(BoundVarList &vars, bool);
  bool canEval(bool maybeCall) const;
private:
  const Identifier *var_;
  Owner<Expression> value_;
};
class ProcessingMode;
class WithModeExpression : public Expression {
public:
  WithModeExpression(const ProcessingMode *, Owner<Expression> &,
		     const Location &);
  InsnPtr compile(Interpreter &, const Environment &, int,
		  const InsnPtr &);
  void markBoundVars(BoundVarList &vars, bool);
  bool canEval(bool maybeCall) const;
private:
  const ProcessingMode *mode_;
  Owner<Expression> expr_;
};
class StyleExpression : public Expression {
public:
  StyleExpression(Vector<const Identifier *> &,
		  NCVector<Owner<Expression> > &,
		  const Location &loc);
  InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
  void markBoundVars(BoundVarList &vars, bool);
  bool canEval(bool maybeCall) const;
protected:
  virtual void unknownStyleKeyword(const Identifier *ident, Interpreter &interp,
                                   const Location &loc) const;
  virtual bool maybeStyleKeyword(const Identifier *ident) const;
  Vector<const Identifier *> keys_;
  NCVector<Owner<Expression> > exprs_;
};
class FlowObj;
class MakeExpression : public StyleExpression {
public:
  MakeExpression(const Identifier *,
                 Vector<const Identifier *> &,
		 NCVector<Owner<Expression> > &,
		 const Location &loc);
  InsnPtr compile(Interpreter &, const Environment &, int, const InsnPtr &);
private:
  InsnPtr compileNonInheritedCs(Interpreter &interp, const Environment &env,
				int stackPos, const InsnPtr &next);
  FlowObj *applyConstNonInheritedCs(FlowObj *, Interpreter &, const Environment &);
  void unknownStyleKeyword(const Identifier *ident, Interpreter &interp,
			   const Location &loc) const;
  bool maybeStyleKeyword(const Identifier *ident) const;
  const Identifier *foc_;
};
inline
const Location &Expression::location() const
{
  return loc_;
}
inline
InsnPtr Expression::optimizeCompile(Owner<Expression> &expr, Interpreter &interp,
				    const Environment &env, int stackPos,
				    const InsnPtr &next)
{
  expr->optimize(interp, env, expr);
  return expr->compile(interp, env, stackPos, next);
}
#ifdef DSSSL_NAMESPACE
}
#endif
#endif /* not Expression_INCLUDED */
 
     |