1 //: Writing to a literal (not computed) address of 0 in a recipe chains two
  2 //: spaces together. When a variable has a property of /space:1, it looks up
  3 //: the variable in the chained/surrounding space. /space:2 looks up the
  4 //: surrounding space of the surrounding space, etc.
  5 
  6 :(scenario closure)
  7 def main [
  8   default-space:space <- new location:type, 30
  9   1:space/names:new-counter <- new-counter
 10   2:num/raw <- increment-counter 1:space/names:new-counter
 11   3:num/raw <- increment-counter 1:space/names:new-counter
 12 ]
 13 def new-counter [
 14   default-space:space <- new location:type, 30
 15   x:num <- copy 23
 16   y:num <- copy 3  # variable that will be incremented
 17   return default-space:space
 18 ]
 19 def increment-counter [
 20   default-space:space <- new location:type, 30
 21   0:space/names:new-counter <- next-ingredient  # outer space must be created by 'new-counter' above
 22   y:num/space:1 <- add y:num/space:1, 1  # increment
 23   y:num <- copy 234  # dummy
 24   return y:num/space:1
 25 ]
 26 +name: lexically surrounding space for recipe increment-counter comes from new-counter
 27 +mem: storing 5 in location 3
 28 
 29 //: To make this work, compute the recipe that provides names for the
 30 //: surrounding space of each recipe.
 31 
 32 :(before "End Globals")
 33 map<recipe_ordinal, recipe_ordinal> Surrounding_space;
 34 
 35 :(before "Transform.push_back(transform_names)")
 36 Transform.push_back(collect_surrounding_spaces);  // idempotent
 37 
 38 :(code)
 39 void collect_surrounding_spaces(const recipe_ordinal r) {
 40   trace(9991, "transform") << "--- collect surrounding spaces for recipe " << get(Recipe, r).name << end();
 41   for (int i = 0;  i < SIZE(get(Recipe, r).steps);  ++i) {
 42   ¦ const instruction& inst = get(Recipe, r).steps.at(i);
 43   ¦ if (inst.is_label) continue;
 44   ¦ for (int j = 0;  j < SIZE(inst.products);  ++j) {
 45   ¦ ¦ if (is_literal(inst.products.at(j))) continue;
 46   ¦ ¦ if (inst.products.at(j).name != "0") continue;
 47   ¦ ¦ if (!is_space(inst.products.at(j))) {
 48   ¦ ¦ ¦ raise << "slot 0 should always have type address:array:location, but is '" << to_string(inst.products.at(j)) << "'\n" << end
#!/usr/bin/env python
# Copyright (C) 2009, 2010  Roman Zimbelmann <romanz@lavabit.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
Run all the tests inside this directory as a test suite.
Usage: ./all_tests.py [verbosity]
"""

import os.path
import sys
rangerpath = os.path.join(os.path.dirname(__file__), '..')
if sys.path[1] != rangerpath:
	sys.path[1:1] = [rangerpath]

import unittest

if __name__ == '__main__':
	verbosity = int(sys.argv[1]) if len(sys.argv) > 1 else 1
	tests     = (fname[:-3] for fname in os.listdir(sys.path[0]) \
	             if fname[:3] == 'tc_' and fname[-3:] == '.py')
	suite     = unittest.TestLoader().loadTestsFromNames(tests)
	result    = unittest.TextTestRunner(verbosity=verbosity).run(suite)
	if len(result.errors + result.failures) > 0:
		sys.exit(1)
> 81 int lookup_name(const reagent& x, const recipe_ordinal default_recipe) { 82 if (!has_property(x, "space")) { 83 ¦ if (Name[default_recipe].empty()) raise << "name not found: " << x.name << '\n' << end(); 84 ¦ return Name[default_recipe][x.name]; 85 } 86 string_tree* p = property(x, "space"); 87 if (!p || !p->atom) raise << "/space property should have exactly one (non-negative integer) value\n" << end(); 88 int n = to_integer(p->value); 89 assert(n >= 0); 90 recipe_ordinal surrounding_recipe = lookup_surrounding_recipe(default_recipe, n); 91 if (surrounding_recipe == -1) return -1; 92 set<recipe_ordinal> done; 93 vector<recipe_ordinal> path; 94 return lookup_name(x, surrounding_recipe, done, path); 95 } 96 97 // If the recipe we need to lookup this name in doesn't have names done yet, 98 // recursively call transform_names on it. 99 int lookup_name(const reagent& x, const recipe_ordinal r, set<recipe_ordinal>& done, vector<recipe_ordinal>& path) { 100 if (!Name[r].empty()) return Name[r][x.name]; 101 if (contains_key(done, r)) { 102 ¦ raise << "can't compute address of '" << to_string(x) << "' because\n" << end(); 103 ¦ for (int i = 1; i < SIZE(path); ++i) { 104 ¦ ¦ raise << path.at(i-1) << " requires computing names of " << path.at(i) << '\n' << end(); 105 ¦ } 106 ¦ raise << path.at(SIZE(path)-1) << " requires computing names of " << r << "..ad infinitum\n" << end(); 107 ¦ return -1; 108 } 109 done.insert(r); 110 path.push_back(r); 111 transform_names(r); // Not passing 'done' through. Might this somehow cause an infinite loop? 112 assert(!Name[r].empty()); 113 return Name[r][x.name]; 114 } 115 116 recipe_ordinal lookup_surrounding_recipe(const recipe_ordinal r, int n) { 117 if (n == 0) return r; 118 if (!contains_key(Surrounding_space, r)) { 119 ¦ raise << "don't know surrounding recipe of '" << get(Recipe, r).name << "'\n" << end(); 120 ¦ return -1; 121 } 122 assert(contains_key(Surrounding_space, r)); 123 return lookup_surrounding_recipe(get(Surrounding_space, r), n-1); 124 } 125 126 //: weaken use-before-set detection just a tad 127 :(replace{} "bool already_transformed(const reagent& r, const map<string, int>& names)") 128 bool already_transformed(const reagent& r, const map<string, int>& names) { 129 if (has_property(r, "space")) { 130 ¦ string_tree* p = property(r, "space"); 131 ¦ if (!p || !p->atom) { 132 ¦ ¦ raise << "/space property should have exactly one (non-negative integer) value in '" << r.original_string << "'\n" << end(); 133 ¦ ¦ return false; 134 ¦ } 135 ¦ if (p->value != "0") return true; 136 } 137 return contains_key(names, r.name); 138 } 139 140 :(scenario missing_surrounding_space) 141 % Hide_errors = true; 142 def f [ 143 local-scope 144 x:num/space:1 <- copy 34 145 ] 146 +error: don't know surrounding recipe of 'f' 147 +error: f: can't find a place to store 'x' 148 149 //: extra test for try_reclaim_locals() from previous layers 150 :(scenario local_scope_ignores_nonlocal_spaces) 151 def new-scope [ 152 new-default-space 153 x:&:num <- new number:type 154 *x:&:num <- copy 34 155 return default-space:space 156 ] 157 def use-scope [ 158 local-scope 159 outer:space <- next-ingredient 160 0:space/names:new-scope <- copy outer:space 161 return *x:&:num/space:1 162 ] 163 def main [ 164 1:space/raw <- new-scope 165 2:num/raw <- use-scope 1:space/raw 166 ] 167 +mem: storing 34 in location 2