コード例 #1
0
        public void CallFunction2()
        {
            var          subject = new Compiler();
            const string sane    = @"
            module A
                x = (a) -> a * 2
                y = (a b c) -> x { a { b { c } } }
                z = y {
                        (a) -> a + 1 
                        (a) -> a + 2 
                        3
                      }                             
            end";

            const string js = @"A = {};
A.x = function (a) {
return (a * 2);
};
A.y = function (a, b, c) {return A.x(a(b(c)));};
A.z = A.y(function (a) {return (a + 1);}, function (a) {return (a + 2);}, 3);
";

            output.WriteLine("CallFunction");
            var result = subject.Translate(sane);

            ScriptAssert.Equal(js, "", result);
        }
コード例 #2
0
        public void VisitExtension()
        {
            var subject = new JsOutputVisitor(config: new JsOutputVisitor.InstanceConfig {
                EmptyModuleWarning = false
            });
            var module = new ModuleNode
            {
                Id   = "A",
                Lets = new List <LetNode>
                {
                    new LetNode
                    {
                        Id   = "a",
                        Expr = new ExternalNode
                        {
                            Body = "function (value) {\n console.log(value); /*comment*/\n}"
                        }
                    }
                }
            };

            const string js     = @"
A = {};
A.a = function (value) { console.log(value); /*comment*/};
";
            var          result = subject.Visit(module);

            ScriptAssert.Equal("", subject.ErrorsSink.GetErrorsString());
            ScriptAssert.Equal(js, result);
        }
コード例 #3
0
        public void ErrorWhenBindingToTheSameName()
        {
            var subject = new JsOutputVisitor(config: new JsOutputVisitor.InstanceConfig {
                EmptyModuleWarning = false
            });
            var module = new ModuleNode
            {
                Id   = "A",
                Lets = new List <LetNode>
                {
                    new LetNode
                    {
                        Id   = "a",
                        Expr = new StringNode
                        {
                            Value = "dupa"
                        }
                    },
                    new LetNode
                    {
                        Id   = "a",
                        Expr = new NumericNode
                        {
                            Value = 1
                        }
                    }
                }
            };
            var ex = Record.Exception(() => subject.Visit(module)) as CompilationException;

            ScriptAssert.Equal("Error:N/A: Variable `a` already exists.", ex.Errors.GetErrorsString());
        }
コード例 #4
0
        public void VisitModule()
        {
            var subject = new JsOutputVisitor();
            var module  = new ModuleNode
            {
                Id = "A"
            };
            var          result = subject.Visit(module);
            const string js     = @"
A = {};
";

            ScriptAssert.Equal("Warning:N/A: Module `A` is empty.", subject.ErrorsSink.GetErrorsString());
            ScriptAssert.Equal(js, result);
        }
コード例 #5
0
        public void DeclareConst()
        {
            var          subject = new Compiler();
            const string sane    = @"
            module A
                x = 1
                y = ""dupa """"dupa"""" ""
            end";

            const string js = @"A = {};
A.x = 1;
A.y = ""dupa \""dupa\"" "";
";

            ScriptAssert.Equal(js, "", subject.Translate(sane));
        }
コード例 #6
0
        public void DeclareFunctions()
        {
            var          subject = new Compiler();
            const string sane    = @"
            module A
                x = (a) -> a * 2
            end";

            const string js = @"A = {};
A.x = function (a) {
return (a * 2);
};
";

            ScriptAssert.Equal(js, "", subject.Translate(sane));
        }
コード例 #7
0
        public void DeclareOperations()
        {
            var          subject = new Compiler();
            const string sane    = @"
            module A
                let x = 1
                let y = x + 1 * 2 - 3 / 4    
                let z = (x + 1) * (2 - 3) / 4    
            end";

            const string js = @"A = {};
A.x = 1;
A.y = ((A.x + (1 * 2)) - (3 / 4));
A.z = (((A.x + 1) * (2 - 3)) / 4);
";

            ScriptAssert.Equal(js, "", subject.Translate(sane));
        }
コード例 #8
0
        public void Array()
        {
            var          subject = new Compiler();
            const string sane    = @"
            module A
                x = [1 2 3 4]
                y = [(x) -> x + 1 (y) -> y * 2 + 1]                             
            end";

            const string js = @"A = {};
A.x = [1, 2, 3, 4];
A.y = [function (x) {return (x + 1);}, function (y) {return ((y * 2) + 1);}];
";

            output.WriteLine("CallFunction");
            var result = subject.Translate(sane);

            ScriptAssert.Equal(js, "", result);
        }
