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(
57 BadExecutionContext context,
58 BadObject[] args,
60 {
62
63 if (proto == BadVoidPrototype.Instance)
64 {
65 throw BadRuntimeException.Create(context.Scope, "Cannot create instance of void", pos);
66 }
67
68 if (proto is BadANativeClassPrototype nativeType)
69 {
70 foreach (BadObject o in nativeType.CreateInstance(context, args))
71 {
72 yield return o;
73 }
74
75 yield break;
76 }
77
78
79 foreach (BadObject o in proto.CreateInstance(context))
80 {
81 obj = o;
82
83 yield return o;
84 }
85
86 obj = obj.Dereference();
87
88 if (obj is not BadClass cls)
89 {
90 throw new BadRuntimeException("Cannot create object from non-class type", pos);
91 }
92
93 //Call Constructor if exists
94
95 if (cls.HasProperty(BadStaticKeys.CONSTRUCTOR_NAME, cls.Scope))
96 {
97 BadObject ctor = cls.GetProperty(BadStaticKeys.CONSTRUCTOR_NAME, context.Scope).Dereference();
98
99 if (ctor is not BadFunction func)
100 {
101 throw new BadRuntimeException("Cannot create object from non-function type", pos);
102 }
103
104 foreach (BadObject o in func.Invoke(args, context))
105 {
106 yield return o;
107 }
108 }
109
110 foreach(BadObject o in cls.Scope.InitializeAttributes())
111 {
112 yield return o;
113 }
114
115 yield return cls;
116 }
117
119 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
120 {
122
123 //Get Type from Right
124 foreach (BadObject o in Right.Left.Execute(context))
125 {
126 obj = o;
127
128 yield return o;
129 }
130
131 obj = obj.Dereference();
132
133 if (obj is not BadClassPrototype ptype)
134 {
135 throw new BadRuntimeException("Cannot create object from non-class type", Position);
136 }
137
138 List<BadObject> args = new List<BadObject>();
139
140 foreach (BadObject o in Right.GetArgs(context, args))
141 {
142 yield return o;
143 }
144
145 foreach (BadObject o in CreateObject(ptype, context, args.ToArray(), Position))
146 {
147 yield return o;
148 }
149 }
150}
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:129
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.