internal CallPoint(int modHandle, EvalStack stack, ElaValue[] locals, FastList<ElaValue[]> captures) { ModuleHandle = modHandle; Locals = locals; Captures = captures; Stack = stack; }
public void EvalStackProveICanCreateInstance() { // Arrange EvalStack my_stack = new EvalStack(); // Assert Assert.IsNotNull(my_stack); }
public void ExpressionProveICanHandleBadExpression3() { // Arrange Expression my_expression = new Expression(); EvalStack eval_stack = new EvalStack(); // Act my_expression.Parse("5", eval_stack); }
public void ExpressionProveICanParse() { // Arrange Expression my_expression = new Expression(); EvalStack eval_stack = new EvalStack(); // Act object[] actual = my_expression.Parse("5 + 9", eval_stack); object[] expected = { 5, '+', 9 }; // Assert CollectionAssert.AreEqual(expected, actual); }
public void TerminalProveICanDisplayExpressionResult() { // Arrange Terminal my_term = new Terminal(); EvalStack eval_stack = new EvalStack(); double result = 44; // Act string actual = my_term.DisplayExpressionResult(result); string expected = " = 44"; // Assert Assert.AreEqual(actual, expected); }
public void EvalStackProveICanAccessLastQ() { // Arrange EvalStack my_stack = new EvalStack(); my_stack.lastq = new object[] { 8, '*', 9 }; // Act object[] actual = my_stack.lastq; object[] expected = { 8, '*', 9 }; // Assert CollectionAssert.AreEqual(actual, expected); }
public void EvalStackProveICanAccessLast() { // Arrange EvalStack my_stack = new EvalStack(); my_stack.last = 48; // Act double actual = my_stack.last; double expected = 48; // Assert Assert.AreEqual(actual, expected); }
public void TerminalProveICanReturnLastExpression() { // Arrange Terminal my_term = new Terminal(); EvalStack my_evalStack = new EvalStack(); my_evalStack.lastq = new object[] { 4, '*', 6 }; // Act string actual = my_term.ReturnLastExpression(my_evalStack); string expected = " 4*6"; // Assert Assert.AreEqual(actual, expected); }
public void ExpressionProveICanParseConstantExp() { // Arrange Expression my_expression = new Expression(); EvalStack eval_stack = new EvalStack(); object[] test_expression = { 'M', '=', 5 }; // Act object[] actual = my_expression.Parse("M = 5", eval_stack); object[] expected = test_expression; // Assert CollectionAssert.AreEqual(actual, expected); }
public void EvaluateProveICanDivideUnevenly() { // Arrange Evaluate division_exp = new Evaluate(); EvalStack last_values = new EvalStack(); object[] test_expression = { 15, '/', 4 }; // Act double actual = division_exp.EvaluateExpression(test_expression, last_values); double expected = 3.75; // Assert Assert.AreEqual(actual, expected); }
public void EvaluateProveICanDivideTerms() { // Arrange Evaluate division_exp = new Evaluate(); EvalStack last_values = new EvalStack(); object[] test_expression = { 15, '/', 3 }; // Act double actual = division_exp.EvaluateExpression(test_expression, last_values); double expected = 5; // Assert Assert.AreEqual(expected, actual); }
public void EvaluateProveICanMultiplyTerms() { // Arrange Evaluate multiplication_exp = new Evaluate(); EvalStack last_values = new EvalStack(); object[] test_expression = { 5, '*', 8 }; // Act double actual = multiplication_exp.EvaluateExpression(test_expression, last_values); double expected = 40; // Assert Assert.AreEqual(expected, actual); }
public void EvaluateProveICanSubtractTerms() { // Arrange Evaluate subtraction_exp = new Evaluate(); EvalStack last_values = new EvalStack(); object[] test_expression = { 9, '-', 3 }; // Act double actual = subtraction_exp.EvaluateExpression(test_expression, last_values); double expected = 6; // Assert Assert.AreEqual(expected, actual); }
public void EvaluateProveICanAddTerms() { // Arrange Evaluate addition_exp = new Evaluate(); EvalStack last_values = new EvalStack(); object[] test_expression = { 5, '+', 6 }; // Act double actual = addition_exp.EvaluateExpression(test_expression, last_values); double expected = 11; // Assert Assert.AreEqual(expected, actual); }
public void EvaluateProveICanGetModulo() { // Arrange Evaluate modulus_exp = new Evaluate(); EvalStack last_values = new EvalStack(); object[] test_expression = { 10, '%', 3 }; // Act double actual = modulus_exp.EvaluateExpression(test_expression, last_values); double expected = 1; // Assert Assert.AreEqual(expected, actual); }
public void EvalStackProveICanGetAndSetConstant() { // Arrange EvalStack my_stack = new EvalStack(); object[] constant_exp = { 'A', '=', 2 }; char constantKey = 'A'; // Act my_stack.SetConstant(constant_exp); int actual = my_stack.GetConstant(constantKey); int expected = 2; // Assert Assert.AreEqual(actual, expected); }
public void ExpressionProveICanDoMathWithConst() { // Arrange Expression my_expression = new Expression(); EvalStack eval_stack = new EvalStack(); Evaluate evaluate = new Evaluate(); eval_stack.SetConstant(new object[] { 'M', '=', 8 }); string test_expression = "M * 4"; object[] test_object = my_expression.Parse(test_expression, eval_stack); //Act double actual = evaluate.EvaluateExpression(test_object, eval_stack); double expected = 32; // Assert Assert.AreEqual(actual, expected); }
private static bool TryCall(ExecutionContext ctx, int offset, ref DyObject?arg1, ref DyObject?arg2, ref DyNativeFunction function, ref DyObject[] locals, ref EvalStack evalStack) { if (ReferenceEquals(ctx.Error, DyVariant.Eta)) { ctx.CallStack.Push(new Caller(function, offset, evalStack, locals)); function = (DyNativeFunction)ctx.CallBackFunction !; ctx.CallBackFunction = null; ctx.Error = null; locals = function.CreateLocals(ctx); if (arg1 is not null) { locals[0] = arg1; } if (arg2 is not null) { locals[1] = arg2; } arg1 = null; arg2 = null; return(true); } return(false); }
private static bool FindCatch(ExecutionContext ctx, ref DyNativeFunction function, ref DyObject[] locals, ref EvalStack evalStack, out int offset) { CatchMark mark = default; Stack <CatchMark> cm; var idx = 1; offset = 0; while (ctx.CatchMarks.TryPeek(idx++, out cm)) { if (cm is not null && cm.Count > 0) { mark = cm.Peek(); break; } } if (mark.Offset == 0) { return(false); } Caller?cp = null; while (ctx.CallStack.Count > mark.StackOffset) { cp = ctx.CallStack.Pop(); //It means that this function was called from an external //context and we have to terminate our search if (ReferenceEquals(cp, Caller.External)) { return(false); } } cm.Pop(); if (cp is not null) { function = cp.Function; locals = cp.Locals; evalStack = cp.EvalStack; } offset = mark.Offset; return(true); }
private static int ThrowIf(ExecutionContext ctx, int offset, ref DyNativeFunction function, ref DyObject[] locals, ref EvalStack evalStack) { var err = ctx.Error !; if (FindCatch(ctx, ref function, ref locals, ref evalStack, out var address)) { ctx.ErrorDump = Dump(ctx, offset, function); return(address); } else { var dump = Dump(ctx, offset, function); var cs = ctx.Trace ?? new DyDebugger(ctx.RuntimeContext.Composition).BuildCallStack(dump); ctx.Error = null; ctx.ErrorDump = null; ctx.Trace = null; throw new DyCodeException(err, cs, null); } }
/// <summary> /// Performs analysis of given call instruction /// </summary> /// <param name="currentBlock"></param> /// <param name="instr"></param> /// <param name="stack"></param> /// <param name="context"></param> private static void AnalyzeCallInstruction(Node currentBlock, Instruction instr, EvalStack stack, Context context) { var method = instr.Method; if (method == null) { throw new ILTranslatorException(string.Format("Instruction {0} has no method", instr.Code)); } int n = method.Parameters.Count; var type = method.DeclaringType; bool isGetTypeFromHandle = method.IsGetTypeFromHandle(); var last = instr; for (int i = n - 1; i >= 0; --i) { var p = method.Parameters[i]; var arg = stack.Pop(); last = arg.Last; SetParam(currentBlock, arg.Instruction, p, method, context); p.Instruction = arg.Instruction; //p.Instruction = last; if (isGetTypeFromHandle && !arg.IsTypeToken) { isGetTypeFromHandle = false; } } if (instr.HasReceiver()) { var r = stack.Pop(); last = r.Last; r.Instruction.ReceiverFor = instr; r.Instruction.BoxingType = ResolveBoxingType(instr, type, context); } last = FixReceiverInsertPoint(currentBlock, last, method); if (!(method.IsInitializeArray() || isGetTypeFromHandle)) { Instruction dup; if (IsDup(stack, out dup)) { var call = new CallInstructionInfo(method, instr) { SwapAfter = true }; dup.EndStack.Push(call); } else { last.BeginStack.Push(new CallInstructionInfo(method, instr)); } } if (instr.HasReturnValue()) { stack.Push(new EvalItem(instr, last)); } }
public void Dup(bool copy) { Push(EvalStack.Peek(), copy); }
/// <summary> /// Clears eval stack. /// </summary> public void Clear() { EvalStack.Clear(); }
public void Push(object value, bool copy) { value = copy ? value.Copy() : value; EvalStack.Push(value); }
public object Pop(bool copy) { var value = EvalStack.Pop(); return(copy ? value.Copy() : value); }