Skip to content

Commit e7fb1b2

Browse files
committed
Changed string to Symbol in Context and everywhere
1 parent d6c421e commit e7fb1b2

4 files changed

Lines changed: 26 additions & 22 deletions

File tree

ngc/Context.fs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,33 @@ open System.Collections.Generic
2525
open System.Reflection
2626
open System.Reflection.Emit
2727

28+
open Naggum.Runtime
2829
open Naggum.Compiler.Reader
2930

3031
type ContextValue =
3132
|Local of LocalBuilder * Type
3233
|Arg of int * Type
3334

3435
type Context =
35-
val types : Dictionary<string,Type>
36-
val functions : Dictionary<string, (Type list -> MethodInfo)>
37-
val locals : Dictionary<string,ContextValue>
36+
val types : Dictionary<Symbol,Type>
37+
val functions : Dictionary<Symbol, (Type list -> MethodInfo)>
38+
val locals : Dictionary<Symbol,ContextValue>
3839
new (t,f,l) =
3940
{types = t; functions = f; locals = l}
4041
new (ctx : Context) =
41-
let t = new Dictionary<string, Type>(ctx.types)
42-
let f = new Dictionary<string, (Type list -> MethodInfo)>(ctx.functions)
43-
let l = new Dictionary<string,ContextValue>(ctx.locals)
42+
let t = new Dictionary<Symbol, Type>(ctx.types)
43+
let f = new Dictionary<Symbol, (Type list -> MethodInfo)>(ctx.functions)
44+
let l = new Dictionary<Symbol,ContextValue>(ctx.locals)
4445
new Context (t,f,l)
4546
new() =
46-
let t = new Dictionary<string, Type>()
47-
let f = new Dictionary<string, (Type list -> MethodInfo)>()
48-
let l = new Dictionary<string,ContextValue>()
47+
let t = new Dictionary<Symbol, Type>()
48+
let f = new Dictionary<Symbol, (Type list -> MethodInfo)>()
49+
let l = new Dictionary<Symbol,ContextValue>()
4950
new Context (t,f,l)
5051

5152
member public this.loadAssembly(asm:Assembly) =
5253
let types = List.ofArray (asm.GetTypes())
53-
List.iter (fun (t:Type) -> this.types.Add(t.FullName,t)) types
54+
List.iter (fun (t:Type) -> this.types.Add(new Symbol(t.FullName),t)) types
5455

5556
let create () =
5657
let context = new Context()

ngc/FormGenerator.fs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type SymbolGenerator(context:Context,name:string) =
4747
interface IGenerator with
4848
member this.Generate ilGen =
4949
try
50-
let ctxval = context.locals.[name]
50+
let ctxval = context.locals.[new Symbol(name)]
5151
match ctxval with
5252
|Local (local, _) ->
5353
ilGen.Emit(OpCodes.Ldloc,local)
@@ -56,7 +56,7 @@ type SymbolGenerator(context:Context,name:string) =
5656
with
5757
| :? KeyNotFoundException -> failwithf "Symbol %A not bound." name
5858
member this.ReturnTypes () =
59-
match context.locals.[name] with
59+
match context.locals.[new Symbol(name)] with
6060
|Local (_,t) -> [t]
6161
|Arg (_,t) -> [t]
6262

