about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--client.c12
-rw-r--r--draw.c175
-rw-r--r--dwm.h11
-rw-r--r--event.c8
-rw-r--r--main.c165
6 files changed, 178 insertions, 195 deletions
diff --git a/Makefile b/Makefile
index a9f129d..221efa4 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@
 
 include config.mk
 
-SRC = client.c draw.c event.c main.c screen.c util.c
+SRC = client.c event.c main.c screen.c util.c
 OBJ = ${SRC:.c=.o}
 
 all: options dwm
diff --git a/client.c b/client.c
index eaf4a10..0251445 100644
--- a/client.c
+++ b/client.c
@@ -200,16 +200,6 @@ focusprev(Arg *arg) {
 	}
 }
 
-Client *
-getclient(Window w) {
-	Client *c;
-
-	for(c = clients; c; c = c->next)
-		if(c->win == w)
-			return c;
-	return NULL;
-}
-
 void
 killclient(Arg *arg) {
 	if(!sel)
@@ -259,7 +249,7 @@ manage(Window w, XWindowAttributes *wa) {
 	XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
 	configure(c); /* propagates border_width, if size doesn't change */
 	updatetitle(c);
-	t = getclient(trans);
+	for(t = clients; t && t->win != c->win; t = t->next);
 	settags(c, t);
 	if(!c->isfloat)
 		c->isfloat = (t != 0) || c->isfixed;
diff --git a/draw.c b/draw.c
deleted file mode 100644
index 0e02d6c..0000000
--- a/draw.c
+++ /dev/null
@@ -1,175 +0,0 @@
-/* (C)opyright MMIV-MMVII Anselm R. Garbe <garbeam at gmail dot com>
- * See LICENSE file for license details.
- */
-#include "dwm.h"
-#include <stdio.h>
-#include <string.h>
-
-/* static */
-
-static Bool
-isoccupied(unsigned int t)
-{
-	Client *c;
-	for(c = clients; c; c = c->next)
-		if(c->tags[t])
-			return True;
-	return False;
-}
-
-static unsigned int
-textnw(const char *text, unsigned int len) {
-	XRectangle r;
-
-	if(dc.font.set) {
-		XmbTextExtents(dc.font.set, text, len, NULL, &r);
-		return r.width;
-	}
-	return XTextWidth(dc.font.xfont, text, len);
-}
-
-static void
-drawtext(const char *text, unsigned long col[ColLast], Bool filledsquare, Bool emptysquare) {
-	int x, y, w, h;
-	static char buf[256];
-	unsigned int len, olen;
-	XGCValues gcv;
-	XRectangle r = { dc.x, dc.y, dc.w, dc.h };
-
-	XSetForeground(dpy, dc.gc, col[ColBG]);
-	XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
-	if(!text)
-		return;
-	w = 0;
-	olen = len = strlen(text);
-	if(len >= sizeof buf)
-		len = sizeof buf - 1;
-	memcpy(buf, text, len);
-	buf[len] = 0;
-	h = dc.font.ascent + dc.font.descent;
-	y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
-	x = dc.x + (h / 2);
-	/* shorten text if necessary */
-	while(len && (w = textnw(buf, len)) > dc.w - h)
-		buf[--len] = 0;
-	if(len < olen) {
-		if(len > 1)
-			buf[len - 1] = '.';
-		if(len > 2)
-			buf[len - 2] = '.';
-		if(len > 3)
-			buf[len - 3] = '.';
-	}
-	if(w > dc.w)
-		return; /* too long */
-	gcv.foreground = col[ColFG];
-	if(dc.font.set) {
-		XChangeGC(dpy, dc.gc, GCForeground, &gcv);
-		XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
-	}
-	else {
-		gcv.font = dc.font.xfont->fid;
-		XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
-		XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
-	}
-	x = (h + 2) / 4;
-	r.x = dc.x + 1;
-	r.y = dc.y + 1;
-	if(filledsquare) {
-		r.width = r.height = x + 1;
-		XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
-	}
-	else if(emptysquare) {
-		r.width = r.height = x;
-		XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
-	}
-}
-
-/* extern */
-
-void
-drawstatus(void) {
-	int i, x;
-
-	dc.x = dc.y = 0;
-	for(i = 0; i < ntags; i++) {
-		dc.w = textw(tags[i]);
-		if(seltag[i])
-			drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
-		else
-			drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
-		dc.x += dc.w;
-	}
-	dc.w = bmw;
-	drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.norm, False, False);
-	x = dc.x + dc.w;
-	dc.w = textw(stext);
-	dc.x = sw - dc.w;
-	if(dc.x < x) {
-		dc.x = x;
-		dc.w = sw - x;
-	}
-	drawtext(stext, dc.norm, False, False);
-	if((dc.w = dc.x - x) > bh) {
-		dc.x = x;
-		drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
-	}
-	XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
-	XSync(dpy, False);
-}
-
-unsigned long
-getcolor(const char *colstr) {
-	Colormap cmap = DefaultColormap(dpy, screen);
-	XColor color;
-
-	if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
-		eprint("error, cannot allocate color '%s'\n", colstr);
-	return color.pixel;
-}
-
-void
-setfont(const char *fontstr) {
-	char *def, **missing;
-	int i, n;
-
-	missing = NULL;
-	if(dc.font.set)
-		XFreeFontSet(dpy, dc.font.set);
-	dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
-	if(missing) {
-		while(n--)
-			fprintf(stderr, "missing fontset: %s\n", missing[n]);
-		XFreeStringList(missing);
-	}
-	if(dc.font.set) {
-		XFontSetExtents *font_extents;
-		XFontStruct **xfonts;
-		char **font_names;
-		dc.font.ascent = dc.font.descent = 0;
-		font_extents = XExtentsOfFontSet(dc.font.set);
-		n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
-		for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
-			if(dc.font.ascent < (*xfonts)->ascent)
-				dc.font.ascent = (*xfonts)->ascent;
-			if(dc.font.descent < (*xfonts)->descent)
-				dc.font.descent = (*xfonts)->descent;
-			xfonts++;
-		}
-	}
-	else {
-		if(dc.font.xfont)
-			XFreeFont(dpy, dc.font.xfont);
-		dc.font.xfont = NULL;
-		if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
-			eprint("error, cannot load font: '%s'\n", fontstr);
-		dc.font.ascent = dc.font.xfont->ascent;
-		dc.font.descent = dc.font.xfont->descent;
-	}
-	dc.font.height = dc.font.ascent + dc.font.descent;
-}
-
-unsigned int
-textw(const char *text) {
-	return textnw(text, strlen(text)) + dc.font.height;
-}
diff --git a/dwm.h b/dwm.h
index 57fab91..2ec0bf7 100644
--- a/dwm.h
+++ b/dwm.h
@@ -103,28 +103,23 @@ extern void configure(Client *c);		/* send synthetic configure event */
 extern void focus(Client *c);			/* focus c, c may be NULL */
 extern void focusnext(Arg *arg);		/* focuses next visible client, arg is ignored  */
 extern void focusprev(Arg *arg);		/* focuses previous visible client, arg is ignored */
