Skip to content

Commit ec160f1

Browse files
author
Louis Jenkins
committed
Added documentation that can be picked up by Haddock
1 parent ee9369b commit ec160f1

File tree

6 files changed

+31
-30
lines changed

6 files changed

+31
-30
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Documentation/

VirtualMachine/ByteCode.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module VirtualMachine.ByteCode where
2121
8 -> readIORef (current_class env) >>= \c -> (return . VString . show . utf8_bytes) (constant_pool c !! (fromIntegral . string_index $ info))
2222
_ -> error $ "Bad Tag: " ++ show (tag info)
2323

24-
{- Starting point of execution of ByteCode isntructions -}
24+
{- | Starting point of execution of ByteCode isntructions -}
2525
execute :: Runtime_Environment -> IO ()
2626
execute env = head <$> readIORef (stack env) -- Take the head of the stack (current stack frame)
2727
>>= \frame -> when (debug_mode env) (debugFrame frame >>= putStrLn) -- Optional Debug
@@ -91,7 +91,7 @@ module VirtualMachine.ByteCode where
9191
utf8_name = cpool !! fromIntegral (name_index name_and_type)
9292
in show . utf8_bytes $ utf8_name
9393

94-
{- Loads from local_variables to operand_stack -}
94+
{- | Loads from local_variables to operand_stack -}
9595
loadOp :: StackFrame -> ByteCode -> IO ()
9696
loadOp frame bc
9797
-- Loads with the index as the next bytecode instruction
@@ -100,7 +100,7 @@ module VirtualMachine.ByteCode where
100100
| bc >= 26 && bc <= 45 = getLocal' frame ((bc - 26) `mod` 4) >>= pushOp frame
101101
| otherwise = error $ "Bad ByteCode Instruction: " ++ show bc
102102

103-
{- Stores from operand_stack to local_variables -}
103+
{- | Stores from operand_stack to local_variables -}
104104
storeOp :: StackFrame -> ByteCode -> IO ()
105105
storeOp frame bc
106106
-- Stores with the index as the next bytecode instruction
@@ -118,7 +118,7 @@ module VirtualMachine.ByteCode where
118118
| otherwise = error $ "Bad ByteCode Instruction: " ++ show bc
119119

120120

121-
{- Stores constant values on the Operand Stack. -}
121+
{- | Stores constant values on the Operand Stack. -}
122122
constOp :: StackFrame -> ByteCode -> IO ()
123123
constOp frame bc
124124
-- Null Reference
@@ -134,7 +134,7 @@ module VirtualMachine.ByteCode where
134134
-- ERROR
135135
| otherwise = error $ "Bad ByteCode Instruction: " ++ show bc
136136

137-
{- Math operations which are abstracted by the Value type. -}
137+
{- | Math operations which are abstracted by the Value type. -}
138138
mathOp :: StackFrame -> ByteCode -> IO ()
139139
mathOp frame bc
140140
| bc >= 96 && bc <= 99 = applyBinaryOp (+)
@@ -160,7 +160,7 @@ module VirtualMachine.ByteCode where
160160
increment = -- 'iinc' has local variable index as first, with value as second indice
161161
join $ modifyLocal frame <$> getNextBC frame <*> ((+) . fromIntegral <$> getNextBC frame)
162162

163-
{- Conditional jump instructions ('if', 'for', and 'while') -}
163+
{- | Conditional jump instructions ('if', 'for', and 'while') -}
164164
cmpOp :: StackFrame -> ByteCode -> IO ()
165165
cmpOp frame bc
166166
| bc >= 148 && bc <= 152 = pushCmp
@@ -199,15 +199,15 @@ module VirtualMachine.ByteCode where
199199
-1 -> LT
200200
_ -> error "Bad Enumerable Value! "
201201

202-
{- Obtains the next ByteCode instruction and increments the PC -}
202+
{- | Obtains the next ByteCode instruction and increments the PC -}
203203
getNextBC :: StackFrame -> IO ByteCode
204204
getNextBC frame = readIORef frame >>= \f -> -- Read StackFrame from passed reference
205205
let segment = code_segment f in -- Retrieve current set of instructions
206206
readIORef (program_counter segment) >>= \n -> -- Read current ByteCode instruction
207207
modifyIORef (program_counter segment) (+1) >> -- Increment PC
208208
return (byte_code segment !! fromIntegral n) -- Return ByteCode instruction
209209

