protected internal override void CheckSemantics(AstHelper astHelper)
        {
            base.CheckSemantics(astHelper);

            FieldName.CheckSemantics(astHelper);
            Value.CheckSemantics(astHelper);
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            base.CheckSemantics(astHelper);

            Left.CheckSemantics(astHelper);
            Right.CheckSemantics(astHelper);
        }
 protected internal override void CheckSemantics(AstHelper astHelper)
 {
     base.CheckSemantics(astHelper);
     ExistingType.CheckSemantics(astHelper);
     if (ExistingType.HasType)
         astHelper.Types.Add(Name.Name, ExistingType.ReferencedType.MakeArrayType());
 }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            base.CheckSemantics(astHelper);
            ExistingType.CheckSemantics(astHelper);

            // if there's no error add the type
            if (ExistingType.HasType)
                astHelper.Types.Add(Name.Name, ExistingType.ReferencedType);
        }
        public void CanCompareQualifier(
            InterpolationQualifier?interpolation1,
            InterpolationQualifier?interpolation2,
            bool invariant1,
            bool invariant2,
            PrecisionQualifier?precision1,
            PrecisionQualifier?precision2,
            StorageQualifier?storage1,
            StorageQualifier?storage2,
            bool expectedResult)
        {
            var result = AstHelper.TypeSpecifiersAreEquivalent(
                new TypeNameSpecifier()
            {
                Identifier = new Identifier()
                {
                    Name = "int"
                },
                Qualifier = new TypeQualifier()
                {
                    Interpolation = interpolation1,
                    Invariant     = invariant1,
                    Precision     = precision1,
                    Storage       = storage1
                }
            },
                new TypeNameSpecifier()
            {
                Identifier = new Identifier()
                {
                    Name = "int"
                },
                Qualifier = new TypeQualifier()
                {
                    Interpolation = interpolation2,
                    Invariant     = invariant2,
                    Precision     = precision2,
                    Storage       = storage2
                }
            },
                true);

            result.Should().Be(expectedResult);
        }
Пример #6
0
        public void variable_initialization_with_undefined_function()
        {
            // let var x:=Foo() in x and
            var target = new LetInExpression
            {
                VariableDeclarations = new List <VariableDeclarationBase>
                {
                    new VariableDeclarationExpression
                    {
                        VariableName =
                            new MemberIdentifierNode {
                            Name = "x"
                        },
                        VariableTypeName = new TypeReferenceNode(),
                        Value            = new FunctionInvokeExpression
                        {
                            FunctionName =
                                new IdentifierNode
                            {
                                Name = "Foo"
                            }
                        }
                    }
                },
                Body = new ListSemiExpression
                {
                    new VariableAccessExpression {
                        VariableName = "x"
                    }
                }
            };
            AstHelper helper = Mother.CreateRuntime();

            try
            {
                target.Compile(helper);
            }
            catch (SemanticException e)
            {
                var error = e.Errors.First() as FunctionNotDefinedError;
                Assert.IsNotNull(error);
            }
        }
        public void CanCompareTypeNameSpecifiers(string name1, string name2, bool expectedResult)
        {
            var result = AstHelper.TypeSpecifiersAreEquivalent(
                new TypeNameSpecifier()
            {
                Identifier = new Identifier()
                {
                    Name = name1
                }
            },
                new TypeNameSpecifier()
            {
                Identifier = new Identifier()
                {
                    Name = name2
                }
            });

            result.Should().Be(expectedResult);
        }
Пример #8
0
        public void logical_binary_comparation_nil_with_string()
        {
            // nil=(nil="pepe")
            var target = new LogicalBinaryOperationExpression
            {
                Left  = new NullLiteralExpression(),
                Right = new LogicalBinaryOperationExpression
                {
                    Left     = new NullLiteralExpression(),
                    Right    = new StringLiteralExpression("pepe"),
                    Operator = TigerOperator.Equal
                },
                Operator = TigerOperator.Equal
            };
            AstHelper helper = Mother.CreateRuntime();

            var result = target.Compile(helper).Eval <bool>();

            Assert.IsFalse(result);
        }
