Sfoglia il codice sorgente

Javascript scope is hard

Matt Coles 9 anni fa
parent
commit
110b9b7587
4 ha cambiato i file con 22 aggiunte e 16 eliminazioni
  1. 0 14
      fizzbuzz.mc
  2. 20 0
      fizzbuzz.mc.js
  3. 1 1
      lib/stdlib.js
  4. 1 1
      lib/str.js

+ 0 - 14
fizzbuzz.mc

@@ -1,14 +0,0 @@
1
-(include str)
2
-(def fizzbuzz
3
-  (assign i 1)
4
-  (repeat $1
5
-    (assign result "")
6
-    (if (eq (modulo i 3) 0) (assign result (str::concat result "Fizz")))
7
-    (if (eq (modulo i 5) 0) (assign result (str::concat result "Buzz")))
8
-    (if (eq result "") (assign result i))
9
-    (log result)
10
-    (assign i (add i 1))
11
-  )
12
-)
13
-
14
-(fizzbuzz 100)

+ 20 - 0
fizzbuzz.mc.js

@@ -0,0 +1,20 @@
1
+var _ = require("./lib/stdlib.js")(this)
2
+var _str = require("./lib/str.js")(this);
3
+_.def(_.ref('fizzbuzz'), function() { 
4
+_.assign(_.ref('i'), {value: 1});
5
+_.repeat(arguments[0], function() { 
6
+_.assign(_.ref('result'), { value: '' });
7
+_.if(_.eq(_.modulo(_.ref('i'), {value: 3}), {value: 0}), function() { 
8
+_.assign(_.ref('result'), _str.concat(_.ref('result'), { value: 'Fizz' }));
9
+});
10
+_.if(_.eq(_.modulo(_.ref('i'), {value: 5}), {value: 0}), function() { 
11
+_.assign(_.ref('result'), _str.concat(_.ref('result'), { value: 'Buzz' }));
12
+});
13
+_.if(_.eq(_.ref('result'), { value: '' }), function() { 
14
+_.assign(_.ref('result'), _.ref('i'));
15
+});
16
+_.log(_.ref('result'));
17
+_.assign(_.ref('i'), _.add(_.ref('i'), {value: 1}));
18
+});
19
+});
20
+_.fizzbuzz({value: 100});

+ 1 - 1
lib/stdlib.js

@@ -36,7 +36,7 @@ const builtins = {
36 36
     function_defs[prop.name] = body;
37 37
   },
38 38
   repeat: function (amount, body) {
39
-    for (ii = 0 ; ii < amount.value; ii++) {
39
+    for (let i = 0 ; i < amount.value; i++) {
40 40
         body.bind(_)() 
41 41
     }
42 42
   },

+ 1 - 1
lib/str.js

@@ -4,7 +4,7 @@ const warnings = {}
4 4
 const builtins = {
5 5
   concat: function () {
6 6
     let str = ""
7
-    for (i = 0; i < arguments.length; i++) {
7
+    for (let i = 0; i < arguments.length; i++) {
8 8
       str += arguments[i].value
9 9
     }
10 10
     return {