File: TransformationMode.cxx

package info (click to toggle)
openjade 1.4devel1-20.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,636 kB
  • sloc: cpp: 90,082; sh: 10,847; ansic: 2,365; lisp: 894; perl: 604; makefile: 443; sed: 93
file content (199 lines) | stat: -rw-r--r-- 5,239 bytes parent folder | download | duplicates (9)
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
// Copyright (c) 1999 Matthias Clasen
// See the file copying.txt for copying permission.

#include "stylelib.h"
#include "TransformationMode.h"
#include "Expression.h"
#include "Interpreter.h"
#include "InterpreterMessages.h"
#include "ELObjMessageArg.h"
#include "VM.h"
#include <stdlib.h> // for qsort

#ifdef DSSSL_NAMESPACE
namespace DSSSL_NAMESPACE {
#endif


TransformationMode::TransformationMode()
{
}

void TransformationMode::Action::get(InsnPtr &insn, Vector<CreateSpecObj *> &specs) const
{
  insn = insn_;
  specs = specs_;
}

const Location &TransformationMode::Action::location() const
{
  return defLoc_;
}

TransformationMode::Action &TransformationMode::Association::action()
{
  return *action_;
}

long TransformationMode::Association::priority() const
{
  return priority_;
}

const TransformationMode::Action &TransformationMode::Association::action() const
{
  return *action_;
}

unsigned TransformationMode::Action::partIndex() const
{
  return partIndex_;
}

const Location &TransformationMode::Association::location() const
{
  return action_->location();
}

extern "C" {

static int associationCompare(const void *p1, const void *p2)
{
  return (*(const TransformationMode::Association *const *)p1)->priority()
       - (*(const TransformationMode::Association *const *)p2)->priority();
};

}

void TransformationMode::compile(Interpreter &interp, const NodePtr &node)
{
  for (size_t j = 0; j < associations_.size(); j++)
    associations_[j].compile(interp, node);
  qsort(&associations_[0], associations_.size(), 
        sizeof(associations_[0]), associationCompare);
}

TransformationMode::Action::Action(unsigned partIndex, Owner<Expression> &expr,
                                   const Location &loc)
: partIndex_(partIndex), defLoc_(loc)
{
  expr.swap(expr_);
}


TransformationMode::Association::Association()
: action_(0), priority_(0), nl_(0)
{
}

TransformationMode::Association::Association(Owner<Expression> &qexpr,
	               Owner<Expression> &pexpr,
                       const Ptr<TransformationMode::Action> &action)
: action_(action), priority_(0), nl_(0)
{
  qexpr.swap(qexpr_);
  pexpr.swap(pexpr_);
}

void TransformationMode::Association::swap(Association &a)
{
  action_.swap(a.action_);
  qexpr_.swap(a.qexpr_);
  pexpr_.swap(a.pexpr_);
}

void TransformationMode::Association::compile(Interpreter &interp, const NodePtr &node)
{
  action_->compile(interp);
  qexpr_->optimize(interp, Environment(), qexpr_);
  ELObj *tem = qexpr_->constantValue();
  if (!tem) {
    InsnPtr insn = qexpr_->compile(interp, Environment(), 0, InsnPtr());
    VM vm(interp);
    EvalContext::CurrentNodeSetter cns(node, 0, vm);
    tem = vm.eval(insn.pointer());
  }
  if (tem->asNodeList()) 
    nl_ = tem->asNodeList();
  else {
    interp.setNextLocation(qexpr_->location());
    interp.message(InterpreterMessages::queryNotANodeList, 
		   ELObjMessageArg(tem, interp));
    nl_ = interp.makeEmptyNodeList();
  }

  pexpr_->optimize(interp, Environment(), pexpr_);
  tem = pexpr_->constantValue();
  if (!tem) {
    InsnPtr insn = pexpr_->compile(interp, Environment(), 0, InsnPtr());
    VM vm(interp);
    EvalContext::CurrentNodeSetter cns(node, 0, vm);
    tem = vm.eval(insn.pointer());
  }
  if (!tem->exactIntegerValue(priority_)) { 
    interp.setNextLocation(pexpr_->location());
    interp.message(InterpreterMessages::priorityNotAnExactInteger,
		   ELObjMessageArg(tem, interp));
    priority_ = 0;
  }
}

void TransformationMode::Action::compile(Interpreter &interp)
{
  expr_->optimize(interp, Environment(), expr_);
  ELObj *tem = expr_->constantValue();
  if (tem) {
    if (tem->asCreateSpec()) 
      specs_.push_back(tem->asCreateSpec());
    else if (tem->asPair())  
      // must be a list of create-specs then
      for (;;) {
        PairObj *pair = tem->asPair();
        if (pair) {
          CreateSpecObj *cspec = pair->car()->asCreateSpec();
          if (!cspec) {
	    interp.setNextLocation(expr_->location());
	    interp.message(InterpreterMessages::notACspecList, 
			   ELObjMessageArg(tem, interp));
	  }
          else 
	    specs_.push_back(cspec);
          tem = pair->cdr();
        } 
        else if (tem->isNil())
          break;
        else {
	  interp.setNextLocation(expr_->location());
	  interp.message(InterpreterMessages::notACspecList,
			 ELObjMessageArg(tem, interp));
          return;
        }
      }
    else {
      interp.setNextLocation(expr_->location());
      interp.message(InterpreterMessages::notACspecOrCspecList,
		     ELObjMessageArg(tem, interp));
      return;
    }
  }
  else {
    insn_ = expr_->compile(interp, Environment(), 0, InsnPtr());
  }
}

void TransformationMode::addAssociation(Owner<Expression> &qexpr,
                                        Owner<Expression> &texpr,
                                        Owner<Expression> &pexpr,
                                        const Location &loc,
                                        Interpreter &interp)
{
  Ptr<Action> action = new Action(interp.currentPartIndex(), texpr, loc);
  Association assoc(qexpr, pexpr, action);
  associations_.resize(associations_.size() + 1);
  associations_.back().swap(assoc);
} 


#ifdef DSSSL_NAMESPACE
}
#endif