Skip to content
This repository
Browse code

Merge pull request #49 from matthewp/defvar

Fixes issue #48, double semi-colons when defvar used within defun
  • Loading branch information...
commit dfe0c27e2807b328b99daaa82015db92a45c6c7c 2 parents de403c2 + 2a583b4
Jacob Rothstein authored October 17, 2012
77  lib/sibilant.js
@@ -11,9 +11,9 @@ var bulkMap = (function(arr, fn) {
11 11
   // arr:required fn:required
12 12
   var index = 0,
13 13
       groupSize = fn.length,
14  
-      retArr = [  ];;
  14
+      retArr = [  ];
15 15
   (function() {
16  
-    var __returnValue__ = undefined;;
  16
+    var __returnValue__ = undefined;
17 17
     while ((index < arr.length)) {
18 18
       __returnValue__ = (function() {
19 19
         retArr.push(fn.apply(undefined, arr.slice(index, (index + groupSize))));
@@ -27,7 +27,7 @@ var bulkMap = (function(arr, fn) {
27 27
 
28 28
 var inject = (function(start, items, fn) {
29 29
   // start:required items:required fn:required
30  
-  var value = start;;
  30
+  var value = start;
31 31
   (function() {
32 32
     if ((items) && (items).constructor.name === "Array") {
33 33
       return items.forEach((function(item, index) {
@@ -65,9 +65,9 @@ var detect = (function(items, fn) {
65 65
   // items:required fn:required
66 66
   var returnItem = undefined,
67 67
       index = 0,
68  
-      items = items;;
  68
+      items = items;
69 69
   return (function() {
70  
-    var __returnValue__ = undefined;;
  70
+    var __returnValue__ = undefined;
71 71
     while ((!((items.length === index) || returnItem))) {
72 72
       __returnValue__ = (function() {
73 73
         (function() {
@@ -84,7 +84,7 @@ var detect = (function(items, fn) {
84 84
 
85 85
 var reject = (function(items, fn) {
86 86
   // items:required fn:required
87  
-  var args = [ items, fn ];;
  87
+  var args = [ items, fn ];
88 88
   return select(items, (function() {
89 89
     return (!fn.apply(undefined, arguments));
90 90
   }));
@@ -116,13 +116,13 @@ var flatten = (function(items) {
116 116
 
117 117
 
118 118
 sibilant.packageInfo = (function() {
119  
-  var fs = require("fs");;
  119
+  var fs = require("fs");
120 120
   return JSON.parse(fs.readFileSync((__dirname + "/../package.json")));
121 121
 });
122 122
 
123 123
 sibilant.versionString = (function() {
124 124
   var package = sibilant.packageInfo(),
125  
-      path = require("path");;
  125
+      path = require("path");
126 126
   return (package.name + " version " + package.version + "\n(at " + path.join(__dirname, "..") + ")");
127 127
 });
128 128
 
@@ -138,12 +138,12 @@ var withDirAndFile = (function(dir, file, fn) {
138 138
   var before = {
139 139
     dir: sibilant.dir,
140 140
     file: sibilant.file
141  
-  };;
  141
+  };
142 142
   (sibilant)["dir"] = dir;
143  
-  (sibilant)["file"] = file;;
144  
-  var retval = fn();;
  143
+  (sibilant)["file"] = file;
  144
+  var retval = fn();
145 145
   (sibilant)["dir"] = before.dir;
146  
-  (sibilant)["file"] = before.file;;
  146
+  (sibilant)["file"] = before.file;
147 147
   return retval;
148 148
 });
149 149
 
@@ -160,7 +160,7 @@ sibilant.version = (function() {
160 160
 
161 161
 (require.extensions)[".sibilant"] = (function(module, filename) {
162 162
   // module:required filename:required
163  
-  var content = sibilant.translateFile(filename);;
  163
+  var content = sibilant.translateFile(filename);
164 164
   return module._compile(content, filename);
165 165
 });
166 166
 sibilant.include = (function(file) {
@@ -175,7 +175,7 @@ sibilant.include = (function(file) {
175 175
       return file = (sibilant.dir + "/" + file);
176 176
     };
177 177
   })();
178  
-  var resolved = require.resolve(file);;
  178
+  var resolved = require.resolve(file);
179 179
   return sibilant.translateFile(resolved);
180 180
 });
181 181
 
@@ -197,14 +197,14 @@ var tokenize = sibilant.tokenize = (function(string) {
197 197
   // string:required
198 198
   var tokens = [  ],
199 199
       parseStack = [ tokens ],
200  
-      specials = [  ];;
  200
+      specials = [  ];
201 201
   var acceptToken = (function(token) {
202 202
     // token:required
203 203
     return (parseStack)[0].push(token);
204 204
   });
205 205
   ;
206 206
   var increaseNesting = (function() {
207  
-    var newArr = [  ];;
  207
+    var newArr = [  ];
208 208
     acceptToken(newArr);
209 209
     return parseStack.unshift(newArr);
210 210
   });
@@ -222,7 +222,7 @@ var tokenize = sibilant.tokenize = (function(string) {
222 222
   var handleToken = (function(token) {
223 223
     // token:required
224 224
     var special = (token)[0],
225  
-        token = token;;
  225
+        token = token;
226 226
     (function() {
227 227
       if ((special === "'")) {
228 228
         token = token.slice(1);
@@ -272,7 +272,7 @@ var tokenize = sibilant.tokenize = (function(string) {
272 272
     // x:required
273 273
     return (sibilant.tokens)[x];
274 274
   })),
275  
-      masterRegex = (new RegExp((orderedRegexen).join("|"), "g"));;
  275
+      masterRegex = (new RegExp((orderedRegexen).join("|"), "g"));
276 276
   string // chain
277 277
     .match(masterRegex)
278 278
     .forEach(handleToken)
@@ -301,7 +301,7 @@ var constructHash = (function(arrayOfArrays) {
301 301
   // arrayOfArrays:required
302 302
   return inject({  }, arrayOfArrays, (function(object, item) {
303 303
     // object:required item:required
304  
-    (object)[(item)[0]] = (object)[(item)[1]];;
  304
+    (object)[(item)[0]] = (object)[(item)[1]];
305 305
     return object;
306 306
   }));
307 307
 });
@@ -310,7 +310,7 @@ var macros = {  };
310 310
 (sibilant)["macros"] = macros;
311 311
 (macros)["return"] = (function(token) {
312 312
   // token:required
313  
-  var defaultReturn = ("return " + translate(token));;
  313
+  var defaultReturn = ("return " + translate(token));
314 314
   return (function() {
315 315
     if ((token) && (token).constructor.name === "Array") {
316 316
       return (function() {
@@ -373,8 +373,8 @@ macros.progn = (function(body) {
373 373
   // body:rest
374 374
   var body = Array.prototype.slice.call(arguments, 0);
375 375
   
376  
-  var lastIndex = Math.max(0, (body.length - 1));;
377  
-  (body)[lastIndex] = [ "return", (body)[lastIndex] ];;
  376
+  var lastIndex = Math.max(0, (body.length - 1));
  377
+  (body)[lastIndex] = [ "return", (body)[lastIndex] ];
378 378
   return (map(body, (function(arg) {
379 379
     // arg:required
380 380
     return (translate(arg) + ";");
@@ -399,7 +399,7 @@ macros.defun = (function(fnName, argsAndBody) {
399 399
     } else {
400 400
       return "var ";
401 401
     };
402  
-  })();;
  402
+  })();
403 403
   return (start + fnNameTr + " = " + macros.lambda.apply(undefined, argsAndBody) + ";\n");
404 404
 });
405 405
 
@@ -408,7 +408,7 @@ macros.defmacro = (function(name, argsAndBody) {
408 408
   var argsAndBody = Array.prototype.slice.call(arguments, 1);
409 409
   
410 410
   var js = macros.lambda.apply(undefined, argsAndBody),
411  
-      name = translate(name);;
  411
+      name = translate(name);
412 412
   (function() {
413 413
     try {
414 414
       return (macros)[name] = eval(js);;
@@ -429,7 +429,7 @@ macros.concat = (function(args) {
429 429
 var transformArgs = (function(arglist) {
430 430
   // arglist:required
431 431
   var last = undefined,
432  
-      args = [  ];;
  432
+      args = [  ];
433 433
   arglist.forEach((function(arg) {
434 434
     // arg:required
435 435
     return (function() {
@@ -451,7 +451,7 @@ var transformArgs = (function(arglist) {
451 451
 
452 452
 macros.reverse = (function(arr) {
453 453
   // arr:required
454  
-  var reversed = [  ];;
  454
+  var reversed = [  ];
455 455
   arr.forEach((function(item) {
456 456
     // item:required
457 457
     return reversed.unshift(item);
@@ -463,7 +463,7 @@ var reverse = macros.reverse;
463 463
 var buildArgsString = (function(args, rest) {
464 464
   // args:required rest:required
465 465
   var argsString = "",
466  
-      optionalCount = 0;;
  466
+      optionalCount = 0;
467 467
   args.forEach((function(arg, optionIndex) {
468 468
     // arg:required optionIndex:required
469 469
     return (function() {
@@ -513,8 +513,8 @@ macros.lambda = (function(arglist, body) {
513 513
     // arg:required
514 514
     return ("rest" === (arg)[0]);
515 515
   })))[0],
516  
-      docString = undefined;;
517  
-  (body)[(body.length - 1)] = [ "return", (body)[(body.length - 1)] ];;
  516
+      docString = undefined;
  517
+  (body)[(body.length - 1)] = [ "return", (body)[(body.length - 1)] ];
518 518
   (function() {
519 519
     if (((typeof((body)[0]) === "string") && (body)[0].match(/^".*"$/))) {
520 520
       return docString = ("/* " + eval(body.shift()) + " */\n");
@@ -528,13 +528,20 @@ macros.lambda = (function(arglist, body) {
528 528
     };
529 529
   })(),
530 530
       argsString = buildArgsString(noRestArgs, rest),
531  
-      commentString = buildCommentString(args);;
  531
+      commentString = buildCommentString(args);
532 532
   return ("(function(" + (map(args, (function(arg) {
533 533
     // arg:required
534 534
     return translate((arg)[1]);
535 535
   }))).join(", ") + ") {" + indent(commentString, docString, argsString, (map(body, (function(stmt) {
536 536
     // stmt:required
537  
-    return (translate(stmt) + ";");
  537
+    var tstmt = translate(stmt);
  538
+    return (tstmt + (function() {
  539
+      if (((tstmt.slice(-1))[0] === ";")) {
  540
+        return "";
  541
+      } else {
  542
+        return ";";
  543
+      };
  544
+    })());
538 545
   }))).join("\n")) + "})");
539 546
 });
540 547
 
@@ -567,7 +574,7 @@ macros.hash = (function(pairs) {
567 574
   var pairStrings = bulkMap(pairs, (function(key, value) {
568 575
     // key:required value:required
569 576
     return (translate(key) + ": " + translate(value));
570  
-  }));;
  577
+  }));
571 578
   return (function() {
572 579
     if ((1 >= pairStrings.length)) {
573 580
       return ("{ " + (pairStrings).join(", ") + " }");
@@ -591,7 +598,7 @@ var literal = (function(string) {
591 598
 
592 599
 var translate = (function(token, hint) {
593 600
   // token:required hint:required
594  
-  var hint = hint;;
  601
+  var hint = hint;
595 602
   (function() {
596 603
     if ((hint && typeof((macros)[hint]) === 'undefined')) {
597 604
       return hint = undefined;
@@ -652,10 +659,10 @@ var translate = (function(token, hint) {
652 659
 (sibilant)["translate"] = translate;
653 660
 var translateAll = (function(contents) {
654 661
   // contents:required
655  
-  var buffer = "";;
  662
+  var buffer = "";
656 663
   tokenize(contents).forEach((function(token) {
657 664
     // token:required
658  
-    var line = translate(token, "statement");;
  665
+    var line = translate(token, "statement");
659 666
     return (function() {
660 667
       if (line) {
661 668
         return buffer = (buffer + line + "\n");
4  src/core.sibilant
@@ -264,7 +264,9 @@
264 264
                   (join "\n"
265 265
                         (map body
266 266
                              (lambda (stmt)
267  
-                               (concat (translate stmt) ";")))))
  267
+                               (defvar tstmt (translate stmt))
  268
+                               (concat tstmt 
  269
+                                 (if (= (last tstmt) ";") "" ";"))))))
268 270
           "})"))
269 271
 
270 272
 
5  test/defvar.sibilant
... ...
@@ -0,0 +1,5 @@
  1
+(defun alert-hello (planet-name)
  2
+  (defvar message (concat "hello " planet-name))
  3
+    message)
  4
+
  5
+(defvar foo (concat "hi " "there"))

0 notes on commit dfe0c27

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