Skip to content
This repository
Browse code

support for switch statements

  • Loading branch information...
commit 514750117d7198ac293e3ee7fdee7cd489062ae7 1 parent 242fad0
Maxwell Krohn authored

Showing 2 changed files with 29 additions and 11 deletions. Show diff stats Hide diff stats

  1. +12 1 lib/coffee-script/nodes.js
  2. +17 10 src/nodes.coffee
13 lib/coffee-script/nodes.js
@@ -2748,6 +2748,17 @@
2748 2748 return this;
2749 2749 };
2750 2750
  2751 + Switch.prototype.callContinuation = function() {
  2752 + var block, code, condition, _i, _len, _ref2, _ref3, _ref4;
  2753 + code = CALL_CONTINUATION();
  2754 + _ref2 = this.cases;
  2755 + for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
  2756 + _ref3 = _ref2[_i], condition = _ref3[0], block = _ref3[1];
  2757 + block.push(code);
  2758 + }
  2759 + return (_ref4 = this.otherwise) != null ? _ref4.push(code) : void 0;
  2760 + };
  2761 +
2751 2762 Switch.prototype.compileNode = function(o) {
2752 2763 var block, body, code, cond, conditions, expr, i, idt1, idt2, _i, _len, _len2, _ref2, _ref3, _ref4, _ref5;
2753 2764 idt1 = o.indent + TAB;
@@ -2829,7 +2840,7 @@
2829 2840
2830 2841 If.prototype.isStatement = function(o) {
2831 2842 var _ref2;
2832   - return (o != null ? o.level : void 0) === LEVEL_TOP || this.bodyNode().isStatement(o) || ((_ref2 = this.elseBodyNode()) != null ? _ref2.isStatement(o) : void 0);
  2843 + return (o != null ? o.level : void 0) === LEVEL_TOP || this.bodyNode().isStatement(o) || ((_ref2 = this.elseBodyNode()) != null ? _ref2.isStatement(o) : void 0) || this.hasContinuation();
2833 2844 };
2834 2845
2835 2846 If.prototype.jumps = function(o) {
27 src/nodes.coffee
@@ -2195,6 +2195,12 @@ exports.Switch = class Switch extends Base
2195 2195 @otherwise?.makeReturn res
2196 2196 this
2197 2197
  2198 + callContinuation : ->
  2199 + code = CALL_CONTINUATION()
  2200 + for [condition,block] in @cases
  2201 + block.push code
  2202 + @otherwise?.push code
  2203 +
2198 2204 compileNode: (o) ->
2199 2205 idt1 = o.indent + TAB
2200 2206 idt2 = o.indent = idt1 + TAB
@@ -2255,7 +2261,8 @@ exports.If = class If extends Base
2255 2261 # to be a statement. Otherwise a conditional operator is safe.
2256 2262 isStatement: (o) ->
2257 2263 o?.level is LEVEL_TOP or
2258   - @bodyNode().isStatement(o) or @elseBodyNode()?.isStatement(o)
  2264 + @bodyNode().isStatement(o) or @elseBodyNode()?.isStatement(o) or
  2265 + @hasContinuation()
2259 2266
2260 2267 jumps: (o) -> @body.jumps(o) or @elseBody?.jumps(o)
2261 2268
@@ -2362,7 +2369,7 @@ CpsCascade =
2362 2369 InlineDeferral =
2363 2370
2364 2371 # Generate this code, inline. Is there a better way?
2365   - #
  2372 + #
2366 2373 # tame =
2367 2374 # Deferrals : class
2368 2375 # constructor: (@continuation) ->
@@ -2374,7 +2381,7 @@ InlineDeferral =
2374 2381 # (inner_params...) =>
2375 2382 # defer_params?.assign_fn?.apply(null, inner_params)
2376 2383 # @_fulfill()
2377   - #
  2384 + #
2378 2385 generate : ->
2379 2386 k = new Literal "continuation"
2380 2387 cnt = new Literal "count"
@@ -2382,10 +2389,10 @@ InlineDeferral =
2382 2389 ns = new Value new Literal tame.const.ns
2383 2390
2384 2391 # make the constructor:
2385   - #
  2392 + #
2386 2393 # constructor: (@continuation) ->
2387 2394 # @count = 1
2388   - #
  2395 + #
2389 2396 k_member = new Value new Literal "this"
2390 2397 k_member.add new Access k
2391 2398 p1 = new Param k_member
@@ -2399,10 +2406,10 @@ InlineDeferral =
2399 2406 constructor_assign = new Assign constructor_name, constructor_code
2400 2407
2401 2408 # make the _fulfill member:
2402   - #
  2409 + #
2403 2410 # _fulfill : ->
2404 2411 # @continuation if ! --@count
2405   - #
  2412 + #
2406 2413 if_expr = new Call k_member, []
2407 2414 if_body = new Block [ if_expr ]
2408 2415 decr = new Op '--', cnt_member
@@ -2419,7 +2426,7 @@ InlineDeferral =
2419 2426 # (inner_params...) =>
2420 2427 # defer_params?.assign_fn?.apply(null, inner_params)
2421 2428 # @_fulfill()
2422   - #
  2429 + #
2423 2430 inc = new Op "++", cnt_member
2424 2431 ip = new Literal "inner_params"
2425 2432 dp = new Literal "defer_params"
@@ -2447,10 +2454,10 @@ InlineDeferral =
2447 2454 obj = new Obj assignments, true
2448 2455 body = new Block [ new Value obj ]
2449 2456 klass = new Class null, null, body
2450   -
  2457 +
2451 2458 # tame =
2452 2459 # Deferrals : <class>
2453   - #
  2460 + #
2454 2461 klass_assign = new Assign cn, klass, "object"
2455 2462 ns_obj = new Obj [ klass_assign ], true
2456 2463 ns_val = new Value ns_obj

0 comments on commit 5147501

Please sign in to comment.
Something went wrong with that request. Please try again.