Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/MoonSharp.Interpreter/Tree/Expression_.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using MoonSharp.Interpreter.Execution;
using MoonSharp.Interpreter.Tree.Expressions;
using MoonSharp.Interpreter.DataStructs;
using MoonSharp.Interpreter.Execution.VM;

namespace MoonSharp.Interpreter.Tree
{
Expand All @@ -17,6 +20,18 @@ public virtual string GetFriendlyDebugName()

public abstract DynValue Eval(ScriptExecutionContext context);

public abstract bool EvalLiteral(out DynValue dv);

public void CompilePossibleLiteral(ByteCode bc)
{
if (EvalLiteral(out var dv))
{
if(dv == null) throw new NullReferenceException("Invalid literal: null");
bc.Emit_Literal(dv);
}
else Compile(bc);
}

public virtual SymbolRef FindDynamic(ScriptExecutionContext context)
{
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MoonSharp.Interpreter.Execution;
using MoonSharp.Interpreter.DataStructs;
using MoonSharp.Interpreter.Execution;


namespace MoonSharp.Interpreter.Tree.Expressions
Expand All @@ -23,5 +24,15 @@ public override DynValue Eval(ScriptExecutionContext context)
{
return expression.Eval(context).ToScalar();
}

public override bool EvalLiteral(out DynValue dv)
{
if (expression.EvalLiteral(out dv))
{
dv = dv.ToScalar();
return true;
}
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using MoonSharp.Interpreter.DataStructs;
using MoonSharp.Interpreter.Execution;
using MoonSharp.Interpreter.Execution.VM;

Expand Down Expand Up @@ -299,28 +300,28 @@ private static OpCode OperatorToOpCode(Operator op)

public override void Compile(Execution.VM.ByteCode bc)
{
m_Exp1.Compile(bc);
m_Exp1.CompilePossibleLiteral(bc);

if (m_Operator == Operator.Or)
{
Instruction i = bc.Emit_Jump(OpCode.JtOrPop, -1);
m_Exp2.Compile(bc);
m_Exp2.CompilePossibleLiteral(bc);
i.NumVal = bc.GetJumpPointForNextInstruction();
return;
}

if (m_Operator == Operator.And)
{
Instruction i = bc.Emit_Jump(OpCode.JfOrPop, -1);
m_Exp2.Compile(bc);
m_Exp2.CompilePossibleLiteral(bc);
i.NumVal = bc.GetJumpPointForNextInstruction();
return;
}


if (m_Exp2 != null)
{
m_Exp2.Compile(bc);
m_Exp2.CompilePossibleLiteral(bc);
}

bc.Emit_Operator(OperatorToOpCode(m_Operator));
Expand All @@ -329,6 +330,50 @@ public override void Compile(Execution.VM.ByteCode bc)
bc.Emit_Operator(OpCode.Not);
}

public override bool EvalLiteral(out DynValue dv)
{
dv = null;
if (!m_Exp1.EvalLiteral(out var v1))
return false;
v1 = v1.ToScalar();
if (!m_Exp2.EvalLiteral(out var v2))
return false;
v2 = v2.ToScalar();
if (m_Operator == Operator.Or)
{
if (v1.CastToBool())
dv = v1;
else
dv = v2;
}
else if (m_Operator == Operator.And)
{
if (!v1.CastToBool())
dv = v1;
else
dv = v2;
}
else if ((m_Operator & COMPARES) != 0)
{
dv = DynValue.NewBoolean(EvalComparison(v1, v2, m_Operator));
}
else if (m_Operator == Operator.StrConcat)
{
string s1 = v1.CastToString();
string s2 = v2.CastToString();

if (s1 == null || s2 == null)
throw new DynamicExpressionException("Attempt to perform concatenation on non-strings.");

dv = DynValue.NewString(s1 + s2);
}
else
{
dv = DynValue.NewNumber(EvalArithmetic(v1, v2));
}
return true;
}

public override DynValue Eval(ScriptExecutionContext context)
{
DynValue v1 = m_Exp1.Eval(context).ToScalar();
Expand Down Expand Up @@ -398,6 +443,8 @@ private double EvalArithmetic(DynValue v1, DynValue v2)
if (mod < 0) mod += d2;
return mod;
}
case Operator.Power:
return Math.Pow(d1, d2);
default:
throw new DynamicExpressionException("Unsupported operator {0}", m_Operator);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using MoonSharp.Interpreter.DataStructs;
using MoonSharp.Interpreter.Execution;

namespace MoonSharp.Interpreter.Tree.Expressions
Expand All @@ -20,6 +21,11 @@ public override DynValue Eval(ScriptExecutionContext context)
return m_Exp.Eval(context);
}

public override bool EvalLiteral(out DynValue dv)
{
throw new InvalidOperationException();
}

public override void Compile(Execution.VM.ByteCode bc)
{
throw new InvalidOperationException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using MoonSharp.Interpreter.DataStructs;
using MoonSharp.Interpreter.Execution;

namespace MoonSharp.Interpreter.Tree.Expressions
Expand All @@ -22,7 +23,7 @@ public Expression[] GetExpressions()
public override void Compile(Execution.VM.ByteCode bc)
{
foreach (var exp in expressions)
exp.Compile(bc);
exp.CompilePossibleLiteral(bc);

if (expressions.Count > 1)
bc.Emit_MkTuple(expressions.Count);
Expand All @@ -35,5 +36,11 @@ public override DynValue Eval(ScriptExecutionContext context)

return DynValue.Void;
}

public override bool EvalLiteral(out DynValue dv)
{
dv = null;
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using MoonSharp.Interpreter.DataStructs;
using MoonSharp.Interpreter.Debugging;
using MoonSharp.Interpreter.Execution;

Expand Down Expand Up @@ -80,8 +81,9 @@ public override void Compile(Execution.VM.ByteCode bc)
++argslen;
}


for (int i = 0; i < m_Arguments.Count; i++)
m_Arguments[i].Compile(bc);
m_Arguments[i].CompilePossibleLiteral(bc);

if (!string.IsNullOrEmpty(m_Name))
{
Expand All @@ -93,6 +95,12 @@ public override void Compile(Execution.VM.ByteCode bc)
}
}

public override bool EvalLiteral(out DynValue dv)
{
dv = null;
return false;
}

public override DynValue Eval(ScriptExecutionContext context)
{
throw new DynamicExpressionException("Dynamic Expressions cannot call functions.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using MoonSharp.Interpreter.DataStructs;
using MoonSharp.Interpreter.Debugging;
using MoonSharp.Interpreter.Execution;
using MoonSharp.Interpreter.Execution.VM;
Expand Down Expand Up @@ -250,6 +251,12 @@ public int Compile(ByteCode bc, Func<int> afterDecl, string friendlyName)
return CompileBody(bc, friendlyName);
}

public override bool EvalLiteral(out DynValue dv)
{
dv = null;
return false;
}


public override void Compile(ByteCode bc)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MoonSharp.Interpreter.Execution;
using MoonSharp.Interpreter.DataStructs;
using MoonSharp.Interpreter.Execution;
using MoonSharp.Interpreter.Execution.VM;

namespace MoonSharp.Interpreter.Tree.Expressions
Expand Down Expand Up @@ -74,5 +75,11 @@ public override DynValue Eval(ScriptExecutionContext context)
else if (i.IsNilOrNan()) throw new DynamicExpressionException("Attempt to index with nil or nan key.");
return b.Table.Get(i) ?? DynValue.Nil;
}

public override bool EvalLiteral(out DynValue dv)
{
dv = null;
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MoonSharp.Interpreter.Execution;
using MoonSharp.Interpreter.DataStructs;
using MoonSharp.Interpreter.Execution;

namespace MoonSharp.Interpreter.Tree.Expressions
{
Expand Down Expand Up @@ -61,5 +62,11 @@ public override DynValue Eval(ScriptExecutionContext context)
{
return m_Value;
}

public override bool EvalLiteral(out DynValue dv)
{
dv = m_Value;
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MoonSharp.Interpreter.Execution;
using MoonSharp.Interpreter.DataStructs;
using MoonSharp.Interpreter.Execution;

namespace MoonSharp.Interpreter.Tree.Expressions
{
Expand Down Expand Up @@ -58,6 +59,12 @@ public override DynValue Eval(ScriptExecutionContext context)
return context.EvaluateSymbolByName(m_VarName);
}

public override bool EvalLiteral(out DynValue dv)
{
dv = null;
return false;
}

public override SymbolRef FindDynamic(ScriptExecutionContext context)
{
return context.FindSymbolByName(m_VarName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using MoonSharp.Interpreter.DataStructs;
using MoonSharp.Interpreter.Execution;

namespace MoonSharp.Interpreter.Tree.Expressions
Expand Down Expand Up @@ -131,5 +132,11 @@ public override DynValue Eval(ScriptExecutionContext context)

return tval;
}

public override bool EvalLiteral(out DynValue dv)
{
dv = null;
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using MoonSharp.Interpreter.Execution;
using System;
using MoonSharp.Interpreter.DataStructs;
using MoonSharp.Interpreter.Execution;
using MoonSharp.Interpreter.Execution.VM;

namespace MoonSharp.Interpreter.Tree.Expressions
Expand Down Expand Up @@ -62,5 +64,31 @@ public override DynValue Eval(ScriptExecutionContext context)
throw new DynamicExpressionException("Unexpected unary operator '{0}'", m_OpText);
}
}

public override bool EvalLiteral(out DynValue dv)
{
dv = null;
if (!m_Exp.EvalLiteral(out var v))
{
return false;
}
switch (m_OpText)
{
case "not":
dv = DynValue.NewBoolean(!v.CastToBool());
return true;
case "#":
return false;
case "-":
double? d = v.CastToNumber();
if (d.HasValue)
{
dv = DynValue.NewNumber(-d.Value);
return true;
}
break;
}
throw new Exception("Invalid literal evaluation");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public override void Compile(Execution.VM.ByteCode bc)
{
foreach (var exp in m_RValues)
{
exp.Compile(bc);
exp.CompilePossibleLiteral(bc);
}

for (int i = 0; i < m_LValues.Count; i++)
Expand Down
4 changes: 2 additions & 2 deletions src/MoonSharp.Interpreter/Tree/Statements/ForLoopStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ public override void Compile(ByteCode bc)

bc.LoopTracker.Loops.Push(L);

m_End.Compile(bc);
m_End.CompilePossibleLiteral(bc);
bc.Emit_ToNum(3);
m_Step.Compile(bc);
bc.Emit_ToNum(2);
m_Start.Compile(bc);
m_Start.CompilePossibleLiteral(bc);
bc.Emit_ToNum(1);

int start = bc.GetJumpPointForNextInstruction();
Expand Down
2 changes: 1 addition & 1 deletion src/MoonSharp.Interpreter/Tree/Statements/IfStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public override void Compile(Execution.VM.ByteCode bc)
if (lastIfJmp != null)
lastIfJmp.NumVal = bc.GetJumpPointForNextInstruction();

ifblock.Exp.Compile(bc);
ifblock.Exp.CompilePossibleLiteral(bc);
lastIfJmp = bc.Emit_Jump(OpCode.Jf, -1);
bc.Emit_Enter(ifblock.StackFrame);
ifblock.Block.Compile(bc);
Expand Down
Loading