Browse Source

Console: String indexer ("abcd"[2]);

Console: Assignment operators (i += 1)

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@4499 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
David Srbecký 17 years ago
parent
commit
18bd0dcfc9
  1. 45
      src/AddIns/Misc/Debugger/Debugger.AddIn/Project/Src/Expressions/AstEvaluator.cs
  2. 114
      src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/AstEval.cs

45
src/AddIns/Misc/Debugger/Debugger.AddIn/Project/Src/Expressions/AstEvaluator.cs

@ -92,9 +92,39 @@ namespace Debugger.AddIn @@ -92,9 +92,39 @@ namespace Debugger.AddIn
public override object VisitAssignmentExpression(AssignmentExpression assignmentExpression, object data)
{
// Calculate right first so that left does not get invalidated by its calculation
Value right = ((Value)assignmentExpression.Right.AcceptVisitor(this, null)).GetPermanentReference();
Value left = (Value)assignmentExpression.Left.AcceptVisitor(this, null);
BinaryOperatorType op;
switch (assignmentExpression.Op) {
case AssignmentOperatorType.Assign: op = BinaryOperatorType.None; break;
case AssignmentOperatorType.Add: op = BinaryOperatorType.Add; break;
case AssignmentOperatorType.ConcatString: op = BinaryOperatorType.Concat; break;
case AssignmentOperatorType.Subtract: op = BinaryOperatorType.Subtract; break;
case AssignmentOperatorType.Multiply: op = BinaryOperatorType.Multiply; break;
case AssignmentOperatorType.Divide: op = BinaryOperatorType.Divide; break;
case AssignmentOperatorType.DivideInteger: op = BinaryOperatorType.DivideInteger; break;
case AssignmentOperatorType.ShiftLeft: op = BinaryOperatorType.ShiftLeft; break;
case AssignmentOperatorType.ShiftRight: op = BinaryOperatorType.ShiftRight; break;
case AssignmentOperatorType.ExclusiveOr: op = BinaryOperatorType.ExclusiveOr; break;
case AssignmentOperatorType.Modulus: op = BinaryOperatorType.Modulus; break;
case AssignmentOperatorType.BitwiseAnd: op = BinaryOperatorType.BitwiseAnd; break;
case AssignmentOperatorType.BitwiseOr: op = BinaryOperatorType.BitwiseOr; break;
case AssignmentOperatorType.Power: op = BinaryOperatorType.Power; break;
default: throw new GetValueException("Unknown operator " + assignmentExpression.Op);
}
Value right;
if (op == BinaryOperatorType.None) {
right = ((Value)assignmentExpression.Right.AcceptVisitor(this, null)).GetPermanentReference();
} else {
BinaryOperatorExpression binOpExpr = new BinaryOperatorExpression();
binOpExpr.Left = assignmentExpression.Left;
binOpExpr.Op = op;
binOpExpr.Right = assignmentExpression.Right;
right = (Value)VisitBinaryOperatorExpression(binOpExpr, null);
}
right = right.GetPermanentReference();
Value left = ((Value)assignmentExpression.Left.AcceptVisitor(this, null));
if (!left.IsReference && left.Type.FullName != right.Type.FullName) {
throw new GetValueException(string.Format("Type {0} expected, {1} seen", left.Type.FullName, right.Type.FullName));
}
@ -163,6 +193,15 @@ namespace Debugger.AddIn @@ -163,6 +193,15 @@ namespace Debugger.AddIn
return target.GetArrayElement(intIndexes.ToArray());
}
if (target.Type.IsPrimitive && target.PrimitiveValue is string) {
if (indexes.Count == 1 && indexes[0].Type.IsInteger) {
int index = (int)indexes[0].PrimitiveValue;
return Eval.CreateValue(context.Process, ((string)target.PrimitiveValue)[index]);
} else {
throw new GetValueException("Expected single integer index");
}
}
PropertyInfo pi = target.Type.GetProperty("Item");
if (pi == null) throw new GetValueException("The object does not have an indexer property");
return target.GetPropertyValue(pi, indexes.ToArray());

114
src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/AstEval.cs

