コード例 #1
0
            public void DigitalWrite(CInterpreter state)
            {
                var pin   = state.ReadArg(0).Int16Value;
                var value = state.ReadArg(1).Int16Value;

                Pins[pin].DigitalValue = value;
            }
コード例 #2
0
        public void TestReadInput_TryReadEmptyInputArray_ReturnsEmptyArray()
        {
            IInterpreter interpreter = new CInterpreter();

            int[] testValue = new int[] { 5 };

            /*program looks like
             * t <- [int]
             */
            var program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("t", new CReadInputASTNode(new CValueASTNode(new int[] { 0 })))
            });

            int[][] inputData = new int[1][];

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() =>
            {
                var result = interpreter.Eval(program, inputStream);

                Assert.AreEqual(result, CIntrinsicsUtils.mNullArray);
            });
        }
コード例 #3
0
ファイル: TestMachineInfo.cs プロジェクト: wowngasb/CLanguage
        static void AssertU16AreEqual(CInterpreter state)
        {
            var expected = state.ReadArg(0);
            var actual   = state.ReadArg(1);

            Assert.AreEqual((ushort)expected, (ushort)actual);
        }
コード例 #4
0
ファイル: TestMachineInfo.cs プロジェクト: wowngasb/CLanguage
        static void AssertBoolsAreEqual(CInterpreter state)
        {
            var expected = state.ReadArg(0);
            var actual   = state.ReadArg(1);

            Assert.AreEqual((int)expected, (int)actual);
        }
コード例 #5
0
        public void TestEval_TestReadArbitraryParameter_ReturnsReadParameter(int index)
        {
            IInterpreter interpreter = new CInterpreter();

            var program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("x", new CReadInputASTNode(new CValueASTNode(new int[] { index })))
            });

            int[][] inputData = new int[][]
            {
                new int[] { 4 }, //x
            };

            int[] expectedResult = new int[] { 2, 2, 2, 2 };

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() =>
            {
                var result = interpreter.Eval(program, inputStream);

                if (index < inputData.Length)
                {
                    Assert.AreEqual(result.Length, 1);
                    Assert.AreEqual(inputData[index][0], result[0]);
                }
                else
                {
                    Assert.AreSame(CIntrinsicsUtils.mNullArray, result);
                }
            });
        }
コード例 #6
0
ファイル: TestMachineInfo.cs プロジェクト: wowngasb/CLanguage
        static void AssertFloatsAreEqual(CInterpreter state)
        {
            var expected = state.ReadArg(0);
            var actual   = state.ReadArg(1);

            Assert.AreEqual((float)expected, (float)actual, 1.0e-6);
        }
コード例 #7
0
        public void TestEval_TestPassArrayAsIfVar_ShouldProcessCorrectlyWithoutExceptions()
        {
            IInterpreter interpreter = new CInterpreter(E_INTERPRETER_ATTRIBUTES.IA_IS_SAFE_DIVISION_ENABLED);

            var program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("x", new CIfThenElseASTNode(new CReadInputASTNode(new CValueASTNode(new int[] { 0 })),
                                                                   new CLambdaPredicateASTNode(E_LOGIC_OP_TYPE.LOT_NEQ, new CValueASTNode(new int[] { -8 }), null),
                                                                   new CValueASTNode(new int[] { 0 }),
                                                                   new CValueASTNode(new int[] { -1 })))
            });

            int[][] inputData = new int[][]
            {
                new int[] { 4 }, //x
            };

            int[] expectedResult = new int[] { 2, 2, 2, 2 };

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() =>
            {
                var result = interpreter.Eval(program, inputStream);
            });
        }
コード例 #8
0
            public void PinMode(CInterpreter state)
            {
                var pin  = state.ReadArg(0).Int16Value;
                var mode = state.ReadArg(1).Int16Value;

                Pins[pin].Mode = mode;
            }
コード例 #9
0
        public void TestIfStatement_FalseCondition_ReturnsEvaluatedExpressionOfChosenBranch()
        {
            IInterpreter interpreter = new CInterpreter();

            int[] testValue = new int[] { 5 };

            /*program looks like
             * t <- 5
             * r <- if t (!= 5) then 5 else -1
             */
            var program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("t", new CValueASTNode(testValue)),
                new CAssignmentASTNode("r", new CIfThenElseASTNode(new CIdentifierASTNode("t"),
                                                                   new CLambdaPredicateASTNode(E_LOGIC_OP_TYPE.LOT_NEQ, new CValueASTNode(testValue), null),
                                                                   new CValueASTNode(testValue),
                                                                   new CValueASTNode(new int[] { -1 })))
            });

            int[][] inputData = new int[3][];

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() =>
            {
                var result = interpreter.Eval(program, inputStream);

                Assert.AreEqual(result.Length, 1);
                Assert.AreEqual(result[0], -1);
            });
        }
