BadScript 2
Loading...
Searching...
No Matches
BadInterfacePrototype.cs
Go to the documentation of this file.
4
6
11{
12 private readonly Dictionary<int, BadInterfacePrototype> s_GenericCache = new Dictionary<int, BadInterfacePrototype>();
17 "Interface",
18 (_, _) => throw new BadRuntimeException("Interfaces cannot be extended")
19 );
20
21 public bool IsGeneric => GenericParameters.Count != 0;
22
23 public string GenericName { get; }
27 private readonly Func<BadObject[], BadInterfaceConstraint[]> m_ConstraintsFunc;
28
29 private readonly Func<BadObject[], BadInterfacePrototype[]> m_InterfacesFunc;
31
36
45 string name,
46 Func<BadObject[], BadInterfacePrototype[]> interfaces,
47 BadMetaData? metaData,
48 Func<BadObject[], BadInterfaceConstraint[]> constraints,
49 string[] genericParameters) : base(
50 name,
51 metaData
52 )
53 {
54 m_ConstraintsFunc = constraints;
55 GenericParameters = genericParameters;
56 m_InterfacesFunc = interfaces;
57 if(IsGeneric)
58 {
59 GenericName = $"{name}<{string.Join(", ", genericParameters)}>";
60 }
61 else
62 {
63 GenericName = name;
64 }
65 }
66
75 string name,
76 Func<BadObject[], BadInterfacePrototype[]> interfaces,
77 BadMetaData? metaData,
78 Func<BadObject[], BadInterfaceConstraint[]> constraints,
79 string[] genericParameters,
80 BadInterfacePrototype genericDefinition,
81 string genericName) : base(
82 name,
83 metaData
84 )
85 {
86 GenericName = genericName;
87 m_ConstraintsFunc = constraints;
88 m_InterfacesFunc = interfaces;
89 IsResolved = true;
90 GenericParameters = genericParameters;
91 m_GenericDefinition = genericDefinition;
92 }
93
94
95 protected override BadClassPrototype? BaseClass { get; }
96
98 public override bool IsAbstract => true;
99
100 public override IReadOnlyCollection<BadInterfacePrototype> Interfaces => m_Interfaces ??= m_InterfacesFunc(Array.Empty<BadObject>());
101
105 public IEnumerable<BadInterfaceConstraint> Constraints => m_Constraints ??= m_ConstraintsFunc(Array.Empty<BadObject>());
106
109 {
110 return s_Prototype;
111 }
112
114 public override IEnumerable<BadObject> CreateInstance(BadExecutionContext caller, bool setThis = true)
115 {
116 throw BadRuntimeException.Create(caller.Scope, "Interfaces cannot be instantiated");
117 }
118
120 public override bool IsSuperClassOf(BadClassPrototype proto)
121 {
122 return GenericParameters.Count != 0 && proto is BadInterfacePrototype iProto && iProto.m_GenericDefinition == this ||
123 base.IsSuperClassOf(proto) ||
124 proto.Interfaces.Any(IsSuperClassOf);
125 }
126
127
129 public override string ToSafeString(List<BadObject> done)
130 {
131 if(IsGeneric)
132 return IsResolved ? $"interface {GenericName}" : $"interface {Name}<{string.Join(", ", GenericParameters)}>";
133 return $"interface {Name}";
134 }
135
136 public bool IsResolved { get; }
138 public IReadOnlyCollection<string> GenericParameters { get; }
139
141 {
142 if(GenericParameters.Count != args.Length)
143 throw new BadRuntimeException("Invalid Generic Argument Count");
144 if(IsResolved)
145 throw new BadRuntimeException("Interface is already resolved");
146
147 int hash = args[0].GetHashCode();
148 //Add the other arguments to the hash
149 for(int i = 1; i < args.Length; i++)
150 {
151 hash = (hash * 397) ^ args[i].GetHashCode();
152 }
153
154 if(s_GenericCache.TryGetValue(hash, out BadInterfacePrototype? cached))
155 {
156 return cached;
157 }
158
159 BadClassPrototype[] types = args.Cast<BadClassPrototype>().ToArray();
161 $"{Name}<{string.Join(", ", types.Select(x=> x is IBadGenericObject g ? g.GenericName : x.Name))}>");
162 s_GenericCache[hash] = result;
163 return result;
164 }
165}
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 Reader Tokens for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains Runtime Interface Objects.