Merge branch 'build-tools' of git+ssh://git.labs.intellij.net/jet into build-tools
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package org.jetbrains.jet.buildtools.ant;
|
||||
|
||||
import static org.jetbrains.jet.buildtools.core.Util.*;
|
||||
import org.apache.tools.ant.Task;
|
||||
import org.jetbrains.jet.buildtools.core.KotlinBytecodeCompiler;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
|
||||
/**
|
||||
* Kotlin bytecode compiler Ant task.
|
||||
*
|
||||
* See
|
||||
* http://evgeny-goldin.org/javadoc/ant/tutorial-writing-tasks.html
|
||||
* http://evgeny-goldin.org/javadoc/ant/Tasks/javac.html - attribute names should be similar to {@code <javac>}.
|
||||
*/
|
||||
public class KotlinBytecodeCompilerTask extends Task {
|
||||
|
||||
private File srcdir;
|
||||
private File file;
|
||||
private File destdir;
|
||||
|
||||
public void setSrcdir ( File srcdir ) { this.srcdir = srcdir; }
|
||||
public void setFile ( File file ) { this.file = file; }
|
||||
public void setDestdir ( File destdir ) { this.destdir = destdir; }
|
||||
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
|
||||
if ( this.destdir == null ) {
|
||||
throw new RuntimeException( "\"destdir\" attribute is not specified" );
|
||||
}
|
||||
|
||||
if (( this.srcdir != null ) || ( this.file != null )) {
|
||||
String src = getPath( this.srcdir != null ? this.srcdir : this.file );
|
||||
String dest = getPath( this.destdir );
|
||||
|
||||
log( String.format( "[%s] => [%s]", src, dest ));
|
||||
KotlinBytecodeCompiler.compileSources( src, dest );
|
||||
}
|
||||
else {
|
||||
throw new RuntimeException( String.format( "Operation is not supported" ));
|
||||
}
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package org.jetbrains.jet.buildtools.ant;
|
||||
|
||||
import org.apache.tools.ant.Task;
|
||||
|
||||
|
||||
/**
|
||||
* Kotlin JavaScript compiler Ant task.
|
||||
* http://evgeny-goldin.org/javadoc/ant/tutorial-writing-tasks.html
|
||||
*/
|
||||
public class KotlinJavaScriptCompilerTask extends Task {
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
log( "KotlinJavaScriptCompilerTask" );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
|
||||
<!-- http://evgeny-goldin.org/javadoc/ant/Types/antlib.html -->
|
||||
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
|
||||
|
||||
<antlib>
|
||||
<taskdef
|
||||
name = "kotlinc"
|
||||
classname = "org.jetbrains.jet.buildtools.ant.KotlinBytecodeCompilerTask"/>
|
||||
<taskdef
|
||||
name = "kotlinJSc"
|
||||
classname = "org.jetbrains.jet.buildtools.ant.KotlinJavaScriptCompilerTask"/>
|
||||
</antlib>
|
||||
@@ -0,0 +1,196 @@
|
||||
<project name="build-tools" default="buildToolsJar">
|
||||
|
||||
<property name="tests-dir" location="${output}/ant-test/build-tools-test"/>
|
||||
<property name="kotlin-home" location="${output}/ant-test/kotlin-home"/>
|
||||
|
||||
|
||||
<taskdef resource="net/sf/antcontrib/antlib.xml">
|
||||
<classpath>
|
||||
<pathelement location="${basedir}/build-tools/lib/ant-contrib-1.0b3.jar"/>
|
||||
</classpath>
|
||||
</taskdef>
|
||||
|
||||
|
||||
<!-- Re-creates ${kotlin-home} and defines <kotlin> tasks using "${kotlin-home}/lib" jars -->
|
||||
<macrodef name="setup-kotlin-home">
|
||||
<sequential>
|
||||
|
||||
<if>
|
||||
<not>
|
||||
<available file="${output}/${output.name}.zip"/>
|
||||
</not>
|
||||
<then>
|
||||
<antcall target="dist"/>
|
||||
</then>
|
||||
</if>
|
||||
|
||||
<delete dir = "${kotlin-home}" failonerror="true"/>
|
||||
<mkdir dir = "${kotlin-home}"/>
|
||||
<unzip src = "${output}/${output.name}.zip"
|
||||
dest = "${kotlin-home}"/>
|
||||
<move todir = "${kotlin-home}">
|
||||
<fileset dir = "${kotlin-home}/kotlinc"/>
|
||||
</move>
|
||||
<delete dir = "${kotlin-home}/kotlinc" failonerror="true"/>
|
||||
|
||||
<taskdef resource = "org/jetbrains/jet/buildtools/ant/antlib.xml">
|
||||
<classpath>
|
||||
<fileset dir = "${kotlin-home}/lib" includes = "*.jar"/>
|
||||
</classpath>
|
||||
</taskdef>
|
||||
</sequential>
|
||||
</macrodef>
|
||||
|
||||
|
||||
<!-- Runs <kotlinc> task specifying either "srcdir" or "file" attribute -->
|
||||
<macrodef name="run-kotlinc">
|
||||
<attribute name="file" default=""/>
|
||||
<attribute name="srcdir" default=""/>
|
||||
<sequential>
|
||||
|
||||
<delete dir = "${tests-dir}" includes = "**/*"/>
|
||||
|
||||
<if>
|
||||
<equals arg1="@{file}" arg2=""/>
|
||||
<then>
|
||||
<kotlinc destdir = "${tests-dir}" srcdir = "@{srcdir}"/>
|
||||
</then>
|
||||
<else>
|
||||
<kotlinc destdir = "${tests-dir}" file = "@{file}"/>
|
||||
</else>
|
||||
</if>
|
||||
</sequential>
|
||||
</macrodef>
|
||||
|
||||
|
||||
<!-- Runs <java> for compiled classes and verifies the output correctness -->
|
||||
<macrodef name="run-java">
|
||||
<attribute name="out"/>
|
||||
<attribute name="classname" default="namespace"/>
|
||||
<attribute name="args" default=""/>
|
||||
<attribute name="equals" default="true"/> <!-- Whether strict equality of output to @{out} required -->
|
||||
<sequential>
|
||||
<var name = "java-out" unset = "true"/>
|
||||
<java outputproperty = "java-out"
|
||||
classname = "@{classname}"
|
||||
failonerror = "true">
|
||||
<classpath>
|
||||
<pathelement path = "${tests-dir}"/>
|
||||
<fileset dir = "${kotlin-home}/lib" includes = "*.jar"/>
|
||||
</classpath>
|
||||
<arg line = "@{args}"/>
|
||||
</java>
|
||||
<if>
|
||||
<or>
|
||||
<equals arg1="${java-out}" arg2="@{out}" forcestring="true"/>
|
||||
<and>
|
||||
<equals arg1="@{equals}" arg2="false"/>
|
||||
<contains string="${java-out}" substring="@{out}"/>
|
||||
</and>
|
||||
</or>
|
||||
<then>
|
||||
<echo>${java-out}</echo>
|
||||
</then>
|
||||
<else>
|
||||
<fail message="Test failed: '${java-out}' (received) != '@{out}' (expected), equals = [@{equals}]"/>
|
||||
</else>
|
||||
</if>
|
||||
</sequential>
|
||||
</macrodef>
|
||||
|
||||
|
||||
<!-- Compiles "Hello.kt" as a single file and a folder, runs <java> and verifies the output -->
|
||||
<macrodef name="hello-test">
|
||||
<attribute name="root"/>
|
||||
<attribute name="args" default=""/>
|
||||
<attribute name="out"/>
|
||||
<sequential>
|
||||
<run-kotlinc file = "@{root}/Hello.kt"/>
|
||||
<run-java args = "@{args}" out = "@{out}"/>
|
||||
<run-kotlinc srcdir = "@{root}"/>
|
||||
<run-java args = "@{args}" out = "@{out}"/>
|
||||
</sequential>
|
||||
</macrodef>
|
||||
|
||||
|
||||
<!-- Runs all Hello tests -->
|
||||
<macrodef name="hello-tests">
|
||||
<sequential>
|
||||
<hello-test root="${basedir}/build-tools/test/hello/1" out="Hello, world!"/>
|
||||
<hello-test root="${basedir}/build-tools/test/hello/2" args="Kotlin-Developer" out="Hello, Kotlin-Developer!"/>
|
||||
<hello-test root="${basedir}/build-tools/test/hello/3" args="Mickey-Mouse" out="Hello, Mickey-Mouse!"/>
|
||||
<hello-test root="${basedir}/build-tools/test/hello/4" args="IT" out="Ciao!"/>
|
||||
<hello-test root="${basedir}/build-tools/test/hello/4" args="FR" out="Salut!"/>
|
||||
<hello-test root="${basedir}/build-tools/test/hello/5" args="Donald-Duck" out="Hello, Donald-Duck!"/>
|
||||
</sequential>
|
||||
</macrodef>
|
||||
|
||||
|
||||
<!-- Compiles, runs and verifies the output of web demo longer examples -->
|
||||
<macrodef name="longer-examples-tests">
|
||||
<sequential>
|
||||
<run-kotlinc srcdir = "${basedir}/build-tools/test/longer-examples" />
|
||||
<run-java classname = "bottles.namespace" equals="false" out="12 bottles of beer on the wall, 12 bottles of beer." />
|
||||
<run-java classname = "maze.namespace" equals="false" out="O ~OOOOOOOOOOOOOO"/>
|
||||
<run-java classname = "life.namespace" equals="false" out="*** ** ** ***"/>
|
||||
<run-java classname = "html.namespace" equals="false" out="<a href="http://jetbrains.com/kotlin">"/>
|
||||
</sequential>
|
||||
</macrodef>
|
||||
|
||||
|
||||
<!-- Verifies build fails if <kotlinc> fails -->
|
||||
<macrodef name="compilation-fail-test">
|
||||
<sequential>
|
||||
<var name="failed" value="false"/>
|
||||
<trycatch property="error-message" reference="bar">
|
||||
<try>
|
||||
<!-- Should fail -->
|
||||
<run-kotlinc srcdir = "${basedir}/build-tools/test/compilation-fail"/>
|
||||
</try>
|
||||
<catch>
|
||||
<var name="failed" value="true"/>
|
||||
</catch>
|
||||
</trycatch>
|
||||
<if>
|
||||
<isfalse value="${failed}"/>
|
||||
<then>
|
||||
<fail message="Compilation should have failed!"/>
|
||||
</then>
|
||||
</if>
|
||||
</sequential>
|
||||
</macrodef>
|
||||
|
||||
|
||||
<!-- Creates build tools distribution jar -->
|
||||
<target name="buildToolsJar" depends="compile">
|
||||
<mkdir dir ="${output}/classes/buildTools"/>
|
||||
<javac destdir="${output}/classes/buildTools" debug="true" debuglevel="lines,vars,source" includeAntRuntime="false">
|
||||
<src>
|
||||
<dirset dir="${basedir}/build-tools">
|
||||
<include name="core/src"/>
|
||||
<include name="ant/src"/>
|
||||
<include name="maven/src"/>
|
||||
<include name="gradle/src"/>
|
||||
</dirset>
|
||||
</src>
|
||||
<compilerarg value="-Xlint:all"/>
|
||||
<classpath>
|
||||
<path refid = "classpath.kotlin"/>
|
||||
<fileset dir = "${idea.sdk}/ant/lib" includes="*.jar"/>
|
||||
</classpath>
|
||||
</javac>
|
||||
<jar destfile="${output}/kotlin-build-tools.jar">
|
||||
<fileset dir = "${output}/classes/buildTools"/>
|
||||
<fileset dir = "${basedir}/build-tools/ant/src" includes="**/*.xml"/>
|
||||
</jar>
|
||||
</target>
|
||||
|
||||
|
||||
<!-- Tests build tools distribution jar -->
|
||||
<target name="buildToolsTest">
|
||||
<setup-kotlin-home/>
|
||||
<hello-tests/>
|
||||
<longer-examples-tests/>
|
||||
<compilation-fail-test/>
|
||||
</target>
|
||||
</project>
|
||||
@@ -0,0 +1,36 @@
|
||||
package org.jetbrains.jet.buildtools.core;
|
||||
|
||||
import org.jetbrains.jet.compiler.CompileEnvironment;
|
||||
|
||||
|
||||
/**
|
||||
* Wrapper class for Kotlin bytecode compiler.
|
||||
*/
|
||||
public class KotlinBytecodeCompiler {
|
||||
|
||||
|
||||
private static CompileEnvironment environment() {
|
||||
CompileEnvironment environment = new CompileEnvironment();
|
||||
environment.setJavaRuntime(CompileEnvironment.findRtJar( true ));
|
||||
|
||||
if ( ! environment.initializeKotlinRuntime()) {
|
||||
throw new RuntimeException( "No Kotlin runtime library found" );
|
||||
}
|
||||
|
||||
return environment;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@code CompileEnvironment#compileBunchOfSources} wrapper.
|
||||
*
|
||||
* @param source compilation source
|
||||
* @param destination compilation destination
|
||||
*/
|
||||
public static void compileSources ( String source, String destination ) {
|
||||
boolean success = environment().compileBunchOfSources( source, null, destination, true, false );
|
||||
if ( ! success ) {
|
||||
throw new RuntimeException( String.format( "[%s] compilation failed, see \"ERROR:\" messages above for more details.", source ));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.jetbrains.jet.buildtools.core;
|
||||
|
||||
|
||||
/**
|
||||
* Wrapper class for Kotlin JavaScript compiler.
|
||||
*/
|
||||
public class KotlinJavaScriptCompiler {
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.jetbrains.jet.buildtools.core;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
/**
|
||||
* General convenient utilities.
|
||||
*/
|
||||
public final class Util {
|
||||
|
||||
private Util () {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@code file.getCanonicalFile().getPath()} convenience wrapper.
|
||||
* @param f file to get its canonical path.
|
||||
* @return file's canonical path
|
||||
*/
|
||||
public static String getPath( File f ) {
|
||||
try {
|
||||
return f.getCanonicalFile().getPath();
|
||||
}
|
||||
catch ( IOException e ) {
|
||||
throw new RuntimeException( String.format( "Failed to resolve canonical file of [%s]: %s", f, e ), e );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* "Bottles.kt" that doesn't compile, see line 6.
|
||||
*/
|
||||
package bottles
|
||||
|
||||
fun main(args : Array<String>
|
||||
if (args.isEmpty) {
|
||||
printBottles(99)
|
||||
}
|
||||
else {
|
||||
try {
|
||||
printBottles(Integer.parseInt(args[0]))
|
||||
}
|
||||
catch (e : NumberFormatException) {
|
||||
System.err?.println("You have passed '${args[0]}' as a number of bottles, " +
|
||||
"but it is not a valid integral number")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun printBottles(bottleCount : Int) {
|
||||
if (bottleCount <= 0) {
|
||||
println("No bottles - no song")
|
||||
return
|
||||
}
|
||||
|
||||
println("The \"${bottlesOfBeer(bottleCount)}\" song\n")
|
||||
|
||||
var bottles = bottleCount
|
||||
while (bottles > 0) {
|
||||
val bottlesOfBeer = bottlesOfBeer(bottles)
|
||||
print("$bottlesOfBeer on the wall, $bottlesOfBeer.\nTake one down, pass it around, ")
|
||||
bottles--
|
||||
println("${bottlesOfBeer(bottles)} on the wall.\n")
|
||||
}
|
||||
println("No more bottles of beer on the wall, no more bottles of beer.\n" +
|
||||
"Go to the store and buy some more, ${bottlesOfBeer(bottleCount)} on the wall.")
|
||||
}
|
||||
|
||||
fun bottlesOfBeer(count : Int) : String =
|
||||
when (count) {
|
||||
0 -> "no more bottles"
|
||||
1 -> "1 bottle"
|
||||
else -> "$count bottles"
|
||||
} + " of beer"
|
||||
|
||||
/*
|
||||
* An excerpt from the Standard Library
|
||||
*/
|
||||
|
||||
// From the std.io package
|
||||
// These are simple functions that wrap standard Java API calls
|
||||
fun print(message : String) { System.out?.print(message) }
|
||||
fun println(message : String) { System.out?.println(message) }
|
||||
|
||||
// From the std package
|
||||
// This is an extension property, i.e. a property that is defined for the
|
||||
// type Array<T>, but does not sit inside the class Array
|
||||
val <T> Array<T>.isEmpty : Boolean get() = size == 0
|
||||
@@ -0,0 +1,3 @@
|
||||
fun main(args : Array<String>) {
|
||||
System.out?.println("Hello, world!")
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun main(args : Array<String>) {
|
||||
if (args.size == 0) {
|
||||
System.out?.println("Please provide a name as a command-line argument")
|
||||
return
|
||||
}
|
||||
System.out?.println("Hello, ${args[0]}!")
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
fun main(args : Array<String>) {
|
||||
for (name in args)
|
||||
System.out?.println("Hello, $name!")
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fun main(args : Array<String>) {
|
||||
val language = if (args.size == 0) "EN" else args[0]
|
||||
System.out?.println(when (language) {
|
||||
"EN" -> "Hello!"
|
||||
"FR" -> "Salut!"
|
||||
"IT" -> "Ciao!"
|
||||
else -> "Sorry, I can't greet you in $language yet"
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class Greeter(val name : String) {
|
||||
fun greet() {
|
||||
System.out?.println("Hello, ${name}!");
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
Greeter(args[0]).greet()
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* This example implements the famous "99 Bottles of Beer" program
|
||||
* See http://99-bottles-of-beer.net/
|
||||
*
|
||||
* The point is to print out a song with the following lyrics:
|
||||
*
|
||||
* The "99 bottles of beer" song
|
||||
*
|
||||
* 99 bottles of beer on the wall, 99 bottles of beer.
|
||||
* Take one down, pass it around, 98 bottles of beer on the wall.
|
||||
*
|
||||
* 98 bottles of beer on the wall, 98 bottles of beer.
|
||||
* Take one down, pass it around, 97 bottles of beer on the wall.
|
||||
*
|
||||
* ...
|
||||
*
|
||||
* 2 bottles of beer on the wall, 2 bottles of beer.
|
||||
* Take one down, pass it around, 1 bottle of beer on the wall.
|
||||
*
|
||||
* 1 bottle of beer on the wall, 1 bottle of beer.
|
||||
* Take one down, pass it around, no more bottles of beer on the wall.
|
||||
*
|
||||
* No more bottles of beer on the wall, no more bottles of beer.
|
||||
* Go to the store and buy some more, 99 bottles of beer on the wall.
|
||||
*
|
||||
* Additionally, you can pass the desired initial number of bottles to use (rather than 99)
|
||||
* as a command-line argument
|
||||
*/
|
||||
package bottles
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
if (args.isEmpty) {
|
||||
printBottles(99)
|
||||
}
|
||||
else {
|
||||
try {
|
||||
printBottles(Integer.parseInt(args[0]))
|
||||
}
|
||||
catch (e : NumberFormatException) {
|
||||
System.err?.println("You have passed '${args[0]}' as a number of bottles, " +
|
||||
"but it is not a valid integral number")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun printBottles(bottleCount : Int) {
|
||||
if (bottleCount <= 0) {
|
||||
println("No bottles - no song")
|
||||
return
|
||||
}
|
||||
|
||||
println("The \"${bottlesOfBeer(bottleCount)}\" song\n")
|
||||
|
||||
var bottles = bottleCount
|
||||
while (bottles > 0) {
|
||||
val bottlesOfBeer = bottlesOfBeer(bottles)
|
||||
print("$bottlesOfBeer on the wall, $bottlesOfBeer.\nTake one down, pass it around, ")
|
||||
bottles--
|
||||
println("${bottlesOfBeer(bottles)} on the wall.\n")
|
||||
}
|
||||
println("No more bottles of beer on the wall, no more bottles of beer.\n" +
|
||||
"Go to the store and buy some more, ${bottlesOfBeer(bottleCount)} on the wall.")
|
||||
}
|
||||
|
||||
fun bottlesOfBeer(count : Int) : String =
|
||||
when (count) {
|
||||
0 -> "no more bottles"
|
||||
1 -> "1 bottle"
|
||||
else -> "$count bottles"
|
||||
} + " of beer"
|
||||
|
||||
/*
|
||||
* An excerpt from the Standard Library
|
||||
*/
|
||||
|
||||
// From the std.io package
|
||||
// These are simple functions that wrap standard Java API calls
|
||||
fun print(message : String) { System.out?.print(message) }
|
||||
fun println(message : String) { System.out?.println(message) }
|
||||
|
||||
// From the std package
|
||||
// This is an extension property, i.e. a property that is defined for the
|
||||
// type Array<T>, but does not sit inside the class Array
|
||||
val <T> Array<T>.isEmpty : Boolean get() = size == 0
|
||||
@@ -0,0 +1,154 @@
|
||||
/**
|
||||
* This is an example of a Type-Safe Groovy-style Builder
|
||||
*
|
||||
* Builders are good for declaratively describing data in your code.
|
||||
* In this example we show how to describe an HTML page in Kotlin.
|
||||
*
|
||||
* See this page for details:
|
||||
* http://confluence.jetbrains.net/display/Kotlin/Type-safe+Groovy-style+builders
|
||||
*/
|
||||
package html
|
||||
|
||||
import java.util.*
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
val result =
|
||||
html {
|
||||
head {
|
||||
title {+"XML encoding with Kotlin"}
|
||||
}
|
||||
body {
|
||||
h1 {+"XML encoding with Kotlin"}
|
||||
p {+"this format can be used as an alternative markup to XML"}
|
||||
|
||||
// an element with attributes and text content
|
||||
a(href = "http://jetbrains.com/kotlin") {+"Kotlin"}
|
||||
|
||||
// mixed content
|
||||
p {
|
||||
+"This is some"
|
||||
b {+"mixed"}
|
||||
+"text. For more see the"
|
||||
a(href = "http://jetbrains.com/kotlin") {+"Kotlin"}
|
||||
+"project"
|
||||
}
|
||||
p {+"some text"}
|
||||
|
||||
// content generated from command-line arguments
|
||||
p {
|
||||
+"Command line arguments were:"
|
||||
ul {
|
||||
for (arg in args)
|
||||
li {+arg}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
println(result)
|
||||
}
|
||||
|
||||
trait Element {
|
||||
fun render(builder : StringBuilder, indent : String)
|
||||
|
||||
fun toString() : String? {
|
||||
val builder = StringBuilder()
|
||||
render(builder, "")
|
||||
return builder.toString()
|
||||
}
|
||||
}
|
||||
|
||||
class TextElement(val text : String) : Element {
|
||||
override fun render(builder : StringBuilder, indent : String) {
|
||||
builder.append("$indent$text\n")
|
||||
}
|
||||
}
|
||||
|
||||
abstract class Tag(val name : String) : Element {
|
||||
val children = ArrayList<Element>()
|
||||
val attributes = HashMap<String, String>()
|
||||
|
||||
protected fun initTag<T : Element>(tag : T, init : T.() -> Unit) : T {
|
||||
tag.init()
|
||||
children.add(tag)
|
||||
return tag
|
||||
}
|
||||
|
||||
override fun render(builder : StringBuilder, indent : String) {
|
||||
builder.append("$indent<$name${renderAttributes()}>\n")
|
||||
for (c in children) {
|
||||
c.render(builder, indent + " ")
|
||||
}
|
||||
builder.append("$indent</$name>\n")
|
||||
}
|
||||
|
||||
private fun renderAttributes() : String? {
|
||||
val builder = StringBuilder()
|
||||
for (a in attributes.keySet()) {
|
||||
builder.append(" $a=\"${attributes[a]}\"")
|
||||
}
|
||||
return builder.toString()
|
||||
}
|
||||
}
|
||||
|
||||
abstract class TagWithText(name : String) : Tag(name) {
|
||||
fun String.plus() {
|
||||
children.add(TextElement(this))
|
||||
}
|
||||
}
|
||||
|
||||
class HTML() : TagWithText("html") {
|
||||
fun head(init : Head.() -> Unit) = initTag(Head(), init)
|
||||
|
||||
fun body(init : Body.() -> Unit) = initTag(Body(), init)
|
||||
}
|
||||
|
||||
class Head() : TagWithText("head") {
|
||||
fun title(init : Title.() -> Unit) = initTag(Title(), init)
|
||||
}
|
||||
|
||||
class Title() : TagWithText("title")
|
||||
|
||||
abstract class BodyTag(name : String) : TagWithText(name) {
|
||||
fun b(init : B.() -> Unit) = initTag(B(), init)
|
||||
fun p(init : P.() -> Unit) = initTag(P(), init)
|
||||
fun h1(init : H1.() -> Unit) = initTag(H1(), init)
|
||||
fun ul(init : UL.() -> Unit) = initTag(UL(), init)
|
||||
fun a(href : String, init : A.() -> Unit) {
|
||||
val a = initTag(A(), init)
|
||||
a.href = href
|
||||
}
|
||||
}
|
||||
|
||||
class Body() : BodyTag("body")
|
||||
class UL() : BodyTag("ul") {
|
||||
fun li(init : LI.() -> Unit) = initTag(LI(), init)
|
||||
}
|
||||
|
||||
class B() : BodyTag("b")
|
||||
class LI() : BodyTag("li")
|
||||
class P() : BodyTag("p")
|
||||
class H1() : BodyTag("h1")
|
||||
class A() : BodyTag("a") {
|
||||
public var href : String
|
||||
get() = attributes["href"]
|
||||
set(value) {
|
||||
attributes["href"] = value
|
||||
}
|
||||
}
|
||||
|
||||
fun html(init : HTML.() -> Unit) : HTML {
|
||||
val html = HTML()
|
||||
html.init()
|
||||
return html
|
||||
}
|
||||
|
||||
// An excerpt from the Standard Library
|
||||
fun <K, V> Map<K, V>.set(key : K, value : V) = this.put(key, value)
|
||||
|
||||
fun println(message : Any?) {
|
||||
System.out?.println(message)
|
||||
}
|
||||
|
||||
fun print(message : Any?) {
|
||||
System.out?.print(message)
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
/**
|
||||
* This is a straightforward implementation of The Game of Life
|
||||
* See http://en.wikipedia.org/wiki/Conway's_Game_of_Life
|
||||
*/
|
||||
package life
|
||||
|
||||
import java.util.Collections.*
|
||||
import java.util.*
|
||||
|
||||
/*
|
||||
* A field where cells live. Effectively immutable
|
||||
*/
|
||||
class Field(
|
||||
val width : Int,
|
||||
val height : Int,
|
||||
// This function tells the constructor which cells are alive
|
||||
// if init(i, j) is true, the cell (i, j) is alive
|
||||
init : (Int, Int) -> Boolean
|
||||
) {
|
||||
private val live : Array<Array<Boolean>> = Array(height) {i -> Array(width) {j -> init(i, j)}}
|
||||
|
||||
private fun liveCount(i : Int, j : Int)
|
||||
= if (i in 0..height-1 &&
|
||||
j in 0..width-1 &&
|
||||
live[i][j]) 1 else 0
|
||||
|
||||
// How many neighbors of (i, j) are alive?
|
||||
fun liveNeighbors(i : Int, j : Int) =
|
||||
liveCount(i - 1, j - 1) +
|
||||
liveCount(i - 1, j) +
|
||||
liveCount(i - 1, j + 1) +
|
||||
liveCount(i, j - 1) +
|
||||
liveCount(i, j + 1) +
|
||||
liveCount(i + 1, j - 1) +
|
||||
liveCount(i + 1, j) +
|
||||
liveCount(i + 1, j + 1)
|
||||
|
||||
// You can say field[i, j], and this function gets called
|
||||
fun get(i : Int, j : Int) = live[i][j]
|
||||
}
|
||||
|
||||
/**
|
||||
* This function takes the present state of the field
|
||||
* and return a new field representing the next moment of time
|
||||
*/
|
||||
fun next(field : Field) : Field {
|
||||
return Field(field.width, field.height) {i, j ->
|
||||
val n = field.liveNeighbors(i, j)
|
||||
if (field[i, j])
|
||||
// (i, j) is alive
|
||||
n in 2..3 // It remains alive iff it has 2 or 3 neighbors
|
||||
else
|
||||
// (i, j) is dead
|
||||
n == 3 // A new cell is born if there are 3 neighbors alive
|
||||
}
|
||||
}
|
||||
|
||||
/** A few colony examples here */
|
||||
fun main(args : Array<String>) {
|
||||
// Simplistic demo
|
||||
printField("***", 3)
|
||||
// "Star burst"
|
||||
printField("""
|
||||
__*__
|
||||
_***_
|
||||
__*__
|
||||
""", 10)
|
||||
// Stable colony
|
||||
printField("""
|
||||
__*__
|
||||
_*_*_
|
||||
__*__
|
||||
""", 3)
|
||||
// Stable from the step 2
|
||||
printField("""
|
||||
__**__
|
||||
__**__
|
||||
__**__
|
||||
""", 3)
|
||||
// Oscillating colony
|
||||
printField("""
|
||||
__**__
|
||||
__**__
|
||||
__**__
|
||||
__**__
|
||||
""", 6)
|
||||
// A fancier oscillating colony
|
||||
printField("""
|
||||
---------------
|
||||
---***---***---
|
||||
---------------
|
||||
-*----*-*----*-
|
||||
-*----*-*----*-
|
||||
-*----*-*----*-
|
||||
---***---***---
|
||||
---------------
|
||||
---***---***---
|
||||
-*----*-*----*-
|
||||
-*----*-*----*-
|
||||
-*----*-*----*-
|
||||
---------------
|
||||
---***---***---
|
||||
---------------
|
||||
""", 10)
|
||||
}
|
||||
|
||||
// UTILITIES
|
||||
|
||||
fun printField(s : String, steps : Int) {
|
||||
var field = makeField(s)
|
||||
for (step in 1..steps) {
|
||||
println("Step: $step")
|
||||
for (i in 0..field.height-1) {
|
||||
for (j in 0..field.width-1) {
|
||||
print(if (field[i, j]) "*" else " ")
|
||||
}
|
||||
println("")
|
||||
}
|
||||
field = next(field)
|
||||
}
|
||||
}
|
||||
|
||||
fun makeField(s : String) : Field {
|
||||
val lines = s.split("\n").sure()
|
||||
val w = max<String?>(lines.toList(), comparator<String?> {o1, o2 ->
|
||||
val l1 : Int = o1?.size ?: 0
|
||||
val l2 = o2?.size ?: 0
|
||||
l1 - l2
|
||||
}).sure()
|
||||
val data = Array(lines.size) {Array(w.size) {false}}
|
||||
|
||||
// workaround
|
||||
for (i in data.indices) {
|
||||
data[i] = Array(w.size) {false}
|
||||
for (j in data[i].indices)
|
||||
data[i][j] = false
|
||||
}
|
||||
|
||||
for (line in lines.indices) {
|
||||
for (x in lines[line].indices) {
|
||||
val c = lines[line].sure()[x]
|
||||
data[line][x] = c == '*'
|
||||
}
|
||||
}
|
||||
|
||||
return Field(w.size, lines.size) {i, j -> data[i][j]}
|
||||
}
|
||||
|
||||
// An excerpt from the Standard Library
|
||||
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 comparator<T> (f : (T, T) -> Int) : Comparator<T> = object : Comparator<T> {
|
||||
override fun compare(o1 : T, o2 : T) : Int = f(o1, o2)
|
||||
}
|
||||
|
||||
fun println(message : Any?) {
|
||||
System.out?.println(message)
|
||||
}
|
||||
|
||||
fun print(message : Any?) {
|
||||
System.out?.print(message)
|
||||
}
|
||||
|
||||
val <T> Array<T>.isEmpty : Boolean get() = size == 0
|
||||
|
||||
fun String.split(s : String) = (this as java.lang.String).split(s)
|
||||
|
||||
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>())
|
||||
@@ -0,0 +1,238 @@
|
||||
/**
|
||||
* Let's Walk Through a Maze.
|
||||
*
|
||||
* Imagine there is a maze whose walls are the big 'O' letters.
|
||||
* Now, I stand where a big 'I' stands and some cool prize lies
|
||||
* somewhere marked with a '$' sign. Like this:
|
||||
*
|
||||
* OOOOOOOOOOOOOOOOO
|
||||
* O O
|
||||
* O$ O O
|
||||
* OOOOO O
|
||||
* O O
|
||||
* O OOOOOOOOOOOOOO
|
||||
* O O I O
|
||||
* O O
|
||||
* OOOOOOOOOOOOOOOOO
|
||||
*
|
||||
* I want to get the prize, and this program helps me do so as soon
|
||||
* as I possibly can by finding a shortest path through the maze.
|
||||
*/
|
||||
package maze
|
||||
|
||||
import java.util.Collections.*
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* This function looks for a path from max.start to maze.end through
|
||||
* free space (a path does not go through walls). One can move only
|
||||
* straightly up, down, left or right, no diagonal moves allowed.
|
||||
*/
|
||||
fun findPath(maze : Maze) : List<#(Int, Int)>? {
|
||||
val previous = HashMap<#(Int, Int), #(Int, Int)>
|
||||
|
||||
val queue = LinkedList<#(Int, Int)>
|
||||
val visited = HashSet<#(Int, Int)>
|
||||
|
||||
queue.offer(maze.start)
|
||||
visited.add(maze.start)
|
||||
while (!queue.isEmpty()) {
|
||||
val cell = queue.poll()
|
||||
if (cell == maze.end) break
|
||||
|
||||
for (newCell in maze.neighbors(cell._1, cell._2)) {
|
||||
if (newCell in visited) continue
|
||||
previous[newCell] = cell
|
||||
queue.offer(newCell)
|
||||
visited.add(cell)
|
||||
}
|
||||
}
|
||||
|
||||
if (previous[maze.end] == null) return null
|
||||
|
||||
val path = ArrayList<#(Int, Int)>()
|
||||
var current = previous[maze.end]
|
||||
while (current != maze.start) {
|
||||
path.add(0, current)
|
||||
current = previous[current]
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
/**
|
||||
* Find neighbors of the (i, j) cell that are not walls
|
||||
*/
|
||||
fun Maze.neighbors(i : Int, j : Int) : List<#(Int, Int)> {
|
||||
val result = ArrayList<#(Int, Int)>
|
||||
addIfFree(i - 1, j, result)
|
||||
addIfFree(i, j - 1, result)
|
||||
addIfFree(i + 1, j, result)
|
||||
addIfFree(i, j + 1, result)
|
||||
return result
|
||||
}
|
||||
|
||||
fun Maze.addIfFree(i : Int, j : Int, result : List<#(Int, Int)>) {
|
||||
if (i !in 0..height-1) return
|
||||
if (j !in 0..width-1) return
|
||||
if (walls[i][j]) return
|
||||
|
||||
result.add(#(i, j))
|
||||
}
|
||||
|
||||
/**
|
||||
* A data class that represents a maze
|
||||
*/
|
||||
class Maze(
|
||||
// Number or columns
|
||||
val width : Int,
|
||||
// Number of rows
|
||||
val height : Int,
|
||||
// true for a wall, false for free space
|
||||
val walls : Array<out Array<out Boolean>>,
|
||||
// The starting point (must not be a wall)
|
||||
val start : #(Int, Int),
|
||||
// The target point (must not be a wall)
|
||||
val end : #(Int, Int)
|
||||
) {
|
||||
}
|
||||
|
||||
/** A few maze examples here */
|
||||
fun main(args : Array<String>) {
|
||||
printMaze("I $")
|
||||
printMaze("I O $")
|
||||
printMaze("""
|
||||
O $
|
||||
O
|
||||
O
|
||||
O
|
||||
O I
|
||||
""")
|
||||
printMaze("""
|
||||
OOOOOOOOOOO
|
||||
O $ O
|
||||
OOOOOOO OOO
|
||||
O O
|
||||
OOOOO OOOOO
|
||||
O O
|
||||
O OOOOOOOOO
|
||||
O OO
|
||||
OOOOOO IO
|
||||
""")
|
||||
printMaze("""
|
||||
OOOOOOOOOOOOOOOOO
|
||||
O O
|
||||
O$ O O
|
||||
OOOOO O
|
||||
O O
|
||||
O OOOOOOOOOOOOOO
|
||||
O O I O
|
||||
O O
|
||||
OOOOOOOOOOOOOOOOO
|
||||
""")
|
||||
}
|
||||
|
||||
// UTILITIES
|
||||
|
||||
fun printMaze(str : String) {
|
||||
val maze = makeMaze(str)
|
||||
|
||||
println("Maze:")
|
||||
val path = findPath(maze)
|
||||
for (i in 0..maze.height - 1) {
|
||||
for (j in 0..maze.width - 1) {
|
||||
val cell = #(i, j)
|
||||
print(
|
||||
if (maze.walls[i][j]) "O"
|
||||
else if (cell == maze.start) "I"
|
||||
else if (cell == maze.end) "$"
|
||||
else if (path != null && path.contains(cell)) "~"
|
||||
else " "
|
||||
)
|
||||
}
|
||||
println("")
|
||||
}
|
||||
println("Result: " + if (path == null) "No path" else "Path found")
|
||||
println("")
|
||||
}
|
||||
|
||||
/**
|
||||
* A maze is encoded in the string s: the big 'O' letters are walls.
|
||||
* I stand where a big 'I' stands and the prize is marked with
|
||||
* a '$' sign.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* OOOOOOOOOOOOOOOOO
|
||||
* O O
|
||||
* O$ O O
|
||||
* OOOOO O
|
||||
* O O
|
||||
* O OOOOOOOOOOOOOO
|
||||
* O O I O
|
||||
* O O
|
||||
* OOOOOOOOOOOOOOOOO
|
||||
*/
|
||||
fun makeMaze(s : String) : Maze {
|
||||
val lines = s.split("\n").sure()
|
||||
val w = max<String?>(lines.toList(), comparator<String?> {o1, o2 ->
|
||||
val l1 : Int = o1?.size ?: 0
|
||||
val l2 = o2?.size ?: 0
|
||||
l1 - l2
|
||||
}).sure()
|
||||
val data = Array<Array<Boolean>>(lines.size) {Array<Boolean>(w.size) {false}}
|
||||
|
||||
var start : #(Int, Int)? = null
|
||||
var end : #(Int, Int)? = null
|
||||
|
||||
for (line in lines.indices) {
|
||||
for (x in lines[line].indices) {
|
||||
val c = lines[line].sure()[x]
|
||||
data[line][x] = c == 'O'
|
||||
when (c) {
|
||||
'I' -> start = #(line, x)
|
||||
'$' -> end = #(line, x)
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (start == null) {
|
||||
throw IllegalArgumentException("No starting point in the maze (should be indicated with 'I')")
|
||||
}
|
||||
|
||||
if (end == null) {
|
||||
throw IllegalArgumentException("No goal point in the maze (should be indicated with a '$' sign)")
|
||||
}
|
||||
|
||||
return Maze(w.size, lines.size, data, start.sure(), end.sure())
|
||||
}
|
||||
|
||||
// An excerpt from the Standard Library
|
||||
val String?.indices : IntRange get() = IntRange(0, this.sure().size)
|
||||
|
||||
val String.size : Int
|
||||
get() = length
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
fun String.split(s : String) = (this as java.lang.String).split(s)
|
||||
|
||||
fun println(message : Any?) {
|
||||
System.out?.println(message)
|
||||
}
|
||||
|
||||
fun print(message : Any?) {
|
||||
System.out?.print(message)
|
||||
}
|
||||
|
||||
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>())
|
||||
@@ -1,9 +1,12 @@
|
||||
<project name="Kotlin" default="dist">
|
||||
|
||||
<property name="output" value="${basedir}/dist"/>
|
||||
<property name="build.number" value="snapshot"/>
|
||||
<property name="output.name" value="kotlin-${build.number}"/>
|
||||
<property name="idea.sdk" value="${basedir}/ideaSDK"/>
|
||||
|
||||
<import file="build-tools/build.xml" optional="false"/>
|
||||
|
||||
<path id="classpath">
|
||||
<fileset dir="${idea.sdk}" includes="*.jar"/>
|
||||
<fileset dir="${basedir}/lib" includes="*.jar"/>
|
||||
@@ -29,7 +32,7 @@
|
||||
|
||||
<target name="compileRT">
|
||||
<mkdir dir="${output}/classes/runtime"/>
|
||||
<javac destdir="${output}/classes/runtime" debug="true" debuglevel="lines,vars,source">
|
||||
<javac destdir="${output}/classes/runtime" debug="true" debuglevel="lines,vars,source" includeAntRuntime="false">
|
||||
<src path="${basedir}/stdlib/src"/>
|
||||
<classpath refid="classpath"/>
|
||||
</javac>
|
||||
@@ -55,10 +58,10 @@
|
||||
</jar>
|
||||
</target>
|
||||
|
||||
|
||||
|
||||
<target name="compile" depends="compileRT">
|
||||
<mkdir dir="${output}/classes/compiler"/>
|
||||
<javac destdir="${output}/classes/compiler" debug="true" debuglevel="lines,vars,source">
|
||||
<javac destdir="${output}/classes/compiler" debug="true" debuglevel="lines,vars,source" includeAntRuntime="false">
|
||||
<src refid="sourcepath"/>
|
||||
<classpath refid="classpath"/>
|
||||
</javac>
|
||||
@@ -75,7 +78,7 @@
|
||||
<delete dir="${output}"/>
|
||||
</target>
|
||||
|
||||
<target name="dist" depends="clean,jarRT,jar">
|
||||
<target name="dist" depends="clean,jarRT,jar,buildToolsJar">
|
||||
<echo file="${output}/build.txt" message="${build.number}"/>
|
||||
<zip destfile="${output}/${output.name}.zip">
|
||||
<zipfileset prefix="kotlinc" file="${output}/build.txt"/>
|
||||
|
||||
@@ -32,12 +32,12 @@ public class KotlinCompiler {
|
||||
@Argument(value = "help", alias = "h", description = "show help")
|
||||
public boolean help;
|
||||
}
|
||||
|
||||
|
||||
private static void usage(PrintStream target) {
|
||||
target.println("Usage: KotlinCompiler [-output <outputDir>|-jar <jarFileName>] [-src <filename or dirname>|-module <module file>] [-includeRuntime] [-excludeStdlib]");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String ... args) {
|
||||
System.setProperty("java.awt.headless", "true");
|
||||
Arguments arguments = new Arguments();
|
||||
try {
|
||||
@@ -53,7 +53,7 @@ public class KotlinCompiler {
|
||||
System.exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (arguments.help) {
|
||||
usage(System.out);
|
||||
System.exit(0);
|
||||
|
||||
Reference in New Issue
Block a user