コード例 #10
0
            public void AnalogRead(CInterpreter state)
            {
                var pin   = state.ReadArg(0).Int16Value;
                var value = Pins[pin].AnalogValue;

                state.Push(value);
            }
コード例 #11
0
ファイル: TestMachineInfo.cs プロジェクト: wowngasb/CLanguage
        static void AssertDoublesAreEqual(CInterpreter state)
        {
            var expected = state.ReadArg(0);
            var actual   = state.ReadArg(1);

            Assert.AreEqual((double)expected, (double)actual, 1.0e-12);
        }
コード例 #12
0
            public void DigitalRead(CInterpreter state)
            {
                var pin   = state.ReadArg(0).Int16Value;
                var value = Pins[pin].DigitalValue;

                state.Push(value);
            }
コード例 #13
0
        public void TestEval_TestSafeDivisionOption_ReturnsCorrectDivisionResult()
        {
            IInterpreter interpreter = new CInterpreter(E_INTERPRETER_ATTRIBUTES.IA_IS_SAFE_DIVISION_ENABLED);

            var program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("x", new CCallASTNode(E_INTRINSIC_FUNC_TYPE.IFT_MAP, new List <IASTNode>()
                {
                    new CUnaryLambdaFuncASTNode(E_OPERATION_TYPE.OT_DIV, new CValueASTNode(new int[] { 0 })),
                    new CValueASTNode(new int[] { 0, 1, 2, 3, 4 })
                }))
            });

            int[][] inputData = new int[][]
            {
                new int[] { 4 }, //x
            };

            int[] expectedResult = new int[] { 2, 2, 2, 2 };

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() =>
            {
                var result = interpreter.Eval(program, inputStream);
            });
        }
コード例 #14
0
        public void TestEval_TestReferencingByIdentifierInExpr_ReturnsValueStoredWithinVar()
        {
            IInterpreter interpreter = new CInterpreter();

            /*program looks like
             * x <- int
             * y <- IF x (> 0) THEN x ELSE NULL
             */
            var program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("x", new CReadInputASTNode(new CValueASTNode(new int[] { 0 }))),
                new CAssignmentASTNode("z", new CIfThenElseASTNode(new CIdentifierASTNode("x", E_NODE_ATTRIBUTES.NA_ID_SHOULD_EXIST),
                                                                   new CLambdaPredicateASTNode(E_LOGIC_OP_TYPE.LOT_GT, new CValueASTNode(new int[] { 0 }), null),
                                                                   new CIdentifierASTNode("x", E_NODE_ATTRIBUTES.NA_ID_SHOULD_EXIST),
                                                                   new CValueASTNode(CIntrinsicsUtils.mNullArray)))
            });

            int[][] inputData = new int[][]
            {
                new int[] { 4 }, //x
            };

            int[] expectedResult = new int[] { 2, 2, 2, 2 };

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() =>
            {
                var result = interpreter.Eval(program, inputStream);

                Assert.AreEqual(result.Length, 1);
                Assert.AreEqual(inputData[0][0], result[0]);
            });
        }
コード例 #15
0
        public void TestVisitLValueIdentifierNode_CorrectArgument_ReturnsName()
        {
            IVisitor <Object> interpreter = new CInterpreter();

            IIdentifierASTNode identifierNode = new CIdentifierASTNode("TestId", E_NODE_ATTRIBUTES.NA_LVALUE);

            var result = interpreter.VisitIdentifierNode(identifierNode);

            Assert.AreEqual(result, identifierNode.Name);
        }
コード例 #16
0
        public void TestVisitValueNode_CorrectArgument_ReturnsName()
        {
            IVisitor <Object> interpreter = new CInterpreter();

            IValueASTNode valueNode = new CValueASTNode(new int[] { 1, 2 });

            var result = interpreter.VisitValueNode(valueNode);

            Assert.AreEqual(result, valueNode.Value);
        }
コード例 #17
0
        static void AssertAreEqual(CInterpreter state)
        {
            var expected = state.ReadArg(0).Int16Value;
            var actual   = state.ReadArg(1).Int16Value;

            if (expected != actual)
            {
                Console.WriteLine($"{expected} != {actual}");
            }
            //Assert.AreEqual ((short)expected, (short)actual);
        }
