[ create a new paste ] login | about

Link: http://codepad.org/DUNWJfX9    [ raw code | fork ]

Plain Text, pasted on May 29:
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
# gen.nas -- namespace "gen"

# Generators and mostly utilities using namespace hacking &c
# Quickly grew overboard ;)

# Note: the fundamental assertion that _the_globals is *the* globals
# could potentially cause problems depending on the loading method
# (driver.nas's import would not work, but FlightGear's io.load_nasal
# would work; which is funny, given that I am using EXPORTS :D).

var EXPORTS = ["_the_globals", "_global_func",
               "public", "namespace", "global",
               "bind_to_caller", "bind_to_namespace",
               "bind_to_namespaces"];

var _level = 0;
while (closure(caller(0)[1], _level)) != nil) _level += 1;
var _the_globals = closure(caller(0)[1], _level-=1);
var _global_func = bind(func{}, _the_globals);
bind = (func{
	var _bind = bind;
	func(fn, namespace, enclosure=nil) {
		if (fn != _global_func)
			return _bind(fn, namespace, enclosure);
		#protect it from getting rebound by returning an equivalent but duplicate function:
		return _bind(_bind(func{}, _the_globals), namespace, enclosure);
	}
})();
var _defined = func(sym) {
	# We must first check the frame->locals hash/namespace
	# (since closure(fn, 0) returns the namespace/closure
	# above it, i.e. PTR(frame->func).func->namespace vs
	# frame->locals).
	if(contains(caller(1)[0], sym)) return 1;
	var fn = caller(1)[1]; var l = 0;
	while((var frame = closure(fn, l)) != nil) {
		if(contains(frame, sym)) return 1;
		l += 1;
	}
	return 0;
};
var _ldefined = func(sym) {
	return contains(caller(1)[0], sym);
};
var _fix_rest = func(sym) {
    var val = caller(1)[0][sym];
    if (typeof(val) == 'vector' and
        size(val) == 1 and
        typeof(val[0]) == 'vector')
        caller(1)[0][val] = val[0];
};

# Lexically bind the function to the caller
var bind_to_caller = func(fn, level=1) {
	if (level < 0) return;
	bind(fn, caller(level+=1)[0], caller(level)[1]);
};
# Bind the function to the namespace and then globals
var bind_to_namespace = func(fn, namespace) {
	if (typeof(namespace) == 'scalar')
		var namespace = _the_globals[namespace];
	bind(fn, namespace, _global_func));
};
# Bind the function to each namespace in turn (the
# first is the top-level one, after globals). Each
# item can be a scalar (name of the sub-namespace)
# or a hash (the namespace itself). If create is
# true, then any names that are not present in a
# namespace are created as a new hash; else this
# returns nil.
var bind_to_namespaces = func(fn, namespaces, create=1) {
	if (typeof(namespace) == 'scalar')
		var namespaces = split(".", namespaces);
	var namespace = _the_globals;
	var save = pop(namespaces);
	var _fn = _global_func;
	foreach (var i; namespaces) {
		if (typeof(i) == 'scalar') {
			if (!contains(namespace, i))
				if (create)
					namespace[i] = {};
				else return;
			var i = namespace[i];
		}
		var _fn = bind(func{}, var namespace = i, _fn);
	}
	if (typeof(save) == 'scalar') {
		if (!contains(namespace, save))
			if (create)
				namespace[save] = {};
			else return;
		var save = namespace[save];
	}
	bind(fn, save, _fn);
};

