BadScript 2
Loading...
Searching...
No Matches
BadNewExpression.cs
Go to the documentation of this file.
8
10
15{
21 public BadNewExpression(BadInvocationExpression right, BadSourcePosition position) : base(false, position)
22 {
23 Right = right;
24 }
25
30
32 public override void Optimize()
33 {
35 }
36
38 public override IEnumerable<BadExpression> GetDescendants()
39 {
41 }
42
55 public static IEnumerable<BadObject> CreateObject(BadClassPrototype proto,
56 BadExecutionContext context,
57 BadObject[] args,
59 {
61
62 if (proto == BadVoidPrototype.Instance)
63 {
64 throw BadRuntimeException.Create(context.Scope, "Cannot create instance of void", pos);
65 }
66
67 if (proto is BadANativeClassPrototype nativeType)
68 {
69 foreach (BadObject o in nativeType.CreateInstance(context, args))
70 {
71 yield return o;
72 }
73
74 yield break;
75 }
76
77 foreach (BadObject o in proto.CreateInstance(context))
78 {
79 obj = o;
80
81 yield return o;
82 }
83
84 obj = obj.Dereference(pos);
85
86 if (obj is not BadClass cls)
87 {
88 throw new BadRuntimeException("Cannot create object from non-class type", pos);
89 }
90
91 //Call Constructor if exists
92
93 if (cls.HasProperty(BadStaticKeys.CONSTRUCTOR_NAME, cls.Scope))
94 {
95 BadObject ctor = cls.GetProperty(BadStaticKeys.CONSTRUCTOR_NAME, context.Scope)
96 .Dereference(pos);
97
98 if (ctor is not BadFunction func)
99 {
100 throw new BadRuntimeException("Cannot create object from non-function type", pos);
101 }
102
103 foreach (BadObject o in func.Invoke(args, context))
104 {
105 yield return o;
106 }
107 }
108
109 foreach (BadObject o in cls.Scope.InitializeAttributes())
110 {
111 yield return o;
112 }
113
114 yield return cls;
115 }
116
118 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
119 {
121
122 //Get Type from Right
123 foreach (BadObject o in Right.Left.Execute(context))
124 {
125 obj = o;
126
127 yield return o;
128 }
129
130 obj = obj.Dereference(Position);
131
132 if (obj is not BadClassPrototype ptype)
133 {
134 throw new BadRuntimeException("Cannot create object from non-class type", Position);
135 }
136
137 List<BadObject> args = new List<BadObject>();
138
139 foreach (BadObject o in Right.GetArgs(context, args))
140 {
141 yield return o;
142 }
143
144 foreach (BadObject o in CreateObject(ptype, context, args.ToArray(), Position))
145 {
146 yield return o;
147 }
148 }
149}
Describes a specific position inside a source file.
Contains Static Data for the BadScript Language.
Base Implementation for all Expressions used inside the Script.
IEnumerable< BadExpression > GetDescendantsAndSelf()
Returns all Descendants of the Expression and the Expression itself.
BadSourcePosition Position
The source Position of the Expression.
IEnumerable< BadObject > Execute(BadExecutionContext context)
Evaluates the Expression within the current Execution Context.
override void Optimize()
Uses the Constant Folding Optimizer to optimize the expression.
IEnumerable< BadObject > GetArgs(BadExecutionContext context, List< BadObject > args)
Returns the argument objects.
static IEnumerable< BadObject > CreateObject(BadClassPrototype proto, BadExecutionContext context, BadObject[] args, BadSourcePosition pos)
Creates an Instance of the Specified Class prototype.
BadNewExpression(BadInvocationExpression right, BadSourcePosition position)
Constructor of the New Expression.
override void Optimize()
Uses the Constant Folding Optimizer to optimize the expression.
override IEnumerable< BadObject > InnerExecute(BadExecutionContext context)
BadInvocationExpression Right
The Constructor Invocation.
override IEnumerable< BadExpression > GetDescendants()
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
virtual BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
Definition BadObject.cs:141
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Implements a function that can be called from the script.
Implements a Type Instance in the BadScript Language.
Definition BadClass.cs:11
Implements a Class Prototype for the BadScript Language.
The Void Prototype, can be assigned to nothing, can not be inherited from, can not be instantiated....
static BadVoidPrototype Instance
The Instance of the BadVoidPrototype.
Contains Shared Data Structures and Functionality.
Contains the Function Expressions for the BadScript2 Language.
Contains the Type Expressions for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.