コード例 #18
0
        //[TestCase(E_OPERATION_TYPE.OT_POW, 3, 2, 9)] works only for double, need to find some trick to implement it
        public void TestVisitBinaryLambdaFuncNode_CheckLambdaFunctorGeneration_ReturnsLambdaFunctor(E_OPERATION_TYPE type, int x, int y, int res)
        {
            IVisitor <Object> interpreter = new CInterpreter();

            Func <int, int, int> lambdaFunctor = null;

            Assert.DoesNotThrow(() => {
                lambdaFunctor = (Func <int, int, int>)interpreter.VisitBinaryLambdaFuncNode(new CBinaryLambdaFuncASTNode(type));
            });

            Assert.AreEqual(res, lambdaFunctor(x, y));
        }
コード例 #19
0
            string ReadString(int pi, CInterpreter state)
            {
                var b     = (byte)state.ReadMemory(pi);
                var bytes = new List <byte> ();

                while (b != 0)
                {
                    bytes.Add(b);
                    pi++;
                    b = (byte)state.ReadMemory(pi);
                }
                return(Encoding.UTF8.GetString(bytes.ToArray()));
            }
コード例 #20
0
        public void TestVisitUnaryLambdaFuncNode_CheckSimpleLambdaPredicateGeneration_ReturnsLambdaPredicate(E_OPERATION_TYPE type, int x, int value, int res)
        {
            IVisitor <Object> interpreter = new CInterpreter();

            Func <int, int> lambdaFunctor = null;

            Assert.DoesNotThrow(() => {
                lambdaFunctor = (Func <int, int>)interpreter.VisitUnaryLambdaFuncNode(
                    new CUnaryLambdaFuncASTNode(type, new CValueASTNode(new int[] { value })));
            });

            Assert.AreEqual(res, lambdaFunctor(x));
        }
コード例 #21
0
        public void TestVisitLambdaPredicateNode_CheckLambdaPredicateGeneration_ReturnsLambdaPredicate(E_LOGIC_OP_TYPE type, int x, int value, bool res)
        {
            IVisitor <Object> interpreter = new CInterpreter();

            Func <int, bool> lambdaPredicate = null;

            Assert.DoesNotThrow(() => {
                lambdaPredicate = (Func <int, bool>)interpreter.VisitLambdaPredicateNode(
                    new CLambdaPredicateASTNode(type, new CValueASTNode(new int[] { value }), null));
            });

            Assert.AreEqual(res, lambdaPredicate(x));
        }
コード例 #22
0
        public void TestEval_NullArguments_ThrowsNullArgumentException()
        {
            IInterpreter interpreter = new CInterpreter();

            IASTNode program = new CProgramASTNode();

            int[][] input = new int[1][];

            IInputStream inputStream = new CInputStream(input);

            Assert.Throws <ArgumentNullException>(() => { interpreter.Eval(program, null); });
            Assert.Throws <ArgumentNullException>(() => { interpreter.Eval(null, inputStream); });
        }
コード例 #23
0
        public void TestVisitCallNode_SimpleProgram_ReturnsValue()
        {
            IInterpreter interpreter = new CInterpreter();

            int[][] inputData = new int[3][];

            int[] testArray = new int[] { 5, 4, -3, 1 };

            int sum = 0;

            foreach (int unit in testArray)
            {
                sum += unit;
            }

            int[] expectedValue = new int[] { testArray[0], sum };

            // simple program that returns a sum of length and a first element of an array
            // program's code looks like this
            // a <- [5, 4, -3, 1]
            // t <- head a
            // s <- sum a
            // r <- concat t s
            IASTNode program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("a", new CValueASTNode(testArray)),
                new CAssignmentASTNode("t", new CCallASTNode(E_INTRINSIC_FUNC_TYPE.IFT_HEAD, new List <IASTNode>()
                {
                    new CIdentifierASTNode("a")
                })),
                new CAssignmentASTNode("s", new CCallASTNode(E_INTRINSIC_FUNC_TYPE.IFT_SUM, new List <IASTNode>()
                {
                    new CIdentifierASTNode("a")
                })),
                new CAssignmentASTNode("r", new CCallASTNode(E_INTRINSIC_FUNC_TYPE.IFT_CONCAT, new List <IASTNode>()
                {
                    new CIdentifierASTNode("t"), new CIdentifierASTNode("s")
                }))
            });

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() => {
                var result = interpreter.Eval(program, inputStream);

                for (int i = 0; i < result.Length; ++i)
                {
                    Assert.AreEqual(expectedValue[i], result[i]);
                }
            });
        }
コード例 #24
0
        public void TestVisitLambdaPredicateNode_CheckModuloComparisonLambdaPredicate_ReturnsLambdaPredicate(int x, int value, int secondOp, bool res)
        {
            IVisitor <Object> interpreter = new CInterpreter();

            Func <int, bool> lambdaPredicate = null;

            Assert.DoesNotThrow(() => {
                lambdaPredicate = (Func <int, bool>)interpreter.VisitLambdaPredicateNode(
                    new CLambdaPredicateASTNode(E_LOGIC_OP_TYPE.LOT_MOD, new CValueASTNode(new int[] { value }),
                                                new CValueASTNode(new int[] { secondOp })));
            });

            Assert.AreEqual(res, lambdaPredicate(x));
        }
