Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 3 commits
  • 13 files changed
  • 0 comments
  • 1 contributor
1  .gitignore
... ... @@ -1,4 +1,3 @@
1   -*.tmproj
2 1 lib/loop/grammar.js
3 2 TODO.txt
4 3 *.loop
75 lib/loop/eval.js
@@ -37,39 +37,6 @@ var loopEval = function(syntaxTree) {
37 37
38 38 var body;
39 39
40   - if (firstToken.type === 'id') {
41   - if (firstToken.contents === 'lambda' || firstToken.contents === 'function') {
42   - var formalArgs = car(remainingTokens);
43   - body = loopEvalCollection(cdr(remainingTokens));
44   -
45   - return {
46   - type: 'funcall',
47   - function: firstToken,
48   - arguments: {
49   - type: 'list',
50   - contents: [
51   - formalArgs,
52   - {
53   - type: 'list',
54   - contents: body
55   - }
56   - ]
57   - }
58   - };
59   - } else { // user defined or built in function
60   - body = loopEvalCollection(remainingTokens);
61   -
62   - return {
63   - type: 'funcall',
64   - function: firstToken,
65   - arguments: {
66   - type: 'list',
67   - contents: body
68   - }
69   - };
70   - }
71   - }
72   -
73 40 if (firstToken.type === 'prop-access') {
74 41 body = loopEvalCollection(remainingTokens);
75 42
@@ -81,22 +48,36 @@ var loopEval = function(syntaxTree) {
81 48 contents: body
82 49 }
83 50 };
84   - }
  51 + } else if (firstToken.contents === 'lambda' || firstToken.contents === 'function') {
  52 + var formalArgs = car(remainingTokens);
  53 + body = loopEvalCollection(cdr(remainingTokens));
85 54
86   - var errorMessage = "Parse error near: `" + firstToken.contents + "`";
87   - if (firstToken.sourceInfo) {
88   - errorMessage += [
89   - ' ',
90   - '-- line: ',
91   - firstToken.sourceInfo.first_line,
92   - " -- col: ",
93   - firstToken.sourceInfo.first_column,
94   - "-",
95   - firstToken.sourceInfo.last_column
96   - ].join('');
97   - }
  55 + return {
  56 + type: 'funcall',
  57 + function: firstToken,
  58 + arguments: {
  59 + type: 'list',
  60 + contents: [
  61 + formalArgs,
  62 + {
  63 + type: 'list',
  64 + contents: body
  65 + }
  66 + ]
  67 + }
  68 + };
  69 + } else { // user defined or built in function
  70 + body = loopEvalCollection(remainingTokens);
98 71
99   - throw new Error(errorMessage);
  72 + return {
  73 + type: 'funcall',
  74 + function: firstToken,
  75 + arguments: {
  76 + type: 'list',
  77 + contents: body
  78 + }
  79 + };
  80 + }