@@ -121,7 +121,7 @@ type LetGenerator(context:Context,typeBuilder:TypeBuilder,bindings:SExp,body:SEx
121121
let generator = gf.MakeGenerator scope_subctx form
122122
let local_type = List.head (generator.ReturnTypes())
123123
let local = ilGen.DeclareLocal(local_type)
124-
scope_subctx.locals.[name] <- Local (local, local_type)
124+
scope_subctx.locals.[new Symbol(name)] <- Local (local, local_type)
125125
generator.Generate ilGen
126126
ilGen.Emit (OpCodes.Stloc,local)
127127
| other -> failwithf "In let bindings: Expected: (name (form))\nGot: %A\n" other
@@ -137,7 +137,7 @@ type LetGenerator(context:Context,typeBuilder:TypeBuilder,bindings:SExp,body:SEx
137137
match binding with
138138
| List [(Atom (Symbol name)); form] ->
139139
let generator = gf.MakeGenerator type_subctx form
140-
type_subctx.locals.[name] <- Local (null,generator.ReturnTypes() |> List.head)
140+
type_subctx.locals.[new Symbol(name)] <- Local (null,generator.ReturnTypes() |> List.head)
141141
| other -> failwithf "In let bindings: Expected: (name (form))\nGot: %A\n" other
142142
| other -> failwithf "In let form: expected: list of bindings\nGot: %A" other
143143
(gf.MakeBody type_subctx body).ReturnTypes()
@@ -181,7 +181,7 @@ type FullIfGenerator(context:Context,typeBuilder:TypeBuilder,condition:SExp,if_t
181181

182182
type FunCallGenerator(context:Context,typeBuilder:TypeBuilder,fname:string,arguments:SExp list,gf:IGeneratorFactory) =
183183
let args_seq = gf.MakeSequence context arguments
184-
let func = context.functions.[fname] <| args_seq.ReturnTypes()
184+
let func = context.functions.[new Symbol(fname)] <| args_seq.ReturnTypes()
185185
interface IGenerator with
186186
member this.Generate ilGen =
187187
args_seq.Generate ilGen
@@ -190,15 +190,15 @@ type FunCallGenerator(context:Context,typeBuilder:TypeBuilder,fname:string,argum
190190
[func.ReturnType]
191191

192192
type DefunGenerator(context:Context,typeBuilder:TypeBuilder,fname:string,parameters:SExp list,body:SExp list,gf:IGeneratorFactory) =
193-
do context.functions.[fname] <- (fun arg_types ->
193+
do context.functions.[new Symbol(fname)] <- (fun arg_types ->
194194
let methodGen = typeBuilder.DefineMethod(fname, MethodAttributes.Public ||| MethodAttributes.Static, typeof<obj>, (Array.ofList arg_types))
195195
let methodILGen = (methodGen.GetILGenerator())
196196
let fun_ctx = new Context(context)
197197
for parm in parameters do
198198
match parm with
199199
| Atom(Symbol parm_name) ->
200200
let parm_idx = (List.findIndex (fun (p) -> p = parm) parameters)
201-
fun_ctx.locals.[parm_name] <- Arg (parm_idx,arg_types.[parm_idx])
201+
fun_ctx.locals.[new Symbol(parm_name)] <- Arg (parm_idx,arg_types.[parm_idx])
202202
| other -> failwithf "In function %A parameter definition:\nExpected: Atom(Symbol)\nGot: %A" fname parm
203203
let bodyGen = gf.MakeBody fun_ctx body
204204
bodyGen.Generate methodILGen
@@ -253,21 +253,21 @@ type NewObjGenerator(context : Context, typeBuilder : TypeBuilder, typeName : st
253253
if typeName.StartsWith "System" then
254254
Type.GetType typeName
255255
else
256-
context.types.[typeName]
256+
context.types.[new Symbol(typeName)]
257257
let arg_types = args_gen.Generate ilGen
258258
ilGen.Emit(OpCodes.Newobj,objType.GetConstructor(Array.ofList argTypes))
259259
member this.ReturnTypes () =
260260
if typeName.StartsWith "System" then
261261
[Type.GetType typeName]
262262
else
263-
[context.types.[typeName]]
263+
[context.types.[new Symbol(typeName)]]
264264

265265
type TypeGenerator(context : Context, typeBuilder : TypeBuilder, typeName : string, parentTypeName: string, members : SExp list, gf : IGeneratorFactory) =
266266
let newTypeBuilder =
267267
if parentTypeName = "" then
268268
Globals.ModuleBuilder.DefineType(typeName, TypeAttributes.Class ||| TypeAttributes.Public, typeof<obj>)
269269
else
270-
Globals.ModuleBuilder.DefineType(typeName, TypeAttributes.Class ||| TypeAttributes.Public, context.types.[parentTypeName])
270+
Globals.ModuleBuilder.DefineType(typeName, TypeAttributes.Class ||| TypeAttributes.Public, context.types.[new Symbol(parentTypeName)])
271271
let newGeneratorFactory = gf.MakeGeneratorFactory newTypeBuilder
272272
let mutable fields : string list = []
273273

@@ -283,7 +283,7 @@ type TypeGenerator(context : Context, typeBuilder : TypeBuilder, typeName : stri
283283
match parm with
284284
| Atom(Symbol parm_name) ->
285285
let parm_idx = (List.findIndex (fun (p) -> p = parm) method_parms)
286-
method_ctx.locals.[parm_name] <- Arg (parm_idx,typeof<obj>)
286+
method_ctx.locals.[new Symbol(parm_name)] <- Arg (parm_idx,typeof<obj>)
287287
| other -> failwithf "In method %A%A parameter definition:\nExpected: Atom(Symbol)\nGot: %A" typeName method_name parm
288288
let body_gen = newGeneratorFactory.MakeBody method_ctx method_body
289289
body_gen.Generate (method_gen.GetILGenerator())

ngc/GeneratorFactory.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ open StringGen
2525
open FormGenerator
2626
open ClrGenerator
2727
open Context
28+
open Naggum.Runtime
2829
open Naggum.MaybeMonad
2930
open Naggum.Compiler.Reader
3031
open Naggum.Compiler.MathGenerator
@@ -86,7 +87,7 @@ type GeneratorFactory(typeBldr:TypeBuilder) =
8687
new InstanceCallGenerator(context, typeBldr, instance, fname, args, this) :> IGenerator
8788
| Atom (Symbol fname) :: args -> //generic funcall pattern
8889
let tryGetType typeName =
89-
try Some (context.types.[typeName]) with
90+
try Some (context.types.[new Symbol(typeName)]) with
9091
| _ ->
9192
try Some (Type.GetType typeName) with
9293
| _ -> None

ngc/ngc.fsproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1313
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
1414
<Name>ngc</Name>
15+
<ReferencePath>
16+
</ReferencePath>
1517
</PropertyGroup>
1618
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1719
<DebugSymbols>true</DebugSymbols>

0 commit comments

Comments
 (0)