From 7e0e5d47c0926ae09df2f14cc08e779f6a4bf42f Mon Sep 17 00:00:00 2001 From: Marimuthu Madasamy Date: Sat, 17 Feb 2018 16:45:52 -0500 Subject: [PATCH] issue-72 compile Idris Bool to java boolean --- .travis.yml | 2 +- .../main/idris/IdrisJvm/Core/ControlFlow.idr | 10 ++++++- .../src/main/idris/IdrisJvm/Core/Foreign.idr | 8 +++--- .../src/main/idris/IdrisJvm/Core/Function.idr | 26 +++++++++++++++++-- .../mmhelloworld/idrisjvm/runtime/Util.java | 14 ++-------- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0f53541b7..df3526d85 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,7 +18,7 @@ env: before_install: - docker pull mmhelloworld/idris:1.2.0 - mkdir -p $HOME/bin/idris-packages $IDRIS_JVM_HOME - - wget https://github.com/mmhelloworld/idris-jvm/releases/download/1.0-SNAPSHOT-20180208/idris-jvm-1.0-SNAPSHOT-20180208.zip + - wget https://github.com/mmhelloworld/idris-jvm/releases/download/1.0-SNAPSHOT-20180217.beta/idris-jvm-1.0-SNAPSHOT-20180217.beta.zip - unzip -o idris-jvm-*.zip -d $HOME/bin - export PATH=`pwd`/bin/travis:$IDRIS_JVM_HOME/codegen/bin:$HOME/.local/bin:$PATH - export PROJECT_ROOT=`pwd` diff --git a/idris-jvm-core/src/main/idris/IdrisJvm/Core/ControlFlow.idr b/idris-jvm-core/src/main/idris/IdrisJvm/Core/ControlFlow.idr index 7a7719b49..1cef8bb77 100644 --- a/idris-jvm-core/src/main/idris/IdrisJvm/Core/ControlFlow.idr +++ b/idris-jvm-core/src/main/idris/IdrisJvm/Core/ControlFlow.idr @@ -279,4 +279,12 @@ cgIfNonNull : Lazy (Asm ()) -> (Lazy (Asm ()) -> SExp -> Asm ()) -> LVar -> Int cgIfNonNull ret cgBody e loc ifExp elseExp = cgIfElse ret cgBody e Ifnull (Just loc) ifExp elseExp cgIfNull : Lazy (Asm ()) -> (Lazy (Asm ()) -> SExp -> Asm ()) -> LVar -> SExp -> SExp -> Asm () -cgIfNull ret cgBody e ifExp elseExp = cgIfElse ret cgBody e Ifnonnull Nothing ifExp elseExp \ No newline at end of file +cgIfNull ret cgBody e ifExp elseExp = cgIfElse ret cgBody e Ifnonnull Nothing ifExp elseExp + +cgIfTrueElse : Lazy (Asm ()) -> (Lazy (Asm ()) -> SExp -> Asm ()) -> LVar -> SExp -> SExp -> Asm () +cgIfTrueElse ret cgBody e ifExp elseExp = cgIfElse ret cgBody e condition Nothing ifExp elseExp where + condition : Label -> Asm () + condition elseLabel = do + Checkcast "java/lang/Boolean" + unboxBool + Ifeq elseLabel diff --git a/idris-jvm-core/src/main/idris/IdrisJvm/Core/Foreign.idr b/idris-jvm-core/src/main/idris/IdrisJvm/Core/Foreign.idr index 615e5d942..c8abe538c 100644 --- a/idris-jvm-core/src/main/idris/IdrisJvm/Core/Foreign.idr +++ b/idris-jvm-core/src/main/idris/IdrisJvm/Core/Foreign.idr @@ -24,8 +24,7 @@ data JForeign = JStatic String String | JInstanceOf String javaToIdris : TypeDescriptor -> Asm () -javaToIdris (FieldDescriptor FieldTyDescBoolean) = - InvokeMethod InvokeStatic utilClass "boolToIdrisBool" "(Z)Ljava/lang/Object;" False +javaToIdris (FieldDescriptor FieldTyDescBoolean) = boxBool javaToIdris (FieldDescriptor FieldTyDescByte) = InvokeMethod InvokeStatic utilClass "byteToIdrisBits8" "(B)Ljava/lang/Object;" False javaToIdris (FieldDescriptor FieldTyDescShort) = @@ -59,8 +58,9 @@ checkcast "java/lang/Object" = pure () checkcast cname = Checkcast cname idrisDescToJava : TypeDescriptor -> Asm () -idrisDescToJava (FieldDescriptor FieldTyDescBoolean) = - InvokeMethod InvokeStatic utilClass "idrisBoolToBool" "(Ljava/lang/Object;)Z" False +idrisDescToJava (FieldDescriptor FieldTyDescBoolean) = do + Checkcast "java/lang/Boolean" + unboxBool idrisDescToJava (FieldDescriptor FieldTyDescByte) = InvokeMethod InvokeStatic utilClass "idrisBits8ToByte" "(Ljava/lang/Object;)B" False idrisDescToJava (FieldDescriptor FieldTyDescShort) = diff --git a/idris-jvm-core/src/main/idris/IdrisJvm/Core/Function.idr b/idris-jvm-core/src/main/idris/IdrisJvm/Core/Function.idr index 93e292f29..4ad9346a2 100644 --- a/idris-jvm-core/src/main/idris/IdrisJvm/Core/Function.idr +++ b/idris-jvm-core/src/main/idris/IdrisJvm/Core/Function.idr @@ -132,11 +132,33 @@ mutual idrisObjectProperty v i ret - cgBody ret (SCon _ 1 "Prelude.Maybe.Just" [(Loc v)]) = do Aload v; ret + cgBody ret (SCon _ 0 "Prelude.Bool.False" []) = do + Field FGetStatic "java/lang/Boolean" "FALSE" "Ljava/lang/Boolean;" + ret + cgBody ret (SCon _ 1 "Prelude.Bool.True" []) = do + Field FGetStatic "java/lang/Boolean" "TRUE" "Ljava/lang/Boolean;" + ret + cgBody ret (SCon _ 0 "Prelude.Maybe.Nothing" []) = do Aconstnull; ret + cgBody ret (SCon _ 1 "Prelude.Maybe.Just" [(Loc v)]) = do Aload v; ret cgBody ret (SCon _ t _ args) = do createIdrisObject t args; ret + cgBody ret (SCase _ e ((SConCase _ 0 "Prelude.Bool.False" [] falseAlt) :: + (SConCase _ 1 "Prelude.Bool.True" [] trueAlt) :: + _)) + = cgIfTrueElse ret cgBody e trueAlt falseAlt + + cgBody ret (SCase _ e ((SConCase _ 1 "Prelude.Bool.True" [] trueAlt) :: + (SDefaultCase falseAlt) :: + _)) + = cgIfTrueElse ret cgBody e trueAlt falseAlt + + cgBody ret (SCase _ e ((SConCase _ 0 "Prelude.Bool.False" [] falseAlt) :: + (SDefaultCase trueAlt) :: + _)) + = cgIfTrueElse ret cgBody e trueAlt falseAlt + cgBody ret (SCase _ e ((SConCase justValueStore 1 "Prelude.Maybe.Just" [_] justExpr) :: (SConCase _ 0 "Prelude.Maybe.Nothing" [] nothingExpr) :: _)) @@ -145,7 +167,7 @@ mutual cgBody ret (SCase _ e ((SConCase justValueStore 1 "Prelude.Maybe.Just" [_] justExpr) :: (SDefaultCase defaultExpr) :: _)) - = cgIfNonNull ret cgBody e justValueStore justExpr defaultExpr + = cgIfNonNull ret cgBody e justValueStore justExpr defaultExpr cgBody ret (SCase _ e ((SConCase _ 0 "Prelude.Maybe.Nothing" [] nothingExpr) :: (SDefaultCase defaultExpr) :: diff --git a/idris-jvm-runtime/src/main/java/io/github/mmhelloworld/idrisjvm/runtime/Util.java b/idris-jvm-runtime/src/main/java/io/github/mmhelloworld/idrisjvm/runtime/Util.java index 8f3bd0335..f58f9d12c 100644 --- a/idris-jvm-runtime/src/main/java/io/github/mmhelloworld/idrisjvm/runtime/Util.java +++ b/idris-jvm-runtime/src/main/java/io/github/mmhelloworld/idrisjvm/runtime/Util.java @@ -3,20 +3,10 @@ import java.math.BigInteger; import java.util.Objects; -import static io.github.mmhelloworld.idrisjvm.runtime.IdrisObject.NO_ARG_CONSTRUCTOR_0; -import static io.github.mmhelloworld.idrisjvm.runtime.IdrisObject.NO_ARG_CONSTRUCTOR_1; import static java.lang.String.format; public class Util { - public static Object boolToIdrisBool(boolean b) { - return b ? NO_ARG_CONSTRUCTOR_1 : NO_ARG_CONSTRUCTOR_0; - } - - public static boolean idrisBoolToBool(Object idrisBool) { - return Runtime.constructorIndex(idrisBool) == 1; - } - public static byte idrisBits8ToByte(Object idrisBits8) { return (byte) ((int) idrisBits8); } @@ -71,7 +61,7 @@ public static boolean equals(Object a, Object b) { } else if (a instanceof BigInteger && b instanceof Integer) { return a.equals(BigInteger.valueOf((Integer) b)); } else if (a instanceof Integer && b instanceof BigInteger) { - return b.equals(BigInteger.valueOf((Integer)a)); + return b.equals(BigInteger.valueOf((Integer) a)); } else { return Objects.equals(a, b); } @@ -139,7 +129,7 @@ public static Object intGreaterThanOrEqualTo(Object m, Object n) { } public static Object stringLessThan(Object m, Object n) { - return boolToInt(((String)m).compareTo((String)n) < 0); + return boolToInt(((String) m).compareTo((String) n) < 0); } public static Object uintLessThan(Object m, Object n) {