More work on variables
This commit is contained in:
parent
05d0de31a0
commit
3d811ff801
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Upsilon.Binder
|
||||
{
|
||||
|
@ -10,7 +11,12 @@ namespace Upsilon.Binder
|
|||
public BoundScope(BoundScope 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)
|
||||
|
|
|
@ -2,7 +2,6 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using Upsilon.BaseTypes.Number;
|
||||
using Upsilon.Binder;
|
||||
using Upsilon.Parser;
|
||||
|
||||
namespace Upsilon.Evaluator
|
||||
{
|
||||
|
@ -10,12 +9,18 @@ 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;
|
||||
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];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue