@@ -48,16 +48,16 @@ type SymbolGenerator(context:Context,name:string) =
4848 try
4949 let ctxval = context.locals.[ name]
5050 match ctxval with
51- | Local ( local, t ) ->
51+ | Local ( local, _ ) ->
5252 ilGen.Emit( OpCodes.Ldloc, local)
53- | Arg index ->
53+ | Arg ( index,_) ->
5454 ilGen.Emit( OpCodes.Ldarg,( int16 index))
5555 with
5656 | :? KeyNotFoundException -> failwithf " Symbol %A not bound." name
5757 member this.ReturnTypes () =
5858 match context.locals.[ name] with
5959 | Local (_, t) -> [ t]
60- | Arg _ -> [ typeof < obj > ]
60+ | Arg (_, t ) -> [ t ]
6161
6262
6363type SequenceGenerator ( context : Context , typeBuilder : TypeBuilder , seq : SExp list , gf : IGeneratorFactory ) =
@@ -179,32 +179,33 @@ type FullIfGenerator(context:Context,typeBuilder:TypeBuilder,condition:SExp,if_t
179179 List.concat ( Seq.ofList [ true_ ret_ type; false_ ret_ type]) //TODO This should return closest common ancestor of these types
180180
181181type FunCallGenerator ( context : Context , typeBuilder : TypeBuilder , fname : string , arguments : SExp list , gf : IGeneratorFactory ) =
182+ let args_seq = gf.MakeSequence context arguments
183+ let func = context.functions.[ fname] <| args_ seq.ReturnTypes()
182184 interface IGenerator with
183185 member this.Generate ilGen =
184- let func = context.functions.[ fname]
185- let args_seq = gf.MakeSequence context arguments
186- let arg_types = args_ seq.ReturnTypes()
187186 args_ seq.Generate ilGen
188187 ilGen.Emit( OpCodes.Call, func)
189188 member this.ReturnTypes () =
190- let func = context.functions.[ fname]
191189 [ func.ReturnType]
192190
193191type DefunGenerator ( context : Context , typeBuilder : TypeBuilder , fname : string , parameters : SExp list , body : SExp list , gf : IGeneratorFactory ) =
192+ do context.functions.[ fname] <- ( fun arg_types ->
193+ let methodGen = typeBuilder.DefineMethod( fname, MethodAttributes.Public ||| MethodAttributes.Static, typeof< obj>, ( Array.ofList arg_ types))
194+ let methodILGen = ( methodGen.GetILGenerator())
195+ let fun_ctx = new Context( context)
196+ for parm in parameters do
197+ match parm with
198+ | Atom( Symbol parm_ name) ->
199+ let parm_idx = ( List.findIndex ( fun ( p ) -> p = parm) parameters)
200+ fun_ ctx.locals.[ parm_ name] <- Arg ( parm_ idx, arg_ types.[ parm_ idx])
201+ | other -> failwithf " In function %A parameter definition:\n Expected: Atom(Symbol)\n Got: %A " fname parm
202+ let bodyGen = gf.MakeBody fun_ ctx body
203+ bodyGen.Generate methodILGen
204+ methodILGen.Emit( OpCodes.Ret)
205+ methodGen :> MethodInfo)
194206 interface IGenerator with
195207 member this.Generate ilGen =
196- let argsDef = Array.create ( List.length parameters) typeof< obj>
197- let methodGen = typeBuilder.DefineMethod( fname, MethodAttributes.Public ||| MethodAttributes.Static, typeof< obj>, argsDef)
198- let methodILGen = ( methodGen.GetILGenerator())
199- context.functions.[ fname] <- methodGen
200- let fun_ctx = new Context( context)
201- for parm in parameters do
202- match parm with
203- | Atom( Symbol parm_ name) -> fun_ ctx.locals.[ parm_ name] <- Arg ( List.findIndex ( fun ( p ) -> p = parm) parameters)
204- | other -> failwithf " In function %A parameter definition:\n Expected: Atom(Symbol)\n Got: %A " fname parm
205- let bodyGen = gf.MakeBody fun_ ctx body
206- bodyGen.Generate methodILGen
207- methodILGen.Emit( OpCodes.Ret)
208+ ()
208209 member this.ReturnTypes () =
209210 [ typeof< Void>]
210211
0 commit comments