1 {-# LANGUAGE OverloadedStrings #-}
3 import qualified Data.ByteString.Lazy as B
12 initNT :: NameType Method
13 initNT = NameType "<init>" $ MethodSignature [] ReturnsVoid
15 helloNT :: NameType Method
16 helloNT = NameType "hello" $ MethodSignature [IntType] ReturnsVoid
18 printlnNT :: NameType Method
19 printlnNT = NameType "println" $ MethodSignature [ObjectType "java/lang/String"] ReturnsVoid
21 outNT :: NameType Field
22 outNT = NameType "out" $ ObjectType "java/io/PrintStream"
24 valueOfNT :: NameType Method
25 valueOfNT = NameType "valueOf" $ MethodSignature [IntType] (Returns $ ObjectType "java/lang/Integer")
27 printfNT :: NameType Method
28 printfNT = NameType "printf" $ MethodSignature [ObjectType "java/lang/String",
29 Array Nothing $ ObjectType "java/lang/Object"] (Returns $ ObjectType "java/io/PrintStream")
33 newMethod [ACC_PUBLIC] "<init>" [] ReturnsVoid $ do
35 i1 INVOKESPECIAL (CMethod "java/lang/Object" initNT)
38 newMethod [ACC_PUBLIC, ACC_STATIC] "main" [Array Nothing $ ObjectType "java/lang/String"] ReturnsVoid $ do
40 i1 INVOKESTATIC (CMethod "Test" helloNT)
43 newMethod [ACC_PUBLIC, ACC_STATIC] "hello" [IntType] ReturnsVoid $ do
44 i1 GETSTATIC (CField "java/lang/System" outNT)
45 i8 LDC1 (CString "Здравствуй, мир!")
46 i1 INVOKEVIRTUAL (CMethod "java/io/PrintStream" printlnNT)
47 i1 GETSTATIC (CField "java/lang/System" outNT)
48 i8 LDC1 (CString "Argument: %d\n")
50 i1 ANEWARRAY (CClass "java/lang/Object")
54 i1 INVOKESTATIC (CMethod "java/lang/Integer" valueOfNT)
56 i1 INVOKEVIRTUAL (CMethod "java/io/PrintStream" printfNT)
60 testClass = generate "Test" test
63 B.writeFile "Test.class" (encodeClass testClass)