# For each symbol created by the function <fn> or
# for each symbol in <fn> (if it is either a hash
# or vector), add the name of the symbol to the
# caller's EXPORT vector. Returns a vector of the
# added symbols and adds the symbols to the caller's
# local namespace if possible (i.e. when <fn> is not
# a vector).
#
# The anonymous function argument is so that you can
# use exactly the same syntax, versus having to
# convert it to or write in hash-style syntax (after
# all, Nasal just splits off another codegen to handle
# func{}s...)
var public = func(fn) {
	var c = caller(1)[0];
	var names = []; var hash = {};
	if (typeof(fn) == 'func') {
		call(fn, nil, nil, hash);
		var names = keys(hash);
	} elsif (typeof(fn) == 'hash') {
		var names = keys(hash = fn);
	} elsif (typeof(fn) == 'vector') {
		var names = fn;
	} else die("invalid/unrecognized argument to public()");
	copy_onto(c, hash);
	if (!contains(c, "EXPORTS"))
		c["EXPORTS"] = [];
	return foreach (var sym; names) {
		append(c["EXPORTS"], sym);
	};
	# In case the behavior changes (they are equivalent):
	#foreach (var sym; names) {
	#	append(c["EXPORTS"], sym);
	#};
	#return names;
};

# Basically the same. FIXME: should we use bind() instead?
var global = func(fn) {
	var c = _the_globals;
	var names = []; var hash = {};
	if (typeof(fn) == 'func') {
		call(fn, nil, nil, hash);
		var names = keys(hash);
	} elsif (typeof(fn) == 'hash') {
		var names = keys(hash = fn);
	} else die("invalid/unrecognized argument to global()");
	copy_onto(c, hash);
	return names;
};

# Runs the function in the namespace, like public().
# Essentially says that the function "describes"
# that namespace (after it runs, of course).
# I could write down a dozen analogies for this...
# Usage:
#   gen.namespace("foo", func {
#       ... #your code here, just write normally 
#   });
# Which roughly translates into C++ as:
#   namespace foo
#   {
#       ... //your code here
#   }
var namespace = func(namespc, fn) {
	if (typeof(namespc) == 'scalar')
		var namespc = _the_globals[namespc];
	bind(fn, _the_globals);
	call(fn, nil, nil, namespc);
};


