From 5a52c235c5a2268741ec83c863bc7aa196300695 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 17 Nov 2018 19:56:52 +0100 Subject: [PATCH] Support more in tables, including local variables that are hidden from the outside --- Upsilon/BaseTypes/LuaTable.cs | 12 ++++++++---- Upsilon/Evaluator/Evaluator.cs | 13 +++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Upsilon/BaseTypes/LuaTable.cs b/Upsilon/BaseTypes/LuaTable.cs index d0ed040..cdb6afb 100644 --- a/Upsilon/BaseTypes/LuaTable.cs +++ b/Upsilon/BaseTypes/LuaTable.cs @@ -1,27 +1,31 @@ using System.Collections.Generic; using System.Linq; +using Upsilon.Binder; namespace Upsilon.BaseTypes { public class LuaTable : LuaType { - private Dictionary _map; + private Dictionary _variableLookup; + private Dictionary _map; public LuaTable() { - _map = new Dictionary(); + _map = new Dictionary(); + _variableLookup = new Dictionary(); } - public LuaTable(Dictionary map) + public LuaTable(Dictionary map) { _map = map; + _variableLookup = map.ToDictionary(x => x.Key.Name, x => x.Key); } public override Type Type => Type.Table; public override object ToCSharpObject() { - return _map.ToDictionary(x => x.Key, x => x.Value.ToCSharpObject()); + return _map.Where(x => !x.Key.Local).ToDictionary(x => x.Key.Name, x => x.Value.ToCSharpObject()); } diff --git a/Upsilon/Evaluator/Evaluator.cs b/Upsilon/Evaluator/Evaluator.cs index 0c7f28a..656d82e 100644 --- a/Upsilon/Evaluator/Evaluator.cs +++ b/Upsilon/Evaluator/Evaluator.cs @@ -249,8 +249,10 @@ namespace Upsilon.Evaluator if (boundFunctionStatement.Identifier.Local) Scope.Set(boundFunctionStatement.Identifier, func); else + { Scope.SetGlobal(boundFunctionStatement.Identifier, func); - _lastValue = func; + _lastValue = func; + } } private void EvaluateUnboundFunctionStatement(UnboundFunctionStatement unboundFunctionStatement) @@ -293,7 +295,7 @@ namespace Upsilon.Evaluator private LuaType EvaluateTableExpression(BoundTableExpression e) { - var dic = new Dictionary(); + var dic = new Dictionary(); var innerEvaluator = new Evaluator(_diagnostics, Scope); var currentPos = 1; foreach (var boundStatement in e.Statements) @@ -301,14 +303,17 @@ namespace Upsilon.Evaluator if (boundStatement.Kind == BoundKind.BoundAssignmentStatement) { var assignment = (BoundVariableAssignment)boundStatement; - var key = assignment.Variable.Name; + var key = assignment.Variable; var value = EvaluateExpression(assignment.BoundExpression); dic.Add(key, value); } else { innerEvaluator.EvaluateStatement(boundStatement); - dic.Add(currentPos.ToString(), innerEvaluator._lastValue); + if (innerEvaluator._lastValue != null) + dic.Add(new VariableSymbol(currentPos.ToString(), innerEvaluator._lastValue.Type, false), + innerEvaluator._lastValue); + innerEvaluator._lastValue = null; } currentPos++; }