summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/castop.h
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2009-05-16 00:19:13 +0000
committerKarl Berry <karl@freefriends.org>2009-05-16 00:19:13 +0000
commitbab45528d65eaafe68a705dbb2a57075c7b7cbd8 (patch)
tree10b4ae2b5195c8dede153ab89359ec00f55f325f /Build/source/utils/asymptote/castop.h
parent8643d90372e9c31e0f461c15c596b60a545bd7d3 (diff)
asymptote 1.72 sources (not integrated into build yet)
git-svn-id: svn://tug.org/texlive/trunk@13110 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Build/source/utils/asymptote/castop.h')
-rw-r--r--Build/source/utils/asymptote/castop.h179
1 files changed, 179 insertions, 0 deletions
diff --git a/Build/source/utils/asymptote/castop.h b/Build/source/utils/asymptote/castop.h
new file mode 100644
index 00000000000..714dfdb0bae
--- /dev/null
+++ b/Build/source/utils/asymptote/castop.h
@@ -0,0 +1,179 @@
+/*****
+ * 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);
+ if(S->empty()) {
+ T x=0;
+ s->push(x);
+ } else {
+ try {
+ s->push(lexical::cast<T>(*S));
+ } catch (lexical::bad_cast&) {
+ ostringstream buf;
+ buf << "invalid cast from string \"" << *S << "\"";
+ vm::error(buf);
+ }
+ }
+}
+
+template<class T, class S>
+void arrayToArray(vm::stack *s)
+{
+ vm::array *a = pop<vm::array*>(s);
+ checkArray(a);
+ size_t size=(size_t) a->size();
+ 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>
+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)
+{
+ camp::file *f = pop<camp::file*>(s);
+ vm::array *c=new vm::array(0);
+ if(f->isOpen()) {
+ Int nx=f->Nx();
+ if(nx == -2) {f->read(nx); f->Nx(-1); if(nx == 0) {s->push(c); return;}}
+ Int ny=f->Ny();
+ if(ny == -2) {f->read(ny); f->Ny(-1); if(ny == 0) {s->push(c); return;}}
+ Int 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);
+}
+
+} // namespace run
+
+#endif // CASTOP_H
+