Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin Output #6

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ddl/Generate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Definitions
import Language

instance Unquote [Field]
instance Unquote [ConstructorDef]
instance Unquote [Char]
instance Quote [Char]
instance Quote [Instance]
Expand All @@ -31,6 +32,8 @@ main = do
dataSwift <- eitherParseFile "templates/data.swift.ede"
dataJava <- eitherParseFile "templates/data.java.ede"
jsonJava <- eitherParseFile "templates/json.java.ede"
dataKt <- eitherParseFile "templates/data.kt.ede"
jsonKt <- eitherParseFile "templates/json.kt.ede"
dataHpp <- eitherParseFile "templates/data.hpp.ede"
dataHpp2 <- eitherParseFile "templates/data.hpp2.ede"
dataCpp <- eitherParseFile "templates/data.cpp.ede"
Expand All @@ -52,6 +55,7 @@ main = do
mylib :: HashMap Text Term
mylib = HashMap.fromList
[ "hasFieldNames" @: hasFieldNames
, "nonSingular" @: nonSingular
, "parens" @: parens
, "constType" @: constType
, "hsType" @: hsType aliasMap
Expand All @@ -60,6 +64,7 @@ main = do
, "csType" @: csType name aliasMap
, "typeEnum" @: typeEnum aliasMap
, "javaType" @: javaType aliasMap
, "ktType" @: ktType aliasMap
, "swiftType" @: swiftType aliasMap
, "hasEnumConstructor" @: hasEnumConstructor
, "psInstances" @: filterInstances PureScript
Expand Down Expand Up @@ -99,6 +104,9 @@ main = do
fname = "out/java/" ++ toPath name ++ "/" ++ dataName d ++ ".java"
either error (\x -> writeFileIfDiffer fname $ LText.unpack x) $ dataJava >>= (\t -> eitherRenderWith mylib t env)
either error (\x -> writeFileIfDiffer ("out/java/" ++ toPath name ++ "/JSON.java") $ LText.unpack x) $ jsonJava >>= (\t -> eitherRenderWith mylib t env)
-- Kotlin
either error (\x -> writeFileIfDiffer ("out/kotlin/" ++ name ++ ".kt") $ LText.unpack x) $ dataKt >>= (\t -> eitherRenderWith mylib t env)
either error (\x -> writeFileIfDiffer "out/kotlin/JSON.kt" $ LText.unpack x) $ jsonKt >>= (\t -> eitherRenderWith mylib t env)
-- C#
either error (\x -> writeFileIfDiffer ("out/csharp/" ++ name ++ ".cs") $ LText.unpack x) $ dataCs >>= (\t -> eitherRenderWith mylib t env)
-- Swift
Expand Down
49 changes: 48 additions & 1 deletion ddl/Language.hs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,48 @@ javaType aliasMap a = case normalize aliasMap a of
Maybe t -> "Maybe<" ++ parens (javaType aliasMap t) ++ ">"
x -> error $ "javaType: " ++ show x

ktType :: AliasMap -> Type -> String
ktType aliasMap = \case
Int -> "Int"
Int32 -> "Int"
Word -> "Int"
Word32 -> "Int"
Float -> "Float"
Bool -> "Bool"
String -> "String"

V2 Int -> "V2I"
V2 Word -> "V2U"
V2 Float -> "V2F"
V2 Bool -> "V2B"
V2 (V2 Float) -> "M22F"
V2 (V3 Float) -> "M32F"
V2 (V4 Float) -> "M42F"

V3 Int -> "V3I"
V3 Word -> "V3U"
V3 Float -> "V3F"
V3 Bool -> "V3B"
V3 (V2 Float) -> "M23F"
V3 (V3 Float) -> "M33F"
V3 (V4 Float) -> "M43F"

V4 Int -> "V4I"
V4 Word -> "V4U"
V4 Float -> "V4F"
V4 Bool -> "V4B"
V4 (V2 Float) -> "M24F"
V4 (V3 Float) -> "M34F"
V4 (V4 Float) -> "M44F"

Array t -> "Array<" ++ parens (ktType aliasMap t) ++ ">"
List t -> "List<" ++ ktType aliasMap t ++ ">"
Maybe t -> parens (ktType aliasMap t) ++ "?"
Map k v -> "Map<" ++ parens (ktType aliasMap k) ++ ", " ++ parens (hsType aliasMap v) ++ ">"
-- user defined
Data t -> t
x -> error $ "unknown type: " ++ show x

typeEnum :: AliasMap -> Type -> String
typeEnum aliasMap a = case normalize aliasMap a of
Data t -> t
Expand Down Expand Up @@ -354,6 +396,11 @@ hasFieldNames :: [Field] -> Bool
hasFieldNames [] = False
hasFieldNames l = all (not . null . fieldName) l

nonSingular :: [ConstructorDef] -> Bool
nonSingular [] = False
nonSingular (_:[]) = False
nonSingular (_:_:_) = True

constType :: DataDef -> String
constType = head . words . show

Expand Down Expand Up @@ -449,4 +496,4 @@ a #:: b = Field a b
serialization:
json: encode/decode
other?
-}
-}
28 changes: 28 additions & 0 deletions ddl/lib/RT.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package RT;

data class V2<A>(val x: A, val y: A)
data class V3<A>(val x: A, val y: A, val z: A)
data class V4<A>(val x: A, val y: A, val z: A, val w: A)

typealias M22F = V2<V2F>
typealias M23F = V3<V2F>
typealias M24F = V4<V2F>
typealias M32F = V2<V3F>
typealias M33F = V3<V3F>
typealias M34F = V4<V3F>
typealias M42F = V2<V4F>
typealias M43F = V3<V4F>
typealias M44F = V4<V4F>

typealias V2F = V2<Float>
typealias V3F = V3<Float>
typealias V4F = V4<Float>
typealias V2I = V2<Int>
typealias V3I = V3<Int>
typealias V4I = V4<Int>
typealias V2U = V2<Int>
typealias V3U = V3<Int>
typealias V4U = V4<Int>
typealias V2B = V2<Boolean>
typealias V3B = V3<Boolean>
typealias V4B = V4<Boolean>
Loading