Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First successful test of a little test script. Need to massage the lo…

…cal runtime a little bit better.
  • Loading branch information...
commit cd491d00f3a2bdb0c3a99730406c535fdb5860bf 1 parent 1ca54c3
@maxtaco authored
View
1  .gitignore
@@ -7,3 +7,4 @@ test/*.js
examples/beautiful_code/parse.coffee
*.gem
/node_modules
+*~
View
3  Cakefile
@@ -29,7 +29,8 @@ header = """
sources = [
'coffee-script', 'grammar', 'helpers'
- 'lexer', 'nodes', 'rewriter', 'scope'
+ 'lexer', 'nodes', 'rewriter', 'scope',
+ 'tame', 'tamerun'
].map (filename) -> "src/#{filename}.coffee"
# Run a CoffeeScript through our node/coffee interpreter.
View
34 lib/coffee-script/nodes.js
@@ -217,6 +217,16 @@
return this.tameNodeFlag;
};
+ Base.prototype.needsRuntime = function() {
+ var child, _i, _len, _ref2;
+ _ref2 = this.flattenChildren();
+ for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
+ child = _ref2[_i];
+ if (child.needsRuntime()) return true;
+ }
+ return false;
+ };
+
Base.prototype.walkAstTamedLoop = function(flood) {
var child, _i, _len, _ref2;
if (this.isLoop() && this.tameNodeFlag) flood = true;
@@ -530,11 +540,23 @@
return new Block(nodes);
};
+ Block.prototype.addRuntime = function() {
+ var assign, call, lib, ns, req;
+ ns = new Value(new Literal(tame["const"].ns));
+ req = new Value(new Literal('require'));
+ lib = '"' + tame["const"].runtime + '"';
+ call = new Call(req, [new Value(new Literal(lib))]);
+ assign = new Assign(ns, call);
+ return this.expressions.unshift(assign);
+ };
+
Block.prototype.tameTransform = function() {
this.walkAstTame();
+ if (this.needsRuntime()) this.addRuntime();
this.walkAstTamedLoop(false);
this.walkCpsPivots();
- return this.cpsRotate();
+ this.cpsRotate();
+ return this;
};
return Block;
@@ -1761,9 +1783,9 @@
return false;
};
- Code.prototype.walkAstPivots = function() {
- if (Code.__super__.walkAstPivots.call(this)) this.cpsPivotFlag = true;
- return false;
+ Code.prototype.walkCpsPivots = function() {
+ Code.__super__.walkCpsPivots.call(this);
+ return this.cpsPivotFlag = false;
};
return Code;
@@ -2322,6 +2344,10 @@
return this.call.compile(o);
};
+ Defer.prototype.needsRuntime = function() {
+ return true;
+ };
+
return Defer;
})();
View
3  lib/coffee-script/tame.js
@@ -29,7 +29,8 @@
c_while: "_continue",
defer_method: "defer",
slot: "__slot",
- assign_fn: "assign_fn"
+ assign_fn: "assign_fn",
+ runtime: "tamerun"
};
}).call(this);
View
48 lib/coffee-script/tamerun.js
@@ -0,0 +1,48 @@
+(function() {
+ var Deferrals, makeDeferReturn;
+ var __slice = Array.prototype.slice;
+
+ makeDeferReturn = function(obj, defer_args, id) {
+ var k, ret, _i, _len, _ref;
+ ret = function() {
+ var inner_args, _ref;
+ inner_args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
+ if (defer_args != null) {
+ if ((_ref = defer_args.assign_fn) != null) _ref.apply(null, inner_args);
+ }
+ return obj._fulfill(id);
+ };
+ if (defer_args) {
+ ret.__tame_trace = {};
+ _ref = ["parent_cb", "file", "line", "func_name"];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ k = _ref[_i];
+ ret.__tame_trace[k] = defer_args[k];
+ }
+ }
+ return ret;
+ };
+
+ exports.Deferrals = Deferrals = (function() {
+
+ function Deferrals(k) {
+ this.continuation = k;
+ this.count = 1;
+ }
+
+ Deferrals.prototype._fulfill = function() {
+ if (--this.count === 0) return this.continuation();
+ };
+
+ Deferrals.prototype.defer = function(args) {
+ var self;
+ this.count++;
+ self = this;
+ return makeDeferReturn(self, args, null);
+ };
+
+ return Deferrals;
+
+ })();
+
+}).call(this);
View
23 src/nodes.coffee
@@ -180,6 +180,11 @@ exports.Base = class Base
@tameNodeFlag = true if child.walkAstTame()
@tameNodeFlag
+ needsRuntime : ->
+ for child in @flattenChildren()
+ return true if child.needsRuntime()
+ return false
+
# Walk all loops that are marked as "tamed" and mark their children
# as being children in a tamed loop. They'll need more translations
# than other nodes. Eventually, "switch" statements might also be "loops
@@ -454,12 +459,22 @@ exports.Block = class Block extends Base
return nodes[0] if nodes.length is 1 and nodes[0] instanceof Block
new Block nodes
+ addRuntime : ->
+ ns = new Value new Literal tame.const.ns
+ req = new Value new Literal 'require'
+ lib = '"' + tame.const.runtime + '"'
+ call = new Call req, [ new Value new Literal lib ]
+ assign = new Assign ns, call
+ @expressions.unshift assign
+
# Perform all steps of the Tame transform
tameTransform : ->
@walkAstTame()
+ @addRuntime() if @needsRuntime()
@walkAstTamedLoop(false)
@walkCpsPivots()
@cpsRotate()
+ this
#### Literal
@@ -1422,9 +1437,9 @@ exports.Code = class Code extends Base
@tameLoopFlag = true if super(false)
false
- walkAstPivots: () ->
- @cpsPivotFlag = true if super()
- false
+ walkCpsPivots: ->
+ super()
+ @cpsPivotFlag = false
#### Param
@@ -1875,6 +1890,8 @@ exports.Defer = class Defer extends Base
scope.add name, 'var'
@call.compile o
+ needsRuntime : -> true
+
#### Await
exports.Await = class Await extends Base
View
6 src/tame.coffee
@@ -1,4 +1,7 @@
+#=======================================================================
+# Compile Time!
+#
exports.AstTamer = class AstTamer
constructor: (rest...) ->
@@ -19,3 +22,6 @@ exports.const =
defer_method : "defer"
slot : "__slot"
assign_fn : "assign_fn"
+ runtime : "tamerun"
+
+
View
35 src/tamerun.coffee
@@ -0,0 +1,35 @@
+
+#=======================================================================
+
+makeDeferReturn = (obj, defer_args, id) ->
+ ret = (inner_args...) ->
+ defer_args?.assign_fn?.apply(null, inner_args)
+ obj._fulfill id
+
+ if defer_args
+ ret.__tame_trace = {}
+ for k in [ "parent_cb", "file", "line", "func_name" ]
+ ret.__tame_trace[k] = defer_args[k]
+
+ ret
+
+#=======================================================================
+# Deferrals
+#
+# A collection of Deferrals that can
+#
+exports.Deferrals = class Deferrals
+
+ constructor: (k) ->
+ @continuation = k
+ @count = 1
+
+ _fulfill : ->
+ @continuation() if --@count == 0
+
+ defer : (args) ->
+ @count++
+ self = this
+ return makeDeferReturn self, args, null
+
+#=======================================================================
Please sign in to comment.
Something went wrong with that request. Please try again.