Skip to content

Commit

Permalink
Added ..= operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Joy-less committed Aug 10, 2023
1 parent fe4fc00 commit 267a140
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
24 changes: 18 additions & 6 deletions src/MoonSharp.Interpreter/Tree/Lexer/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private Token ReadToken()
{
char next = CursorCharNext();
if (next == '.')
return PotentiallyDoubleCharOperator('.', TokenType.Op_Concat, TokenType.VarArgs, fromLine, fromCol);
return PotentiallyDoubleCharOperator(TokenType.Op_Concat, '.', TokenType.VarArgs, "...", '=', TokenType.Op_Assignment, "..=", fromLine, fromCol);
else if (LexerUtils.CharIsDigit(next))
return ReadNumberToken(fromLine, fromCol, true);
else
Expand Down Expand Up @@ -545,8 +545,25 @@ private Token PotentiallyDoubleCharOperator(char expectedSecondChar, TokenType s
else
return CreateToken(singleCharToken, fromLine, fromCol, op);
}
private Token PotentiallyDoubleCharOperator(TokenType singleCharToken, char expectedSecondChar, TokenType doubleCharToken, string doubleCharText, char alternateExpectedSecondChar, TokenType alternateDoubleCharToken, string alternateDoubleCharText, int fromLine, int fromCol)
{
string op = CursorChar().ToString();

CursorCharNext();

if (CursorChar() == expectedSecondChar)
{
CursorCharNext();
return CreateToken(doubleCharToken, fromLine, fromCol, doubleCharText);
}
else if (CursorChar() == alternateExpectedSecondChar)
{
CursorCharNext();
return CreateToken(alternateDoubleCharToken, fromLine, fromCol, alternateDoubleCharText);
}
else
return CreateToken(singleCharToken, fromLine, fromCol, op);
}

private Token CreateNameToken(string name, int fromLine, int fromCol)
{
Expand All @@ -562,7 +579,6 @@ private Token CreateNameToken(string name, int fromLine, int fromCol)
}
}


private Token CreateToken(TokenType tokenType, int fromLine, int fromCol, string text = null)
{
Token t = new Token(tokenType, m_SourceId, fromLine, fromCol, m_Line, m_Col, m_PrevLineTo, m_PrevColTo)
Expand All @@ -588,9 +604,5 @@ private string ReadNameToken()

return name.ToString();
}




}
}
17 changes: 9 additions & 8 deletions src/MoonSharp.Interpreter/Tree/Statements/AssignmentStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,24 @@ public AssignmentStatement(ScriptLoadingContext lcontext, Expression firstExpres
m_LValues.Add(CheckVar(lcontext, e));
}

char assignmentType = lcontext.Lexer.Current.Text[0];
string assignmentType = lcontext.Lexer.Current.Text;

CheckTokenType(lcontext, TokenType.Op_Assignment);

m_RValues = Expression.ExprList(lcontext);

// Replace e.g. "a += b" with "a = a + b"
if (assignmentType != '=')
if (assignmentType != "=")
{
TokenType ArithmeticOperation = assignmentType switch
{
'+' => TokenType.Op_Add,
'-' => TokenType.Op_MinusOrSub,
'*' => TokenType.Op_Mul,
'/' => TokenType.Op_Div,
'%' => TokenType.Op_Mod,
'^' => TokenType.Op_Pwr,
"+=" => TokenType.Op_Add,
"-=" => TokenType.Op_MinusOrSub,
"*=" => TokenType.Op_Mul,
"/=" => TokenType.Op_Div,
"%=" => TokenType.Op_Mod,
"^=" => TokenType.Op_Pwr,
"..=" => TokenType.Op_Concat,
_ => throw new InternalErrorException($"Assignment operator not recognised: {assignmentType}"),
};

Expand Down

0 comments on commit 267a140

Please sign in to comment.