sai-language
Version:
An object-oriented language designed to afford code comprehension and maintenance. Transpiles in-place to Javascript.
185 lines (181 loc) • 5.87 kB
JavaScript
// Javascript source for state transpiled by SAI
//
;
var prototype=new function() {
this.Constructor=function(){};
this.__tobelocked=[];
this.__tobefrozen=[];
this.__contracts=[];
this.__unverified=true;
this.isof={};
return this;
}();
var $AI=require("sai-library");
// Generated code follows
var __context={"name":"state","loader":"SAI.GetSourceFromPaths","path":"state.sai","mtime":"2018-03-08T17:40:47.865Z","fetched":"2018-03-16T13:56:11.426Z"};
var $state$getfn = function(states, state) {
return $AI.assert(states[state], "SAI runtime: STATE " + state + " does not exist; available states are " + $AI.keys_op(states).join(','));
}
var isa = prototype.isa = 'State';
var $bindfail = function(m) {
throw new Error("SAI: A call to " + m + " on object " + isa + " has come unbound from any instance of that object. (If this is not in error, mark the declaration of " + m + " as unbound.)");
}
prototype.isof['State'] = {
version: '0.0.0-unspecified',
isa: isa,
context: __context,
type: "main"
};
prototype.__tobelocked = prototype.__tobelocked.concat(["Instantiate", "Timer", "isa"]);
prototype.__tobefrozen = prototype.__tobefrozen.concat(["isof"]);
var $1g = prototype['Instantiate'] || function() {};
prototype['Instantiate'] = function(p) {
var $_d, $_c, _then, _goto, $15, $14, $13, $12, $11, $10, $9, $8, _feat, _dsc, _adjacent, _position, _newpos, _pos, $7, $6, $5, $4, $3, $2, $1, $0, _features, _size, _moves, _maze, _Valid, _Step, _i, _Rand, $ = this['Instantiate'] ? this : $bindfail('Instantiate'); {
_Rand = function(p) {
var _i = p; {
return (Math.floor((_i * Math.random())));
}
};
_Step = function(p) {
{
return (((_Rand(2)) ? ((0 - (1))) : (1)));
}
};
_Valid = function(p) {
var _i = p; {
return (((_i + _size) % _size));
}
};
_maze = [];
_moves = 0;
_size = 12;
_features = {
pit: 'You feel a draft!',
bats: 'You hear the leathery flapping of wings!',
troll: 'You smell the stench of the troll!',
escape: 'You see the glow of daylight!'
};
var $0 = 0,
$1 = _size,
$2 = 1;
if ($2 <= 0) throw new Error("SAI Runtime: COUNT STEP value should be positive.");
for (; $0 < $1; $0 = $0 + $2) {
_maze.push('empty');;;
}
var $6 = _features,
$5 = undefined;
if (undefined === $6) $6 = {};
for ($5 in $6) {
$4 = $6[$5];
_maze[_Rand(_size)] = $5;;
}
var $15 = function() {
var _goto, _then;
var $states = {
'start': function(p) {
{
$AI.debug_op('You\'re lost in a cave!');
do {
_pos = _Rand(_size);
} while (!((_maze[_pos] === 'empty')));
_goto('move', _pos);
}
},
'move': function(p) {
var _newpos = p; {
_moves = (_moves || 0) + 1;
_position = _Valid(_newpos);
$AI.debug_op(' move ' + _moves + ' ... now in room ' + _position);
_goto(_maze[_position]);
}
},
'empty': function(p) {
{
var $12 = [(_position - 1), (_position + 1)],
$11;
if (undefined === $12) $12 = [];
var $13 = $12.length;
for ($11 = 0; $11 < $13; $11++) {
_adjacent = $12[$11];
var $9 = _features,
_feat = undefined;
if (undefined === $9) $9 = {};
for (_feat in $9) {
_dsc = $9[_feat];
if ($8 = ((_feat === _maze[_Valid(_adjacent)]))) {
$AI.debug_op(_dsc);
};
};
}
$.Timer(800, _then('step'));
}
},
'step': function(p) {
{
_goto('move', (_position + _Step()));
}
},
'bats': function(p) {
{
$AI.debug_op('SWOOP!! The bats moved you randomly!');
_goto('move', _Rand(_size));
}
},
'pit': function(p) {
{
$AI.debug_op('WHEEEE THUD -- You died, you fell in a pit.');
_goto('gameover');
}
},
'troll': function(p) {
{
$AI.debug_op('CHOMP CHOMP CHURMP CHULF BURRP You died, the troll got you.');
_goto('gameover');
}
},
'escape': function(p) {
{
$AI.debug_op('=== CONGRATULATIONS == You got out!');
_goto('gameover');
}
},
'gameover': function(p) {
{
$AI.debug_op('Game complete in ' + _moves + ' moves.');
}
}
}
_goto = function(state) {
var statefn = $state$getfn($states, state);
var fn = undefined,
l = arguments.length,
args = new Array(l - 1);
for (var li = 1; li < l; li++) args[li - 1] = arguments[li];
setImmediate(function() {
statefn.apply($, args)
});
};
_then = function(state) {
var statefn = $state$getfn($states, state);
return function() {
statefn.apply($, arguments);
};
};
_goto('start');
}();
$AI.debug_op('Synchronous code complete.');
}
};
var $2g = prototype['Timer'] || function() {};
prototype['Timer'] = function(p, _cb) {
var $_d, $_c, $16, _time = p,
$ = this['Timer'] ? this : $bindfail('Timer'); {
setTimeout(_cb, _time);
}
};
$AI.finalizePrototype(prototype);
if (prototype.isof[prototype.isa].type === "singleton") { prototype.Constructor(); prototype.Instantiate(); }
var result=prototype.isof[prototype.isa].type === "main" ? prototype.constructor() : prototype;
exports=result; try { module.exports=result; } catch(e) {};
return result;