210-
{- Obtains the next two ByteCode instructions as a short, and increments the PC by 2 -}
210+
{- | Obtains the next two ByteCode instructions as a short, and increments the PC by 2 -}
211211
getNextShort :: StackFrame -> IO Word16
212212
getNextShort frame = replicateM 2 (getNextBC frame) >>= \(i1:i2:_) ->
213213
return $ fromIntegral i1 `shift` 8 .|. fromIntegral i2

VirtualMachine/Environment.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ module VirtualMachine.Environment where
88
import VirtualMachine.ByteCode
99
import Data.Maybe
1010

11-
{- Starting point for the runtime, invoked to instantiate the runtime environment -}
11+
{- | Starting point for the runtime, invoked to instantiate the runtime environment -}
1212
init :: Bool -> IO Runtime_Environment
1313
init debug = Environment <$> newIORef undefined -- 'current_class' is not available yet
1414
<*> newIORef Map.empty -- 'class_map' is originally empty
1515
<*> newIORef [] -- The 'stack' is initially empty
1616
<*> return debug -- 'debug' is used for conditional logging
1717

18-
{- Minimal bootstrap class loader -}
18+
{- | Minimal bootstrap class loader -}
1919
loadClass :: Runtime_Environment -> ClassFile -> IO ()
2020
loadClass env cf =
2121
toClass cf >>= \c -> modifyIORef' (class_map env) (Map.insert classString c)
@@ -28,7 +28,7 @@ module VirtualMachine.Environment where
2828
name = cp !! fromIntegral (name_index class_info)
2929
in show $ utf8_bytes name
3030