コード例 #9
0
        public void CheckType()
        {
            var          subject = new Compiler();
            const string sane    = @"
            module A
                a : int
                a = ""dwa""
            end
";
            const string js      = @"A = {};
A.log = function (value) { 
                        console.log(value); /*comment*/ 
                    };
A.a = 2;
A.b = `value: ${A.a}`;";
            var          result  = subject.Translate(sane);

            ScriptAssert.Equal(js, "", result);
        }
コード例 #10
0
        public void CallFunction()
        {
            var          subject = new Compiler();
            const string sane    = @"
            module A
                x = (a) -> a * 2
                y = (b) -> x { b + 3 }                     
            end";

            const string js = @"A = {};
A.x = function (a) {
return (a * 2);
};
A.y = function (b) {
return A.x((b + 3));
};
";

            output.WriteLine("CallFunction");
            var result = subject.Translate(sane);

            ScriptAssert.Equal(js, "", result);
        }
コード例 #11
0
        public void VisitArray()
        {
            var subject = new JsOutputVisitor();
            var module  = new ModuleNode
            {
                Id   = "A",
                Lets = new List <LetNode>
                {
                    new LetNode
                    {
                        Id   = "a",
                        Expr = new ArrayNode
                        {
                            Exprs = new List <ExprNode>
                            {
                                new NumericNode
                                {
                                    Value = 0
                                },
                                new NumericNode
                                {
                                    Value = 1
                                }
                            }
                        }
                    }
                }
            };
            var          result = subject.Visit(module);
            const string js     = @"
A = {};
A.a = [0, 1];
";

            ScriptAssert.Equal("", subject.ErrorsSink.GetErrorsString());
            ScriptAssert.Equal(js, result);
        }
コード例 #12
0
        public void VisitLet()
        {
            var subject = new JsOutputVisitor();
            var module  = new ModuleNode
            {
                Id   = "A",
                Lets = new List <LetNode>
                {
                    new LetNode
                    {
                        Id   = "a",
                        Expr = new StringNode
                        {
                            Value = "dupa"
                        }
                    },
                    new LetNode
                    {
                        Id   = "b",
                        Expr = new NumericNode
                        {
                            Value = 1
                        }
                    },
                }
            };
            var          result = subject.Visit(module);
            const string js     = @"
A = {};
A.a = ""dupa"";
A.b = 1;
";

            ScriptAssert.Equal("", subject.ErrorsSink.GetErrorsString());
            ScriptAssert.Equal(js, result);
        }
コード例 #13
0
        public void ExtensionValue()
        {
            var          subject = new Compiler();
            const string sane    = @"
            module A
                log = ```
                    function (value) { 
                        console.log(value); /*comment*/ 
                    }
                      ```
                a = 2
                b = ```\`value: ${A.a}\````
            end
";
            const string js      = @"A = {};
A.log = function (value) { 
                        console.log(value); /*comment*/ 
                    };
A.a = 2;
A.b = `value: ${A.a}`;";
            var          result  = subject.Translate(sane);

            ScriptAssert.Equal(js, "", result);
        }
コード例 #14
0
        public void DeclareLetIn()
        {
            var          subject = new Compiler();
            const string sane    = @"
            module A
                x = 
                    let
                        a = 1
                        b = 2
                    in
                        a + b
                    end
            end";

            const string js = @"A = {};
A.x = function () {
var a = 1;
var b = 2;
return (a + b);
}();
";

            ScriptAssert.Equal(js, "", subject.Translate(sane));
        }
