More work on variables

This commit is contained in:
Deukhoofd 2018-11-11 21:03:50 +01:00
parent 05d0de31a0
commit 3d811ff801
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
4 changed files with 26 additions and 15 deletions

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
namespace Upsilon.Binder
{
@ -12,6 +13,11 @@ namespace Upsilon.Binder
_parentScope = parentScope;
_variables = new Dictionary<string, VariableSymbol>();
}
public BoundScope(Dictionary<VariableSymbol, object> variables, BoundScope parentScope)
{
_parentScope = parentScope;
_variables = variables.ToDictionary(x => x.Key.Name, x => x.Key);
}
public void SetVariable(VariableSymbol var)
{

View File

@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using Upsilon.BaseTypes.Number;
using Upsilon.Binder;
using Upsilon.Parser;
namespace Upsilon.Evaluator
{
@ -10,13 +9,19 @@ namespace Upsilon.Evaluator
{
private readonly Diagnostics _diagnostics;
public Script Script { get; }
private Dictionary<string, object> _variables = new Dictionary<string, object>();
private readonly Dictionary<VariableSymbol, object> _variables = new Dictionary<VariableSymbol, object>();
public Evaluator(Script script, Diagnostics diagnostics)
{
_diagnostics = diagnostics;
Script = script;
}
public Evaluator(Script script, Diagnostics diagnostics, Dictionary<VariableSymbol, object> vars)
{
_diagnostics = diagnostics;
Script = script;
_variables = vars;
}
public object Evaluate(BoundScript e)
{
@ -35,6 +40,8 @@ namespace Upsilon.Evaluator
return EvaluateUnaryExpression((BoundUnaryExpression) e);
case BoundKind.BoundAssignmentExpression:
return EvaluateAssignmentExpression((BoundAssignmenExpression) e);
case BoundKind.VariableExpression:
return EvaluateVariableExpression((BoundVariableExpression) e);
default:
throw new NotImplementedException();
}
@ -81,17 +88,14 @@ namespace Upsilon.Evaluator
private object EvaluateAssignmentExpression(BoundAssignmenExpression e)
{
var variableName = e.Variable.Name;
var val = EvaluateExpression(e.Expression);
_variables[variableName] = val;
_variables[e.Variable] = val;
return val;
}
private object EvaluateVariableExpression(VariableExpressionSyntax e)
private object EvaluateVariableExpression(BoundVariableExpression e)
{
var varName = e.Identifier.Name;
_diagnostics.LogUnknownVariable(e.Span, varName);
return null;
return _variables[e.Variable];
}
}

View File

@ -13,13 +13,13 @@ namespace Upsilon.Evaluator
public Diagnostics Diagnostics { get; }
private Binder.Binder Binder { get; }
public Script(string scriptString)
public Script(string scriptString, Dictionary<VariableSymbol, object> variables = null)
{
ScriptString = new SourceText(scriptString);
Diagnostics = new Diagnostics(ScriptString);
_parsed = Parser.Parser.Parse(scriptString, Diagnostics);
Binder = new Binder.Binder(new BoundScope(null), Diagnostics);
Evaluator = new Evaluator(this, Diagnostics);
Binder = new Binder.Binder(new BoundScope(variables, null), Diagnostics);
Evaluator = new Evaluator(this, Diagnostics, variables);
}
public object Evaluate()

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Upsilon;
using Upsilon.Binder;
using Upsilon.Evaluator;
namespace Yc
@ -10,7 +11,7 @@ namespace Yc
static void Main(string[] args)
{
Console.WriteLine("Upsilon REPL");
Dictionary<string, object> variables = new Dictionary<string, object>();
Dictionary<VariableSymbol, object> variables = new Dictionary<VariableSymbol, object>();
while (true)
{
Console.Write("» ");
@ -20,7 +21,7 @@ namespace Yc
return;
}
var parsed = new Script(input);
var parsed = new Script(input, variables);
if (parsed.Diagnostics.Messages.Count > 0)
{
Console.ForegroundColor = ConsoleColor.Red;