100 81 case 'prop-access':
101 82 var key = contents.key;
102 83 var value = contents.value;
33 lib/loop/uglify.js
@@ -230,6 +230,39 @@ var progn = function(tree) {
230 230 throw new Error("debugger can not take any arguments");
231 231 }
232 232 return ['name', 'debugger'];
  233 + } else if (f.contents === 'cond') {
  234 + var mainList = [];
  235 + var currentList = mainList;
  236 + var listLen = args.length;
  237 +
  238 + if (listLen === 0) {
  239 + throw new Error("Cond must have at least one argument!");
  240 + }
  241 +
  242 + _.each(args, function(pair, index) {
  243 + var condition = progn(pair.function);
  244 + var body = processBody(pair.arguments.contents);
  245 +
  246 + if (condition.length === 2 && condition[0] === 'name' && condition[1] === 'else') {
  247 + if (listLen === index + 1) {
  248 + currentList.push('block', body);
  249 + } else {
  250 + throw new Error("else must be last argument in a cond!");
  251 + }
  252 + } else {
  253 + currentList.push('if', condition, ['block', body]);
  254 +
  255 + if (listLen === index + 1) {
  256 + currentList.push(null);
  257 + } else {
  258 + var newList = [];
  259 + currentList.push(newList);
  260 + currentList = newList;
  261 + }
  262 + }
  263 + });
  264 +
  265 + return mainList;
233 266 } else if (f.contents === 'if') {
234 267 var condition = args.shift();
235 268 body = args;
256 loop.tmproj
... ... @@ -0,0 +1,256 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3 +<plist version="1.0">
  4 +<dict>
  5 + <key>documents</key>
  6 + <array>
  7 + <dict>
  8 + <key>expanded</key>
  9 + <true/>
  10 + <key>name</key>
  11 + <string>loop</string>
  12 + <key>regexFolderFilter</key>
  13 + <string>!.*/(\.[^/]*|CVS|node_modules|_darcs|_MTN|\{arch\}|blib|.*~\.nib|.*\.(framework|app|pbproj|pbxproj|xcode(proj)?|bundle))$</string>
  14 + <key>sourceDirectory</key>
  15 + <string></string>
  16 + </dict>
  17 + </array>
  18 + <key>fileHierarchyDrawerWidth</key>
  19 + <integer>432</integer>
  20 + <key>metaData</key>
  21 + <dict>
  22 + <key>DEVELOPMENT_NOTES.txt</key>
  23 + <dict>
  24 + <key>caret</key>
  25 + <dict>
  26 + <key>column</key>
  27 + <integer>18</integer>
  28 + <key>line</key>
  29 + <integer>25</integer>
  30 + </dict>
  31 + <key>columnSelection</key>
  32 + <false/>
  33 + <key>firstVisibleColumn</key>
  34 + <integer>0</integer>
  35 + <key>firstVisibleLine</key>
  36 + <integer>0</integer>
  37 + <key>selectFrom</key>
  38 + <dict>
  39 + <key>column</key>
  40 + <integer>6</integer>
  41 + <key>line</key>
  42 + <integer>25</integer>
  43 + </dict>
  44 + <key>selectTo</key>
  45 + <dict>
  46 + <key>column</key>
  47 + <integer>18</integer>
  48 + <key>line</key>
  49 + <integer>25</integer>
  50 + </dict>
  51 + </dict>
  52 + <key>lib/loop.js</key>
  53 + <dict>
  54 + <key>caret</key>
  55 + <dict>
  56 + <key>column</key>
  57 + <integer>0</integer>
  58 + <key>line</key>
  59 + <integer>0</integer>
  60 + </dict>
  61 + <key>firstVisibleColumn</key>
  62 + <integer>0</integer>
  63 + <key>firstVisibleLine</key>
  64 + <integer>0</integer>
  65 + </dict>
  66 + <key>lib/loop/compiler.js</key>
  67 + <dict>
  68 + <key>caret</key>
  69 + <dict>
  70 + <key>column</key>
  71 + <integer>0</integer>
  72 + <key>line</key>
  73 + <integer>0</integer>
  74 + </dict>
  75 + <key>firstVisibleColumn</key>
  76 + <integer>0</integer>
  77 + <key>firstVisibleLine</key>
  78 + <integer>0</integer>
  79 + </dict>
  80 + <key>lib/loop/executable.js</key>
  81 + <dict>
  82 + <key>caret</key>
  83 + <dict>
  84 + <key>column</key>
  85 + <integer>41</integer>
  86 + <key>line</key>
  87 + <integer>47</integer>
  88 + </dict>
  89 + <key>firstVisibleColumn</key>
  90 + <integer>0</integer>
  91 + <key>firstVisibleLine</key>
  92 + <integer>0</integer>
  93 + </dict>
  94 + <key>lib/loop/grammar.js</key>
  95 + <dict>
  96 + <key>caret</key>
  97 + <dict>
  98 + <key>column</key>
  99 + <integer>0</integer>
  100 + <key>line</key>
  101 + <integer>0</integer>
  102 + </dict>
  103 + <key>firstVisibleColumn</key>
  104 + <integer>0</integer>
  105 + <key>firstVisibleLine</key>
  106 + <integer>0</integer>
  107 + </dict>
  108 + <key>lib/loop/macro-compiler.js</key>
  109 + <dict>
  110 + <key>caret</key>
  111 + <dict>
  112 + <key>column</key>
  113 + <integer>0</integer>
  114 + <key>line</key>
  115 + <integer>390</integer>
  116 + </dict>
  117 + <key>firstVisibleColumn</key>
  118 + <integer>0</integer>
  119 + <key>firstVisibleLine</key>
  120 + <integer>362</integer>
  121 + </dict>
  122 + <key>lib/loop/parser.js</key>
  123 + <dict>
  124 + <key>caret</key>
  125 + <dict>
  126 + <key>column</key>
  127 + <integer>0</integer>
  128 + <key>line</key>
  129 + <integer>0</integer>
  130 + </dict>
  131 + <key>firstVisibleColumn</key>
  132 + <integer>0</integer>
  133 + <key>firstVisibleLine</key>
  134 + <integer>0</integer>
  135 + </dict>
  136 + <key>lib/loop/reverse-compiler.js</key>
  137 + <dict>
  138 + <key>caret</key>
  139 + <dict>
  140 + <key>column</key>
  141 + <integer>0</integer>
  142 + <key>line</key>
  143 + <integer>727</integer>
  144 + </dict>
  145 + <key>firstVisibleColumn</key>
  146 + <integer>0</integer>
  147 + <key>firstVisibleLine</key>
  148 + <integer>707</integer>
  149 + </dict>
  150 + <key>lib/loop/util/helpers.js</key>
  151 + <dict>
  152 + <key>caret</key>
  153 + <dict>
  154 + <key>column</key>
  155 + <integer>16</integer>
  156 + <key>line</key>
  157 + <integer>15</integer>
  158 + </dict>
  159 + <key>columnSelection</key>
  160 + <false/>
  161 + <key>firstVisibleColumn</key>
  162 + <integer>0</integer>
  163 + <key>firstVisibleLine</key>
  164 + <integer>0</integer>
  165 + <key>selectFrom</key>
  166 + <dict>
  167 + <key>column</key>
  168 + <integer>8</integer>
  169 + <key>line</key>
  170 + <integer>15</integer>
  171 + </dict>
  172 + <key>selectTo</key>
  173 + <dict>
  174 + <key>column</key>
  175 + <integer>16</integer>
  176 + <key>line</key>
  177 + <integer>15</integer>
  178 + </dict>
  179 + </dict>
  180 + <key>lib/loop/util/tree-inspector.js</key>
  181 + <dict>
  182 + <key>caret</key>
  183 + <dict>
  184 + <key>column</key>
  185 + <integer>19</integer>
  186 + <key>line</key>
  187 + <integer>14</integer>
  188 + </dict>
  189 + <key>firstVisibleColumn</key>
  190 + <integer>0</integer>
  191 + <key>firstVisibleLine</key>
  192 + <integer>0</integer>
  193 + </dict>
  194 + <key>spec/eval-spec.js</key>
  195 + <dict>
  196 + <key>caret</key>
  197 + <dict>
  198 + <key>column</key>
  199 + <integer>0</integer>
  200 + <key>line</key>
  201 + <integer>0</integer>
  202 + </dict>
  203 + <key>firstVisibleColumn</key>
  204 + <integer>0</integer>
  205 + <key>firstVisibleLine</key>
  206 + <integer>0</integer>
  207 + </dict>
  208 + <key>spec/integration/macro-spec.js</key>
  209 + <dict>
  210 + <key>caret</key>
  211 + <dict>
  212 + <key>column</key>
  213 + <integer>18</integer>
  214 + <key>line</key>
  215 + <integer>535</integer>
  216 + </dict>
  217 + <key>firstVisibleColumn</key>
  218 + <integer>0</integer>
  219 + <key>firstVisibleLine</key>
  220 + <integer>462</integer>
  221 + </dict>
  222 + <key>spec/reverse-compiler/converter-spec.js</key>
  223 + <dict>
  224 + <key>caret</key>
  225 + <dict>
  226 + <key>column</key>
  227 + <integer>30</integer>
  228 + <key>line</key>
  229 + <integer>430</integer>
  230 + </dict>
  231 + <key>firstVisibleColumn</key>
  232 + <integer>0</integer>
  233 + <key>firstVisibleLine</key>
  234 + <integer>392</integer>
  235 + </dict>
  236 + <key>spec/tree-to-uglify-tree-spec.js</key>
  237 + <dict>
  238 + <key>caret</key>
  239 + <dict>
  240 + <key>column</key>
  241 + <integer>0</integer>
  242 + <key>line</key>
  243 + <integer>0</integer>
  244 + </dict>
  245 + <key>firstVisibleColumn</key>
  246 + <integer>0</integer>
  247 + <key>firstVisibleLine</key>
  248 + <integer>0</integer>
  249 + </dict>
  250 + </dict>
  251 + <key>showFileHierarchyDrawer</key>
  252 + <true/>
  253 + <key>windowFrame</key>
  254 + <string>{{187, 30}, {1751, 1342}}</string>
  255 +</dict>
  256 +</plist>
63 spec/compile-integration-spec.js → spec/integration/general-spec.js
... ... @@ -1,6 +1,6 @@
1 1 var vows = require("vows");
2 2 var assert = require("assert");
3   -var loop = require(__dirname + "/../lib/loop");
  3 +var loop = require(__dirname + "/../../lib/loop");
4 4
5 5 vows.describe("integration spec").addBatch({
6 6 'it should be able to compile a function call': function() {
@@ -82,40 +82,6 @@ vows.describe("integration spec").addBatch({
82 82 assert.equal(loop.compile(code), '(function(x,y){console.log(x+y)})(10,20)');
83 83 },
84 84
85   - 'it should be able to handle an if statement': function() {
86   - var code = "(if x (y))";
87   - assert.equal(loop.compile(code), 'if(x){y()}');
88   - },
89   -
90   - 'it should be able to handle an if statement with no block conditions': function() {
91   - var code = "(if x)";
92   -
93   - assert.equal(loop.compile(code), 'if(x){}');
94   - },
95   -
96   - 'it should be able to handle an if statement with multiple block conditions': function() {
97   - var code = "(if x (y) (+ 10 20))";
98   -
99   - assert.equal(loop.compile(code), 'if(x){y();10+20}');
100   - },
101   -
102   - 'it should allow non list types in the conditions': function() {
103   - var code = "(if true (y))";
104   -
105   - assert.equal(loop.compile(code), 'if(true){y()}');
106   - },
107   -
108   - 'it should allow a ! in the conditions': function() {
109   - var code = "(if (! foo) (y))";
110   -
111   - assert.equal(loop.compile(code), 'if(!foo){y()}');
112   - },
113   -
114   - 'it should allow === as a comparison operator': function() {
115   - var code = "(if (=== x 10) (console.log \"foo\"))";
116   - assert.equal(loop.compile(code), 'if(x===10){console.log("foo")}');
117   - },
118   -
119 85 'it should be able to create an object from the {} function': function() {
120 86 var code = "(var (x ({})))";
121 87 assert.equal(loop.compile(code), 'var x={}');
@@ -200,33 +166,6 @@ vows.describe("integration spec").addBatch({
200 166 assert.equal(loop.compile(code), "var foo");
201 167 },
202 168
203   - 'it should not insert semicolons into an if': function() {
204   - var defaultOptions = {
205   - indent_start : 0,
206   - indent_level : 2,
207   - quote_keys : false,
208   - space_colon : true,
209   - beautify : true,
210   - ascii_only : false,
211   - inline_script: false
212   - };
213   -
214   - var code = "";
215   - code += "(function (str options)";
216   - code += " (if (=== (typeof options) 'undefined')";
217   - code += " (= options {})))";
218   -
219   - var expected = "";
220   - expected += "(function(str, options) {\n";
221   - expected += " if (typeof options === \"undefined\") {\n";
222   - expected += " options = {};\n";
223   - expected += " }\n";
224   - expected += "});";
225   -
226   - assert.equal(loop.compile(code, defaultOptions),
227   - expected);
228   - },
229   -
230 169 'it should use || as OR': function() {
231 170 assert.equal(loop.compile("(|| a b)"),
232 171 "a||b");
191 spec/integration/if-cond-spec.js
... ... @@ -0,0 +1,191 @@
  1 +var vows = require("vows");
  2 +var assert = require("assert");
  3 +var loop = require(__dirname + "/../../lib/loop");
  4 +
  5 +var defaultOptions = {
  6 + indent_start : 0,
  7 + indent_level : 2,
  8 + quote_keys : false,
  9 + space_colon : true,
  10 + beautify : true,
  11 + ascii_only : false,
  12 + inline_script: false
  13 +};
  14 +
  15 +vows.describe("if + cond").addBatch({
  16 + 'it should be able to handle an if statement': function() {
  17 + var code = "(if x (y))";
  18 + assert.equal(loop.compile(code), 'if(x){y()}');
  19 + },
  20 +
  21 + 'it should be able to handle an if statement with no block conditions': function() {
  22 + var code = "(if x)";
  23 +
  24 + assert.equal(loop.compile(code), 'if(x){}');
  25 + },
  26 +
  27 + 'it should be able to handle an if statement with multiple block conditions': function() {
  28 + var code = "(if x (y) (+ 10 20))";
  29 +
  30 + assert.equal(loop.compile(code), 'if(x){y();10+20}');
  31 + },
  32 +
  33 + 'it should allow non list types in the conditions': function() {
  34 + var code = "(if true (y))";
  35 +
  36 + assert.equal(loop.compile(code), 'if(true){y()}');
  37 + },
  38 +
  39 + 'it should allow a ! in the conditions': function() {
  40 + var code = "(if (! foo) (y))";
  41 +
  42 + assert.equal(loop.compile(code), 'if(!foo){y()}');
  43 + },
  44 +
  45 + 'it should allow === as a comparison operator': function() {
  46 + var code = "(if (=== x 10) (console.log \"foo\"))";
  47 + assert.equal(loop.compile(code), 'if(x===10){console.log("foo")}');
  48 + },
  49 +
  50 + 'it should not insert semicolons into an if': function() {
  51 + var code = "";
  52 + code += "(function (str options)";
  53 + code += " (if (=== (typeof options) 'undefined')";
  54 + code += " (= options {})))";
  55 +
  56 + var expected = "";
  57 + expected += "(function(str, options) {\n";
  58 + expected += " if (typeof options === \"undefined\") {\n";
  59 + expected += " options = {};\n";
  60 + expected += " }\n";
  61 + expected += "});";
  62 +
  63 + assert.equal(loop.compile(code, defaultOptions),
  64 + expected);
  65 + },
  66 +
  67 + 'it should allow a cond statement': function() {
  68 + var code = '';
  69 + code += '(cond';
  70 + code += ' (true (console.log true)))';
  71 +
  72 + var expected = '';
  73 + expected += 'if (true) {\n';
  74 + expected += ' console.log(true);\n';
  75 + expected += '};';
  76 +
  77 + assert.equal(loop.compile(code, defaultOptions), expected);
  78 + },
  79 +
  80 + 'it should allow an else if in a cond': function() {
  81 + var code = '';
  82 + code += '(cond';
  83 + code += ' (true (console.log 1))';
  84 + code += ' (true (console.log 2)))';
  85 +
  86 + var expected = '';
  87 + expected += 'if (true) {\n';
  88 + expected += ' console.log(1);\n';
  89 + expected += '} else if (true) {\n';
  90 + expected += ' console.log(2);\n';
  91 + expected += '};';
  92 +
  93 + assert.equal(loop.compile(code, defaultOptions), expected);
  94 + },
  95 +
  96 + 'it should allow multiple else ifs in a cond': function() {
  97 + var code = '';
  98 + code += '(cond';
  99 + code += ' (true (console.log 1))';
  100 + code += ' (true (console.log 2))';
  101 + code += ' (true (console.log 3)))';
  102 +
  103 + var expected = '';
  104 + expected += 'if (true) {\n';
  105 + expected += ' console.log(1);\n';
  106 + expected += '} else if (true) {\n';
  107 + expected += ' console.log(2);\n';
  108 + expected += '} else if (true) {\n';
  109 + expected += ' console.log(3);\n';
  110 + expected += '};';
  111 +
  112 + assert.equal(loop.compile(code, defaultOptions), expected);
  113 + },
  114 +
  115 + 'it should use the correct arguments in a cond': function() {
  116 + var code = '';
  117 + code += '(cond';
  118 + code += ' ((=== x 1)';
  119 + code += ' (console.log "1")';
  120 + code += ' (console.log "2")))';
  121 +
  122 + var expected = '';
  123 + expected += 'if (x === 1) {\n';
  124 + expected += ' console.log("1");\n';
  125 + expected += ' console.log("2");\n';
  126 + expected += '};';
  127 +
  128 + assert.equal(loop.compile(code, defaultOptions), expected);
  129 + },
  130 +
  131 + 'it should allow a number in a cond': function() {
  132 + var code = '';
  133 + code += '(cond';
  134 + code += ' (1 (console.log "1")))';
  135 +
  136 + var expected = '';
  137 + expected += 'if (1) {\n';
  138 + expected += ' console.log("1");\n';
  139 + expected += '};';
  140 +
  141 + assert.equal(loop.compile(code, defaultOptions), expected);
  142 + },
  143 +
  144 + 'it should allow an else to a cond\'s if': function() {
  145 + var code = '';
  146 + code += '(cond';
  147 + code += ' (false (console.log 1))';
  148 + code += ' (else (console.log 2)))';
  149 +
  150 + var expected = '';
  151 + expected += 'if (false) {\n';
  152 + expected += ' console.log(1);\n';
  153 + expected += '} else {\n';
  154 + expected += ' console.log(2);\n';
  155 + expected += '};';
  156 +
  157 + assert.equal(loop.compile(code, defaultOptions), expected);
  158 + },
  159 +
  160 + 'it should error with an else if it isn\'t the last clause': function() {
  161 + var code = '';
  162 + code += '(cond';
  163 + code += ' (foo (console.log 1))';
  164 + code += ' (else (console.log 2))';
  165 + code += ' (bar (console.log 3))';
  166 + code += ' (baz (console.log 4)))';
  167 +
  168 + assert.throws(function() {
  169 + loop.compile(code, defaultOptions);
  170 + });
  171 + },
  172 +
  173 + 'it should be ok if cond has an empty body': function() {
  174 + var code = '';
  175 + code += '(cond';
  176 + code += ' (foo))';
  177 +
  178 + var expected = '';
  179 + expected += 'if (foo) {};';
  180 +
  181 + assert.equal(loop.compile(code, defaultOptions), expected);
  182 + },
  183 +
  184 + 'it should raise an error if cond has no body': function() {
  185 + var code = '';
  186 + code += '(cond)';
  187 + assert.throws(function() {
  188 + loop.compile(code);
  189 + });
  190 + }
  191 +}).export(module);
4 spec/macro-matching-spec.js → spec/macros/macro-matching-spec.js
... ... @@ -1,7 +1,7 @@
1 1 var vows = require("vows");
2 2 var assert = require("assert");
3   -var loop = require(__dirname + "/../lib/loop");
4   -var macroCompiler = require(__dirname + "/../lib/loop/macro-compiler");
  3 +var loop = require(__dirname + "/../../lib/loop");
  4 +var macroCompiler = require(__dirname + "/../../lib/loop/macro-compiler");
5 5
6 6 var parseCode = function(code) {
7 7 return loop.parse(code)[0];
0  spec/integration/binary-spec.js → spec/uglify-compiling/binary-spec.js
File renamed without changes
0  spec/integration/booleans-spec.js → spec/uglify-compiling/booleans-spec.js
File renamed without changes
0  spec/integration/keywords-spec.js → spec/uglify-compiling/keywords-spec.js
File renamed without changes
0  spec/integration/number-spec.js → spec/uglify-compiling/number-spec.js
File renamed without changes
2  spec/tree-to-uglify-tree-spec.js → spec/uglify-compiling/tree-to-uglify-tree-spec.js
... ... @@ -1,6 +1,6 @@
1 1 var vows = require("vows");
2 2 var assert = require("assert");
3   -var loop = require(__dirname + "/../lib/loop");
  3 +var loop = require(__dirname + "/../../lib/loop");
4 4
5 5 vows.describe("phase 3: transform from eval'ed syntax into uglifyjs parse tree").addBatch({
6 6 'it should eval a function call': function() {
0  spec/integration/unary-spec.js → spec/uglify-compiling/unary-spec.js
File renamed without changes

No commit comments for this range

Something went wrong with that request. Please try again.