@ -14,6 +14,7 @@ namespace Debugger.Tests.TestPrograms @@ -14,6 +14,7 @@ namespace Debugger.Tests.TestPrograms
{
public static void Main()
{
bool flag = true;
byte b = 1;
int i = 4;
float pi = 3.14f;
@ -42,35 +43,54 @@ namespace Debugger.Tests { @@ -42,35 +43,54 @@ namespace Debugger.Tests {
{
StartTest("AstEval.cs");
Eval("b");
Eval("i");
Eval("pi");
Eval("pi - 3");
Eval("b + i");
Eval("i + b");
Eval("b + pi");
Eval("pi + b");
Eval("hi + pi");
Eval("pi + hi");
Eval("pi + \" \" + hi");
string input = @"
b
i
pi
pi - 3
b + i
i + b
b + pi
pi + b
hi + pi
pi + hi
pi + ' ' + hi
(5 + 6) % (1 + 2)
b + 3 == i
b + 4 == i
true == true
true == false
array
arrays
array[1]
array[i]
array[i - 1]
list
list[1]
list[i]
hi[1]
'abcd'[2]
list.Add(42); list.Add(52);
list
i = 10
-i
++i
i++
i += 1
i
flag
!flag
";
Eval("(5 + 6) % (1 + 2)");
Eval("b + 3 == i");
Eval("b + 4 == i");
Eval("true == true");
Eval("true == false");
input = input.Replace("'", "\"");
Eval("array");
Eval("arrays");
Eval("array[1]");
Eval("array[i]");
Eval("array[i - 1]");
Eval("list");
Eval("list[1]");
Eval("list[i]");
Eval("list.Add(42); list.Add(52);");
Eval("list");
foreach(string line in input.Split('\n')) {
Eval(line.Trim());
}
EndTest();
}
@ -78,13 +98,21 @@ namespace Debugger.Tests { @@ -78,13 +98,21 @@ namespace Debugger.Tests {
void Eval(string expr)
{
string restultFmted;
try {
Value result = AstEvaluator.Evaluate(expr, SupportedLanguage.CSharp, process.SelectedStackFrame);
restultFmted = AstEvaluator.FormatValue(result);
} catch (GetValueException e) {
restultFmted = "Error: " + e.Message;
if (string.IsNullOrEmpty(expr)) {
restultFmted = null;
} else {
try {
Value result = AstEvaluator.Evaluate(expr, SupportedLanguage.CSharp, process.SelectedStackFrame);
restultFmted = AstEvaluator.FormatValue(result);
} catch (GetValueException e) {
restultFmted = "Error: " + e.Message;
}
}
if (restultFmted != null) {
ObjectDump("Eval", " " + expr + " = " + restultFmted + " ");
} else {
ObjectDump("Eval", " " + expr);
}
ObjectDump("Eval", " " + expr + " = " + restultFmted + " ");
}
}
}
@ -98,7 +126,8 @@ namespace Debugger.Tests { @@ -98,7 +126,8 @@ namespace Debugger.Tests {
<ProcessStarted />
<ModuleLoaded>mscorlib.dll (No symbols)</ModuleLoaded>
<ModuleLoaded>AstEval.exe (Has symbols)</ModuleLoaded>
<DebuggingPaused>Break AstEval.cs:27,4-27,40</DebuggingPaused>
<DebuggingPaused>Break AstEval.cs:28,4-28,40</DebuggingPaused>
<Eval> </Eval>
<Eval> b = 1 </Eval>
<Eval> i = 4 </Eval>
<Eval> pi = 3.14 </Eval>
@ -110,11 +139,13 @@ namespace Debugger.Tests { @@ -110,11 +139,13 @@ namespace Debugger.Tests {
<Eval> hi + pi = hi3.14 </Eval>
<Eval> pi + hi = 3.14hi </Eval>
<Eval> pi + " " + hi = 3.14 hi </Eval>
<Eval> </Eval>
<Eval> (5 + 6) % (1 + 2) = 2 </Eval>
<Eval> b + 3 == i = True </Eval>
<Eval> b + 4 == i = False </Eval>
<Eval> true == true = True </Eval>
<Eval> true == false = False </Eval>
<Eval> </Eval>
<Eval> array = Char[] {H, e, l, l, o} </Eval>
<Eval> arrays = Char[][] {Char[] {H, e, l, l, o}, Char[] {w, o, r, l, d}} </Eval>
<Eval> array[1] = e </Eval>
@ -123,8 +154,21 @@ namespace Debugger.Tests { @@ -123,8 +154,21 @@ namespace Debugger.Tests {
<Eval> list = List&lt;Char&gt; {H, e, l, l, o} </Eval>
<Eval> list[1] = e </Eval>
<Eval> list[i] = o </Eval>
<Eval> list.Add(42); list.Add(52); = </Eval>
<Eval> hi[1] = i </Eval>
<Eval> "abcd"[2] = c </Eval>
<Eval> </Eval>
<Eval> list.Add(42); list.Add(52);</Eval>
<Eval> list = List&lt;Char&gt; {H, e, l, l, o, *, 4} </Eval>
<Eval> </Eval>
<Eval> i = 10 = 10 </Eval>
<Eval> -i = Error: Language feature not implemented: UnaryOperatorExpression </Eval>
<Eval> ++i = Error: Language feature not implemented: UnaryOperatorExpression </Eval>
<Eval> i++ = Error: Language feature not implemented: UnaryOperatorExpression </Eval>
<Eval> i += 1 = 11 </Eval>
<Eval> i = 11 </Eval>
<Eval> flag = True </Eval>
<Eval> !flag = Error: Language feature not implemented: UnaryOperatorExpression </Eval>
<Eval> </Eval>
<ProcessExited />
</Test>
</DebuggerTests>

Loading…
Cancel
Save