コード例 #15
0
        public void VisitCall()
        {
            var subject = new JsOutputVisitor(config: new JsOutputVisitor.InstanceConfig {
                EmptyModuleWarning = false
            });
            var module = new ModuleNode
            {
                Id   = "A",
                Lets = new List <LetNode>
                {
                    new LetNode
                    {
                        Id   = "f",
                        Expr = new FuncNode
                        {
                            Parameters = new List <ParamNode>
                            {
                                new ParamNode
                                {
                                    Id = "arg0"
                                },
                                new ParamNode
                                {
                                    Id = "arg1"
                                }
                            },
                            Body = new BinaryExprNode
                            {
                                Id   = "+",
                                Left = new ReferenceNode
                                {
                                    Id = "arg0"
                                },
                                Right = new ReferenceNode
                                {
                                    Id = "arg1"
                                }
                            }
                        }
                    },
                    new LetNode
                    {
                        Id   = "main",
                        Expr = new FuncNode
                        {
                            Parameters = new List <ParamNode>(),
                            Body       = new LetInNode
                            {
                                Lets = new List <LetNode>
                                {
                                    new LetNode
                                    {
                                        Id   = "x",
                                        Expr = new NumericNode
                                        {
                                            Value = 1
                                        }
                                    }
                                },
                                Body = new CallNode
                                {
                                    Expr = new ReferenceNode
                                    {
                                        Id = "f"
                                    },
                                    Parameters = new List <ExprNode>
                                    {
                                        new ReferenceNode
                                        {
                                            Id = "x"
                                        },
                                        new NumericNode
                                        {
                                            Value = 2
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            };

            const string js     = @"
A = {};
A.f = function (arg0, arg1) {
return (arg0 + arg1);
};
A.main = function () {
return function () {
var x = 1;
return A.f(x, 2);
}();
};
";
            var          result = subject.Visit(module);

            ScriptAssert.Equal("", subject.ErrorsSink.GetErrorsString());
            ScriptAssert.Equal(js, result);
        }
コード例 #16
0
        public void VisitLetInNode()
        {
            var subject = new JsOutputVisitor(config: new JsOutputVisitor.InstanceConfig {
                EmptyModuleWarning = false
            });
            var module = new ModuleNode
            {
                Id   = "A",
                Lets = new List <LetNode>
                {
                    new LetNode
                    {
                        Id   = "evalNow",
                        Expr = new LetInNode
                        {
                            Lets = new List <LetNode>
                            {
                                new LetNode
                                {
                                    Id   = "a",
                                    Expr = new NumericNode
                                    {
                                        Value = 1
                                    }
                                },
                                new LetNode
                                {
                                    Id   = "b",
                                    Expr = new NumericNode
                                    {
                                        Value = 2
                                    }
                                }
                            },
                            Body = new BinaryExprNode
                            {
                                Id   = "+",
                                Left = new ReferenceNode
                                {
                                    Id = "a"
                                },
                                Right = new ReferenceNode
                                {
                                    Id = "b"
                                }
                            }
                        }
                    }
                }
            };

            const string js     = @"
A = {};
A.evalNow = function () {
var a = 1;
var b = 2;
return (a + b);
}();
";
            var          result = subject.Visit(module);

            ScriptAssert.Equal("", subject.ErrorsSink.GetErrorsString());
            ScriptAssert.Equal(js, result);
        }
コード例 #17
0
        public void VisitFunc()
        {
            var subject = new JsOutputVisitor(config: new JsOutputVisitor.InstanceConfig {
                EmptyModuleWarning = false
            });
            var module = new ModuleNode
            {
                Id   = "A",
                Lets = new List <LetNode>
                {
                    new LetNode
                    {
                        Id   = "out",
                        Expr = new NumericNode
                        {
                            Value = 10
                        }
                    },
                    new LetNode
                    {
                        Id   = "f",
                        Expr = new FuncNode
                        {
                            Parameters = new List <ParamNode>
                            {
                                new ParamNode
                                {
                                    Id = "arg0"
                                },
                                new ParamNode
                                {
                                    Id = "arg1"
                                }
                            },
                            Body = new BinaryExprNode
                            {
                                Id   = "+",
                                Left = new StringNode
                                {
                                    Value = "dupa"
                                },
                                Right = new BinaryExprNode
                                {
                                    Id   = "+",
                                    Left = new ReferenceNode
                                    {
                                        Id = "arg0"
                                    },
                                    Right = new ReferenceNode
                                    {
                                        Id = "out"
                                    }
                                }
                            }
                        }
                    }
                }
            };

            const string js     = @"
A = {};
A.out = 10;
A.f = function (arg0, arg1) {
return (""dupa"" + (arg0 + A.out));
};
";
            var          result = subject.Visit(module);

            ScriptAssert.Equal("", subject.ErrorsSink.GetErrorsString());
            ScriptAssert.Equal(js, result);
        }