Пример #9
0
        public void simple_function_call()
        {
            // Foo()
            var target = new FunctionInvokeExpression
            {
                FunctionName = new IdentifierNode {
                    Name = "Foo"
                }
            };
            AstHelper helper = Mother.CreateRuntime();

            try
            {
                target.Compile(helper);
            }
            catch (SemanticException e)
            {
                var error = e.Errors.First() as FunctionNotDefinedError;
                Assert.IsNotNull(error);
            }
        }
        public void FunctionInvoked_ComplexFunctions(string program, int result)
        {
            var scope = new AstHelper(null);
            Expression <Func <int, int, int> > sum = (a, b) => a + b;

            scope.Functions.Add("sum", new FunctionReference(sum, typeof(int), typeof(int), typeof(int)));
            Expression <Func <int, int, int> > resta = (a, b) => a - b;

            scope.Functions.Add("resta", new FunctionReference(resta, typeof(int), typeof(int), typeof(int)));
            Expression <Func <int, int, int> > prod = (a, b) => a * b;

            scope.Functions.Add("prod", new FunctionReference(prod, typeof(int), typeof(int), typeof(int)));
            Expression <Func <int, int, int> > div = (a, b) => a / b;

            scope.Functions.Add("div", new FunctionReference(div, typeof(int), typeof(int), typeof(int)));

            Expression expression = ProgramExpression.Compile(scope, program);

            int lamda = Expression.Lambda <Func <int> >(expression).Compile()();

            Assert.AreEqual(lamda, result);
        }
Пример #11
0
        public void variable_declaration_without_type_equals_null()
        {
            // let var x:=nil in x and
            var target = new LetInExpression
            {
                VariableDeclarations = new List <VariableDeclarationBase>
                {
                    new VariableDeclarationExpression
                    {
                        VariableName =
                            new MemberIdentifierNode {
                            Name = "x"
                        },
                        VariableTypeName = new TypeReferenceNode(),
                        Value            = new NullLiteralExpression()
                    }
                },
                Body = new ListSemiExpression
                {
                    new VariableAccessExpression {
                        VariableName = "x"
                    }
                }
            };
            AstHelper helper = Mother.CreateRuntime();

            try
            {
                target.Compile(helper);
            }
            catch (SemanticException e)
            {
                var error = e.Errors.First() as CanNotInferTypeError;
                Assert.IsNotNull(error);
                Assert.Pass();
            }
            Assert.Fail();
        }
        public void CanCompareStructTypeSpecifiers(string s1m1name, string s1m1type, string s2m1name, string s2m1type, bool expectedResult)
        {
            var specifier1 = new StructTypeSpecifier();
            var member1    = new StructMemberDeclaration();

            member1.Identifiers.Add(new Identifier()
            {
                Name = s1m1name
            });
            member1.Type = new TypeNameSpecifier()
            {
                Identifier = new Identifier()
                {
                    Name = s1m1type
                }
            };
            specifier1.Members.Add(member1);

            var specifier2 = new StructTypeSpecifier();
            var member2    = new StructMemberDeclaration();

            member2.Identifiers.Add(new Identifier()
            {
                Name = s2m1name
            });
            member2.Type = new TypeNameSpecifier()
            {
                Identifier = new Identifier()
                {
                    Name = s2m1type
                }
            };
            specifier2.Members.Add(member2);

            var result = AstHelper.TypeSpecifiersAreEquivalent(specifier1, specifier2);

            result.Should().Be(expectedResult);
        }
        public void field_not_defined()
        {
            var target = new FieldAccessExpression
            {
                FieldName = new IdentifierNode {
                    Name = "Foo"
                },
                Target = new StringLiteralExpression("abc")
            };
            AstHelper helper = Mother.CreateRuntime();

            try
            {
                target.Compile(helper);
            }
            catch (SemanticException e)
            {
                var error = e.Errors.First() as FieldNotFoundError;
                Assert.IsNotNull(error);
                Assert.Pass();
            }
            Assert.Fail();
        }
Пример #14
0
        void IAstVisitor <ResolveContext> .Visit(TextNode node, ResolveContext context)
        {
            var txt = node.Text.Text;

            if (Regex.IsMatch(txt, "^>>Po\\.\\d+$"))//^>>Po\.\d+$
            {
                string id = txt.Substring(txt.LastIndexOf('.') + 1);
                if (int.TryParse(id, out var _))
                {
                    try
                    {
                        node.HasReferer = true;
                        var reply = context.Controller.GetReplyAsync(id).ConfigureAwait(false)
                                    .GetAwaiter().GetResult();
                        node.Refer = AstHelper.LoadHtmlAsync(reply.Com).ConfigureAwait(false).GetAwaiter().GetResult();
                    }
                    catch
                    {
                    }
                }
            }
            return;
        }
