BadScript 2
Loading...
Searching...
No Matches
BadReturnExpression.cs
Go to the documentation of this file.
7
9
14{
21 public BadReturnExpression(BadExpression? right, BadSourcePosition position, bool isRefReturn) : base(
22 false,
23 position
24 )
25 {
26 Right = right;
27 IsRefReturn = isRefReturn;
28 }
29
33 public bool IsRefReturn { get; }
34
38 public BadExpression? Right { get; private set; }
39
44 public void SetRight(BadExpression? expr)
45 {
46 Right = expr;
47 }
48
50 public override IEnumerable<BadExpression> GetDescendants()
51 {
52 if (Right == null)
53 {
54 yield break;
55 }
56
57 foreach (BadExpression right in Right.GetDescendantsAndSelf())
58 {
59 yield return right;
60 }
61 }
62
64 public override void Optimize()
65 {
66 if (Right != null)
67 {
69 }
70 }
71
73 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
74 {
75 BadObject value = BadObject.Null;
76
77 if (Right == null)
78 {
79 if (context.Scope.FunctionObject != null &&
80 context.Scope.FunctionObject.ReturnType == BadVoidPrototype.Instance)
81 {
83 }
84 else
85 {
86 context.Scope.SetReturnValue(value);
87 }
88
89 yield return value;
90 yield break;
91 }
92
93 foreach (BadObject obj in Right.Execute(context))
94 {
95 value = obj;
96
97 yield return obj;
98 }
99
100 if (!IsRefReturn)
101 {
102 value = value.Dereference();
103 }
104
105 if (context.Scope.FunctionObject != null &&
106 context.Scope.FunctionObject.ReturnType == BadVoidPrototype.Instance)
107 {
108 if (!context.Scope.FunctionObject.IsSingleLine)
109 {
110 throw BadRuntimeException.Create(context.Scope, "Cannot return a value from a void function", Position);
111 }
112 context.Scope.SetReturnValue(BadVoidPrototype.Object);
113 yield break;
114 }
115
116 context.Scope.SetReturnValue(value);
117
118 yield return value;
119 }
120
122 public override string ToString()
123 {
124 return "return " + (Right?.ToString() ?? "");
125 }
126}
Describes a specific position inside a source file.
static BadExpression Optimize(BadExpression expr)
Optimizes the given expression.
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.
Implements the Return expression that is used to exit the current function with an Optional Return Va...
override void Optimize()
Uses the Constant Folding Optimizer to optimize the expression.
override IEnumerable< BadObject > InnerExecute(BadExecutionContext context)
BadReturnExpression(BadExpression? right, BadSourcePosition position, bool isRefReturn)
Constructor of the Return Expression.
bool IsRefReturn
Indicates if the return value is meant to be a reference.
void SetRight(BadExpression? expr)
Sets the return value.
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
void SetReturnValue(BadObject? value)
Sets the Return value of this scope.
Definition BadScope.cs:759
BadFunction? FunctionObject
The Function Object of the Scope.
Definition BadScope.cs:378
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
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 BadScript2 Constant Folding Optimizations.
Contains the Controlflow Expressions for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.