From 1e14a3a678e68b9e07c399b38c3e9c3733166d76 Mon Sep 17 00:00:00 2001 From: Kenny van Vulpen Date: Thu, 23 Dec 2021 23:06:18 +0100 Subject: [PATCH] MoLang fixes --- src/Alex.MoLang.TestApp/Program.cs | 30 ++- src/Alex.MoLang/Parser/ExprTraverser.cs | 254 ++++++------------ .../Expressions/ArrayAccessExpression.cs | 4 +- .../Parser/Expressions/ForEachExpression.cs | 15 +- .../Parser/Expressions/LoopExpression.cs | 3 + .../Parser/Expressions/StatementExpression.cs | 2 +- .../Parser/Expressions/ThisExpression.cs | 4 +- src/Alex.MoLang/Parser/IExprVisitor.cs | 4 +- src/Alex.MoLang/Parser/MoLangParser.cs | 11 +- .../Parser/Visitors/ExprConnectingVisitor.cs | 42 +-- .../Parser/Visitors/FindingVisitor.cs | 4 +- .../Parser/Visitors/FirstFindingVisitor.cs | 6 +- src/Alex.MoLang/Runtime/MoLangEnvironment.cs | 1 + src/Alex.MoLang/Runtime/Struct/ArrayStruct.cs | 2 +- .../Bedrock/MoLang/MoLangVector2Expression.cs | 13 +- .../Bedrock/MoLang/MoLangVector3Expression.cs | 19 +- .../Bedrock/MoLang/MoLangVector4Expression.cs | 25 +- 17 files changed, 179 insertions(+), 260 deletions(-) diff --git a/src/Alex.MoLang.TestApp/Program.cs b/src/Alex.MoLang.TestApp/Program.cs index fcac2e0f8..021d3e3b8 100644 --- a/src/Alex.MoLang.TestApp/Program.cs +++ b/src/Alex.MoLang.TestApp/Program.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Linq; using System.Reflection; using System.Text; using System.Text.Json; @@ -27,20 +28,33 @@ static void Main(string[] args) TokenIterator tokenIterator = new TokenIterator(@"t.a = 213 + 2 / 0.5 + 5 + 2 * 3; +query.debug_output(1 + 2 * 3); + array.test.0 = 100; array.test[1] = 200; array.test[2] = 10.5; +query.debug_output(array.test[1]); + +t.b = 3; +loop(10, { + array.test[t.b] = array.test[t.b - 1] + 2; + t.b = t.b + 1; +}); for_each(v.r, array.test, { t.a = t.a + v.r; query.debug_output('hello1', t.a, v.r); }); +t.b = 0; loop(10, { + t.b = t.b + 1; t.a = t.a + math.cos((Math.PI / 180.0f) * 270); -query.debug_output('hello', 'test', t.a, array.test[2]); + query.debug_output(array.test[t.b]); +query.debug_output('hello', 'test', t.a, array.test[2], t.b); }); +query.debug_output(query.life_time()); return t.a;"); MoLangParser parser = new MoLangParser(tokenIterator); @@ -57,17 +71,20 @@ static void Main(string[] args) try { - const int runs = 100000; + const int runs = 1;///10000; + const int warmup = 5; double totalTicks = 0; double longest = 0; double shortest =long.MaxValue; IMoValue value; - for (int i = 0; i < runs; i++) + for (int i = 0; i < runs + warmup; i++) { sw.Restart(); value = runtime.Execute(expressions); + if (i < warmup) + continue; var elapsed = sw.Elapsed.TotalMilliseconds; @@ -118,5 +135,12 @@ public double Lifetime return _sw.Elapsed.TotalSeconds; } } + + [MoFunction("debug_output")] + public void DebugOutput(MoParams mo) + { + var str = string.Join(" ", mo.GetParams().Select(x => x.AsString())); + Console.WriteLine(str); + } } } \ No newline at end of file diff --git a/src/Alex.MoLang/Parser/ExprTraverser.cs b/src/Alex.MoLang/Parser/ExprTraverser.cs index 6047dce59..bd12dba89 100644 --- a/src/Alex.MoLang/Parser/ExprTraverser.cs +++ b/src/Alex.MoLang/Parser/ExprTraverser.cs @@ -32,216 +32,112 @@ public static IExpression FindFirst(Predicate predicate, params IEx } public class ExprTraverser { - private bool _stopTraversal = false; - public readonly List Visitors = new List(); - public IEnumerable Traverse(IEnumerable expressions) + private bool _stop = false; + public IExpression[] Traverse(IExpression[] expressions) { - var enumerable = expressions as IExpression[] ?? expressions.ToArray(); - - foreach (IExprVisitor visitor in Visitors) { - visitor.BeforeTraverse(enumerable); - } - - _stopTraversal = false; - - foreach (var expression in TraverseArray(enumerable)) - { - yield return expression; - } - //TraverseArray(expressions); - - foreach (IExprVisitor visitor in Visitors) { - visitor.AfterTraverse(enumerable); - } + TraverseArray(expressions); - // return expressions; + return expressions.Where(x => x != null).ToArray(); } - private IEnumerable TraverseArray(IEnumerable expressions) + private void TraverseArray(IExpression[] expressions) { - //var list = expressions.ToList(); + foreach (IExprVisitor visitor in Visitors) { + visitor.BeforeTraverse(expressions); + } - //for (var i = 0; i < list.Count; i++) - //for (var index = 0; index < expressions.Length; index++) - foreach(var e in expressions) + for (var index = 0; index < expressions.Length; index++) { - var expression = e; - // IExpression expression = expressions[index]; - + IExpression expression = expressions[index]; + if (expression == null) throw new MoLangRuntimeException("Expression was null", null); - var removeCurrent = false; - var traverseChildren = true; - var traverseCurrent = true; + expressions[index] = TraverseExpr(expression, null); - foreach (var visitor in Visitors) - { - var result = visitor.OnVisit(expression); - - if (result is ActionType at) - { - switch (at) - { - case ActionType.RemoveCurrent: - removeCurrent = true; - - break; - - case ActionType.StopTraversal: - _stopTraversal = true; - - break; - - case ActionType.DontTraverseCurrentAndChildren: - traverseCurrent = false; - traverseChildren = false; - - break; - - case ActionType.DontTraverseChildren: - traverseChildren = false; - - break; - } - } - else if (result is IExpression result1) - { - expression = result1; - } - } - - if (!traverseCurrent) + if (_stop) { break; } + } - if (traverseChildren && !removeCurrent) - { - expression = TraverseExpr(expression); - } + foreach (IExprVisitor visitor in Visitors) { + visitor.AfterTraverse(expressions); + } - foreach (IExprVisitor visitor in Visitors) + //return expressions.Where(x => x != null).ToArray(); + } + + private IExpression TraverseExpr(IExpression expression, IExpression parent) + { + Visit(expression); + expression.Meta.Parent = parent; + foreach (var field in GetAllProperties(expression.GetType())) + { + if (!typeof(IEnumerable).IsAssignableFrom(field.PropertyType) + && !typeof(IExpression).IsAssignableFrom(field.PropertyType)) { - visitor.OnLeave(expression); + continue; } - - if (removeCurrent) + + //field.setAccessible(true); + var fieldValue = GetFieldValue(field, expression); + if (fieldValue == null) + continue; + + if (fieldValue is IExpression original) { - //list.Remove(expression); - // expressions[index] = null;//.Remove(expression); + fieldValue = TraverseExpr(original, expression); } - else + else if (fieldValue is IEnumerable expressions) { - // expressions[index] = expression; - - yield return expression; - //expressions[i] = expression;//.set(i, expression); + var exprs = expressions.ToArray(); + foreach (var ex in exprs) + { + if (ex != null) + ex.Meta.Parent = expression; + } + TraverseArray(exprs); + + fieldValue = exprs; } + + SetFieldValue(field, expression, fieldValue); - if (_stopTraversal) + if (_stop) { break; } } - //return expressions.Where(x => x != null).ToArray(); + OnLeave(expression); + return expression; } - private IExpression TraverseExpr(IExpression expression) + private void Visit(IExpression expression) { - foreach (var field in GetAllProperties(expression.GetType())) + // VisitationResult visitationResult = VisitationResult.None; + foreach (var visitor in Visitors) { - //field.setAccessible(true); - var fieldValue = GetFieldValue(field, expression); - - if (fieldValue is IExpression subExpr) - { - var removeCurrent = false; - var traverseChildren = true; - var traverseCurrent = true; - - foreach (var visitor in Visitors) - { - var result = visitor.OnVisit(subExpr); - - if (result is ActionType at) - { - switch (at) - { - case ActionType.RemoveCurrent: - removeCurrent = true; - break; - - case ActionType.StopTraversal: - _stopTraversal = true; - - break; - - case ActionType.DontTraverseCurrentAndChildren: - traverseCurrent = false; - traverseChildren = false; - - break; - - case ActionType.DontTraverseChildren: - traverseChildren = false; - - break; - } - } - else if (result is IExpression result1) - { - subExpr = result1; - } - } - - if (!traverseCurrent) - { - break; - } - - if (traverseChildren && !removeCurrent) - { - subExpr = TraverseExpr(subExpr); - } - - foreach (var visitor in Visitors) - { - visitor.OnLeave(subExpr); - } - - if (removeCurrent) - { - SetFieldValue(field, expression, null); - } - else - { - if (subExpr != fieldValue) - SetFieldValue(field, expression, subExpr); - } - - if (_stopTraversal) - { - break; - } - } - else if (fieldValue != null && fieldValue.GetType().IsArray) - { - var array = (object[]) fieldValue; - //var exprs = array.Where(x => x is IExpression).Cast().ToArray(); + visitor.OnVisit(this, expression); + } - //exprs = TraverseArray(array.Where(x => x is IExpression).Cast()).ToArray(); + // return visitationResult; + } - SetFieldValue( - field, expression, - TraverseArray(array.Where(x => x is IExpression).Cast()).ToArray()); - } + private void OnLeave(IExpression expression) + { + foreach (var visitor in Visitors) + { + visitor.OnLeave(expression); } + } - return expression; + public void Stop() + { + _stop = true; } private static ConcurrentDictionary _cachedProperties = @@ -284,12 +180,16 @@ private void SetFieldValue(PropertyInfo field, object obj, object value) } } - public enum ActionType + [Flags] + public enum VisitationResult { - RemoveCurrent, - StopTraversal, - DontTraverseCurrentAndChildren, - DontTraverseChildren + None, + + RemoveCurrent = 0x01, + StopTraversal = 0x02, + DontTraverseChildren = 0x04, + DontTraverseCurrent = 0x08, + DontTraverseCurrentAndChildren = DontTraverseCurrent | DontTraverseChildren } } } \ No newline at end of file diff --git a/src/Alex.MoLang/Parser/Expressions/ArrayAccessExpression.cs b/src/Alex.MoLang/Parser/Expressions/ArrayAccessExpression.cs index 278f6c939..cb4373d78 100644 --- a/src/Alex.MoLang/Parser/Expressions/ArrayAccessExpression.cs +++ b/src/Alex.MoLang/Parser/Expressions/ArrayAccessExpression.cs @@ -18,7 +18,7 @@ public ArrayAccessExpression(IExpression array, IExpression index) /// public override IMoValue Evaluate(MoScope scope, MoLangEnvironment environment) { - var name = Array is NameExpression expression ? expression.Name.ToString() : Array.Evaluate(scope, environment).AsString(); + var name = Array is NameExpression expression ? expression.Name.Path.ToString() : Array.Evaluate(scope, environment).AsString(); var path = new MoPath($"{name}.{(int)Index.Evaluate(scope, environment).AsDouble()}"); return environment.GetValue(path); @@ -27,7 +27,7 @@ public override IMoValue Evaluate(MoScope scope, MoLangEnvironment environment) /// public override void Assign(MoScope scope, MoLangEnvironment environment, IMoValue value) { - var name = Array is NameExpression expression ? expression.Name.ToString() : Array.Evaluate(scope, environment).AsString(); + var name = Array is NameExpression expression ? expression.Name.Path.ToString() : Array.Evaluate(scope, environment).AsString(); var path = new MoPath($"{name}.{(int)Index.Evaluate(scope, environment).AsDouble()}"); environment.SetValue(path, value); diff --git a/src/Alex.MoLang/Parser/Expressions/ForEachExpression.cs b/src/Alex.MoLang/Parser/Expressions/ForEachExpression.cs index b5350c1c9..7d968827e 100644 --- a/src/Alex.MoLang/Parser/Expressions/ForEachExpression.cs +++ b/src/Alex.MoLang/Parser/Expressions/ForEachExpression.cs @@ -24,15 +24,18 @@ public override IMoValue Evaluate(MoScope scope, MoLangEnvironment environment) IMoValue array = Array.Evaluate(scope, environment); if (array is VariableStruct vs) { - MoScope scope2 = new MoScope(); + MoScope subScope = new MoScope(); foreach (IMoValue value in vs.Map.Values) { - Variable.Assign(scope2, environment, value is VariableStruct vss ? vss.Map.FirstOrDefault().Value : value); - Body.Evaluate(scope2, environment); + subScope.IsContinue = false; + subScope.IsBreak = false; + + Variable.Assign(subScope, environment, value is VariableStruct vss ? vss.Map.FirstOrDefault().Value : value); + Body.Evaluate(subScope, environment); - if (scope2.ReturnValue != null) { - return scope2.ReturnValue; - } else if (scope2.IsBreak) { + if (subScope.ReturnValue != null) { + return subScope.ReturnValue; + } else if (subScope.IsBreak) { break; } } diff --git a/src/Alex.MoLang/Parser/Expressions/LoopExpression.cs b/src/Alex.MoLang/Parser/Expressions/LoopExpression.cs index 3fc1b13cb..6fb97a93e 100644 --- a/src/Alex.MoLang/Parser/Expressions/LoopExpression.cs +++ b/src/Alex.MoLang/Parser/Expressions/LoopExpression.cs @@ -21,6 +21,9 @@ public override IMoValue Evaluate(MoScope scope, MoLangEnvironment environment) MoScope subScope = new MoScope(); while (loop > 0) { + subScope.IsContinue = false; + subScope.IsBreak = false; + Body.Evaluate(subScope, environment); loop--; diff --git a/src/Alex.MoLang/Parser/Expressions/StatementExpression.cs b/src/Alex.MoLang/Parser/Expressions/StatementExpression.cs index 9a5a4760a..8f52ad556 100644 --- a/src/Alex.MoLang/Parser/Expressions/StatementExpression.cs +++ b/src/Alex.MoLang/Parser/Expressions/StatementExpression.cs @@ -17,7 +17,7 @@ public override IMoValue Evaluate(MoScope scope, MoLangEnvironment environment) break; } } - + return DoubleValue.Zero; } diff --git a/src/Alex.MoLang/Parser/Expressions/ThisExpression.cs b/src/Alex.MoLang/Parser/Expressions/ThisExpression.cs index 1e651f359..b55cf7344 100644 --- a/src/Alex.MoLang/Parser/Expressions/ThisExpression.cs +++ b/src/Alex.MoLang/Parser/Expressions/ThisExpression.cs @@ -6,11 +6,11 @@ namespace Alex.MoLang.Parser.Expressions { public class ThisExpression : Expression { - public static readonly MoPath _this = new MoPath("context.this"); + public static readonly MoPath _this = new MoPath("this"); /// public override IMoValue Evaluate(MoScope scope, MoLangEnvironment environment) { - return environment.GetValue(_this); + return environment.ThisVariable;// environment.GetValue(_this); } /// diff --git a/src/Alex.MoLang/Parser/IExprVisitor.cs b/src/Alex.MoLang/Parser/IExprVisitor.cs index d48bdf6b8..e744703a5 100644 --- a/src/Alex.MoLang/Parser/IExprVisitor.cs +++ b/src/Alex.MoLang/Parser/IExprVisitor.cs @@ -5,7 +5,7 @@ namespace Alex.MoLang.Parser public interface IExprVisitor { void BeforeTraverse(IEnumerable expressions); - object OnVisit(IExpression expression); + void OnVisit(ExprTraverser traverser, IExpression expression); void OnLeave(IExpression expression); void AfterTraverse(IEnumerable expressions); @@ -20,7 +20,7 @@ public virtual void BeforeTraverse(IEnumerable expressions) } /// - public abstract object OnVisit(IExpression expression); + public abstract void OnVisit(ExprTraverser traverser, IExpression expression); /// public virtual void OnLeave(IExpression expression) diff --git a/src/Alex.MoLang/Parser/MoLangParser.cs b/src/Alex.MoLang/Parser/MoLangParser.cs index 598d95d8a..bc63d4f99 100644 --- a/src/Alex.MoLang/Parser/MoLangParser.cs +++ b/src/Alex.MoLang/Parser/MoLangParser.cs @@ -22,7 +22,7 @@ public class MoLangParser private readonly TokenIterator _tokenIterator; private readonly List _readTokens = new List(); - private static readonly ExprTraverser ExprTraverser; + // private static readonly ExprTraverser ExprTraverser; static MoLangParser() { PrefixParselets.Add(TokenType.Name, new NameParselet()); @@ -61,8 +61,8 @@ static MoLangParser() InfixParselets.Add(TokenType.Arrow, new GenericBinaryOpParselet(Precedence.Arrow)); InfixParselets.Add(TokenType.Assign, new AssignParselet()); - ExprTraverser = new ExprTraverser(); - ExprTraverser.Visitors.Add(new ExprConnectingVisitor()); + //ExprTraverser = new ExprTraverser(); + //ExprTraverser.Visitors.Add(new ExprConnectingVisitor()); } public MoLangParser(TokenIterator iterator) @@ -72,6 +72,9 @@ public MoLangParser(TokenIterator iterator) public IExpression[] Parse() { + // var traverser = new ExprTraverser(); + // traverser.Visitors.Add(new ExprConnectingVisitor()); + Stopwatch sw = Stopwatch.StartNew(); try @@ -92,7 +95,7 @@ public IExpression[] Parse() } } while (MatchToken(TokenType.Semicolon)); - return ExprTraverser.Traverse(exprs).ToArray(); + return exprs.ToArray();// traverser.Traverse(exprs.ToArray()); } catch (Exception ex) { diff --git a/src/Alex.MoLang/Parser/Visitors/ExprConnectingVisitor.cs b/src/Alex.MoLang/Parser/Visitors/ExprConnectingVisitor.cs index f61633179..1327a249b 100644 --- a/src/Alex.MoLang/Parser/Visitors/ExprConnectingVisitor.cs +++ b/src/Alex.MoLang/Parser/Visitors/ExprConnectingVisitor.cs @@ -5,41 +5,45 @@ namespace Alex.MoLang.Parser.Visitors { public class ExprConnectingVisitor : ExprVisitor { - private LinkedList Stack { get; set; } = new LinkedList(); - private IExpression Previous { get; set; } + //private LinkedList Stack { get; set; } = new LinkedList(); + // private LinkedList _previousStack = null; + + private IExpression _last = null; /// public override void BeforeTraverse(IEnumerable expressions) { - Stack.Clear(); - Previous = null; + _last = null; + // _previousStack = Stack; + // Stack = new LinkedList(); } /// - public override object OnVisit(IExpression expression) + public override void OnVisit(ExprTraverser traverser, IExpression expression) { - if (Stack.Count > 0) { - expression.Meta.Parent = Stack.Last.Value;// .Attributes["parent"] = Stack.Last; - } + var previous = _last; + expression.Meta.Previous = previous; - if (Previous != null && expression.Meta.Parent != null - && Previous.Meta.Parent != null - && expression.Meta.Parent == Previous.Meta.Parent) + if (previous != null && previous != expression.Meta.Parent) { - expression.Meta.Previous = Previous;// .Attributes["previous"] = Previous; - Previous.Meta.Next = expression;// .Attributes["next"] = expression; + previous.Meta.Next = expression; } - - Stack.AddLast(expression); - - return expression; + + //Stack.AddLast(expression); } /// public override void OnLeave(IExpression expression) { - Previous = expression; - Stack.RemoveLast(); + _last = expression; + //Stack.RemoveLast(); + } + + /// + public override void AfterTraverse(IEnumerable expressions) + { + base.AfterTraverse(expressions); + //Stack = _previousStack; } } } \ No newline at end of file diff --git a/src/Alex.MoLang/Parser/Visitors/FindingVisitor.cs b/src/Alex.MoLang/Parser/Visitors/FindingVisitor.cs index f756ec1fa..bd574dc54 100644 --- a/src/Alex.MoLang/Parser/Visitors/FindingVisitor.cs +++ b/src/Alex.MoLang/Parser/Visitors/FindingVisitor.cs @@ -13,14 +13,12 @@ public FindingVisitor(Predicate predicate) { } /// - public override object OnVisit(IExpression expression) + public override void OnVisit(ExprTraverser traverser, IExpression expression) { if (_predicate(expression)) { FoundExpressions.Add(expression); } - - return null; } } } \ No newline at end of file diff --git a/src/Alex.MoLang/Parser/Visitors/FirstFindingVisitor.cs b/src/Alex.MoLang/Parser/Visitors/FirstFindingVisitor.cs index 1b77d9521..74255a740 100644 --- a/src/Alex.MoLang/Parser/Visitors/FirstFindingVisitor.cs +++ b/src/Alex.MoLang/Parser/Visitors/FirstFindingVisitor.cs @@ -12,16 +12,14 @@ public FirstFindingVisitor(Predicate predicate) { } /// - public override object OnVisit(IExpression expression) + public override void OnVisit(ExprTraverser traverser, IExpression expression) { if (_predicate(expression)) { Found = expression; - return ExprTraverser.ActionType.StopTraversal; + traverser.Stop(); } - - return null; } } } \ No newline at end of file diff --git a/src/Alex.MoLang/Runtime/MoLangEnvironment.cs b/src/Alex.MoLang/Runtime/MoLangEnvironment.cs index 2b5cb0630..907637e69 100644 --- a/src/Alex.MoLang/Runtime/MoLangEnvironment.cs +++ b/src/Alex.MoLang/Runtime/MoLangEnvironment.cs @@ -16,6 +16,7 @@ public class MoLangEnvironment : IMoValue public object Value => Structs; public Dictionary Structs { get; } = new(StringComparer.OrdinalIgnoreCase); + public IMoValue ThisVariable { get; set; } = DoubleValue.Zero; public IMoValue GetValue(MoPath name) { return GetValue(name, MoParams.Empty); diff --git a/src/Alex.MoLang/Runtime/Struct/ArrayStruct.cs b/src/Alex.MoLang/Runtime/Struct/ArrayStruct.cs index b297edb17..d56acf131 100644 --- a/src/Alex.MoLang/Runtime/Struct/ArrayStruct.cs +++ b/src/Alex.MoLang/Runtime/Struct/ArrayStruct.cs @@ -19,7 +19,7 @@ public override void Set(MoPath key, IMoValue value) //string[] parts = key.ToString().Split("."); //parts[^1] = int.Parse(parts[^1]).ToString(); - base.Set(key.Last, value); + base.Set(key, value); } } } \ No newline at end of file diff --git a/src/Alex.ResourcePackLib/Json/Bedrock/MoLang/MoLangVector2Expression.cs b/src/Alex.ResourcePackLib/Json/Bedrock/MoLang/MoLangVector2Expression.cs index 092ab4738..e2bebe86d 100644 --- a/src/Alex.ResourcePackLib/Json/Bedrock/MoLang/MoLangVector2Expression.cs +++ b/src/Alex.ResourcePackLib/Json/Bedrock/MoLang/MoLangVector2Expression.cs @@ -3,6 +3,7 @@ using Alex.MoLang.Parser; using Alex.MoLang.Runtime; using Alex.MoLang.Runtime.Value; +using Alex.MoLang.Utils; using Alex.ResourcePackLib.Json.Converters.Bedrock; using Microsoft.Xna.Framework; using Newtonsoft.Json; @@ -45,14 +46,10 @@ public MoLangVector2Expression(Dictionary keyframe private Vector2 Evaluate(MoLangRuntime runtime, IExpression[] xExpressions, IExpression[] yExpressions, Vector2 currentValue) { - IMoValue x = runtime.Execute(xExpressions, new Dictionary() - { - {"this", new DoubleValue(currentValue.X)} - }); - IMoValue y = runtime.Execute(yExpressions, new Dictionary() - { - {"this", new DoubleValue(currentValue.Y)} - }); + runtime.Environment.ThisVariable = new DoubleValue(currentValue.X); + IMoValue x = runtime.Execute(xExpressions); + runtime.Environment.ThisVariable = new DoubleValue(currentValue.Y); + IMoValue y = runtime.Execute(yExpressions); return new Vector2(x.AsFloat(), y.AsFloat()); } diff --git a/src/Alex.ResourcePackLib/Json/Bedrock/MoLang/MoLangVector3Expression.cs b/src/Alex.ResourcePackLib/Json/Bedrock/MoLang/MoLangVector3Expression.cs index 667d13780..5546ae1a1 100644 --- a/src/Alex.ResourcePackLib/Json/Bedrock/MoLang/MoLangVector3Expression.cs +++ b/src/Alex.ResourcePackLib/Json/Bedrock/MoLang/MoLangVector3Expression.cs @@ -4,6 +4,7 @@ using Alex.MoLang.Parser; using Alex.MoLang.Runtime; using Alex.MoLang.Runtime.Value; +using Alex.MoLang.Utils; using Alex.ResourcePackLib.Json.Converters.Bedrock; using Microsoft.Xna.Framework; using Newtonsoft.Json; @@ -56,18 +57,12 @@ public MoLangVector3Expression(Dictionary keyframe private Vector3 Evaluate(MoLangRuntime runtime, IExpression[] xExpressions, IExpression[] yExpressions, IExpression[] zExpressions, Vector3 currentValue) { - IMoValue x = runtime.Execute(xExpressions, new Dictionary(StringComparer.OrdinalIgnoreCase) - { - {"this", new DoubleValue(currentValue.X)} - }); - IMoValue y = runtime.Execute(yExpressions, new Dictionary(StringComparer.OrdinalIgnoreCase) - { - {"this", new DoubleValue(currentValue.Y)} - }); - IMoValue z = runtime.Execute(zExpressions, new Dictionary(StringComparer.OrdinalIgnoreCase) - { - {"this", new DoubleValue(currentValue.Z)} - }); + runtime.Environment.ThisVariable = new DoubleValue(currentValue.X); + IMoValue x = runtime.Execute(xExpressions); + runtime.Environment.ThisVariable = new DoubleValue(currentValue.Y); + IMoValue y = runtime.Execute(yExpressions); + runtime.Environment.ThisVariable = new DoubleValue(currentValue.Z); + IMoValue z = runtime.Execute(zExpressions); return new Vector3(x.AsFloat(), y.AsFloat(), z.AsFloat()); } diff --git a/src/Alex.ResourcePackLib/Json/Bedrock/MoLang/MoLangVector4Expression.cs b/src/Alex.ResourcePackLib/Json/Bedrock/MoLang/MoLangVector4Expression.cs index 943bec064..0449eeac2 100644 --- a/src/Alex.ResourcePackLib/Json/Bedrock/MoLang/MoLangVector4Expression.cs +++ b/src/Alex.ResourcePackLib/Json/Bedrock/MoLang/MoLangVector4Expression.cs @@ -5,6 +5,7 @@ using Alex.MoLang.Parser.Expressions; using Alex.MoLang.Runtime; using Alex.MoLang.Runtime.Value; +using Alex.MoLang.Utils; using Alex.ResourcePackLib.Json.Converters.Bedrock; using Microsoft.Xna.Framework; using Newtonsoft.Json; @@ -49,22 +50,14 @@ public MoLangVector4Expression(Dictionary keyframe private Vector4 Evaluate(MoLangRuntime runtime, IExpression[] xExpressions, IExpression[] yExpressions, IExpression[] zExpressions, IExpression[] wExpressions, Vector4 currentValue) { - IMoValue x = runtime.Execute(xExpressions, new Dictionary(StringComparer.OrdinalIgnoreCase) - { - {"this", new DoubleValue(currentValue.X)} - }); - IMoValue y = runtime.Execute(yExpressions, new Dictionary(StringComparer.OrdinalIgnoreCase) - { - {"this", new DoubleValue(currentValue.Y)} - }); - IMoValue z = runtime.Execute(zExpressions, new Dictionary(StringComparer.OrdinalIgnoreCase) - { - {"this", new DoubleValue(currentValue.Z)} - }); - IMoValue w = runtime.Execute(wExpressions, new Dictionary(StringComparer.OrdinalIgnoreCase) - { - {"this", new DoubleValue(currentValue.W)} - }); + runtime.Environment.ThisVariable = new DoubleValue(currentValue.X); + IMoValue x = runtime.Execute(xExpressions); + runtime.Environment.ThisVariable = new DoubleValue(currentValue.Y); + IMoValue y = runtime.Execute(yExpressions); + runtime.Environment.ThisVariable = new DoubleValue(currentValue.Z); + IMoValue z = runtime.Execute(zExpressions); + runtime.Environment.ThisVariable = new DoubleValue(currentValue.W); + IMoValue w = runtime.Execute(wExpressions); return new Vector4(x.AsFloat(), y.AsFloat(), z.AsFloat(), w.AsFloat()); }