11module Naggum.Assembler.Assembler
22
33open System
4+ open System.Reflection
45open System.Reflection .Emit
56
67open Naggum.Assembler .Representation
@@ -11,9 +12,40 @@ let processMetadataItem = function
1112 | Atom ( Symbol " .entrypoint" ) -> EntryPoint
1213 | other -> failwithf " Unrecognized metadata item definition: %A " other
1314
15+ let resolveAssembly _ =
16+ Assembly.GetAssembly( typeof< Int32>) // TODO: Assembly resolver
17+
18+ let resolveType name =
19+ let result = Type.GetType name // TODO: Resolve types from the assembler context
20+ if isNull result then
21+ failwithf " Type %s could not be found" name
22+
23+ result
24+
25+ let resolveTypes =
26+ List.map ( function
27+ | Atom ( Symbol name) -> resolveType name
28+ | other -> failwithf " Unrecognized type: %A " other)
29+
30+ let processMethodSignature = function
31+ | [ Atom ( Symbol assembly)
32+ Atom ( Symbol typeName)
33+ Atom ( Symbol methodName)
34+ List argumentTypes
35+ Atom ( Symbol returnType)] ->
36+ { Assembly = Some ( resolveAssembly assembly) // TODO: Resolve types from current assembly
37+ ContainingType = Some ( resolveType typeName) // TODO: Resolve methods without a type (e.g. assembly methods)
38+ Name = methodName
39+ ArgumentTypes = resolveTypes argumentTypes
40+ ReturnType = resolveType typeName }
41+ | other -> failwithf " Unrecognized method signature: %A " other
42+
1443let processInstruction = function
15- | List ([ Atom ( Symbol " ldstr" ); Atom ( Symbol string)]) -> Ldstr string
16- | List ([ Atom ( Symbol " call" ); Atom ( Symbol methodName)]) -> failwithf " Method calls are not supported now"
44+ | List ([ Atom ( Symbol " ldstr" ); Atom ( Object (:? string as s))]) ->
45+ Ldstr s
46+ | List ([ Atom ( Symbol " call" ); List ( calleeSignature)]) ->
47+ let signature = processMethodSignature calleeSignature
48+ Call signature
1749 | List ([ Atom ( Symbol " ret" )]) -> Ret
1850 | other -> failwithf " Unrecognized instruction: %A " other
1951
@@ -32,12 +64,18 @@ let addBody body method' =
3264 body
3365
3466let processAssemblyUnit = function
35- | List ( Atom ( Symbol " .method" ) :: Atom ( Symbol name) :: List arguments :: List metadata :: body) ->
67+ | List ( Atom ( Symbol " .method" )
68+ :: Atom ( Symbol name)
69+ :: List argumentTypes
70+ :: Atom ( Symbol returnType)
71+ :: List metadata
72+ :: body) ->
3673 let definition =
3774 { Metadata = Set.empty
3875 Visibility = Public // TODO: Determine method visibility
3976 Name = name
40- ReturnType = typeof< Void> // TODO: Determine method return type
77+ ArgumentTypes = resolveTypes argumentTypes
78+ ReturnType = resolveType returnType
4179 Body = List.empty }
4280 definition
4381 |> addMetadata metadata
0 commit comments