summaryrefslogtreecommitdiff
path: root/graphics/asymptote/castop.h
blob: cfabf94eedb50a23f15e5a12bbcfb2fec6afdfed (plain)
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
/*****
 * castop.h
 * Tom Prince 2005/3/18
 *
 * Defines some runtime functions used by the stack machine.
 *
 *****/

#ifndef CASTOP_H
#define CASTOP_H

#include <cfloat>

#include "common.h"
#include "stack.h"
#include "fileio.h"
#include "lexical.h"
#include "mathop.h"
#include "array.h"

namespace run {

using vm::read;
using vm::pop;

template<class T, class S>
void cast(vm::stack *s)
{
  s->push((S) pop<T>(s));
}

void castDoubleInt(vm::stack *s)
{
  double x=pop<double>(s);
  s->push(Intcast(x));
}

template<class T>
void stringCast(vm::stack *s)
{
  ostringstream buf;
  buf.precision(DBL_DIG);
  buf << pop<T>(s);
  s->push(buf.str());
}

template<class T>
void castString(vm::stack *s)
{
  string *S=pop<string*>(s);
  try {
    s->push(lexical::cast<T>(*S));
  } catch (lexical::bad_cast&) {
    s->push(vm::Default);
  }
}

void initialized(vm::stack *s)
{
  s->push(!vm::isdefault(pop(s)));
}

template<class T, class S>
void arrayToArray(vm::stack *s)
{
  vm::array *a=pop<vm::array*>(s);
  size_t size=checkArray(a);
  vm::array *c=new vm::array(size);
  for(size_t i=0; i < size; i++)
    (*c)[i]=(S) read<T>(a,i);
  s->push(c);
}

template<class T, class S>
void array2ToArray2(vm::stack *s)
{
  vm::array *a=pop<vm::array*>(s);
  size_t size=checkArray(a);
  vm::array *c=new vm::array(size);
  for(size_t i=0; i < size; ++i) {
    vm::array *ai=vm::read<vm::array*>(a,i);
    size_t aisize=checkArray(ai);
    vm::array *ci=new vm::array(aisize);
    (*c)[i]=ci;
    for(size_t j=0; j < aisize; ++j)
      (*ci)[j]=(S) read<T>(ai,j);
  }
  s->push(c);
}

template<class T>
void read(vm::stack *s)
{
  camp::file *f = pop<camp::file*>(s);
  T val=T();
  if(f->isOpen()) {
    f->read(val);
    if(f->LineMode()) f->nexteol();
    if(interact::interactive) f->purgeStandard(val);
  }
  s->push(val);
}

inline Int Limit(Int nx) {return nx == 0 ? Int_MAX : nx;}
inline void reportEof(camp::file *f, Int count)
{
  if(count > 0) {
    ostringstream buf;
    buf << "EOF after reading " << count
        << " values from file '" << f->filename() << "'.";
    vm::error(buf);
  }
}

template<class T>
void readArray(vm::stack *s, Int nx=-1, Int ny=-1, Int nz=-1)
{
  camp::file *f = pop<camp::file*>(s);
  vm::array *c=new vm::array(0);
  if(f->isOpen()) {
    if(nx != -1 && f->Nx() != -1) nx=f->Nx();
    if(nx == -2) {f->read(nx); f->Nx(-1); if(nx == 0) {s->push(c); return;}}
    if(ny != -1 && f->Ny() != -1) ny=f->Ny();
    if(ny == -2) {f->read(ny); f->Ny(-1); if(ny == 0) {s->push(c); return;}}
    if(nz != -1 && f->Nz() != -1) nz=f->Nz();
    if(nz == -2) {f->read(nz); f->Nz(-1); if(nz == 0) {s->push(c); return;}}
    T v;
    if(nx >= 0) {
      for(Int i=0; i < Limit(nx); i++) {
        if(ny >= 0) {
          vm::array *ci=new vm::array(0);
          for(Int j=0; j < Limit(ny); j++) {
            if(nz >= 0) {
              vm::array *cij=new vm::array(0);
              bool break2=false;
              for(Int k=0; k < Limit(nz); k++) {
                f->read(v);
                if(f->error()) {
                  if(nx && ny && nz) reportEof(f,(i*ny+j)*nz+k);
                  s->push(c);
                  return;
                }
                if(k == 0) {
                  if(j == 0) c->push(ci);
                  ci->push(cij);
                }
                cij->push(v);
                if(f->LineMode() && f->nexteol()) {
                  if(f->nexteol()) break2=true;
                  break;
                }
              }
              if(break2) break;
            } else {
              f->read(v);
              if(f->error()) {
                if(nx && ny) reportEof(f,i*ny+j);
                s->push(c);
                return;
              }
              if(j == 0) c->push(ci);
              ci->push(v);
              if(f->LineMode() && f->nexteol()) break;
            }
          }
        } else {
          f->read(v);
          if(f->error()) {
            if(nx) reportEof(f,i);
            s->push(c);
            return;
          }
          c->push(v);
          if(f->LineMode() && f->nexteol()) break;
        }
      }
    } else {
      for(;;) {
        f->read(v);
        if(f->error()) break;
        c->push(v);
        if(f->LineMode() && f->nexteol()) break;
      }
    }
    if(interact::interactive) f->purgeStandard(v);
  }
  s->push(c);
}

template<class T>
void readArray1(vm::stack *s)
{
  readArray<T>(s,0);
}

template<class T>
void readArray2(vm::stack *s)
{
  readArray<T>(s,0,0);
}

template<class T>
void readArray3(vm::stack *s)
{
  readArray<T>(s,0,0,0);
}

} // namespace run

#endif // CASTOP_H