Skip to content

Commit

Permalink
issue-72 compile Idris Bool to java boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
mmhelloworld committed Feb 17, 2018
1 parent a277e55 commit 7e0e5d4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
10 changes: 9 additions & 1 deletion idris-jvm-core/src/main/idris/IdrisJvm/Core/ControlFlow.idr
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
8 changes: 4 additions & 4 deletions idris-jvm-core/src/main/idris/IdrisJvm/Core/Foreign.idr
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down Expand Up @@ -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) =
Expand Down
26 changes: 24 additions & 2 deletions idris-jvm-core/src/main/idris/IdrisJvm/Core/Function.idr
Original file line number Diff line number Diff line change
Expand Up @@ -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) ::
_))
Expand All @@ -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) ::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7e0e5d4

Please sign in to comment.