ソースを参照

Javascript scope is hard

Matt Coles 9 年 前
コミット
110b9b7587
共有4 個のファイルを変更した22 個の追加16 個の削除を含む
  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
-(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

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

+ 1 - 1
lib/str.js

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