-extern Client *getclient(Window w);		/* return client of w */
 extern void killclient(Arg *arg);		/* kill c nicely */
 extern void manage(Window w, XWindowAttributes *wa);	/* manage new client */
 Client *nexttiled(Client *c);			/* returns tiled successor of c */
 extern void resize(Client *c, int x, int y,
-		int w, int h, Bool sizehints);	/* resize c*/
+		int w, int h, Bool sizehints);	/* resize with given coordinates c*/
 extern void updatesizehints(Client *c);		/* update the size hint variables of c */
 extern void updatetitle(Client *c);		/* update the name of c */
 extern void unmanage(Client *c);		/* destroy c */
 extern void zoom(Arg *arg);			/* zooms the focused client to master area, arg is ignored */
 
-/* draw.c */
-extern void drawstatus(void);			/* draw the bar */
-extern unsigned long getcolor(const char *colstr);	/* return color of colstr */
-extern void setfont(const char *fontstr);	/* set the font for DC */
-extern unsigned int textw(const char *text);	/* return the width of text in px*/
-
 /* event.c */
 extern void grabkeys(void);			/* grab all keys defined in config.h */
 extern void procevent(void);			/* process pending X events */
 
 /* main.c */
+extern void drawstatus(void);			/* draw the bar */
+extern unsigned int textw(const char *text);	/* return the width of text in px*/
 extern void quit(Arg *arg);			/* quit dwm nicely */
 extern void sendevent(Window w, Atom a, long value);	/* send synthetic event to w */
 extern int xerror(Display *dsply, XErrorEvent *ee);	/* dwm's X error handler */