##
# Utilities
#
public(func {
	# Turns a scalar bit of code into a function and binds it
	# to the namespace inside the enclosure.
	var make_code = func(code, namespace, enclosure=nil) {
		if (typeof(code) == 'func') return bind(code, namespace, enclosure);
		if (typeof(code) != 'scalar') die("invalid argument to make_code()");
		code = compile(code);
		code = bind(code, namespace, enclosure);
		return code;
	};

	# Create a new hash from the symbools of the caller
	# if they are not listed in the ignore vector.
	var new_hash = func(ignore...) {
		_fix_rest("ignore");
		var c = caller(1)[0];
		var m = {};
		foreach (var sym; keys(c)) {
			if (econtains(ignore, sym)) continue;
			m[sym] == c[sym];
		}
		return m;
	};

	# Create a new object instance (similar to above,
	# but uses the 'me' symbol for the parents vector
	# and ignores the arg and me symbols)
	var new_obj = func(ignore...) {
		_fix_rest("ignore");
		var c = caller(1)[0];
		var m = { parents: [c.me] };
		foreach (var sym; keys(c)) {
			if ((sym == "me" or sym == "arg") !=
			    econtains(ignore, sym)) continue;
			m[sym] == c[sym];
		}
		return m;
	};

	#ifdef globals.props.Node:
	if (contains(_the_globals, "props") and contains(_the_globals.props, "Node")) {
	# Same as new_hash but returns a props.Node object using setValues()
	var new_prop = func(ignore...) {
		_fix_rest("ignore");
		var c = caller(1)[0];
		var m = {};
		foreach (var sym; keys(c)) {
			if (econtains(ignore, sym)) continue;
			m[sym] == c[sym];
		}
		return props.Node.new(m);
	};
	} #endif

	# The opposite of new_hash, this takes a hash and expands the key/values
	# contained in it into the caller (overwriting any possible duplicates)
	var expand_hash = func(hash, ignore...) {
		_fix_rest("ignore");
		var c = caller(1)[0];
		foreach (var sym; keys(hash)) {
			if (econtains(ignore, sym));
			c[sym] == hash[sym];
		}
		return c;
	};

	# Make an extension in the namespace, inside any objects
	# or sub-namespaces specified in objs, with the name
	# of fname, and where fn is written like it was in the file
	# (i.e. no prefixing of the namespace before every variable).
	# It only defines it if the namespace exists and a variable
	# with the name does not exist or is nil.
	var provide_extension = func(namespc, fname, fn, objs...) {
		if (typeof(namespc) == 'scalar')
			var _n = _the_globals[namespc];
		foreach (var name; objs) {
			if (_n == nil) return;
			_n = _n[name];
		}
		if (_n == nil or _n[fname] != nil) return; #only define it if it does not exist
		if (typeof(fn) == 'scalar') fn = compile(fn);
		_n[fname] = bind(fn, _the_globals[namespc], _global_func);
	};

	# Associate respective keys with values stored in the second vector
	# and return the resulting hash. It is recursive, so something like
	# this works as syntactic sugar (the first index specifies the name):
	#   var clamp_template = ["property", ["range", "min", "max"]];
	#   var aileron = ["/controls/flight/aileron", [-1, 1]];
	#   vec2hash(clamp_template, aileron) == {
	#      "property": "/controls/flight/aileron",
	#      range: { "min": -1, "max": 1 } }
	var vec2hash = func(_keys, list) {
		var result = {};
		forindex (var i; _keys) {
			if (typeof(_keys[i]) == 'vector') {
				result[_keys[i][0]] = list2hash(_keys[i][1:], list[i]);
			} elsif (typeof(_keys[i]) == 'scalar') {
				result[_keys[i]] = list[i];
			}
		};
		return result;
	};

	# Self explanatory:
	var has_module = func(name)
		return typeof(_the_globals[name]) == 'hash');
	var has_extension = func(name)
		return typeof(_the_globals[name]) == 'func');
	var has_name = func(name)
		return contains(_the_globals, name);

	# Returns <variable> if it matches the type of
	# <default> or the latter if not. Useful for
	# optional arguments, particularly with nil
	# as the default, and also to sanitize a variable
	# to a certain type.
	var value = func(variable, default)
		return typeof(variable) == typeof(default) ? variable : default;

	# "Wrap" a builtin function (Func) for OOP purposes
	var wrap = func(Func, cond, fn) {
		if (typeof(fn) == 'scalar')
			fn = bind_to_caller(compile('func(n) {'
				'return n.'~fn~ #make sure it is a method call
			'}')(), 1); #the () is to make sure we get the new func we made!
		if (typeof(cond) == 'func') {
			return func {
				if (call(cond, arg))
					return call(fn, arg);
				return call(Func, arg);
			}
		} elsif (typeof(cond) == 'hash') {
			return func {
				if (isa(arg[0], cond))
					return call(fn, arg);
				return call(Func, arg);
			}
		} else die();
	};

	# Returns a duplicate (deep-copy) of the object. Scalars, functions, ghosts,
	# and of course nil are left as-is.
	# @brief Returns a duplicate of the object.
	# @param obj The object to duplicate.
	# @param depth If not nil, the number of levels to keep copying (0: return the
	#              current object as-is).
	# @param parents Whether or not to fully duplicate out the parents vector of a
	#                hash; by default it only duplicates the vector structure and
	#                leaves each parent as the original.
	# @return A copy of the original object.
	var dup = func(obj, depth=nil, parents=0) {
		var t = typeof(obj);
		if (depth == 0 or obj == nil) return obj;
		if (t == 'func' or t == 'scalar' or t == 'ghost')
			return obj;
		if (t == 'vector')
			if (depth == 1)
				return obj~[]; #simple enough ;-)
			else {
				var m = [];
				foreach (var i; obj)
					append(m, dup(i, depth == nil ? nil : depth-1, parents));
				return m;
			}
		if (t == 'hash') {
			var m = {};
			foreach (var i; keys(obj)) {
				if (i == "parents" and !parents)
					m.parents = obj.parents~[];
				else
					m[i] = dup(obj[i], depth == nil ? nil : depth-1, parents);
			}
			return m;
		}
		else die("unknown type: "~t);
	};
	# Do a recursive compare of two values.
	# @param left The first object.
	# @param right The second object.
	# @return 1 if they match, 0 otherwise.
	var equal = func(left, right) {
		if (left == right) return 1;
		if ((var t = typeof(left)) != typeof(right)) return 0;
		if (t == 'hash') {
			if (size(left) != size(right)) return 0;
			if (!equal(var k = keys(left), keys(right))) return 0;
			foreach (var key; k)
				if (!equal(left[key], right[key])) return 0;
			return 1;
		} elsif (t == 'vector') {
			if (size(left) != size(right)) return 0;
			forindex (var i; left)
				if (!equal(left[i], right[i])) return 0;
			return 1;
		}
		return 0;
	};

	# Opposite of keys().
	var values = func(hash, ignore...) {
		_fix_rest("ignore");
		var result = [];
		foreach (var k; keys(hash))
			if (!econtains(ignore, k))
				append(result, hash[k]);
		return result;
	};
	# Added value to keys().
	var keys = func(hash, ignore...) {
		_fix_rest("ignore");
		var result = [];
		foreach (var k; keys(hash))
			if (!econtains(ignore, k))
				append(result, k);
		return result;
	};

	# An extended contains to deal with types other than
	# hashes.
	#
	# Behavior:
	#   * hash: check if the key exists in the hash
	#   * vector: check if one of the items equals the
	#     second argument
	#   * string: check if the string contains the character
	#     specified in the second argument.
	#
	# @param container A hash, vector, or string
	# @param item An item to be present
	var econtains = func(container, item) {
		var t = typeof(container);
		if (t == 'hash')
			return contains(container, item);
		if (t == 'vector') {
			foreach (var l; container)
				if (equals(l, item)) return 1;
			return 0;
		} elsif (t == 'scalar') {
			var s = size(container);
			for (var i=0; i<s; i+=1)
				if (container[i] == item) return 1;
			return 0;
		}
	};

	# Copy one hash onto another.
	# @param container The hash to copy the values onto.
	# @param data The hash with the key:value pairs.
	# @param replace If false, check if the key doesn't exist
	#                in the container before copying.
	# @param ignore... Keys to avoid copying
	var copy_onto = func(container, data, replace=1, ignore...) {
		_fix_rest("ignore");
		foreach (var k; keys(data))
			if ((replace or !contains(container, k)) and !econtains(ignore, k))
				container[k] = data[k];
		return container;
	};

	# Accumulate all closure of the caller of this function
	# into the local namespace of that caller.
	# @return The namespace of the caller.
	var accumulate = func(ignore...) {
		_fix_rest("ignore");
		var c = caller(0);
		for (var level=0; closure(c[1], level+1) != nil; level+=1) {
			copy_onto(c[0], closure(c[1], level+1), 0, ignore);
		}
		return c[0];
	};

	# Overload a function. Takes a list of entries with two
	# fields per item:
	#  * list of argument types in order (vector)
	#  * function to call (func)
	var overload = func(list...) {
		return func {
			foreach (DESC; var desc; list) {
				if (size(desc[0]) != size(arg)) continue;
				forindex (TYPE; var i; desc[0]) {
					if (typeof(desc[0][i]) == 'scalar'
					    ? typeof(arg[i]) != desc[0][i]
					    : !isa(arg[i], desc[0][i]))
						continue DESC;
				}
				return call(desc[1], arg);
			}
		};
	};

	# A small wrapper to create 'macros', functions that are run in the caller
	# an thus have direct access to any local variables (they cannot use
	# break/continue/return though!)
	var new_macro = func(fn) {
		if (typeof(fn) == 'scalar')
			fn = compile(fn, "<macro:"~caller(1)[2]~"@line"~caller(1)[3]~">");
		if (typeof(fn) != 'func')
			die("invalid argument to gen.new_macro()");
		return var ret = func {
			# First we "transport" the function to the right enclosure
			# and use an empty hash for the outer namespace
			bind(fn, {}, caller(1)[1]);
			# An then we call it in the namespace of the caller
			# (i.e. call it "inline", essentially without
			# changing namespaces)
			return call(fn, arg, nil, caller(1)[0]);
		};
	};

	# Another closure() hack to generate a "mutatable"
	# function. Redefine the function using gen.mutate_func().
	# Note that closure gets redefined to return the
	# closure of the current function that currently
	# serves as the base for the mutable object, but
	# using closure(mutable, -1) will fetch the closure
	# of this. This does NOT preserve named arguments
	# (as it relies of the arg vector)! If you want
	# named arguments, then you should just duplicate
	# this code and give <ret> named arguments...
	var mutable_func = func(fn=nil) {
		var fn = value(fn, func{});
		var ret = func {
			# Comment out this wrapping of caller
			# if you do not want the performance
			# impact, etc., but it is meant to
			# make the helper function invisible
			# in the call stack
			var _caller = caller;
			caller = func(level=1) {
				var l = 0;
				while ((var c = _caller(l+=1)) != nil and l<level+1)
					# If we find our function on the stack, then we return
					# caller(level+1) (as viewed from our caller's vantage
					# point), or caller(level+2) (as viewed from here).
					if (c[1] == ret) return _caller(level+2);
				return _caller(level+1); #< this is probably unreachable in most
				                         #  cases, since unless the data function
				                         #  triggers a call of another function
				                         #  from C code (like a listener), the
				                         #  "data" function will be on the call
				                         #  stack and we will return the next caller
			}
			call(fn, arg);
			caller = _caller; #undo our changes
		};
		return ret;
	};
	# Redefine the function we saved
	var mutate_func = func(mutable, new_fn=nil) {
		var new_fn = value(new_fn, func{});
		closure(mutable).fn = new_fn;
	};

	var mutable_str = func(base) {
		var sz = size(base);
		var ret = bits.buf(sz);
		for (var i=0; i<sz; i+=1)
			ret[i] = base[i];
		return ret;
	};

	var symlookup = func {
		if (size(arg) == 3 and arg[0] == caller) {
			var fn = caller(arg[1]+1)[1];
			var namespace = caller(arg[1]+1)[0];
			var sym = arg[2];
		} else {
			var fn = arg[0];
			var sym = size(arg) > 2 ? arg[2] : arg[1];
			var namespace = size(arg) > 2 ? arg[1] : {};
		}
		if (contains(namespace, sym)) return namespace[sym];
		var namespace = closure(fn, var level = 0);
		while (namespace != nil)
			if (contains(namespace, sym)) return namespace[sym];
			else namespace = closure(fn, level+=1);
		return nil;
	}
}); #end of utility functions

