Support more in tables, including local variables that are hidden from the outside

This commit is contained in:
Deukhoofd 2018-11-17 19:56:52 +01:00
parent 44a2048153
commit 5a52c235c5
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
2 changed files with 17 additions and 8 deletions

View File

@ -1,27 +1,31 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Upsilon.Binder;
namespace Upsilon.BaseTypes namespace Upsilon.BaseTypes
{ {
public class LuaTable : LuaType public class LuaTable : LuaType
{ {
private Dictionary<string, LuaType> _map; private Dictionary<string, VariableSymbol> _variableLookup;
private Dictionary<VariableSymbol, LuaType> _map;
public LuaTable() public LuaTable()
{ {
_map = new Dictionary<string, LuaType>(); _map = new Dictionary<VariableSymbol, LuaType>();
_variableLookup = new Dictionary<string, VariableSymbol>();
} }
public LuaTable(Dictionary<string, LuaType> map) public LuaTable(Dictionary<VariableSymbol, LuaType> map)
{ {
_map = map; _map = map;
_variableLookup = map.ToDictionary(x => x.Key.Name, x => x.Key);
} }
public override Type Type => Type.Table; public override Type Type => Type.Table;
public override object ToCSharpObject() 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());
} }

View File

@ -249,9 +249,11 @@ namespace Upsilon.Evaluator
if (boundFunctionStatement.Identifier.Local) if (boundFunctionStatement.Identifier.Local)
Scope.Set(boundFunctionStatement.Identifier, func); Scope.Set(boundFunctionStatement.Identifier, func);
else else
{
Scope.SetGlobal(boundFunctionStatement.Identifier, func); Scope.SetGlobal(boundFunctionStatement.Identifier, func);
_lastValue = func; _lastValue = func;
} }
}
private void EvaluateUnboundFunctionStatement(UnboundFunctionStatement unboundFunctionStatement) private void EvaluateUnboundFunctionStatement(UnboundFunctionStatement unboundFunctionStatement)
{ {
@ -293,7 +295,7 @@ namespace Upsilon.Evaluator
private LuaType EvaluateTableExpression(BoundTableExpression e) private LuaType EvaluateTableExpression(BoundTableExpression e)
{ {
var dic = new Dictionary<string, LuaType>(); var dic = new Dictionary<VariableSymbol, LuaType>();
var innerEvaluator = new Evaluator(_diagnostics, Scope); var innerEvaluator = new Evaluator(_diagnostics, Scope);
var currentPos = 1; var currentPos = 1;
foreach (var boundStatement in e.Statements) foreach (var boundStatement in e.Statements)
@ -301,14 +303,17 @@ namespace Upsilon.Evaluator
if (boundStatement.Kind == BoundKind.BoundAssignmentStatement) if (boundStatement.Kind == BoundKind.BoundAssignmentStatement)
{ {
var assignment = (BoundVariableAssignment)boundStatement; var assignment = (BoundVariableAssignment)boundStatement;
var key = assignment.Variable.Name; var key = assignment.Variable;
var value = EvaluateExpression(assignment.BoundExpression); var value = EvaluateExpression(assignment.BoundExpression);
dic.Add(key, value); dic.Add(key, value);
} }
else else
{ {
innerEvaluator.EvaluateStatement(boundStatement); 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++; currentPos++;
} }