Merge pull request #117 from hellerve/misc

Miscellaneous fixes
This commit is contained in:
Erik Svedäng 2017-11-29 15:50:12 +01:00 committed by GitHub
commit 7d3931c46a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 51 additions and 1 deletions

View File

@ -7,6 +7,11 @@
(set! &total (f &total (nth xs i))))
total)))
(defn first [a]
@(Array.nth a 0))
(defn last [a]
@(Array.nth a (Int.dec (Array.count a))))
)
;; BUGS! These function definitions will not create the required typedef for Array, for instance:

View File

@ -1,4 +1,5 @@
(defmodule Char
(register = (Fn [Char Char] Bool))
(register str (Fn [Char] String))
(register to-int (Fn [Char] Int))
(register from-int (Fn [Int] Char))

View File

@ -26,5 +26,7 @@
(defn max [a b] (if (> a b) a b))
(defn min [a b] (if (< a b) a b))
)
(defn even? [a] (= (mod a 2) 0))
(defn odd? [a] (not (even? a)))
)

View File

@ -30,4 +30,7 @@
(defn max [a b] (if (> a b) a b))
(defn min [a b] (if (< a b) a b))
(defn even? [a] (= (mod a 2l) 0l))
(defn odd? [a] (not (even? a)))
)

View File

@ -68,3 +68,6 @@
(defmacro ==> [:rest forms]
(thread-last-internal forms))
(defmacro swap! [x y]
(list 'let (array 'tmp y) (list 'do (list 'set! &y x) (list 'set! &x 'tmp))))

View File

@ -261,6 +261,10 @@ string String_from_MINUS_chars(Array a) {
return s;
}
bool Char__EQ_(char a, char b) {
return a == b;
}
string Char_str(char c) {
char *buffer = CARP_MALLOC(3);
snprintf(buffer, 3, "\\%c", c);

16
test/array.carp Normal file
View File

@ -0,0 +1,16 @@
(load "Test.carp")
(use Array)
(use Test)
(defn main []
(with-test test
(assert-equal test
1
(first &[1 2 3])
"first works as expected")
(assert-equal test
\c
(last &[\a \b \c])
"last works as expected")
(print-test-results test)))

View File

@ -17,4 +17,12 @@
1
(abs -1)
"abs works as expected")
(assert-equal test
false
(even? 3)
"even? works as expected")
(assert-equal test
true
(odd? 3)
"odd? works as expected")
(print-test-results test)))

View File

@ -17,4 +17,12 @@
1l
(abs -1l)
"abs works as expected")
(assert-equal test
false
(even? 3l)
"even? works as expected")
(assert-equal test
true
(odd? 3l)
"odd? works as expected")
(print-test-results test)))