Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/MoonSharp.Interpreter/Execution/VM/OpCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ internal enum OpCode
IterUpd, // Updates the var part of an iterator

NilCoalescing,
NilCoalescingInverse,

// Meta
Invalid, // Crashes the executor with an unrecoverable NotImplementedException. This MUST always be the last opcode in enum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ private DynValue Processing_Loop(int instructionPtr, bool canAwait = false)
instructionPtr = ExecNilCoalescingAssignment(i, instructionPtr);
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
break;
case OpCode.NilCoalescingInverse:
instructionPtr = ExecNilCoalescingAssignmentInverse(i, instructionPtr);
if (instructionPtr == YIELD_SPECIAL_TRAP) goto yield_to_calling_coroutine;
break;
case OpCode.Invalid:
throw new NotImplementedException(string.Format("Invalid opcode : {0}", i.String));
default:
Expand Down Expand Up @@ -864,6 +868,18 @@ private int ExecNilCoalescingAssignment(Instruction i, int instructionPtr)
m_ValueStack.Pop();
return instructionPtr;
}

private int ExecNilCoalescingAssignmentInverse(Instruction i, int instructionPtr)
{
ref DynValue lhs = ref m_ValueStack.Peek(1);
if (lhs.IsNotNil())
{
m_ValueStack.Set(1, m_ValueStack.Peek());
}

m_ValueStack.Pop();
return instructionPtr;
}

private int ExecRet(Instruction i, int currentPtr)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public enum Operator
BitXor = 0x100000,
BitLShift = 0x200000,
BitRShiftA = 0x400000,
BitRShiftL = 0x4800000
BitRShiftL = 0x4800000,
NilCoalescingInverse = 0x9000000,
}

/// <summary>
Expand Down Expand Up @@ -65,6 +66,7 @@ class LinkedList
const Operator LOGIC_OR = Operator.Or;
const Operator NIL_COAL_ASSIGN = Operator.NilCoalescing;
const Operator SHIFTS = Operator.BitLShift | Operator.BitRShiftA | Operator.BitRShiftL;
const Operator NIL_COAL_INVERSE = Operator.NilCoalescingInverse;

public static object BeginOperatorChain()
{
Expand Down Expand Up @@ -158,6 +160,9 @@ private static Expression CreateSubTree(LinkedList list, ScriptLoadingContext lc

if ((opfound & NIL_COAL_ASSIGN) != 0)
nodes = PrioritizeLeftAssociative(nodes, lcontext, NIL_COAL_ASSIGN);

if ((opfound & NIL_COAL_INVERSE) != 0)
nodes = PrioritizeLeftAssociative(nodes, lcontext, NIL_COAL_INVERSE);

if (nodes.Next != null || nodes.Prev != null)
throw new InternalErrorException("Expression reduction didn't work! - 1");
Expand Down Expand Up @@ -261,6 +266,8 @@ private static Operator ParseBinaryOperator(Token token)
return Operator.Power;
case TokenType.Op_NilCoalesce:
return Operator.NilCoalescing;
case TokenType.Op_NilCoalesceInverse:
return Operator.NilCoalescingInverse;
case TokenType.Op_Or:
return Operator.BitOr;
case TokenType.Op_And:
Expand Down Expand Up @@ -346,6 +353,8 @@ public static OpCode OperatorToOpCode(Operator op)
return OpCode.BRShiftA;
case Operator.BitRShiftL:
return OpCode.BRShiftL;
case Operator.NilCoalescingInverse:
return OpCode.NilCoalescingInverse;
default:
throw new InternalErrorException("Unsupported operator {0}", op);
}
Expand Down Expand Up @@ -401,6 +410,12 @@ public override bool EvalLiteral(out DynValue dv)
else dv = v1;
return true;
}
if (m_Operator == Operator.NilCoalescingInverse)
{
if (v1.IsNotNil()) dv = v2;
else dv = v1;
return true;
}
if (m_Operator == Operator.Or)
{
if (v1.CastToBool())
Expand Down Expand Up @@ -453,7 +468,12 @@ public override DynValue Eval(ScriptExecutionContext context)
if (m_Operator == Operator.NilCoalescing)
{
if (v1.IsNil()) return m_Exp2.Eval(context);
else return v1;
return v1;
}
if (m_Operator == Operator.NilCoalescingInverse)
{
if (v1.IsNotNil()) return m_Exp2.Eval(context);
return v1;
}
if (m_Operator == Operator.Or)
{
Expand Down
14 changes: 12 additions & 2 deletions src/MoonSharp.Interpreter/Tree/Lexer/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,20 @@ private Token ReadToken()
CursorCharNext();
return CreateToken(TokenType.Op_NilCoalescingAssignment, fromLine, fromCol, "??=");
}
else

return CreateToken(TokenType.Op_NilCoalesce, fromLine, fromCol, "??");
}

if (next == '!')
{
char next2 = CursorCharNext();
if (next2 == '=')
{
return CreateToken(TokenType.Op_NilCoalesce, fromLine, fromCol, "??");
CursorCharNext();
return CreateToken(TokenType.Op_NilCoalescingAssignmentInverse, fromLine, fromCol, "?!=");
}

return CreateToken(TokenType.Op_NilCoalesceInverse, fromLine, fromCol, "?!");
}

return CreateToken(TokenType.Ternary, fromLine, fromCol, "?");
Expand Down
1 change: 1 addition & 0 deletions src/MoonSharp.Interpreter/Tree/Lexer/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public bool IsBinaryOperator()
case TokenType.Op_MinusOrSub:
case TokenType.Op_Add:
case TokenType.Op_NilCoalesce:
case TokenType.Op_NilCoalesceInverse:
case TokenType.Op_Or:
case TokenType.Op_And:
case TokenType.Op_Xor:
Expand Down
2 changes: 2 additions & 0 deletions src/MoonSharp.Interpreter/Tree/Lexer/TokenType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ enum TokenType
Op_OrEq,
//Nil operators
Op_NilCoalesce,
Op_NilCoalesceInverse,
Op_NilCoalescingAssignment,
Op_NilCoalescingAssignmentInverse,

Ternary,
Comment,
Expand Down
10 changes: 10 additions & 0 deletions src/MoonSharp.Interpreter/Tree/Statements/AssignmentStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public AssignmentStatement(ScriptLoadingContext lcontext, Token startToken)
AssignmentOp = Operator.NilCoalescing;
m_RValues = Expression.ExprList(lcontext);
}
else if (lcontext.Syntax == ScriptSyntax.CLike && lcontext.Lexer.Current.Type == TokenType.Op_NilCoalesceInverse)
{
CheckTokenType(lcontext, TokenType.Op_NilCoalesceInverse);
AssignmentOp = Operator.NilCoalescingInverse;
m_RValues = Expression.ExprList(lcontext);
}
else
{
if (names.Count > 0)
Expand Down Expand Up @@ -127,6 +133,10 @@ public AssignmentStatement(ScriptLoadingContext lcontext, Expression firstExpres
AssignmentOp = Operator.NilCoalescing;
lcontext.Lexer.Next();
break;
case TokenType.Op_NilCoalescingAssignmentInverse:
AssignmentOp = Operator.NilCoalescingInverse;
lcontext.Lexer.Next();
break;
default:
CheckTokenType(lcontext, TokenType.Op_Assignment);
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = 10 ?! 100
print(x)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
100
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x ?!= 500
print(x)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x = 10
x ?!= 500
print(x)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
500