List literals
This commit is contained in:
@@ -67,6 +67,19 @@ class Kiss {
|
|||||||
pos: Context.currentPos(),
|
pos: Context.currentPos(),
|
||||||
expr: ECall(readerExpToHaxeExpr(func, specialForms), [for (bodyExp in body) readerExpToHaxeExpr(bodyExp, specialForms)])
|
expr: ECall(readerExpToHaxeExpr(func, specialForms), [for (bodyExp in body) readerExpToHaxeExpr(bodyExp, specialForms)])
|
||||||
};
|
};
|
||||||
|
case List(elements):
|
||||||
|
{
|
||||||
|
pos: Context.currentPos(),
|
||||||
|
expr: ENew({
|
||||||
|
pack: ["kiss"],
|
||||||
|
name: "List"
|
||||||
|
}, [
|
||||||
|
{
|
||||||
|
pos: Context.currentPos(),
|
||||||
|
expr: EArrayDecl([for (elementExp in elements) readerExpToHaxeExpr(elementExp, specialForms)])
|
||||||
|
}
|
||||||
|
])
|
||||||
|
}
|
||||||
case RawHaxe(code):
|
case RawHaxe(code):
|
||||||
Context.parse(code, Context.currentPos());
|
Context.parse(code, Context.currentPos());
|
||||||
default:
|
default:
|
||||||
|
|||||||
52
src/kiss/List.hx
Normal file
52
src/kiss/List.hx
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package kiss;
|
||||||
|
|
||||||
|
@:forward(length, concat, contains, copy, filter, indexOf, // insert is re-implemented with negative index support
|
||||||
|
iterator, join, keyValueIterator,
|
||||||
|
lastIndexOf, map, pop, push, remove, resize, reverse, shift, // slice is re-implemented with negative index support
|
||||||
|
sort,
|
||||||
|
// splice is re-implemented with negative index support,
|
||||||
|
toString, unshift)
|
||||||
|
abstract List<T>(Array<T>) from Array<T> to Array<T> {
|
||||||
|
public inline function new(a:Array<T>) {
|
||||||
|
this = a;
|
||||||
|
}
|
||||||
|
|
||||||
|
@:from
|
||||||
|
static public function fromArray<T>(a:Array<T>) {
|
||||||
|
return new List<T>(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
@:to
|
||||||
|
public function toArray<T>() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline function realIndex(idx:Int) {
|
||||||
|
return if (idx < 0) this.length + idx else idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
@:arrayAccess
|
||||||
|
public inline function get<T>(idx:Int):Null<T> {
|
||||||
|
return this[realIndex(idx)];
|
||||||
|
}
|
||||||
|
|
||||||
|
@:arrayAccess
|
||||||
|
public inline function arrayWrite(idx:Int, v:T):T {
|
||||||
|
this[realIndex(idx)] = v;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
function insert(idx:Int, v:T) {
|
||||||
|
this.insert(realIndex(idx), v);
|
||||||
|
}
|
||||||
|
|
||||||
|
function slice(start:Int, ?end:Int) {
|
||||||
|
if (end == null)
|
||||||
|
end = this.length;
|
||||||
|
return this.slice(realIndex(start), realIndex(end));
|
||||||
|
}
|
||||||
|
|
||||||
|
function splice(start:Int, len:Int) {
|
||||||
|
return this.splice(realIndex(start), len);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,4 +28,17 @@ class BasicTestCase extends Test {
|
|||||||
function testMethod() {
|
function testMethod() {
|
||||||
Assert.equals(5, new BasicTestCase().myMethod());
|
Assert.equals(5, new BasicTestCase().myMethod());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testArray() {
|
||||||
|
var arr = BasicTestCase.myArray;
|
||||||
|
Assert.equals(3, arr.length);
|
||||||
|
Assert.equals(1, arr[0]);
|
||||||
|
Assert.equals(2, arr[1]);
|
||||||
|
Assert.equals(3, arr[2]);
|
||||||
|
|
||||||
|
// Kiss arrays can be negatively indexed like Python
|
||||||
|
Assert.equals(3, arr[-1]);
|
||||||
|
Assert.equals(2, arr[-2]);
|
||||||
|
Assert.equals(1, arr[-3]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,4 +16,7 @@
|
|||||||
(defprop myField 5)
|
(defprop myField 5)
|
||||||
|
|
||||||
// (defmethod) declares instance methods
|
// (defmethod) declares instance methods
|
||||||
(defmethod myMethod [] this.myField)
|
(defmethod myMethod [] this.myField)
|
||||||
|
|
||||||
|
// [...] returns a Kiss array (they have special features and convert implicitly)
|
||||||
|
(defvar myArray [1 2 3])
|
||||||
Reference in New Issue
Block a user