1 {-# LANGUAGE OverloadedStrings #-}
3 import qualified Data.ByteString.Lazy as B
10 import JVM.Generator.Instructions
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 invokeSpecial "java/lang/Object" initNT
38 newMethod [ACC_PUBLIC, ACC_STATIC] "main" [Array Nothing $ ObjectType "java/lang/String"] ReturnsVoid $ do
40 invokeStatic "Test" helloNT
43 newMethod [ACC_PUBLIC, ACC_STATIC] "hello" [IntType] ReturnsVoid $ do
44 getStaticField "java/lang/System" outNT
45 loadString "Здравствуй, мир!"
46 invokeVirtual "java/io/PrintStream" printlnNT
47 getStaticField "java/lang/System" outNT
48 loadString "Argument: %d\n"
50 allocArray "java/lang/Object"
54 invokeStatic "java/lang/Integer" valueOfNT
56 invokeVirtual "java/io/PrintStream" printfNT
60 testClass = generate "Test" test
63 B.writeFile "Test.class" (encodeClass testClass)