コード例 #25
0
        public void TestEval_EmptyProgram_ReturnsEmptyArray()
        {
            IInterpreter interpreter = new CInterpreter();

            IASTNode program = new CProgramASTNode();

            int[][] input = new int[1][];

            IInputStream inputStream = new CInputStream(input);

            var result = interpreter.Eval(program, inputStream);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf <int[]>(result);
            Assert.IsEmpty(result);
        }
コード例 #26
0
        public void TestEval_SimpleProgramWithInputAndIntrinsics_ReturnsResult()
        {
            IInterpreter interpreter = new CInterpreter();

            /*program looks like
             * x <- [int]
             * y <- [int]
             * z <- VecOp (-) x y
             */
            var program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("x", new CReadInputASTNode(new CValueASTNode(new int[] { 0 }))),
                new CAssignmentASTNode("y", new CReadInputASTNode(new CValueASTNode(new int[] { 1 }))),
                new CAssignmentASTNode("z", new CCallASTNode(E_INTRINSIC_FUNC_TYPE.IFT_VECOP,
                                                             new List <IASTNode>()
                {
                    new CBinaryLambdaFuncASTNode(E_OPERATION_TYPE.OT_SUB),
                    new CIdentifierASTNode("x"),
                    new CIdentifierASTNode("y")
                }))
            });

            int[][] inputData = new int[][]
            {
                new int[] { 4, 4, 4, 4 }, //x
                new int[] { 2, 2, 2, 2 }  //y
            };

            int[] expectedResult = new int[] { 2, 2, 2, 2 };

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() =>
            {
                var result = interpreter.Eval(program, inputStream);

                for (int i = 0; i < Math.Min(result.Length, expectedResult.Length); ++i)
                {
                    Assert.AreEqual(expectedResult[i], result[i]);
                }
            });
        }
コード例 #27
0
        public void TestVisitUnaryLambdaFuncNode_CheckNestedLambdaPredicateGeneration_ReturnsLambdaPredicate()
        {
            IVisitor <Object> interpreter = new CInterpreter();

            Func <int, int> lambdaFunctor = null;

            Func <int, int> expectedLambdaFunctor = (int x) => { return(x + x * (x - 2)); };

            int inputParameter = 5;

            Assert.DoesNotThrow(() => {
                // the interpreter should infer x + x * (x - 2) expression
                lambdaFunctor = (Func <int, int>)interpreter.VisitUnaryLambdaFuncNode(
                    new CUnaryLambdaFuncASTNode(E_OPERATION_TYPE.OT_ADD,
                                                new CUnaryLambdaFuncASTNode(E_OPERATION_TYPE.OT_MUL,
                                                                            new CUnaryLambdaFuncASTNode(E_OPERATION_TYPE.OT_SUB, new CValueASTNode(new int[] { 2 })))));
            });

            Assert.AreEqual(expectedLambdaFunctor(inputParameter), lambdaFunctor(inputParameter));
        }
コード例 #28
0
        public void TestVisitAssigmentNode_SingleAssigmentOfValue_ReturnsValue()
        {
            IInterpreter interpreter = new CInterpreter();

            int[][] inputData = new int[3][];

            int[] expectedValue = new int[] { 42 };

            // simple program that should assign 42 to a variable t
            IASTNode program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("t", new CValueASTNode(expectedValue))
            });

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() => {
                var result = interpreter.Eval(program, inputStream);

                Assert.AreEqual(result, expectedValue);
            });
        }
コード例 #29
0
        public void TestVisitAssigmentNode_SequentialAssigmentOfValueViaReference_ReturnsValue()
        {
            IInterpreter interpreter = new CInterpreter();

            int[][] inputData = new int[3][];

            int[] expectedValue = new int[] { 42 };

            // simple program that should assign 42 to a variable t
            // now program looks like t <- 42 \n v <- t
            IASTNode program = new CProgramASTNode(new List <IASTNode>()
            {
                new CAssignmentASTNode("t", new CValueASTNode(expectedValue)),
                new CAssignmentASTNode("v", new CIdentifierASTNode("t"))
            });

            IInputStream inputStream = new CInputStream(inputData);

            Assert.DoesNotThrow(() => {
                var result = interpreter.Eval(program, inputStream);

                Assert.AreEqual(result, expectedValue);
            });
        }
コード例 #30
0
            public void SerialPrintlnS(CInterpreter state)
            {
                var p = state.ReadArg(0).PointerValue;

                SerialOut.WriteLine(state.ReadString(p));
            }