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.Collections.Generic;
using System.Linq;
namespace Upsilon.Binder namespace Upsilon.Binder
{ {
@ -12,6 +13,11 @@ namespace Upsilon.Binder
_parentScope = parentScope; _parentScope = parentScope;
_variables = new Dictionary<string, VariableSymbol>(); _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) public void SetVariable(VariableSymbol var)
{ {

View File

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

View File

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

View File

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