Пример #15
0
        public void nil_equals_nil()
        {
            // nil = nil
            var target = new LogicalBinaryOperationExpression
            {
                Left     = new NullLiteralExpression(),
                Right    = new NullLiteralExpression(),
                Operator = TigerOperator.Equal
            };
            AstHelper helper = Mother.CreateRuntime();

            try
            {
                target.Compile(helper);
            }
            catch (SemanticException e)
            {
                var error = e.Errors.First() as CanNotInferTypeError;
                Assert.IsNotNull(error);
                Assert.Pass();
            }
            Assert.Fail();
        }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            base.CheckSemantics(astHelper);

            Target.CheckSemantics(astHelper);
        }
 public MemberWithSameNameAlreadyDefinedError(string memberName, AstHelper helper, SourceLocation location)
     : base(location)
 {
     MemberName = memberName;
     Helper     = helper;
 }
Пример #18
0
        private static void Main(string[] args)
        {
            Console.Title = "Tiger Compiler version 1.0";
            Console.WriteLine("Tiger Compiler version 1.0");
            Console.WriteLine("Copyright (C) 2011 - 2012 Ariel Rodriguez & Eric Hernandez & Dayan Gonzalez");

            FileInfo fi;

            if (args.Length == 0 || args[0] == "")
            {
                fi = new FileInfo(Environment.CurrentDirectory + "\\program.tig");
            }
            else
            {
                string path = args.Aggregate("", (current, s) => current + s);
                fi = new FileInfo(path);
            }

            if (!fi.Exists)
            {
                Console.WriteLine("(0, 0): File '{0}' cannot be found.", fi.FullName);
                Console.WriteLine();
                return;
            }
            string fileName = fi.Name.Substring(0, fi.Name.Length - 4);

            try
            {
                var assemblyName = new AssemblyName("Program");
                assemblyName.Version = new Version(1, 0);

                AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName,
                                                                                                AssemblyBuilderAccess.
                                                                                                Save);

                ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("Program", "Program.exe");

                AstHelper runtime = CreateRuntime(moduleBuilder);

                TypeBuilder   typeBuilder   = moduleBuilder.DefineType("Program");
                MethodBuilder methodBuilder = typeBuilder.DefineMethod("Main", MethodAttributes.Static);

                Expression <Func <string[], int> > lambda = WrapMain(ProgramExpression.CompileFile(runtime, fi.FullName));
                lambda.CompileToMethod(methodBuilder);

                typeBuilder.CreateType();

                moduleBuilder.CreateGlobalFunctions();
                assemblyBuilder.SetEntryPoint(methodBuilder);
                assemblyBuilder.Save(fileName + ".exe");
            }
            catch (SyntaxException e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
            catch (SemanticException e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
            //catch (Exception e)
            //{
            //    Console.WriteLine(e);
            //    Console.ReadLine();
            //}
        }
Пример #19
0
        private static AstHelper CreateRuntime(ModuleBuilder module)
        {
            var result = new AstHelper(module);

            result.Types.Add("int", typeof(int));
            result.Types.Add("string", typeof(string));
            result.Types.Add("bool", typeof(bool));

            Expression <Action <string> > print = a => Console.Write(a);

            result.Functions.Add("print", new FunctionReference(print, typeof(void), typeof(string)));
            Expression <Action <string> > printline = a => Console.WriteLine(a);

            result.Functions.Add("printline", new FunctionReference(printline, typeof(void), typeof(string)));

            Expression <Action <int> > printi = a => Console.WriteLine(a);

            result.Functions.Add("print", new FunctionReference(printi, typeof(void), typeof(int)));

            Expression <Func <string> > readLine = () => Console.ReadLine();

            result.Functions.Add("readline", new FunctionReference(readLine, typeof(string)));

            Expression <Func <int, string> > str = a => a.ToString(CultureInfo.InvariantCulture);

            result.Functions.Add("str", new FunctionReference(str, typeof(string), typeof(int)));

            // function ord(s : string) : int
            // Return the ASCII value of the first character of s, or €1 if s is empty.
            Expression <Func <string, int> > ord = a => string.IsNullOrEmpty(a) ? -1 : (int)a[0];

            result.Functions.Add("ord", new FunctionReference(ord, typeof(int), typeof(string)));

            // function chr(i : int) : string
            // Return a single-character string for ASCII value i. Terminate program if i is out of range.
            // todo when chr is called terminate the program if i is out of range
            Expression <Func <int, string> > chr = a => ((char)a).ToString();

            result.Functions.Add("chr", new FunctionReference(chr, typeof(string), typeof(int)));

            // function size(s : string) : int
            // Return the number of characters in s.
            Expression <Func <string, int> > size = a => a.Length;

            result.Functions.Add("size", new FunctionReference(size, typeof(int), typeof(string)));

            // function substring(s:string,f:int,n:int):string
            // Return the substring of s starting at the character f (first character is numbered zero) and going for n characters.
            Expression <Func <string, int, int, string> > substring = (s, f, n) => s.Substring(f, n);

            result.Functions.Add("substring",
                                 new FunctionReference(substring, typeof(int), typeof(string), typeof(int),
                                                       typeof(int)));

            // function concat (s1:string, s2:string):string
            // Return a new string consisting of s1 followed by s2.
            Expression <Func <string, string, string> > concat = (s1, s2) => s1 + s2;

            result.Functions.Add("concat",
                                 new FunctionReference(concat, typeof(string), typeof(string), typeof(string)));

            // function not(i : int) : int
            // Return 1 if i is zero, 0 otherwise.
            Expression <Func <int, int> > not = i => i == 0 ? 1 : 0;

            result.Functions.Add("not", new FunctionReference(not, typeof(int), typeof(int)));

            // function exit(i : int)
            // Terminate execution of the program with code i.
            ConstructorInfo            constructor = typeof(ExitException).GetConstructor(new[] { typeof(int) });
            ParameterExpression        parameter   = Expression.Parameter(typeof(int));
            Expression <Action <int> > exit        =
                Expression.Lambda <Action <int> >(Expression.Throw(Expression.New(constructor, parameter)), parameter);

            result.Functions.Add("exit", new FunctionReference(exit, typeof(void), typeof(int)));

            return(result);
        }
        public void CanCompareQualifierLayout(
            string q1m1name,
            string q1m1value,
            string q1m2name,
            string q1m2value,
            string q2m1name,
            string q2m1value,
            string q2m2name,
            string q2m2value,
            bool expectedResult)
        {
            var specifier1 = new TypeNameSpecifier()
            {
                Identifier = new Identifier()
                {
                    Name = "int"
                },
                Qualifier = new TypeQualifier()
            };

            specifier1.Qualifier.Layout.Add(new LayoutIdQualifier()
            {
                Identifier = new Identifier()
                {
                    Name = q1m1name
                },
                Order = new IntegerLiteral()
                {
                    LiteralValue = q1m1value
                }
            });
            specifier1.Qualifier.Layout.Add(new LayoutIdQualifier()
            {
                Identifier = new Identifier()
                {
                    Name = q1m2name
                },
                Order = new IntegerLiteral()
                {
                    LiteralValue = q1m2value
                }
            });

            var specifier2 = new TypeNameSpecifier()
            {
                Identifier = new Identifier()
                {
                    Name = "int"
                },
                Qualifier = new TypeQualifier()
            };

            specifier2.Qualifier.Layout.Add(new LayoutIdQualifier()
            {
                Identifier = new Identifier()
                {
                    Name = q2m1name
                },
                Order = new IntegerLiteral()
                {
                    LiteralValue = q2m1value
                }
            });
            specifier2.Qualifier.Layout.Add(new LayoutIdQualifier()
            {
                Identifier = new Identifier()
                {
                    Name = q2m2name
                },
                Order = new IntegerLiteral()
                {
                    LiteralValue = q2m2value
                }
            });

            var result = AstHelper.TypeSpecifiersAreEquivalent(specifier1, specifier2, true);

            result.Should().Be(expectedResult);
        }
 public MemberWithSameSignatureAlreadyDefinedError(string memberName, IEnumerable <Type> argTypes,
                                                   AstHelper helper, SourceLocation location)
     : base(memberName, helper, location)
 {
     ArgTypes = argTypes;
 }
        protected internal override void CheckSemantics(AstHelper astHelper)
        {
            base.CheckSemantics(astHelper);

            Target.CheckSemantics(astHelper);
        }
Пример #23
0
 public VariableAlreadyDefinedError(string variableName, AstHelper helper, SourceLocation location)
     : base(variableName, helper, location)
 {
 }