瀏覽代碼

Refactor maths functions to be in the maths library

Matt Coles 9 年之前
父節點
當前提交
90de0f95cb
共有 6 個文件被更改,包括 35 次插入35 次删除
  1. 4 4
      adventofcode/advent01-1.mc
  2. 3 3
      adventofcode/advent02-1.mc
  3. 3 3
      examples/examples.mc
  4. 4 4
      examples/fizzbuzz.mc
  5. 20 0
      libjs/maths.js
  6. 1 21
      libjs/stdlib.js

+ 4 - 4
adventofcode/advent01-1.mc

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

+ 3 - 3
adventofcode/advent02-1.mc

10
 (repeat (str::length input)
10
 (repeat (str::length input)
11
   (assign current (str::charAtIndex input i))
11
   (assign current (str::charAtIndex input i))
12
   (if (eq current "x") 
12
   (if (eq current "x") 
13
-    (assign currNum (add currNum 1)) |
13
+    (assign currNum (maths::add currNum 1)) |
14
     (if (eq current "\n") 
14
     (if (eq current "\n") 
15
       (assign currNum 1) 
15
       (assign currNum 1) 
16
-      (assign result (add result (add (add (multiply 2 (multiply resultone resulttwo)) (add (multiply 2 (multiply resulttwo resultthree)) (multiply 2 (multiply resultone resultthree)))) (maths::min (multiply resultone resulttwo) (multiply resulttwo resultthree) (multiply resultthree resultone)))))
16
+      (assign result (maths::add result (maths::add (maths::add (maths::multiply 2 (maths::multiply resultone resulttwo)) (maths::add (maths::multiply 2 (maths::multiply resulttwo resultthree)) (maths::multiply 2 (maths::multiply resultone resultthree)))) (maths::min (maths::multiply resultone resulttwo) (maths::multiply resulttwo resultthree) (maths::multiply resultthree resultone)))))
17
       (assign resultone "")
17
       (assign resultone "")
18
       (assign resulttwo "")
18
       (assign resulttwo "")
19
       (assign resultthree "") | 
19
       (assign resultthree "") | 
22
         (if (eq currNum 3) (assign resultthree (str::concat resultthree current)))
22
         (if (eq currNum 3) (assign resultthree (str::concat resultthree current)))
23
     )
23
     )
24
   )
24
   )
25
-  (assign i (add i 1))
25
+  (assign i (maths::add i 1))
26
 )
26
 )
27
 (log result)
27
 (log result)

+ 3 - 3
examples/examples.mc

1
 ; This is a test program
1
 ; This is a test program
2
-(include str)
2
+(include str maths)
3
 (assign twelve 12) ; This assigns the variable twelve, to the number literal 12
3
 (assign twelve 12) ; This assigns the variable twelve, to the number literal 12
4
-(assign myvar (add twelve (subtract 6 2))) ; This assigns the variable myvar, to the result of adding the variable twelve to the result of subtracting 2 from 6
4
+(assign myvar (maths::add twelve (maths::subtract 6 2))) ; This assigns the variable myvar, to the result of adding the variable twelve to the result of subtracting 2 from 6
5
 (log myvar) ; This logs the value of myvar
5
 (log myvar) ; This logs the value of myvar
6
 (log 6) ; This logs the number literal 6
6
 (log 6) ; This logs the number literal 6
7
 (assign twelve myvar) ; This reassigns the variable twelve to the value of the variable myvar
7
 (assign twelve myvar) ; This reassigns the variable twelve to the value of the variable myvar
12
   (log twelve) 
12
   (log twelve) 
13
   (log 6) 
13
   (log 6) 
14
   (log 6) 
14
   (log 6) 
15
-  (assign scopelol (add twelve 5)) 
15
+  (assign scopelol (maths::add twelve 5)) 
16
   (log scopelol)
16
   (log scopelol)
17
 )
17
 )
18
 (def argTest (log $1) (log $2)) ; Functions take an unlimited number of arguments that can be referred to by $n
18
 (def argTest (log $1) (log $2)) ; Functions take an unlimited number of arguments that can be referred to by $n

+ 4 - 4
examples/fizzbuzz.mc

1
-(include str)
1
+(include str maths)
2
 (def fizzbuzz
2
 (def fizzbuzz
3
   (assign i 1)
3
   (assign i 1)
4
   (repeat $1
4
   (repeat $1
5
     (assign result "")
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")))
6
+    (if (eq (maths::modulo i 3) 0) (assign result (str::concat result "Fizz")))
7
+    (if (eq (maths::modulo i 5) 0) (assign result (str::concat result "Buzz")))
8
     (if (eq result "") (assign result i))
8
     (if (eq result "") (assign result i))
9
     (log result)
9
     (log result)
10
-    (assign i (add i 1))
10
+    (assign i (maths::add i 1))
11
   )
11
   )
12
 1)
12
 1)
13
 
13
 

+ 20 - 0
libjs/maths.js

6
     return {
6
     return {
7
       value: Math.min.apply(null, argArr.map((arg) => arg.value))
7
       value: Math.min.apply(null, argArr.map((arg) => arg.value))
8
     }
8
     }
9
+  },
10
+  add: function (arg1, arg2) {
11
+    return {
12
+      value: (arg1.value + arg2.value)
13
+    }
14
+  },
15
+  subtract: function (arg1, arg2) {
16
+    return {
17
+      value: (arg1.value - arg2.value)
18
+    }
19
+  },
20
+  multiply: function (arg1, arg2) {
21
+    return {
22
+      value: (arg1.value * arg2.value)
23
+    }
24
+  },
25
+  modulo: function (arg1, arg2) {
26
+    return {
27
+      value: (arg1.value % arg2.value)
28
+    }
9
   }
29
   }
10
 }
30
 }
11
 
31
 

+ 1 - 21
libjs/stdlib.js

9
     }
9
     }
10
     global_obj[ref.name] = value.value
10
     global_obj[ref.name] = value.value
11
   },
11
   },
12
-  add: function (arg1, arg2) {
13
-    return {
14
-      value: (arg1.value + arg2.value)
15
-    }
16
-  },
17
-  subtract: function (arg1, arg2) {
18
-    return {
19
-      value: (arg1.value - arg2.value)
20
-    }
21
-  },
22
   log: function (ref) {
12
   log: function (ref) {
23
-    console.log(JSON.stringify(ref.value))
13
+    console.log(ref.value)
24
   },
14
   },
25
   ref: function (refname) {
15
   ref: function (refname) {
26
     return {
16
     return {
68
   neg: function (pred) {
58
   neg: function (pred) {
69
     return !pred
59
     return !pred
70
   },
60
   },
71
-  modulo: function (arg1, arg2) {
72
-    return {
73
-      value: (arg1.value % arg2.value)
74
-    }
75
-  },
76
-  multiply: function (arg1, arg2) {
77
-    return {
78
-      value: (arg1.value * arg2.value)
79
-    }
80
-  }
81
 }
61
 }
82
 
62
 
83
 
63