diff --git a/event.c b/event.c
index db3ce46..dfe2552 100644
--- a/event.c
+++ b/event.c
@@ -20,6 +20,14 @@ KEYS
 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
 #define MOUSEMASK		(BUTTONMASK | PointerMotionMask)
 
+static Client *
+getclient(Window w) {
+	Client *c;
+
+	for(c = clients; c && c->win != w; c = c->next);
+	return c;
+}
+
 static void
 movemouse(Client *c) {
 	int x1, y1, ocx, ocy, di, nx, ny;
diff --git a/main.c b/main.c
index 20450db..77ce0df 100644
--- a/main.c
+++ b/main.c
@@ -61,6 +61,94 @@ cleanup(void) {
 	free(seltag);
 }
 
+static unsigned int
+textnw(const char *text, unsigned int len) {
+	XRectangle r;
+
+	if(dc.font.set) {
+		XmbTextExtents(dc.font.set, text, len, NULL, &r);
+		return r.width;
+	}
+	return XTextWidth(dc.font.xfont, text, len);
+}
+
+static void
+drawtext(const char *text, unsigned long col[ColLast], Bool filledsquare, Bool emptysquare) {
+	int x, y, w, h;
+	static char buf[256];
+	unsigned int len, olen;
+	XGCValues gcv;
+	XRectangle r = { dc.x, dc.y, dc.w, dc.h };
+
+	XSetForeground(dpy, dc.gc, col[ColBG]);
+	XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
+	if(!text)
+		return;
+	w = 0;
+	olen = len = strlen(text);
+	if(len >= sizeof buf)
+		len = sizeof buf - 1;
+	memcpy(buf, text, len);
+	buf[len] = 0;
+	h = dc.font.ascent + dc.font.descent;
+	y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
+	x = dc.x + (h / 2);
+	/* shorten text if necessary */
+	while(len && (w = textnw(buf, len)) > dc.w - h)
+		buf[--len] = 0;
+	if(len < olen) {
+		if(len > 1)
+			buf[len - 1] = '.';
+		if(len > 2)
+			buf[len - 2] = '.';
+		if(len > 3)
+			buf[len - 3] = '.';
+	}
+	if(w > dc.w)
+		return; /* too long */
+	gcv.foreground = col[ColFG];
+	if(dc.font.set) {
+		XChangeGC(dpy, dc.gc, GCForeground, &gcv);
+		XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
+	}
+	else {
+		gcv.font = dc.font.xfont->fid;
+		XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
+		XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
+	}
+	x = (h + 2) / 4;
+	r.x = dc.x + 1;
+	r.y = dc.y + 1;
+	if(filledsquare) {
+		r.width = r.height = x + 1;
+		XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
+	}
+	else if(emptysquare) {
+		r.width = r.height = x;
+		XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
+	}
+}
+
+static unsigned long
+getcolor(const char *colstr) {
+	Colormap cmap = DefaultColormap(dpy, screen);
+	XColor color;
+
+	if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
+		eprint("error, cannot allocate color '%s'\n", colstr);
+	return color.pixel;
+}
+
+static Bool
+isoccupied(unsigned int t) {
+	Client *c;
+
+	for(c = clients; c; c = c->next)
+		if(c->tags[t])
+			return True;
+	return False;
+}
+
 static void
 scan(void) {
 	unsigned int i, num;
@@ -82,6 +170,47 @@ scan(void) {
 }
 
 static void
+setfont(const char *fontstr) {
+	char *def, **missing;
+	int i, n;
+
+	missing = NULL;
+	if(dc.font.set)
+		XFreeFontSet(dpy, dc.font.set);
+	dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
+	if(missing) {
+		while(n--)
+			fprintf(stderr, "missing fontset: %s\n", missing[n]);
+		XFreeStringList(missing);
+	}
+	if(dc.font.set) {
+		XFontSetExtents *font_extents;
+		XFontStruct **xfonts;
+		char **font_names;
+		dc.font.ascent = dc.font.descent = 0;
+		font_extents = XExtentsOfFontSet(dc.font.set);
+		n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
+		for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
+			if(dc.font.ascent < (*xfonts)->ascent)
+				dc.font.ascent = (*xfonts)->ascent;
+			if(dc.font.descent < (*xfonts)->descent)
+				dc.font.descent = (*xfonts)->descent;
+			xfonts++;
+		}
+	}
+	else {
+		if(dc.font.xfont)
+			XFreeFont(dpy, dc.font.xfont);
+		dc.font.xfont = NULL;
+		if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
+			eprint("error, cannot load font: '%s'\n", fontstr);
+		dc.font.ascent = dc.font.xfont->ascent;
+		dc.font.descent = dc.font.xfont->descent;
+	}
+	dc.font.height = dc.font.ascent + dc.font.descent;
+}
+
+static void
 setup(void) {
 	int i, j;
 	unsigned int mask;
@@ -171,6 +300,37 @@ xerrorstart(Display *dsply, XErrorEvent *ee) {
 /* extern */
 
 void
+drawstatus(void) {
+	int i, x;
+
+	dc.x = dc.y = 0;
+	for(i = 0; i < ntags; i++) {
+		dc.w = textw(tags[i]);
+		if(seltag[i])
+			drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
+		else
+			drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
+		dc.x += dc.w;
+	}
+	dc.w = bmw;
+	drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.norm, False, False);
+	x = dc.x + dc.w;
+	dc.w = textw(stext);
+	dc.x = sw - dc.w;
+	if(dc.x < x) {
+		dc.x = x;
+		dc.w = sw - x;
+	}
+	drawtext(stext, dc.norm, False, False);
+	if((dc.w = dc.x - x) > bh) {
+		dc.x = x;
+		drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
+	}
+	XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
+	XSync(dpy, False);
+}
+
+void
 sendevent(Window w, Atom a, long value) {
 	XEvent e;
 
@@ -184,6 +344,11 @@ sendevent(Window w, Atom a, long value) {
 	XSync(dpy, False);
 }
 
+unsigned int
+textw(const char *text) {
+	return textnw(text, strlen(text)) + dc.font.height;
+}
+
 void
 quit(Arg *arg) {
 	readin = running = False;
ision' href='/akkartik/mu/blame/048typecheck.cc?h=main&id=3b0aa1ac5196ce077fae897d78ca5226005065e5'>^
23d3a022 ^

112613fe ^
efb7c8d6 ^
1ead3562 ^
23d3a022 ^

112613fe ^
23d3a022 ^

112613fe ^
efb7c8d6 ^
1ead3562 ^
23d3a022 ^
112613fe ^

23d3a022 ^

c91caafd ^
ada5eb55 ^







5f98a10c ^

1ead3562 ^
c91caafd ^
192d59d3 ^
c91caafd ^
9dcbec39 ^
d082b176 ^
ada5eb55 ^






5f98a10c ^

1ead3562 ^
760f683f ^
d082b176 ^

760f683f ^
cf363343 ^


1ead3562 ^
a0331a9b ^
cf363343 ^
9dcbec39 ^
a70ce311 ^


1ead3562 ^
a70ce311 ^
1ead3562 ^
a70ce311 ^
01ce563d ^
dd660682 ^
192d59d3 ^
a70ce311 ^


07c594eb ^







2caaa7f1 ^















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

                                                                              




                                                                           
 

                                                               
          
                 
                  
 
                                          
 

                                                                              
                                                               
 












                                                                                                          
       
                                                         
                                  
                                                                                     
                                                  
                                           



                                                         


   








                                                                                                
                                   
                     



                                    

                                                       
                                         
                                                                                   







                                                                                                                       

 
                                                                                             
                            
                                                                   




                                                                                                        
                                                 
                                                                                     
                          
   
                                                                 
                                                                                            
                                                                                                          

           
                       
                         
                                                                                                                     

             
                                
                                                                                                                                                                                                                                                                   


             
 
 







                                                                              
                                            
          
                  
                   
 

                                     
 
                                                       
          

                  
 

                              
 
                                                                      
          
                  

               

                              
 







                                                 

                                                             
          
             
                 
 
                                                   
 






                                                       

                                      
          
                                    

               
                                                                           


                                         
          
                            
 
                                                                                                                                                                                      


                                                                           
                  
 
          
             
                                                             
                     
                          


                      







                                            















                                                        
//: Some simple sanity checks for types, and also attempts to guess them where
//: they aren't provided.
//:
//: You still have to provide the full type the first time you mention a
//: variable in a recipe. You have to explicitly name :offset and :variant
//: every single time. You can't use the same name with multiple types in a
//: single recipe.

:(scenario transform_fails_on_reusing_name_with_different_type)
% Hide_errors = true;
def main [
  x:num <- copy 1
  x:bool <- copy 1
]
+error: main: 'x' used with multiple types

//: we need surrounding-space info for type-checking variables in other spaces
:(after "Transform.push_back(collect_surrounding_spaces)")
Transform.push_back(check_or_set_types_by_name);  // idempotent

// Keep the name->type mapping for all recipes around for the entire
// transformation phase.
:(before "End Globals")
map<recipe_ordinal, set<reagent, name_lt> > Types_by_space;  // internal to transform; no need to snapshot
:(before "End Reset")
Types_by_space.clear();
:(before "End transform_all")
Types_by_space.clear();
:(before "End Types")
struct name_lt {
  bool operator()(const reagent& a, const reagent& b) const { return a.name < b.name; }
};

:(code)
void check_or_set_types_by_name(const recipe_ordinal r) {
  recipe& caller = get(Recipe, r);
  trace(9991, "transform") << "--- deduce types for recipe " << caller.name << end();
  for (int i = 0;  i < SIZE(caller.steps);  ++i) {
    instruction& inst = caller.steps.at(i);
    for (int in = 0;  in < SIZE(inst.ingredients);  ++in)
      check_or_set_type(inst.ingredients.at(in), caller);
    for (int out = 0;  out < SIZE(inst.products);  ++out)
      check_or_set_type(inst.products.at(out), caller);
  }
}

void check_or_set_type(reagent& curr, const recipe& caller) {
  if (is_literal(curr)) return;
  if (is_integer(curr.name)) return;  // no type-checking for raw locations
  set<reagent, name_lt>& known_types = Types_by_space[owning_recipe(curr, caller.ordinal)];
  deduce_missing_type(known_types, curr, caller);
  check_type(known_types, curr, caller);
}

void deduce_missing_type(set<reagent, name_lt>& known_types, reagent& x, const recipe& caller) {
  // Deduce Missing Type(x, caller)
  if (x.type) return;
  if (is_jump_target(x.name)) {
    x.type = new type_tree("label");
    return;
  }
  if (known_types.find(x) == known_types.end()) return;
  const reagent& exemplar = *known_types.find(x);
  x.type = new type_tree(*exemplar.type);
  trace(9992, "transform") << x.name << " <= " << names_to_string(x.type) << end();
  // spaces are special; their type includes their /names property
  if (is_mu_space(x) && !has_property(x, "names")) {
    if (!has_property(exemplar, "names")) {
      raise << maybe(caller.name) << "missing /names property for space variable '" << exemplar.name << "'\n" << end();
      return;
    }
    x.properties.push_back(pair<string, string_tree*>("names", new string_tree(*property(exemplar, "names"))));
  }
}

void check_type(set<reagent, name_lt>& known_types, const reagent& x, const recipe& caller) {
  if (is_literal(x)) return;
  if (!x.type) return;  // might get filled in by other logic later
  if (is_jump_target(x.name)) {
    if (!x.type->atom || x.type->name != "label")
      raise << maybe(caller.name) << "non-label '" << x.name << "' must begin with a letter\n" << end();
    return;
  }
  if (known_types.find(x) == known_types.end()) {
    trace(9992, "transform") << x.name << " => " << names_to_string(x.type) << end();
    known_types.insert(x);
  }
  if (!types_strictly_match(known_types.find(x)->type, x.type)) {
    raise << maybe(caller.name) << "'" << x.name << "' used with multiple types\n" << end();
    raise << "  " << to_string(known_types.find(x)->type) << " vs " << to_string(x.type) << '\n' << end();
    return;
  }
  if (is_mu_array(x)) {
    if (!x.type->right) {
      raise << maybe(caller.name) << "'" << x.name << ": can't be just an array. What is it an array of?\n" << end();
      return;
    }
    if (!x.type->right->right) {
      raise << caller.name << " can't determine the size of array variable '" << x.name << "'. Either allocate it separately and make the type of '" << x.name << "' an address, or specify the length of the array in the type of '" << x.name << "'.\n" << end();
      return;
    }
  }
}

recipe_ordinal owning_recipe(const reagent& x, recipe_ordinal r) {
  for (int s = space_index(x); s > 0; --s) {
    if (!contains_key(Surrounding_space, r)) break;  // error raised elsewhere
    r = Surrounding_space[r];
  }
  return r;
}

:(scenario transform_fills_in_missing_types)
def main [
  x:num <- copy 11
  y:num <- add x, 1
]
# x is in location 2, y in location 3
+mem: storing 12 in location 3

:(scenario transform_fills_in_missing_types_in_product)
def main [
  x:num <- copy 11
  x <- copy 12
]
# x is in location 2
+mem: storing 12 in location 2

:(scenario transform_fills_in_missing_types_in_product_and_ingredient)
def main [
  x:num <- copy 11
  x <- add x, 1
]
# x is in location 2
+mem: storing 12 in location 2

:(scenario transform_fills_in_missing_label_type)
def main [
  jump +target
  1:num <- copy 0
  +target
]
-mem: storing 0 in location 1

:(scenario transform_fails_on_missing_types_in_first_mention)
% Hide_errors = true;
def main [
  x <- copy 1
  x:num <- copy 2
]
+error: main: missing type for 'x' in 'x <- copy 1'

:(scenario transform_fails_on_wrong_type_for_label)
% Hide_errors = true;
def main [
  +foo:num <- copy 34
]
+error: main: non-label '+foo' must begin with a letter

:(scenario typo_in_address_type_fails)
% Hide_errors = true;
def main [
  y:&:charcter <- new character:type
  *y <- copy 67
]
+error: main: unknown type charcter in 'y:&:charcter <- new character:type'

:(scenario array_type_without_size_fails)
% Hide_errors = true;
def main [
  x:@:num <- merge 2, 12, 13
]
+error: main can't determine the size of array variable 'x'. Either allocate it separately and make the type of 'x' an address, or specify the length of the array in the type of 'x'.

:(scenarios transform)
:(scenario transform_checks_types_of_identical_reagents_in_multiple_spaces)
def foo [  # dummy
]
def main [
  local-scope
  0:space/names:foo <- copy null  # specify surrounding space
  x:bool <- copy true
  x:num/space:1 <- copy 34
  x/space:1 <- copy 35
]
$error: 0

:(scenario transform_handles_empty_reagents)
% Hide_errors = true;
def main [
  add *
]
+error: illegal name '*'
# no crash

:(scenario transform_checks_types_in_surrounding_spaces)
% Hide_errors = true;
# 'x' is a bool in foo's space
def foo [
  local-scope
  x:bool <- copy false
  return default-space/names:foo
]
# try to read 'x' as a num in foo's space
def main [
  local-scope
  0:space/names:foo <- foo
  x:num/space:1 <- copy 34
]
error: foo: 'x' used with multiple types