-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathAssignmentStatement.cs
More file actions
146 lines (117 loc) · 4.21 KB
/
Copy pathAssignmentStatement.cs
File metadata and controls
146 lines (117 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections.Generic;
using MoonSharp.Interpreter.Debugging;
using MoonSharp.Interpreter.Execution;
using MoonSharp.Interpreter.Tree.Expressions;
namespace MoonSharp.Interpreter.Tree.Statements
{
class AssignmentStatement : Statement
{
List<IVariable> m_LValues = new List<IVariable>();
List<Expression> m_RValues;
SourceRef m_Ref;
public AssignmentStatement(ScriptLoadingContext lcontext, Token startToken)
: base(lcontext)
{
List<string> names = new List<string>();
Token first = startToken;
while (true)
{
Token name = CheckTokenType(lcontext, TokenType.Name);
names.Add(name.Text);
if (lcontext.Lexer.Current.Type != TokenType.Comma)
break;
lcontext.Lexer.Next();
}
if (first.Type == TokenType.Local && lcontext.Lexer.Current.Type == TokenType.Op_Assignment && lcontext.Lexer.Current.Text != "=") {
throw new SyntaxErrorException(lcontext.Lexer.Current, $"Expected '=' after local, got '{lcontext.Lexer.Current.Text}'.");
}
if (lcontext.Lexer.Current.Type == TokenType.Op_Assignment)
{
CheckTokenType(lcontext, TokenType.Op_Assignment);
m_RValues = Expression.ExprList(lcontext);
}
else
{
m_RValues = new List<Expression>();
}
foreach (string name in names)
{
var localVar = lcontext.Scope.TryDefineLocal(name);
var symbol = new SymbolRefExpression(lcontext, localVar);
m_LValues.Add(symbol);
}
Token last = lcontext.Lexer.Current;
m_Ref = first.GetSourceRefUpTo(last);
lcontext.Source.Refs.Add(m_Ref);
}
public AssignmentStatement(ScriptLoadingContext lcontext, Expression firstExpression, Token first)
: base(lcontext)
{
m_LValues.Add(CheckVar(lcontext, firstExpression));
List<Expression> leftExpressions = new() {firstExpression};
while (lcontext.Lexer.Current.Type == TokenType.Comma)
{
lcontext.Lexer.Next();
Expression exp = Expression.PrimaryExp(lcontext);
m_LValues.Add(CheckVar(lcontext, exp));
leftExpressions.Add(exp);
}
Token assignmentType = lcontext.Lexer.Current;
CheckTokenType(lcontext, TokenType.Op_Assignment);
m_RValues = Expression.ExprList(lcontext);
// Replace e.g. "a += b" with "a = a + b"
if (assignmentType.Text != "=")
{
TokenType operationTokenType = assignmentType.Text switch
{
"+=" => 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.Text}"),
};
assignmentType.Text = "=";
for (int valueIndex = 0; valueIndex < m_RValues.Count; valueIndex++)
{
if (valueIndex < leftExpressions.Count)
{
object operatorChain = BinaryOperatorExpression.BeginOperatorChain();
BinaryOperatorExpression.AddExpressionToChain(operatorChain, leftExpressions[valueIndex]);
BinaryOperatorExpression.AddOperatorToChain(operatorChain, new Token(operationTokenType, first.SourceId, first.FromLine, first.FromCol, first.ToLine, first.ToCol, first.PrevLine, first.PrevCol));
BinaryOperatorExpression.AddExpressionToChain(operatorChain, m_RValues[valueIndex]);
m_RValues[valueIndex] = BinaryOperatorExpression.CommitOperatorChain(operatorChain, lcontext);
}
}
}
Token last = lcontext.Lexer.Current;
m_Ref = first.GetSourceRefUpTo(last);
lcontext.Source.Refs.Add(m_Ref);
}
private IVariable CheckVar(ScriptLoadingContext lcontext, Expression firstExpression)
{
IVariable v = firstExpression as IVariable;
if (v == null)
throw new SyntaxErrorException(lcontext.Lexer.Current, "unexpected symbol near '{0}' - not a l-value", lcontext.Lexer.Current);
return v;
}
public override void Compile(Execution.VM.ByteCode bc)
{
using (bc.EnterSource(m_Ref))
{
foreach (var exp in m_RValues)
{
exp.Compile(bc);
}
for (int i = 0; i < m_LValues.Count; i++)
m_LValues[i].CompileAssignment(bc,
Math.Max(m_RValues.Count - 1 - i, 0), // index of r-value
i - Math.Min(i, m_RValues.Count - 1)); // index in last tuple
bc.Emit_Pop(m_RValues.Count);
}
}
}
}