a couple of hacks to make life example work (still not working)
This commit is contained in:
@@ -169,4 +169,6 @@ public final class JsProgram extends JsNode {
|
|||||||
public NodeKind getKind() {
|
public NodeKind getKind() {
|
||||||
return NodeKind.PROGRAM;
|
return NodeKind.PROGRAM;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package js;
|
|||||||
import js.annotations.library
|
import js.annotations.library
|
||||||
import js.annotations.library
|
import js.annotations.library
|
||||||
import js.annotations.native
|
import js.annotations.native
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
library("println")
|
library("println")
|
||||||
fun println() {}
|
fun println() {}
|
||||||
@@ -24,3 +25,7 @@ native
|
|||||||
fun setInterval(callback : ()-> Unit, ms : Int) {}
|
fun setInterval(callback : ()-> Unit, ms : Int) {}
|
||||||
native
|
native
|
||||||
fun setInterval(callback : ()-> Unit) {}
|
fun setInterval(callback : ()-> Unit) {}
|
||||||
|
|
||||||
|
|
||||||
|
library("collectionsMax")
|
||||||
|
public fun max<T>(col : Collection<T>, comp : Comparator<T>) : T = f
|
||||||
|
|||||||
@@ -7,3 +7,8 @@ library
|
|||||||
trait Iterable<T> {
|
trait Iterable<T> {
|
||||||
fun iterator() : java.util.Iterator<T> {}
|
fun iterator() : java.util.Iterator<T> {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
library("splitString")
|
||||||
|
public fun String.split(regex : String) : Array<String> {
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,14 @@ public open class Iterator<T>() {
|
|||||||
open fun hasNext() : Boolean {}
|
open fun hasNext() : Boolean {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
library
|
||||||
|
val Collections = object {
|
||||||
|
library("max")
|
||||||
|
public fun max<T>(col : Collection<T>, comp : Comparator<T>) : T = f
|
||||||
|
}
|
||||||
|
|
||||||
|
private val f : Nothing
|
||||||
|
|
||||||
library
|
library
|
||||||
public open class ArrayList<erased E>() : java.util.List<E> {
|
public open class ArrayList<erased E>() : java.util.List<E> {
|
||||||
override public fun size() : Int {}
|
override public fun size() : Int {}
|
||||||
@@ -56,7 +64,7 @@ public trait Collection<erased E> : java.lang.Iterable<E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
library
|
library
|
||||||
public trait List<erased E> : java.util.Collection<E> {
|
public trait List<erased E> : Collection<E> {
|
||||||
override fun size() : Int
|
override fun size() : Int
|
||||||
override fun isEmpty() : Boolean
|
override fun isEmpty() : Boolean
|
||||||
override fun contains(o : Any?) : Boolean
|
override fun contains(o : Any?) : Boolean
|
||||||
@@ -81,7 +89,7 @@ public trait List<erased E> : java.util.Collection<E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
library
|
library
|
||||||
public trait Set<erased E> : java.util.Collection<E> {
|
public trait Set<erased E> : Collection<E> {
|
||||||
override fun size() : Int
|
override fun size() : Int
|
||||||
override fun isEmpty() : Boolean
|
override fun isEmpty() : Boolean
|
||||||
override fun contains(o : Any?) : Boolean
|
override fun contains(o : Any?) : Boolean
|
||||||
@@ -144,8 +152,3 @@ public class StringBuilder() {
|
|||||||
public fun append(obj : Any) : StringBuilder
|
public fun append(obj : Any) : StringBuilder
|
||||||
public fun toString() : String
|
public fun toString() : String
|
||||||
}
|
}
|
||||||
|
|
||||||
library("splitString")
|
|
||||||
public fun String.split(regex : String) : Array<String> {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
package java.util.Collections;
|
|
||||||
|
|
||||||
import js.annotations.library
|
|
||||||
import java.util.Collection
|
|
||||||
import java.util.Comparator
|
|
||||||
|
|
||||||
library("collectionsMax")
|
|
||||||
public fun max<T>(col : Collection<T>, comp : Comparator<T>) {}
|
|
||||||
@@ -31,8 +31,7 @@ public abstract class Config {
|
|||||||
PATH_TO_JS_LIB_SRC + "\\core\\core.kt",
|
PATH_TO_JS_LIB_SRC + "\\core\\core.kt",
|
||||||
PATH_TO_JS_LIB_SRC + "\\raphael\\raphael.kt",
|
PATH_TO_JS_LIB_SRC + "\\raphael\\raphael.kt",
|
||||||
PATH_TO_JS_LIB_SRC + "\\core\\json.kt",
|
PATH_TO_JS_LIB_SRC + "\\core\\json.kt",
|
||||||
PATH_TO_JS_LIB_SRC + "\\html5\\core.kt",
|
PATH_TO_JS_LIB_SRC + "\\html5\\core.kt"
|
||||||
PATH_TO_JS_LIB_SRC + "\\core\\javautilcollections.kt"
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -86,7 +86,13 @@ public final class StandardClasses {
|
|||||||
standardClasses.declare().forFQ("jet.IntRange").kotlinClass("NumberRange")
|
standardClasses.declare().forFQ("jet.IntRange").kotlinClass("NumberRange")
|
||||||
.methods("iterator", "contains").properties("start", "size", "end", "reversed");
|
.methods("iterator", "contains").properties("start", "size", "end", "reversed");
|
||||||
|
|
||||||
|
standardClasses.declare().forFQ("jet.sure").kotlinFunction("sure");
|
||||||
|
|
||||||
standardClasses.declare().forFQ("jet.Any.toString").kotlinFunction("toString");
|
standardClasses.declare().forFQ("jet.Any.toString").kotlinFunction("toString");
|
||||||
|
|
||||||
|
standardClasses.declare().forFQ("java.util.Collections.<no name provided>.max").kotlinFunction("collectionsMax");
|
||||||
|
|
||||||
|
standardClasses.declare().forFQ("jet.CharSequence.get").kotlinFunction("getChar");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -64,6 +64,12 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
|
|||||||
if (value instanceof Double) {
|
if (value instanceof Double) {
|
||||||
return context.program().getNumberLiteral((Double) value);
|
return context.program().getNumberLiteral((Double) value);
|
||||||
}
|
}
|
||||||
|
if (value instanceof String) {
|
||||||
|
return context.program().getStringLiteral((String) value);
|
||||||
|
}
|
||||||
|
if (value instanceof Character) {
|
||||||
|
return context.program().getStringLiteral(value.toString());
|
||||||
|
}
|
||||||
//TODO: all values
|
//TODO: all values
|
||||||
throw new AssertionError("Unsupported constant expression" + expression.toString());
|
throw new AssertionError("Unsupported constant expression" + expression.toString());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -130,6 +130,7 @@ public final class FunctionTranslator extends AbstractTranslator {
|
|||||||
return (!functionDeclaration.hasBlockBody()) && (!JetStandardClasses.isUnit(functionReturnType));
|
return (!functionDeclaration.hasBlockBody()) && (!JetStandardClasses.isUnit(functionReturnType));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: refactor
|
||||||
@NotNull
|
@NotNull
|
||||||
private JsBlock wrapWithReturnIfNeeded(@NotNull JsNode body, boolean needsReturn) {
|
private JsBlock wrapWithReturnIfNeeded(@NotNull JsNode body, boolean needsReturn) {
|
||||||
if (!needsReturn) {
|
if (!needsReturn) {
|
||||||
@@ -147,11 +148,16 @@ public final class FunctionTranslator extends AbstractTranslator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void addReturnToBlockStatement(@NotNull JsBlock bodyBlock) {
|
private void addReturnToBlockStatement(@NotNull JsBlock bodyBlock) {
|
||||||
List<JsStatement> statements = bodyBlock.getStatements();
|
AstUtil.mutateLastExpression(bodyBlock, new AstUtil.Mutator() {
|
||||||
int lastIndex = statements.size() - 1;
|
@Override
|
||||||
JsStatement lastStatement = statements.get(lastIndex);
|
@NotNull
|
||||||
JsReturn returnStatement = new JsReturn(AstUtil.extractExpressionFromStatement(lastStatement));
|
public JsNode mutate(@NotNull JsNode node) {
|
||||||
statements.set(lastIndex, returnStatement);
|
if (!(node instanceof JsExpression)) {
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
return new JsReturn((JsExpression) node);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
@@ -470,6 +470,17 @@
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Kotlin.comparator = function(f) {
|
||||||
|
var result = new Kotlin.Comparator;
|
||||||
|
result.compare = function(el1, el2) {
|
||||||
|
return f(el1, el2);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
Kotlin.getChar = function(str, index) {
|
||||||
|
return str.charAt(index);
|
||||||
|
};
|
||||||
|
|
||||||
Kotlin.collectionsMax = function (col, comp) {
|
Kotlin.collectionsMax = function (col, comp) {
|
||||||
var it = col.iterator();
|
var it = col.iterator();
|
||||||
if (col.isEmpty()) {
|
if (col.isEmpty()) {
|
||||||
@@ -573,6 +584,10 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Kotlin.sure = function(obj) {
|
||||||
|
return obj;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copyright 2010 Tim Down.
|
* Copyright 2010 Tim Down.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -2,9 +2,10 @@
|
|||||||
* This is a straightforward implementation of The Game of Life
|
* This is a straightforward implementation of The Game of Life
|
||||||
* See http://en.wikipedia.org/wiki/Conway's_Game_of_Life
|
* See http://en.wikipedia.org/wiki/Conway's_Game_of_Life
|
||||||
*/
|
*/
|
||||||
package life
|
|
||||||
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import java.lang.split;
|
||||||
|
import js.*;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* A field where cells live. Effectively immutable
|
* A field where cells live. Effectively immutable
|
||||||
@@ -119,11 +120,24 @@ fun printField(s : String, steps : Int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun <T> Array<T>.toList() : List<T> = this.to(ArrayList<T>())
|
||||||
|
|
||||||
|
val String?.size : Int
|
||||||
|
get() = if (this != null) this.length else 0;
|
||||||
|
|
||||||
|
fun <T, C: Collection<T>> Array<T>.to(result: C) : C {
|
||||||
|
for (elem in this)
|
||||||
|
result.add(elem)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
fun makeField(s : String) : Field {
|
fun makeField(s : String) : Field {
|
||||||
val lines = s.split("\n").sure()
|
val lines : Array<String> = s.split("\n")
|
||||||
val w = max<String?>(lines.toList(), comparator<String?> {o1, o2 ->
|
|
||||||
val l1 : Int = o1?.size ?: 0
|
val w = max<String>(lines.toList(), comparator<String> {o1, o2 ->
|
||||||
val l2 = o2?.size ?: 0
|
val l1 : Int = o1.size
|
||||||
|
val l2 = o2.size
|
||||||
l1 - l2
|
l1 - l2
|
||||||
}).sure()
|
}).sure()
|
||||||
val data = Array(lines.size) {Array(w.size) {false}}
|
val data = Array(lines.size) {Array(w.size) {false}}
|
||||||
@@ -137,7 +151,7 @@ fun makeField(s : String) : Field {
|
|||||||
|
|
||||||
for (line in lines.indices) {
|
for (line in lines.indices) {
|
||||||
for (x in lines[line].indices) {
|
for (x in lines[line].indices) {
|
||||||
val c = lines[line].sure()[x]
|
val c = lines[line][x]
|
||||||
data[line][x] = c == '*'
|
data[line][x] = c == '*'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -150,19 +164,4 @@ val String?.indices : IntRange get() = IntRange(0, this.sure().size)
|
|||||||
|
|
||||||
fun <K, V> Map<K, V>.set(k : K, v : V) { put(k, v) }
|
fun <K, V> Map<K, V>.set(k : K, v : V) { put(k, v) }
|
||||||
|
|
||||||
fun comparator<T> (f : (T, T) -> Int) : Comparator<T> = object : Comparator<T> {
|
|
||||||
override fun compare(o1 : T, o2 : T) : Int = f(o1, o2)
|
|
||||||
}
|
|
||||||
|
|
||||||
val <T> Array<T>.isEmpty : Boolean get() = size == 0
|
val <T> Array<T>.isEmpty : Boolean get() = size == 0
|
||||||
|
|
||||||
val String.size : Int
|
|
||||||
get() = length
|
|
||||||
|
|
||||||
fun <T, C: Collection<T>> Array<T>.to(result: C) : C {
|
|
||||||
for (elem in this)
|
|
||||||
result.add(elem)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
fun <T> Array<T>.toList() : List<T> = this.to(ArrayList<T>())
|
|
||||||
|
|||||||
Reference in New Issue
Block a user