summaryrefslogtreecommitdiff
path: root/graphics/asymptote/varinit.cc
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /graphics/asymptote/varinit.cc
Initial commit
Diffstat (limited to 'graphics/asymptote/varinit.cc')
-rw-r--r--graphics/asymptote/varinit.cc80
1 files changed, 80 insertions, 0 deletions
diff --git a/graphics/asymptote/varinit.cc b/graphics/asymptote/varinit.cc
new file mode 100644
index 0000000000..e84c62bb7e
--- /dev/null
+++ b/graphics/asymptote/varinit.cc
@@ -0,0 +1,80 @@
+/*****
+ * varinit.cc
+ * Andy Hammerlindl 2005/07/01
+ *
+ * Variable initializer are syntax that finish code such as
+ * int var = ...
+ * As such, they are translated to yield a certain type, the type of the
+ * variable. Expressions are a special case that can be translated without an
+ * associated variable or its type.
+ *****/
+
+#include "varinit.h"
+#include "coenv.h"
+#include "runtime.h"
+#include "runarray.h"
+
+namespace absyntax {
+
+using namespace types;
+using namespace trans;
+
+void definit::prettyprint(ostream &out, Int indent)
+{
+ prettyname(out, "definit",indent);
+}
+
+void definit::transToType(coenv &e, types::ty *target)
+{
+ if (target->kind != ty_error) {
+ access *a=e.e.lookupInitializer(target);
+
+ if (a)
+ a->encode(CALL, getPos(), e.c);
+ else {
+ em.error(getPos());
+ em << "no default initializer for type '" << *target << "'";
+ }
+ }
+}
+
+void arrayinit::prettyprint(ostream &out, Int indent)
+{
+ prettyname(out, "arrayinit",indent);
+
+ for (mem::list<varinit *>::iterator p = inits.begin(); p != inits.end(); ++p)
+ (*p)->prettyprint(out, indent+2);
+ if (rest)
+ rest->prettyprint(out, indent+1);
+}
+
+void arrayinit::transMaker(coenv &e, Int size, bool rest) {
+ // Push the number of cells and call the array maker.
+ e.c.encode(inst::intpush, size);
+ e.c.encode(inst::builtin, rest ? run::newAppendedArray :
+ run::newInitializedArray);
+}
+
+void arrayinit::transToType(coenv &e, types::ty *target)
+{
+ types::ty *celltype;
+ if (target->kind != types::ty_array) {
+ em.error(getPos());
+ em << "array initializer used for non-array";
+ celltype = types::primError();
+ }
+ else {
+ celltype = ((types::array *)target)->celltype;
+ }
+
+ // Push the values on the stack.
+ for (mem::list<varinit *>::iterator p = inits.begin(); p != inits.end(); ++p)
+ (*p)->transToType(e, celltype);
+
+ if (rest)
+ rest->transToType(e, target);
+
+ transMaker(e, (Int)inits.size(), (bool)rest);
+}
+
+} // namespace absyntax