コード例 #1
0
        void Function_throws_when_any_param_type_not_int_or_double()
        {
            var name = new IdentifierToken("SendRequest", default);

            var scope = new SymbolScope();

            var param1 = new FunctionParamAst(null, new IdentifierToken("length", default),
                                              new OperatorToken(Operator.Colon, default), new IdentifierToken("int", default));
            var param2 = new FunctionParamAst(null, new IdentifierToken("ip", default),
                                              new OperatorToken(Operator.Colon, default), new IdentifierToken("string", default));
            var paramList = new FunctionParamListAst(new FunctionParamAst[] { param1, param2 });

            var ast = new FunctionAst(
                new KeywordToken(Keyword.Fn, default), name, new OperatorToken(Operator.LeftParen, default),
                paramList, new OperatorToken(Operator.RightParen, default),
                new OperatorToken(Operator.Arrow, default), returnType: new IdentifierToken("int", default),
                new Mock <BlockStatementAst>().Object
                );

            var generator = ConfigureGenerator(scope, mock =>
            {
            });

            Assert.Throws <SemanticException>(() => generator.ProcessFunction(ast));
        }
コード例 #2
0
        void Function_throws_when_duplicated_param_name()
        {
            var name = new IdentifierToken("SendRequest", default);

            var scope = new SymbolScope();

            var param1 = new FunctionParamAst(null, new IdentifierToken("length1", default),
                                              new OperatorToken(Operator.Colon, default), new IdentifierToken("int", default));
            var param2 = new FunctionParamAst(null, new IdentifierToken("length1", default),
                                              new OperatorToken(Operator.Colon, default), new IdentifierToken("int", default));
            var paramList = new FunctionParamListAst(new FunctionParamAst[] { param1, param2 });

            var functionBlock = new Mock <BlockStatementAst>().Object;
            var ast           = new FunctionAst(
                new KeywordToken(Keyword.Fn, default), name, new OperatorToken(Operator.LeftParen, default),
                paramList, new OperatorToken(Operator.RightParen, default),
                new OperatorToken(Operator.Arrow, default), returnType: new IdentifierToken("int", default),
                functionBlock
                );

            var generator = ConfigureGenerator(scope, mock =>
            {
                mock.Setup(p => p.ProcessBlockStatement(functionBlock, It.IsAny <bool>()));
            });

            Assert.Throws <SemanticException>(() => generator.ProcessFunction(ast));
        }
コード例 #3
0
        void Function_success_when_some_param_name_exist_in_shallow()
        {
            var name = new IdentifierToken("SendRequest", default);

            var scope = new SymbolScope();

            scope.AddSymbol(new VariableSymbol("length1", false, false, DataType.Double));

            var param1 = new FunctionParamAst(null, new IdentifierToken("length1", default),
                                              new OperatorToken(Operator.Colon, default), new IdentifierToken("int", default));
            var param2 = new FunctionParamAst(null, new IdentifierToken("length2", default),
                                              new OperatorToken(Operator.Colon, default), new IdentifierToken("int", default));
            var paramList = new FunctionParamListAst(new FunctionParamAst[] { param1, param2 });

            var bodyBlock = new Mock <BlockStatementAst>().Object;

            var ast = new FunctionAst(
                new KeywordToken(Keyword.Fn, default), name, new OperatorToken(Operator.LeftParen, default),
                paramList, new OperatorToken(Operator.RightParen, default),
                new OperatorToken(Operator.Arrow, default), returnType: new IdentifierToken("int", default),
                bodyBlock
                );

            var generator = ConfigureGenerator(scope, mock =>
            {
                mock.Setup(p => p.ProcessBlockStatement(bodyBlock, It.IsAny <bool>())).Returns(true);
            });

            generator.ProcessFunction(ast);
        }
コード例 #4
0
        void Function_success_and_write_self_and_params_to_symbol_table()
        {
            var name = new IdentifierToken("SendRequest", default);

            var scope = new SymbolScope();

            scope.AddSymbol(new VariableSymbol("lll3", false, false, DataType.Long));

            var param1 = new FunctionParamAst(null, new IdentifierToken("length1", default),
                                              new OperatorToken(Operator.Colon, default), new IdentifierToken("int", default));
            var paramList = new FunctionParamListAst(new FunctionParamAst[] { param1 });

            var functionBlock = new Mock <BlockStatementAst>().Object;

            var ast = new FunctionAst(
                new KeywordToken(Keyword.Fn, default), name, new OperatorToken(Operator.LeftParen, default),
                paramList, new OperatorToken(Operator.RightParen, default),
                new OperatorToken(Operator.Arrow, default), returnType: new IdentifierToken("int", default),
                functionBlock
                );

            var generator = ConfigureGenerator(scope, mock =>
            {
                mock.Setup(p => p.ProcessBlockStatement(functionBlock, It.IsAny <bool>())).Returns(false);
            });

            generator.ProcessFunction(ast);

            // Assert


            // self
            Assert.True(scope.FindSymbolShallow(ast.Name.Value, out Symbol symx));
            var funcSym = Assert.IsType <FunctionSymbol>(symx);

            Assert.Equal(DataType.Long, funcSym.ReturnType);
            Assert.Equal(DataType.Long, funcSym.ParamTypes.Single());

            // params
            Assert.True(scope.FindSymbolShallow(name.Value, out Symbol s0));
            scope = (s0 as FunctionSymbol).BodyBlockScope;

            Assert.True(scope.FindSymbolShallow(param1.Name.Value, out Symbol sym1));
            var var1 = Assert.IsType <VariableSymbol>(sym1);

            Assert.Equal(param1.IsConstant, var1.IsConstant);
            Assert.False(var1.IsGlobal);
            Assert.Equal(DataType.Long, var1.Type);
            Assert.Equal(param1.Name.Value, var1.Name);
        }