Reworked bound variables into specific LuaType class instead of anonymous objects
This commit is contained in:
33
Upsilon/BaseTypes/Boolean.cs
Normal file
33
Upsilon/BaseTypes/Boolean.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
namespace Upsilon.BaseTypes
|
||||
{
|
||||
public class Boolean : LuaType
|
||||
{
|
||||
public Boolean(bool value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public override Type Type => Type.Boolean;
|
||||
public bool Value { get; }
|
||||
|
||||
public static implicit operator bool(Boolean b)
|
||||
{
|
||||
return b.Value;
|
||||
}
|
||||
|
||||
public static implicit operator Boolean(bool b)
|
||||
{
|
||||
return new Boolean(b);
|
||||
}
|
||||
|
||||
public static Boolean operator ! (Boolean n)
|
||||
{
|
||||
return new Boolean(!n.Value);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Value.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
7
Upsilon/BaseTypes/LuaType.cs
Normal file
7
Upsilon/BaseTypes/LuaType.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Upsilon.BaseTypes
|
||||
{
|
||||
public abstract class LuaType
|
||||
{
|
||||
public abstract Type Type { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
namespace Upsilon.BaseTypes.Number
|
||||
{
|
||||
public abstract class Number
|
||||
public abstract class Number : LuaType
|
||||
{
|
||||
protected abstract bool IsFloat { get; }
|
||||
|
||||
public override Type Type => Type.Number;
|
||||
|
||||
#region Binary Operators
|
||||
|
||||
public static Number operator + (Number a, Number b)
|
||||
|
||||
@@ -21,5 +21,11 @@ namespace Upsilon.BaseTypes.Number
|
||||
{
|
||||
return Value.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
public static implicit operator double(NumberDouble n)
|
||||
{
|
||||
return n.Value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@ namespace Upsilon.BaseTypes.Number
|
||||
return Value.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
public static explicit operator long(NumberLong n)
|
||||
public static implicit operator long(NumberLong n)
|
||||
{
|
||||
return n.Value;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
using Upsilon.BaseTypes;
|
||||
using Upsilon.BaseTypes.Number;
|
||||
using Upsilon.Parser;
|
||||
using Boolean = Upsilon.BaseTypes.Boolean;
|
||||
using Type = Upsilon.BaseTypes.Type;
|
||||
|
||||
namespace Upsilon.Binder
|
||||
@@ -94,7 +96,7 @@ namespace Upsilon.Binder
|
||||
{
|
||||
var value = e.Value;
|
||||
var type = Type.Nil;
|
||||
object outValue = null;
|
||||
LuaType outValue = null;
|
||||
switch (value)
|
||||
{
|
||||
case double d:
|
||||
@@ -105,9 +107,9 @@ namespace Upsilon.Binder
|
||||
type = Type.Number;
|
||||
outValue = new NumberLong(l);
|
||||
break;
|
||||
case bool _:
|
||||
case bool b:
|
||||
type = Type.Boolean;
|
||||
outValue = value;
|
||||
outValue = new Boolean(b);
|
||||
break;
|
||||
default:
|
||||
_diagnostics.LogUnknownType(e.Span);
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Upsilon.Binder
|
||||
{
|
||||
public class BoundLiteralExpression : BoundExpression
|
||||
{
|
||||
public BoundLiteralExpression(object value, Type type)
|
||||
public BoundLiteralExpression(LuaType value, Type type)
|
||||
{
|
||||
Value = value;
|
||||
Type = type;
|
||||
@@ -12,6 +12,6 @@ namespace Upsilon.Binder
|
||||
|
||||
public override BoundKind Kind => BoundKind.BoundLiteralExpression;
|
||||
public override Type Type { get; }
|
||||
public object Value { get; }
|
||||
public LuaType Value { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Upsilon.BaseTypes;
|
||||
|
||||
namespace Upsilon.Binder
|
||||
{
|
||||
@@ -13,7 +14,7 @@ namespace Upsilon.Binder
|
||||
_parentScope = parentScope;
|
||||
_variables = new Dictionary<string, VariableSymbol>();
|
||||
}
|
||||
public BoundScope(Dictionary<VariableSymbol, object> variables, BoundScope parentScope)
|
||||
public BoundScope(Dictionary<VariableSymbol, LuaType> variables, BoundScope parentScope)
|
||||
{
|
||||
_parentScope = parentScope;
|
||||
_variables = variables.ToDictionary(x => x.Key.Name, x => x.Key);
|
||||
|
||||
@@ -1,30 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Upsilon.BaseTypes;
|
||||
using Upsilon.BaseTypes.Number;
|
||||
using Upsilon.Binder;
|
||||
using Boolean = Upsilon.BaseTypes.Boolean;
|
||||
|
||||
namespace Upsilon.Evaluator
|
||||
{
|
||||
public class Evaluator
|
||||
{
|
||||
private readonly Diagnostics _diagnostics;
|
||||
private object _value;
|
||||
private LuaType _value;
|
||||
private Script Script { get; }
|
||||
private readonly Dictionary<VariableSymbol, object> _variables = new Dictionary<VariableSymbol, object>();
|
||||
private readonly Dictionary<VariableSymbol, LuaType> _variables = new Dictionary<VariableSymbol, LuaType>();
|
||||
|
||||
public Evaluator(Script script, Diagnostics diagnostics)
|
||||
{
|
||||
_diagnostics = diagnostics;
|
||||
Script = script;
|
||||
}
|
||||
public Evaluator(Script script, Diagnostics diagnostics, Dictionary<VariableSymbol, object> vars)
|
||||
public Evaluator(Script script, Diagnostics diagnostics, Dictionary<VariableSymbol, LuaType> vars)
|
||||
{
|
||||
_diagnostics = diagnostics;
|
||||
Script = script;
|
||||
_variables = vars;
|
||||
}
|
||||
|
||||
public object Evaluate(BoundScript e)
|
||||
public LuaType Evaluate(BoundScript e)
|
||||
{
|
||||
EvaluateStatement(e.Statement);
|
||||
return _value;
|
||||
@@ -55,7 +57,7 @@ namespace Upsilon.Evaluator
|
||||
_value = EvaluateExpression(e.Expression);
|
||||
}
|
||||
|
||||
private object EvaluateExpression(BoundExpression e)
|
||||
private LuaType EvaluateExpression(BoundExpression e)
|
||||
{
|
||||
switch (e.Kind)
|
||||
{
|
||||
@@ -72,7 +74,7 @@ namespace Upsilon.Evaluator
|
||||
}
|
||||
}
|
||||
|
||||
private object EvaluateUnaryExpression(BoundUnaryExpression e)
|
||||
private LuaType EvaluateUnaryExpression(BoundUnaryExpression e)
|
||||
{
|
||||
var operand = EvaluateExpression(e.InExpression);
|
||||
switch (e.Operator.Kind)
|
||||
@@ -82,13 +84,13 @@ namespace Upsilon.Evaluator
|
||||
case BoundUnaryOperator.OperatorKind.Negation:
|
||||
return -((Number)operand);
|
||||
case BoundUnaryOperator.OperatorKind.LogicalNegation:
|
||||
return !(bool) operand;
|
||||
return !(Boolean) operand;
|
||||
default:
|
||||
throw new Exception("Invalid Unary Operator: " + e.Operator.Kind);
|
||||
}
|
||||
}
|
||||
|
||||
private object EvaluateBinaryExpression(BoundBinaryExpression e)
|
||||
private LuaType EvaluateBinaryExpression(BoundBinaryExpression e)
|
||||
{
|
||||
var left = EvaluateExpression(e.LeftExpression);
|
||||
var right = EvaluateExpression(e.RightExpression);
|
||||
@@ -103,9 +105,9 @@ namespace Upsilon.Evaluator
|
||||
case BoundBinaryOperator.OperatorKind.Division:
|
||||
return ((Number)left) / ((Number)right);
|
||||
case BoundBinaryOperator.OperatorKind.Equality:
|
||||
return Equals(left, right);
|
||||
return new Boolean(Equals(left, right));
|
||||
case BoundBinaryOperator.OperatorKind.Inequality:
|
||||
return !Equals(left, right);
|
||||
return new Boolean(!Equals(left, right));
|
||||
default:
|
||||
throw new Exception("Invalid Binary Operator: " + e.Operator);
|
||||
}
|
||||
@@ -118,7 +120,7 @@ namespace Upsilon.Evaluator
|
||||
_value = val;
|
||||
}
|
||||
|
||||
private object EvaluateVariableExpression(BoundVariableExpression e)
|
||||
private LuaType EvaluateVariableExpression(BoundVariableExpression e)
|
||||
{
|
||||
return _variables[e.Variable];
|
||||
}
|
||||
@@ -134,7 +136,7 @@ namespace Upsilon.Evaluator
|
||||
private void EvaluateBoundIfStatement(BoundIfStatement boundBlockStatement)
|
||||
{
|
||||
var condition = EvaluateExpression(boundBlockStatement.Condition.Expression);
|
||||
if ((bool) condition)
|
||||
if ((Boolean) condition)
|
||||
{
|
||||
EvaluateBoundBlockStatement(boundBlockStatement.Block);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using Upsilon.BaseTypes;
|
||||
using Upsilon.Binder;
|
||||
using Upsilon.Parser;
|
||||
using Upsilon.Text;
|
||||
@@ -13,13 +14,13 @@ namespace Upsilon.Evaluator
|
||||
public Diagnostics Diagnostics { get; }
|
||||
private Binder.Binder Binder { get; }
|
||||
|
||||
public Script(string scriptString, Dictionary<VariableSymbol, object> variables = null)
|
||||
public Script(string scriptString, Dictionary<VariableSymbol, LuaType> variables = null)
|
||||
{
|
||||
ScriptString = new SourceText(scriptString);
|
||||
Diagnostics = new Diagnostics(ScriptString);
|
||||
_parsed = Parser.Parser.Parse(scriptString, Diagnostics);
|
||||
if (variables == null)
|
||||
variables = new Dictionary<VariableSymbol, object>();
|
||||
variables = new Dictionary<VariableSymbol, LuaType>();
|
||||
Binder = new Binder.Binder(new BoundScope(variables, null), Diagnostics);
|
||||
Evaluator = new Evaluator(this, Diagnostics, variables);
|
||||
}
|
||||
@@ -34,7 +35,7 @@ namespace Upsilon.Evaluator
|
||||
return Evaluator.Evaluate(Bind());
|
||||
}
|
||||
|
||||
public T Evaluate<T>()
|
||||
public T Evaluate<T>() where T : LuaType
|
||||
{
|
||||
return (T)Evaluator.Evaluate(Bind());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user