31-
{- Primer for the runtime, which sets up and executes the 'main' method -}
31+
{- | Primer for the runtime, which sets up and executes the 'main' method -}
3232
start :: Runtime_Environment -> IO ()
3333
start env = (fromJust . Map.lookup "main") <$> -- Look for "main" function (result is a pair)
3434
((snd . head . Map.toList) <$> -- Take the first (only) result and the second of pair (class)

VirtualMachine/Stack.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ module VirtualMachine.Stack where
66
import VirtualMachine.Stack_Frame
77
import VirtualMachine.ByteCode
88

9-
{- Obtains the current stack frame -}
9+
{- | Obtains the current stack frame -}
1010
getCurrentFrame :: Runtime_Environment -> IO StackFrame
1111
getCurrentFrame env = head <$> readIORef (stack env)
1212

13-
{- Pops the current stack frame off of the stack -}
13+
{- | Pops the current stack frame off of the stack -}
1414
popFrame :: Runtime_Environment -> IO ()
1515
popFrame env = modifyIORef' (stack env) tail
1616

17-
{- Create a frame for the passed method and pushes on stack -}
17+
{- | Create a frame for the passed method and pushes on stack -}
1818
pushFrame :: Runtime_Environment -> Method -> IO ()
1919
pushFrame env meth = createFrame meth >>= \f -> modifyIORef (stack env) ((:) f)

VirtualMachine/Stack_Frame.hs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module VirtualMachine.Stack_Frame where
66
import VirtualMachine.Types
77
import Data.Array.IO
88

9-
{-
9+
{- |
1010
Constructs a stack frame from the passed method. Each stack frame keeps track
1111
of it's local variables, operand stack, and code segment, which is composed
1212
of the instructions and the current program counter.
@@ -32,39 +32,39 @@ module VirtualMachine.Stack_Frame where
3232
| n > 0 = (:) <$> newIORef (VReference 0) <*> createLocals (n - 1)
3333
| otherwise = error $ "Error while attempting to create locals: n=" ++ show n
3434

35-
{- Obtain the reference to the PC of this stack frame -}
35+
{- | Obtain the reference to the PC of this stack frame -}
3636
getPC :: StackFrame -> IO (IORef Word32)
3737
getPC frame = program_counter . code_segment <$> readIORef frame
3838

39-
{- Obtain the PC of this stack frame -}
39+
{- | Obtain the PC of this stack frame -}
4040
getPC' :: Integral a => StackFrame -> IO a
4141
getPC' frame = getPC frame >>= \f -> fromIntegral <$> readIORef f
4242

43-
{- Set the PC of this stack frame -}
43+
{- | Set the PC of this stack frame -}
4444
setPC :: Integral a => StackFrame -> a -> IO ()
4545
setPC frame pc = getPC frame >>= flip writeIORef (fromIntegral pc)
4646

47-
{- Mutate the PC of this stack frame -}
47+
{- | Mutate the PC of this stack frame -}
4848
modifyPC :: Integral a => StackFrame -> (a -> a) -> IO ()
4949
modifyPC frame f = (f <$> getPC' frame) >>= setPC frame
5050

51-
{- Maximum PC for this stack frame (without going out of bounds) -}
51+
{- | Maximum PC for this stack frame (without going out of bounds) -}
5252
maxPC :: Integral a => StackFrame -> IO a
5353
maxPC frame = fromIntegral . length <$> getInstructions frame
5454

5555
getInstructions :: StackFrame -> IO Instructions
5656
getInstructions frame = byte_code . code_segment <$> readIORef frame
5757

58-
{- Pushes a value on the operand stack -}
58+
{- | Pushes a value on the operand stack -}
5959
pushOp :: StackFrame -> Value -> IO ()
6060
pushOp frame val = (operand_stack <$> readIORef frame) >>= flip modifyIORef (val :)
6161

62-
{- Pops the operand off of the stack -}
62+
{- | Pops the operand off of the stack -}
6363
popOp :: StackFrame -> IO Value
6464
popOp frame = readIORef frame >>= \f -> readIORef (operand_stack f)
6565
>>= \ops -> writeIORef (operand_stack f) (tail ops) >> return (head ops)
6666

67-
{-
67+
{- |
6868
Pushes a value as a local variable at the given index.
6969
-}
7070
putLocal :: (Integral a) => StackFrame -> a -> Value -> IO ()
@@ -73,10 +73,10 @@ module VirtualMachine.Stack_Frame where
7373
modifyLocal :: (Integral a) => StackFrame -> a -> (Value -> Value) -> IO ()
7474
modifyLocal frame idx f = getLocal frame idx >>= flip modifyIORef f
7575

76-
{- Returns the value of the local variable. -}
76+
{- | Returns the value of the local variable. -}
7777
getLocal' :: (Integral a) => StackFrame -> a -> IO Value
7878
getLocal' frame idx = readIORef frame >>= \f -> readIORef (local_variables f !! fromIntegral idx)
7979

80-
{- Returns a reference to the local variable -}
80+
{- | Returns a reference to the local variable -}
8181
getLocal :: Integral a => StackFrame -> a -> IO (IORef Value)
8282
getLocal frame idx = (!! fromIntegral idx) . local_variables <$> readIORef frame

VirtualMachine/Types.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,33 +97,33 @@ module VirtualMachine.Types where
9797

9898
type Operand = Value
9999

100-
{- Runtime representation of a method -}
100+
{- | Runtime representation of a method -}
101101
data Method = Method {
102102
method_code :: Instructions,
103103
method_locals :: Word16
104104
}
105105

106-
{- Runtime representation of a field -}
106+
{- | Runtime representation of a field -}
107107
data Field = Field {
108108
field_value :: Value
109109
}
110110

111-
{- Runtime representation of a Class -}
111+
{- | Runtime representation of a Class -}
112112
data Class = Class {
113113
constant_pool :: [CP_Info],
114114
method_map :: IORef (Map String Method),
115115
field_map :: IORef (Map String Field)
116116
}
117117

118-
{- Representation of a runtime environment -}
118+
{- | Representation of a runtime environment -}
119119
data Runtime_Environment = Environment {
120120
current_class :: IORef Class,
121121
class_map :: IORef (Map String Class),
122122
stack :: Stack,
123123
debug_mode :: Bool
124124
}
125125

126-
{- Representation of a stack frame. -}
126+
{- | Representation of a stack frame. -}
127127
data Stack_Frame = Frame {
128128
local_variables :: [Local_Variable],
129129
operand_stack :: IORef [Operand],

0 commit comments

Comments
 (0)