This new version supports named array positions. With this you can define structs and objects as arrays!
Example:
var stack = {
.sp 0,
.data {nil},
.push ^:[d, stack] {
stack.data[stack.sp] = d;
stack.sp = stack.sp + 1;
},
.pop ^:stack {
if(stack.sp == 0) {
return;
}
stack.sp = stack.sp - 1;
return stack.data[stack.sp];
},
.isEmpty ^:stack {
return (stack.sp == 0);
}
};
.sp 0,
.data {nil},
.push ^:[d, stack] {
stack.data[stack.sp] = d;
stack.sp = stack.sp + 1;
},
.pop ^:stack {
if(stack.sp == 0) {
return;
}
stack.sp = stack.sp - 1;
return stack.data[stack.sp];
},
.isEmpty ^:stack {
return (stack.sp == 0);
}
};
Stack object!
// Using this stack object
// Push an integer
[~stack.push stack:stack d:10];
// Push a string
[~stack.push stack:stack d:"Hello!\n"];
// Push a function
[~stack.push stack:stack d:@main];
// Pop everything and then print...
var d;
while(![~stack.isEmpty stack:stack]) {
d = [~stack.pop stack:stack].string;
[print string:"%s\n" format:{d}];
}
// Push an integer
[~stack.push stack:stack d:10];
// Push a string
[~stack.push stack:stack d:"Hello!\n"];
// Push a function
[~stack.push stack:stack d:@main];
// Pop everything and then print...
var d;
while(![~stack.isEmpty stack:stack]) {
d = [~stack.pop stack:stack].string;
[print string:"%s\n" format:{d}];
}
Download
No comments:
Post a Comment