@@ -19,25 +19,64 @@ type AssignmentOperator =
1919 | AssignXorBitwise
2020 | AssignAndBitwise
2121
22+ type TypeInfo =
23+ {
24+ IsRecord: bool
25+ IsUnion: bool
26+ }
27+
2228type Type =
2329 // Built in
24- | Integer
25- | Double
26- | Boolean
27- | String
28-
2930 | Object
3031 | Dynamic
32+
3133 | Void
3234 | MetaType
3335
36+ | Integer
37+ | Double
38+ | Boolean
39+ | String
40+
3441 | List of Type
3542 | Nullable of Type
3643
3744 | Generic of name : string
38- | TypeReference of Ident * generics : Type list
45+ | TypeReference of Ident * generics : Type list * info : TypeInfo
3946 | Function of argTypes : Type list * returnType : Type
4047
48+ static member reference ( ident , ? generics , ? isRecord , ? isUnion ) =
49+ let info : TypeInfo = { IsRecord = defaultArg isRecord false ; IsUnion = defaultArg isUnion false }
50+ TypeReference( ident, defaultArg generics [], info)
51+
52+ static member needsCast ( source : Type ) ( target : Type ) =
53+ match source, target with
54+ | _, Object
55+ | _, Dynamic -> false
56+ | Void, Void
57+ | MetaType, MetaType
58+ | Integer, Integer
59+ | Double, Double
60+ | Boolean, Boolean
61+ | String, String -> false
62+
63+ | List source, List target -> Type.needsCast source target
64+ | Nullable source, Nullable target -> Type.needsCast source target
65+ | source, Nullable target -> Type.needsCast source target
66+ | Generic source, Generic target -> source <> target
67+
68+ // We should be able to detect class hierarchy here too
69+ | TypeReference( sourceIdent, sourceGen, _),
70+ TypeReference( targetIdent, targetGen, _) ->
71+ not (
72+ sourceIdent.Name = targetIdent.Name
73+ && sourceIdent.ImportModule = targetIdent.ImportModule
74+ && sourceGen.Length = targetGen.Length
75+ && not ( List.zip sourceGen targetGen |> List.exists ( fun ( s , t ) -> Type.needsCast s t))
76+ )
77+
78+ | _ -> true
79+
4180type Ident =
4281 { ImportModule: string option
4382 Name: string
@@ -206,7 +245,7 @@ type Statement =
206245 static member whileStatement ( test , body ) =
207246 WhileStatement( test, body)
208247 static member breakStatement (? label ) =
209- BreakStatement( label)
248+ BreakStatement( label)
210249 static member continueStatement (? label ) =
211250 ContinueStatement( label)
212251 static member tryStatement ( body , ? handlers , ? finalizer ) =
@@ -215,8 +254,9 @@ type Statement =
215254 addToScope ident.Name
216255 LocalVariableDeclaration( ident, kind, value)
217256 /// Variables that won't be added to scope
218- static member tempVariableDeclaration ( ident : Ident , ? value ) =
219- LocalVariableDeclaration( ident, Final, value)
257+ static member tempVariableDeclaration ( ident : Ident , ? isMutable , ? value ) =
258+ let isMutable = defaultArg isMutable false
259+ LocalVariableDeclaration( ident, ( if isMutable then Var else Final), value)
220260 static member functionDeclaration ( name : string , args : FunctionArg list , body : Statement list , returnType : Type , ? genParams : GenericParam list ) =
221261 LocalFunctionDeclaration {
222262 Name = name
0 commit comments