BadScript 2
Loading...
Searching...
No Matches
BadInterfacePrototype.cs
Go to the documentation of this file.
3
5
10{
15 (_, _) => throw new BadRuntimeException("Interfaces cannot be extended"),
16 null
17 );
18
22 private readonly Func<BadObject[], BadInterfaceConstraint[]> m_ConstraintsFunc;
23
25
26 private readonly Func<BadObject[], BadInterfacePrototype[]> m_InterfacesFunc;
27
28 private readonly Dictionary<int, BadInterfacePrototype> s_GenericCache =
29 new Dictionary<int, BadInterfacePrototype>();
30
35
37
45 public BadInterfacePrototype(string name,
46 Func<BadObject[], BadInterfacePrototype[]> interfaces,
47 BadMetaData? metaData,
48 Func<BadObject[], BadInterfaceConstraint[]> constraints,
49 string[] genericParameters) : base(name,
50 metaData
51 )
52 {
53 m_ConstraintsFunc = constraints;
54 GenericParameters = genericParameters;
55 m_InterfacesFunc = interfaces;
56
57 if (IsGeneric)
58 {
59 GenericName = $"{name}<{string.Join(", ", genericParameters)}>";
60 }
61 else
62 {
63 GenericName = name;
64 }
65 }
66
74 private BadInterfacePrototype(string name,
75 Func<BadObject[], BadInterfacePrototype[]> interfaces,
76 BadMetaData? metaData,
77 Func<BadObject[], BadInterfaceConstraint[]> constraints,
78 string[] genericParameters,
79 BadInterfacePrototype genericDefinition,
80 string genericName) : base(name,
81 metaData
82 )
83 {
84 GenericName = genericName;
85 m_ConstraintsFunc = constraints;
86 m_InterfacesFunc = interfaces;
87 IsResolved = true;
88 GenericParameters = genericParameters;
89 m_GenericDefinition = genericDefinition;
90 }
91
92
93 protected override BadClassPrototype? BaseClass { get; }
94
96 public override bool IsAbstract => true;
97
98 public override IReadOnlyCollection<BadInterfacePrototype> Interfaces =>
99 m_Interfaces ??= m_InterfacesFunc(Array.Empty<BadObject>());
100
104 public IEnumerable<BadInterfaceConstraint> Constraints =>
105 m_Constraints ??= m_ConstraintsFunc(Array.Empty<BadObject>());
106
107#region IBadGenericObject Members
108
109 public bool IsGeneric => GenericParameters.Count != 0;
110
111 public string GenericName { get; }
112
113 public bool IsResolved { get; }
114
115 public IReadOnlyCollection<string> GenericParameters { get; }
116
118 {
119 if (GenericParameters.Count != args.Length)
120 {
121 throw new BadRuntimeException("Invalid Generic Argument Count");
122 }
123
124 if (GenericParameters.Count == 0)
125 {
126 return this;
127 }
128
129 if (IsResolved)
130 {
131 throw new BadRuntimeException("Interface is already resolved");
132 }
133
134 int hash = args[0]
135 .GetHashCode();
136
137 //Add the other arguments to the hash
138 for (int i = 1; i < args.Length; i++)
139 {
140 hash = (hash * 397) ^
141 args[i]
142 .GetHashCode();
143 }
144
145 if (s_GenericCache.TryGetValue(hash, out BadInterfacePrototype? cached))
146 {
147 return cached;
148 }
149
150 BadClassPrototype[] types = args.Cast<BadClassPrototype>()
151 .ToArray();
152
154 _ => m_InterfacesFunc(args),
155 MetaData,
156 _ => m_ConstraintsFunc(args),
157 GenericParameters.ToArray(),
158 this,
159 $"{Name}<{string.Join(", ", types.Select(x => x is IBadGenericObject g ? g.GenericName : x.Name))}>"
160 );
161 s_GenericCache[hash] = result;
162
163 return result;
164 }
165
166#endregion
167
170 {
171 return s_Prototype;
172 }
173
175 public override IEnumerable<BadObject> CreateInstance(BadExecutionContext caller, bool setThis = true)
176 {
177 throw BadRuntimeException.Create(caller.Scope, "Interfaces cannot be instantiated");
178 }
179
181 public override bool IsSuperClassOf(BadClassPrototype proto)
182 {
183 return (GenericParameters.Count != 0 &&
184 proto is BadInterfacePrototype iProto &&
185 iProto.m_GenericDefinition == this) ||
186 base.IsSuperClassOf(proto) ||
187 proto.Interfaces.Any(IsSuperClassOf);
188 }
189
190
192 public override string ToSafeString(List<BadObject> done)
193 {
194 if (IsGeneric)
195 {
196 return IsResolved
197 ? $"interface {GenericName}"
198 : $"interface {Name}<{string.Join(", ", GenericParameters)}>";
199 }
200
201 return $"interface {Name}";
202 }
203}
Implements a Meta Data container for an expression.
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
Implements a Class Prototype for the BadScript Language.
IReadOnlyCollection< BadInterfacePrototype > Interfaces
The Implemented Interfaces.
readonly BadMetaData MetaData
The Metadata of the Class.
BadInterfacePrototype(string name, Func< BadObject[], BadInterfacePrototype[]> interfaces, BadMetaData? metaData, Func< BadObject[], BadInterfaceConstraint[]> constraints, string[] genericParameters, BadInterfacePrototype genericDefinition, string genericName)
Creates a new Interface Prototype.
bool IsResolved
Indicates if the Object was already resolved to a concrete type.
static readonly BadClassPrototype s_Prototype
The Prototype for the Interface Prototype.
BadObject CreateGeneric(BadObject[] args)
Resolves the Generic Object to a concrete type.
override IEnumerable< BadObject > CreateInstance(BadExecutionContext caller, bool setThis=true)
override bool IsSuperClassOf(BadClassPrototype proto)
Returns true if the Class is a Subclass of the given Class.true if the Class is a Subclass of the giv...
BadInterfacePrototype(string name, Func< BadObject[], BadInterfacePrototype[]> interfaces, BadMetaData? metaData, Func< BadObject[], BadInterfaceConstraint[]> constraints, string[] genericParameters)
Creates a new Interface Prototype.
IEnumerable< BadInterfaceConstraint > Constraints
The Constraints of this Interface.
readonly Func< BadObject[], BadInterfacePrototype[]> m_InterfacesFunc
IReadOnlyCollection< string > GenericParameters
The Generic Parameters of the Object.
readonly Func< BadObject[], BadInterfaceConstraint[]> m_ConstraintsFunc
Backing Function of the Constraints Property.
override IReadOnlyCollection< BadInterfacePrototype > Interfaces
BadInterfaceConstraint?[] m_Constraints
The Constraints of this Interface.
bool IsGeneric
Indicates if the Object is a Generic Object.
readonly Dictionary< int, BadInterfacePrototype > s_GenericCache
Contains the Parser for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains Runtime Interface Objects.