From 02a55f7183f1497284accb3b89abab4a103d6e68 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 23 Jun 2015 12:05:18 -0700 Subject: 1629 - new helper for constructing containers --- 030container.cc | 27 +++++++++++++++++++++++++++ 034exclusive_container.cc | 17 +++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/030container.cc b/030container.cc index cff5dcf1..551a76a4 100644 --- a/030container.cc +++ b/030container.cc @@ -371,6 +371,33 @@ void check_container_field_types() { } } +//:: Construct types out of their constituent fields. Doesn't currently do +//:: type-checking but *does* match sizes. +:(before "End Primitive Recipe Declarations") +MERGE, +:(before "End Primitive Recipe Numbers") +Recipe_number["merge"] = MERGE; +:(before "End Primitive Recipe Implementations") +case MERGE: { + products.resize(1); + for (long long int i = 0; i < SIZE(ingredients); ++i) + for (long long int j = 0; j < SIZE(ingredients.at(i)); ++j) + products.at(0).push_back(ingredients.at(i).at(j)); + break; +} + +:(scenario merge) +container foo [ + x:number + y:number +] + +recipe main [ + 1:foo <- merge 3:literal, 4:literal +] ++mem: storing 3 in location 1 ++mem: storing 4 in location 2 + //:: helpers :(code) diff --git a/034exclusive_container.cc b/034exclusive_container.cc index 7455cf30..08aaf3f7 100644 --- a/034exclusive_container.cc +++ b/034exclusive_container.cc @@ -126,3 +126,20 @@ exclusive-container foo [ else if (command == "exclusive-container") { insert_container(command, exclusive_container, in); } + +//:: To construct exclusive containers out of variant types, use 'merge'. +:(scenario lift_to_exclusive_container) +exclusive-container foo [ + x:number + y:number +] + +recipe main [ + 1:number <- copy 34:literal + 2:foo <- merge 0:literal/x, 1:number + 4:foo <- merge 1:literal/x, 1:number +] ++mem: storing 0 in location 2 ++mem: storing 34 in location 3 ++mem: storing 1 in location 4 ++mem: storing 34 in location 5 -- cgit 1.4.1-2-gfad0