Kaynağa Gözat

Add some advent of code testing and some more modules

Matt Coles 9 yıl önce
ebeveyn
işleme
530d569455
3 değiştirilmiş dosya ile 52 ekleme ve 0 silme
  1. 13 0
      adventofcode/advent01-1.mc
  2. 29 0
      libjs/fs.js
  3. 10 0
      libjs/str.js

+ 13 - 0
adventofcode/advent01-1.mc

1
+; Requires the input for Advent of Code(http://adventofcode.com/) Day 1
2
+(log result)
3
+(include str fs)
4
+(assign input (fs::readIn "advent.txt"))
5
+(assign i 0)
6
+(assign result 0)
7
+(repeat (str::length input)
8
+  (assign current (str::charAtIndex input i))
9
+  (if (eq current "(") (assign result (add result 1)))
10
+  (if (eq current ")") (assign result (subtract result 1)))
11
+  (assign i (add i 1))
12
+)
13
+(log result)

+ 29 - 0
libjs/fs.js

1
+var fs = require("fs")
2
+
3
+const builtins = {
4
+  readIn: function (path) {
5
+    let str = fs.readFileSync(path.value, { encoding: 'utf-8' })
6
+    return {
7
+      value: str
8
+    }
9
+  }
10
+}
11
+
12
+const my_handler = {
13
+  get: function (target, prop) {
14
+    let methods = Object.keys(builtins)
15
+    if (methods.includes(prop)) {
16
+      return builtins[prop]
17
+    } else {
18
+      console.error("Undefined function call! No such function: ", prop)
19
+    }
20
+  }, 
21
+}
22
+
23
+const proxy_obj = new Proxy({}, my_handler)
24
+let _
25
+
26
+module.exports = function (self) {
27
+  _ = self
28
+  return proxy_obj
29
+}

+ 10 - 0
libjs/str.js

7
     return {
7
     return {
8
       value: str
8
       value: str
9
     }
9
     }
10
+  },
11
+  length: function (str) {
12
+    return {
13
+      value: str.value.length
14
+    }
15
+  }, 
16
+  charAtIndex: function (str, i) {
17
+    return {
18
+      value: str.value.charAt(i.value)
19
+    }
10
   }
20
   }
11
 }
21
 }
12
 
22