very simple try/catch/finally support.
This commit is contained in:
@@ -393,4 +393,11 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
|
|||||||
@NotNull TranslationContext context) {
|
@NotNull TranslationContext context) {
|
||||||
return ForTranslator.translate(expression, context);
|
return ForTranslator.translate(expression, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@NotNull
|
||||||
|
public JsNode visitTryExpression(@NotNull JetTryExpression expression,
|
||||||
|
@NotNull TranslationContext context) {
|
||||||
|
return TryTranslator.translate(expression, context);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
package org.jetbrains.k2js.translate.expression;
|
||||||
|
|
||||||
|
import com.google.dart.compiler.backend.js.ast.JsBlock;
|
||||||
|
import com.google.dart.compiler.backend.js.ast.JsCatch;
|
||||||
|
import com.google.dart.compiler.backend.js.ast.JsName;
|
||||||
|
import com.google.dart.compiler.backend.js.ast.JsTry;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.jet.lang.psi.*;
|
||||||
|
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||||
|
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
||||||
|
import org.jetbrains.k2js.translate.general.Translation;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static com.google.dart.compiler.util.AstUtil.convertToBlock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Talanov Pavel
|
||||||
|
*/
|
||||||
|
public final class TryTranslator extends AbstractTranslator {
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static JsTry translate(@NotNull JetTryExpression expression,
|
||||||
|
@NotNull TranslationContext context) {
|
||||||
|
return (new TryTranslator(expression, context)).translate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private final JetTryExpression expression;
|
||||||
|
|
||||||
|
private TryTranslator(@NotNull JetTryExpression expression,
|
||||||
|
@NotNull TranslationContext context) {
|
||||||
|
super(context);
|
||||||
|
this.expression = expression;
|
||||||
|
}
|
||||||
|
|
||||||
|
private JsTry translate() {
|
||||||
|
JsTry result = new JsTry();
|
||||||
|
result.setTryBlock(translateTryBlock());
|
||||||
|
result.setFinallyBlock(translateFinallyBlock());
|
||||||
|
result.getCatches().addAll(translateCatches());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private JsBlock translateFinallyBlock() {
|
||||||
|
JetFinallySection finallyBlock = expression.getFinallyBlock();
|
||||||
|
if (finallyBlock == null) return null;
|
||||||
|
|
||||||
|
return convertToBlock(Translation.translateAsStatement(finallyBlock.getFinalExpression(), context()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private JsBlock translateTryBlock() {
|
||||||
|
return convertToBlock(Translation.translateAsStatement(expression.getTryBlock(), context()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private List<JsCatch> translateCatches() {
|
||||||
|
List<JsCatch> result = new ArrayList<JsCatch>();
|
||||||
|
for (JetCatchClause catchClause : expression.getCatchClauses()) {
|
||||||
|
result.add(translateCatchClause(catchClause));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private JsCatch translateCatchClause(@NotNull JetCatchClause catchClause) {
|
||||||
|
JetParameter catchParameter = catchClause.getCatchParameter();
|
||||||
|
assert catchParameter != null : "Valid catch must have a parameter.";
|
||||||
|
|
||||||
|
JsName parameterName = context().declareLocalVariable(catchParameter);
|
||||||
|
JsCatch result = new JsCatch(context().jsScope(), parameterName.getIdent());
|
||||||
|
result.setBody(translateCatchBody(catchClause));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private JsBlock translateCatchBody(@NotNull JetCatchClause catchClause) {
|
||||||
|
JetExpression catchBody = catchClause.getCatchBody();
|
||||||
|
if (catchBody == null) {
|
||||||
|
return convertToBlock(program().getEmptyStmt());
|
||||||
|
}
|
||||||
|
return convertToBlock(Translation.translateAsStatement(catchBody, context()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ public class TranslatorVisitor<T> extends JetVisitor<T, TranslationContext> {
|
|||||||
@Override
|
@Override
|
||||||
@NotNull
|
@NotNull
|
||||||
public T visitJetElement(JetElement expression, TranslationContext context) {
|
public T visitJetElement(JetElement expression, TranslationContext context) {
|
||||||
throw new RuntimeException("Unsupported expression encountered:" + expression.toString());
|
throw new UnsupportedOperationException("Unsupported expression encountered:" + expression.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,10 +42,21 @@ public final class SystemTest extends JavaClassesTest {
|
|||||||
checkOutput("ifAsExpression.kt", "20\n", "10", "20");
|
checkOutput("ifAsExpression.kt", "20\n", "10", "20");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void objectOrientedHello() throws Exception {
|
||||||
|
checkOutput("objectOrientedHello.kt", "Hello, Pavel!\n", "Pavel");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void multiLanguageHello() throws Exception {
|
public void multiLanguageHello() throws Exception {
|
||||||
checkOutput("multiLanguageHello.kt", "Salut!\n", "FR");
|
checkOutput("multiLanguageHello.kt", "Salut!\n", "FR");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void nullChecks() throws Exception {
|
||||||
|
checkOutput("nullChecks.kt", "No number supplied");
|
||||||
|
checkOutput("nullChecks.kt", "6", "2", "3");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
// Return null if str does not hold a number
|
||||||
|
fun parseInt(str : String) : Int? {
|
||||||
|
try{
|
||||||
|
return Integer.parseInt(str)
|
||||||
|
} catch (e: NumberFormatException) {
|
||||||
|
System.out?.println("One of argument isn't Int")
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main(args : Array<String>) {
|
||||||
|
if (args.size < 2) {
|
||||||
|
System.out?.print("No number supplied");
|
||||||
|
} else {
|
||||||
|
val x = parseInt(args[0])
|
||||||
|
val y = parseInt(args[1])
|
||||||
|
|
||||||
|
// We cannot say 'x * y' now because they may hold nulls
|
||||||
|
if (x != null && y != null) {
|
||||||
|
System.out?.print(x * y) // Now we can
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
class Greeter(name : String) {
|
||||||
|
val name = name
|
||||||
|
fun greet() {
|
||||||
|
System.out?.println("Hello, ${name}!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main(args : Array<String>) {
|
||||||
|
Greeter(args[0]).greet()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user