Howto talk:Implementing a simple bytecode interpreter in Nasal

From FlightGear wiki
Jump to navigation Jump to search
var Stack = {
new: func(size=512) {
 var m = {parents:[Stack]};
 m.stack = [];
 m._size = size;
 setsize(m.stack, m._size);
 m._current = 0;
 return m;
},
push: func(item) {
 append(me.stack, item);
 me._current += 1;
},
pop: func() {
me._current -= 1;
return pop(me.stack);
},
free: func() {
return me._size-me._current-1;
},
top: func() {
return me._current;
},
}; # Stack

var STACKSIZE = 64;
var s = Stack.new(STACKSIZE);
var x=100;
s.push(x);
var value = s.pop();
if (x != value) die("WTF");

for (var i=0;i<111;i+1)
	s.push(i);

print( s.free() );

if (STACKSIZE - i != s.free() ) die("WTF ...");