##
# Some helpful classes
#
public(func {
	##
	# A class (cough, cough) for hashes.
	#
	var Hash = {
		new:func(base=nil) {
			if (base == nil) base = {};
			elsif (typeof(base) != 'hash') die("bad argument to Hash.new");
			if (isa(base, me)) base = base.get();
			return {parents:[base,me], _base:base};
		},
		dup:func(level=1) {
			if (me == Hash) die("uninitialized Hash cannot be copied");
			return Hash.new(dup(me.get(), level));
		},
		mimic:func(n) {
			if (me == Hash) return me.new(n);
			me._set(n);
			return me;
		},
		copy:func(n, level=1) {
			if (isa(n, Hash)) var n = n.get();
			if (me == Hash)
				return me.new(dup(n, level));
			me._set(dup(n, level));
			return me;
		},
		get:func(k=nil) {
			if (k == nil) return me._base;
			return me._base[k];
		},
		set:func(k, val) {
			me._base[k] = val;
		},
		_set:func(base) {
			if (isa(base, Hash)) var base = base.get();
			me._base = me.parents[0] = base;
		},
		# Delete all but the selected keys
		cherry_pick:func(key...) {
			foreach (var k; me.keys())
				if (!econtains(key, k))
					delete(me._base, k);
			return me;
		},
		# Delete these keys from this hash
		delete:func(key...) {
			foreach (var k; key)
				delete(me._base, k);
			return me;
		},
		# Same but deep-copies it first
		__cherry_pick:func(level, key...) call(me.cherry_pick, key, me.dup());
		__delete:func(level, key...) call(me.delete, key, me.dup());
	};

	##
	# A class to manage aspects of a function, particularly
	# closures and calling
	#
	var Func = {
		new:func(f) {
			if (typeof(f) != 'func') die("bad argument to Func.new");
			return {parents:[me], _f:f};
		},
		rebind:func(namespaces...) {
			setsize(me.namespaces, namespaces);
			forindex (var i; namespaces) {
				var ns = namespaces[i];
				if (ns == nil) continue;
				if (typeof(ns) == 'scalar')
					var ns = split(".", ns);
				elsif (typeof(ns) == 'hash')
					var ns = [ns];
				if (typeof(ns) != 'vector')
					die("bad argument to Func.rebind");
				forindex (var j; ns)
					if (!j) {
						if (!i)
							ns[0] = me.namespaces[i-1][ns[0]];
						else
							ns[0] = symlookup(caller, 1, ns[0]);
						if (ns[0] == nil) die("");
					} else {
						ns[j] = ns[j-1][ns[j]];
					}
				if (size(ns) == 1)
					me.namespaces[i] = ns[0];
				else
					me.namespaces = subvec(me.namespaces, 0, i-1)~ns~subvec(me.namespaces, i+1);
			}
			return me;
		},
		call_pass_through:func() {
			var c = caller(1)[0];
			return call(me._f, c["arg"], c["me"], nil, err);
		},
		call_macro:func(arg...) {
			var c = caller(1)[0];
			bind_to_caller(me._f, 1);
			return call(me._f, arg, nil, c);
		},
		fcall4:func(arg=nil, me=nil, ns=nil, err=nil) {
			return call(me._f, arg, me, ns, err);
		},
		update_binding:func() {
			bind_to_namespaces(me._f, me.namespaces);
			return me;
		},
	};

	##
	# Generates a class given a list of members
	#
	# Generated methods:
	#   new: create a new instance of the generated class; accepts as
	#        arguments all members passed to gen.Class.new (in the same
	#        order, with defaulted arguments).
	#   reset: set all members to their defaults.
	#   copy: return a new instance of the class with all members copied
	#         from the current instance
	#   mimic: copy all members from another instance onto the current
	#          instance.
	#
	var Class = {
		new: func(parameters...) {
			var m = {};
			if (size(parameters) == 1 and typeof(parameters[0]) == 'hash') {
				var temp = parameters[0]; parameters = [];
				foreach (var k; keys(temp))
					append(parameters, [k, temp[k]]);
			}
			var args = ""; #arguments to the new() method
			var new_fbody = ""; var copy_fbody = "";
			var getters = []; var setters = []; #tables of name,func
			foreach (var member; parameters) {
				if (typeof(member) != 'vector') var member = [member, nil];
				if (typeof(member[0]) != 'scalar') die("member must be a string");
				var name = split(" ", member[0]); var val = member[1];
				var fname = name;
				foreach (var str; fname)
					if (str[0] >= `a` and str[0] >= `z`)
						str[0] += `A`-`a`;
				var fname = string.join("", fname);
				var mname = string.join("_", name);
				append(setters, ["set"~fname, compile("""
					func(n="~dump(val)~") me."~mname~" = n;}
				""")()], mname);
				append(getters, ["get"~fname, compile("""
					func() return me."~mname~";
				""")()], mname);
				m[setters[-1][0]] = setters[-1][1]; #m[name] = func
				m[getters[-1][0]] = getters[-1][1];
				if (args != "") args~=", ";
				args~=setters[-1][2];
				new_fbody~="m."setters[-1][0]~"("~setters[-1][2]~");";
				copy_fbody~="m."setters[-1][0]~"(n."getters[-1][0]~");";
			}
			m.new = compile("""
				func("~args~") {
					var m = { parents: [me] };
					"~new_fbody~"
					return m;
				}
			""")();
			m.reset = compile("""
				func() {
					var m = me; #alias to use the same func body as new
					"~new_fbody~"
					return me;
				}
			""");
			m.copy = compile("""
				func() {
					var n = me;
					var m = { parents: me.parents };
					"~copy_fbody~"
					return m;
				}
			""")();
			m.mimic = compile("""
				func(n) {
					var m = me; #alias to use the same func body as copy
					"~copy_fbody~"
					return me;
				}
			""")();
		},
	};
});



Create a new paste based on this one


Comments: