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 );
17
21 private readonly Func<BadObject[], BadInterfaceConstraint[]> m_ConstraintsFunc;
22
24
25 private readonly Func<BadObject[], BadInterfacePrototype[]> m_InterfacesFunc;
26
27 private readonly Dictionary<int, BadInterfacePrototype> s_GenericCache =
28 new Dictionary<int, BadInterfacePrototype>();
29
34
36
44 public BadInterfacePrototype(string name,
45 Func<BadObject[], BadInterfacePrototype[]> interfaces,
46 BadMetaData? metaData,
47 Func<BadObject[], BadInterfaceConstraint[]> constraints,
48 string[] genericParameters) : base(name,
49 metaData
50 )
51 {
52 m_ConstraintsFunc = constraints;
53 GenericParameters = genericParameters;
54 m_InterfacesFunc = interfaces;
55
56 if (IsGeneric)
57 {
58 GenericName = $"{name}<{string.Join(", ", genericParameters)}>";
59 }
60 else
61 {
62 GenericName = name;
63 }
64 }
65
73 private BadInterfacePrototype(string name,
74 Func<BadObject[], BadInterfacePrototype[]> interfaces,
75 BadMetaData? metaData,
76 Func<BadObject[], BadInterfaceConstraint[]> constraints,
77 string[] genericParameters,
78 BadInterfacePrototype genericDefinition,
79 string genericName) : base(name,
80 metaData
81 )
82 {
83 GenericName = genericName;
84 m_ConstraintsFunc = constraints;
85 m_InterfacesFunc = interfaces;
86 IsResolved = true;
87 GenericParameters = genericParameters;
88 m_GenericDefinition = genericDefinition;
89 }
90
91
92 protected override BadClassPrototype? BaseClass { get; }
93
95 public override bool IsAbstract => true;
96
97 public override IReadOnlyCollection<BadInterfacePrototype> Interfaces =>
98 m_Interfaces ??= m_InterfacesFunc(Array.Empty<BadObject>());
99
103 public IEnumerable<BadInterfaceConstraint> Constraints =>
104 m_Constraints ??= m_ConstraintsFunc(Array.Empty<BadObject>());
105
106#region IBadGenericObject Members
107
108 public bool IsGeneric => GenericParameters.Count != 0;
109
110 public string GenericName { get; }
111
112 public bool IsResolved { get; }
113
114 public IReadOnlyCollection<string> GenericParameters { get; }
115
117 {
118 if (GenericParameters.Count != args.Length)
119 {
120 throw new BadRuntimeException("Invalid Generic Argument Count");
121 }
122
123 if (GenericParameters.Count == 0)
124 {
125 return this;
126 }
127
128 if (IsResolved)
129 {
130 throw new BadRuntimeException("Interface is already resolved");
131 }
132
133 int hash = args[0]
134 .GetHashCode();
135
136 //Add the other arguments to the hash
137 for (int i = 1; i < args.Length; i++)
138 {
139 hash = (hash * 397) ^
140 args[i]
141 .GetHashCode();
142 }
143
144 if (s_GenericCache.TryGetValue(hash, out BadInterfacePrototype? cached))
145 {
146 return cached;
147 }
148
149 BadClassPrototype[] types = args.Cast<BadClassPrototype>()
150 .ToArray();
151
153 _ => m_InterfacesFunc(args),
154 MetaData,
155 _ => m_ConstraintsFunc(args),
156 GenericParameters.ToArray(),
157 this,
158 $"{Name}<{string.Join(", ", types.Select(x => x is IBadGenericObject g ? g.GenericName : x.Name))}>"
159 );
160 s_GenericCache[hash] = result;
161
162 return result;
163 }
164
165#endregion
166
169 {
170 return s_Prototype;
171 }
172
174 public override IEnumerable<BadObject> CreateInstance(BadExecutionContext caller, bool setThis = true)
175 {
176 throw BadRuntimeException.Create(caller.Scope, "Interfaces cannot be instantiated");
177 }
178
180 public override bool IsSuperClassOf(BadClassPrototype proto)
181 {
182 return (GenericParameters.Count != 0 &&
183 proto is BadInterfacePrototype iProto &&
184 iProto.m_GenericDefinition == this) ||
185 base.IsSuperClassOf(proto) ||
186 proto.Interfaces.Any(IsSuperClassOf);
187 }
188
189
191 public override string ToSafeString(List<BadObject> done)
192 {
193 if (IsGeneric)
194 {
195 return IsResolved
196 ? $"interface {GenericName}"
197 : $"interface {Name}<{string.Join(", ", GenericParameters)}>";
198 }
199
200 return $"interface {Name}";
201 }
202}
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.