| 1 |
// Copyright (c) 1999 Matthias Clasen |
| 2 |
// See the file copying.txt for copying permission. |
| 3 |
|
| 4 |
#include "stylelib.h" |
| 5 |
#include "TransformationMode.h" |
| 6 |
#include "Expression.h" |
| 7 |
#include "Interpreter.h" |
| 8 |
#include "InterpreterMessages.h" |
| 9 |
#include "ELObjMessageArg.h" |
| 10 |
#include "VM.h" |
| 11 |
#include <stdlib.h> // for qsort |
| 12 |
|
| 13 |
#ifdef DSSSL_NAMESPACE |
| 14 |
namespace DSSSL_NAMESPACE { |
| 15 |
#endif |
| 16 |
|
| 17 |
|
| 18 |
TransformationMode::TransformationMode() |
| 19 |
{ |
| 20 |
} |
| 21 |
|
| 22 |
void TransformationMode::Action::get(InsnPtr &insn, Vector<CreateSpecObj *> &specs) const |
| 23 |
{ |
| 24 |
insn = insn_; |
| 25 |
specs = specs_; |
| 26 |
} |
| 27 |
|
| 28 |
const Location &TransformationMode::Action::location() const |
| 29 |
{ |
| 30 |
return defLoc_; |
| 31 |
} |
| 32 |
|
| 33 |
TransformationMode::Action &TransformationMode::Association::action() |
| 34 |
{ |
| 35 |
return *action_; |
| 36 |
} |
| 37 |
|
| 38 |
long TransformationMode::Association::priority() const |
| 39 |
{ |
| 40 |
return priority_; |
| 41 |
} |
| 42 |
|
| 43 |
const TransformationMode::Action &TransformationMode::Association::action() const |
| 44 |
{ |
| 45 |
return *action_; |
| 46 |
} |
| 47 |
|
| 48 |
unsigned TransformationMode::Action::partIndex() const |
| 49 |
{ |
| 50 |
return partIndex_; |
| 51 |
} |
| 52 |
|
| 53 |
const Location &TransformationMode::Association::location() const |
| 54 |
{ |
| 55 |
return action_->location(); |
| 56 |
} |
| 57 |
|
| 58 |
extern "C" { |
| 59 |
|
| 60 |
static int associationCompare(const void *p1, const void *p2) |
| 61 |
{ |
| 62 |
return (*(const TransformationMode::Association *const *)p1)->priority() |
| 63 |
- (*(const TransformationMode::Association *const *)p2)->priority(); |
| 64 |
}; |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
void TransformationMode::compile(Interpreter &interp, const NodePtr &node) |
| 69 |
{ |
| 70 |
for (size_t j = 0; j < associations_.size(); j++) |
| 71 |
associations_[j].compile(interp, node); |
| 72 |
qsort(&associations_[0], associations_.size(), |
| 73 |
sizeof(associations_[0]), associationCompare); |
| 74 |
} |
| 75 |
|
| 76 |
TransformationMode::Action::Action(unsigned partIndex, Owner<Expression> &expr, |
| 77 |
const Location &loc) |
| 78 |
: partIndex_(partIndex), defLoc_(loc) |
| 79 |
{ |
| 80 |
expr.swap(expr_); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
TransformationMode::Association::Association() |
| 85 |
: action_(0), priority_(0), nl_(0) |
| 86 |
{ |
| 87 |
} |
| 88 |
|
| 89 |
TransformationMode::Association::Association(Owner<Expression> &qexpr, |
| 90 |
Owner<Expression> &pexpr, |
| 91 |
const Ptr<TransformationMode::Action> &action) |
| 92 |
: action_(action), priority_(0), nl_(0) |
| 93 |
{ |
| 94 |
qexpr.swap(qexpr_); |
| 95 |
pexpr.swap(pexpr_); |
| 96 |
} |
| 97 |
|
| 98 |
void TransformationMode::Association::swap(Association &a) |
| 99 |
{ |
| 100 |
action_.swap(a.action_); |
| 101 |
qexpr_.swap(a.qexpr_); |
| 102 |
pexpr_.swap(a.pexpr_); |
| 103 |
} |
| 104 |
|
| 105 |
void TransformationMode::Association::compile(Interpreter &interp, const NodePtr &node) |
| 106 |
{ |
| 107 |
action_->compile(interp); |
| 108 |
qexpr_->optimize(interp, Environment(), qexpr_); |
| 109 |
ELObj *tem = qexpr_->constantValue(); |
| 110 |
if (!tem) { |
| 111 |
InsnPtr insn = qexpr_->compile(interp, Environment(), 0, InsnPtr()); |
| 112 |
VM vm(interp); |
| 113 |
EvalContext::CurrentNodeSetter cns(node, 0, vm); |
| 114 |
tem = vm.eval(insn.pointer()); |
| 115 |
} |
| 116 |
if (tem->asNodeList()) |
| 117 |
nl_ = tem->asNodeList(); |
| 118 |
else { |
| 119 |
interp.setNextLocation(qexpr_->location()); |
| 120 |
interp.message(InterpreterMessages::queryNotANodeList, |
| 121 |
ELObjMessageArg(tem, interp)); |
| 122 |
nl_ = interp.makeEmptyNodeList(); |
| 123 |
} |
| 124 |
|
| 125 |
pexpr_->optimize(interp, Environment(), pexpr_); |
| 126 |
tem = pexpr_->constantValue(); |
| 127 |
if (!tem) { |
| 128 |
InsnPtr insn = pexpr_->compile(interp, Environment(), 0, InsnPtr()); |
| 129 |
VM vm(interp); |
| 130 |
EvalContext::CurrentNodeSetter cns(node, 0, vm); |
| 131 |
tem = vm.eval(insn.pointer()); |
| 132 |
} |
| 133 |
if (!tem->exactIntegerValue(priority_)) { |
| 134 |
interp.setNextLocation(pexpr_->location()); |
| 135 |
interp.message(InterpreterMessages::priorityNotAnExactInteger, |
| 136 |
ELObjMessageArg(tem, interp)); |
| 137 |
priority_ = 0; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
void TransformationMode::Action::compile(Interpreter &interp) |
| 142 |
{ |
| 143 |
expr_->optimize(interp, Environment(), expr_); |
| 144 |
ELObj *tem = expr_->constantValue(); |
| 145 |
if (tem) { |
| 146 |
if (tem->asCreateSpec()) |
| 147 |
specs_.push_back(tem->asCreateSpec()); |
| 148 |
else if (tem->asPair()) |
| 149 |
// must be a list of create-specs then |
| 150 |
for (;;) { |
| 151 |
PairObj *pair = tem->asPair(); |
| 152 |
if (pair) { |
| 153 |
CreateSpecObj *cspec = pair->car()->asCreateSpec(); |
| 154 |
if (!cspec) { |
| 155 |
interp.setNextLocation(expr_->location()); |
| 156 |
interp.message(InterpreterMessages::notACspecList, |
| 157 |
ELObjMessageArg(tem, interp)); |
| 158 |
} |
| 159 |
else |
| 160 |
specs_.push_back(cspec); |
| 161 |
tem = pair->cdr(); |
| 162 |
} |
| 163 |
else if (tem->isNil()) |
| 164 |
break; |
| 165 |
else { |
| 166 |
interp.setNextLocation(expr_->location()); |
| 167 |
interp.message(InterpreterMessages::notACspecList, |
| 168 |
ELObjMessageArg(tem, interp)); |
| 169 |
return; |
| 170 |
} |
| 171 |
} |
| 172 |
else { |
| 173 |
interp.setNextLocation(expr_->location()); |
| 174 |
interp.message(InterpreterMessages::notACspecOrCspecList, |
| 175 |
ELObjMessageArg(tem, interp)); |
| 176 |
return; |
| 177 |
} |
| 178 |
} |
| 179 |
else { |
| 180 |
insn_ = expr_->compile(interp, Environment(), 0, InsnPtr()); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
void TransformationMode::addAssociation(Owner<Expression> &qexpr, |
| 185 |
Owner<Expression> &texpr, |
| 186 |
Owner<Expression> &pexpr, |
| 187 |
const Location &loc, |
| 188 |
Interpreter &interp) |
| 189 |
{ |
| 190 |
Ptr<Action> action = new Action(interp.currentPartIndex(), texpr, loc); |
| 191 |
Association assoc(qexpr, pexpr, action); |
| 192 |
associations_.resize(associations_.size() + 1); |
| 193 |
associations_.back().swap(assoc); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
#ifdef DSSSL_NAMESPACE |
| 198 |
} |
| 199 |
#endif |