Exemplo n.º 1
0
        public static Subroutine Parse(Tokenizer tokenizer)
        {
            tokenizer.CurrentIs(t =>
                                t is Keyword k && (
                                    k == Keyword.Constructor ||
                                    k == Keyword.Function ||
                                    k == Keyword.Method));

            var type = tokenizer.GetCurrentThenMove() as Keyword;

            tokenizer.CurrentIs(VaribleType.IsValid);
            var returnType = new VaribleType(tokenizer.GetCurrentThenMove());

            tokenizer.CurrentIsIdentifier();
            var name = tokenizer.GetCurrentThenMove() as Identifier;

            var parameters = ParseParameters(tokenizer);

            tokenizer.CurrentIs(Symbol.OpenCurly).Move();
            var localVaribles = ParseLocalVaribles(tokenizer);
            var statements    = Statement.ParseStatements(tokenizer);

            tokenizer.CurrentIs(Symbol.CloseCurly).Move();

            if (type == Keyword.Constructor)
            {
                return(new Constructor(name, returnType, parameters, localVaribles, statements));
            }
            if (type == Keyword.Method)
            {
                return(new Method(name, returnType, parameters, localVaribles, statements));
            }
            return(new Function(name, returnType, parameters, localVaribles, statements));
        }
Exemplo n.º 2
0
        private void AddClassLevelVaribles(Tokenizer tokenizer)
        {
            while (tokenizer.Current is Keyword keyword && (
                       keyword == Keyword.Static || keyword == Keyword.Field))
            {
                tokenizer.Move().CurrentIs(VaribleType.IsValid);
                var type = new VaribleType(tokenizer.GetCurrentThenMove());

                tokenizer.CurrentIsIdentifier();
                AddVarible(keyword,
                           new Varible(type, tokenizer.GetCurrentThenMove() as Identifier));

                while (tokenizer.Current.Equals(Symbol.Commna))
                {
                    tokenizer.Move().CurrentIsIdentifier();
                    AddVarible(keyword,
                               new Varible(type, tokenizer.GetCurrentThenMove() as Identifier));
                }

                tokenizer.CurrentIs(Symbol.SemiColon).Move();
            }

            void AddVarible(Keyword keyword, Varible varible)
            {
                if (keyword == Keyword.Field)
                {
                    Fields.Add(varible);
                }
                else
                {
                    StaticFields.Add(varible);
                }
            }
        }
Exemplo n.º 3
0
 protected Subroutine(Identifier name,
                      VaribleType returnType,
                      IEnumerable <Varible> arguments,
                      IEnumerable <Varible> localVaribles,
                      IEnumerable <Statement> statements)
 {
     Name          = name;
     ReturnType    = returnType;
     Arguments     = arguments.ToList();
     LocalVaribles = localVaribles.ToList();
     Statements    = statements;
 }
Exemplo n.º 4
0
        private static IEnumerable <Varible> ParseLocalVaribles(Tokenizer tokenizer)
        {
            var varibles = new List <Varible>();

            while (tokenizer.Current is Keyword keyword && keyword == Keyword.Var)
            {
                tokenizer.Move().CurrentIs(VaribleType.IsValid);
                var type = new VaribleType(tokenizer.GetCurrentThenMove());

                tokenizer.CurrentIsIdentifier();
                varibles.Add(new Varible(type, tokenizer.GetCurrentThenMove() as Identifier));

                while (tokenizer.Current.Equals(Symbol.Commna))
                {
                    tokenizer.Move().CurrentIsIdentifier();
                    varibles.Add(new Varible(type, tokenizer.GetCurrentThenMove() as Identifier));
                }

                tokenizer.CurrentIs(Symbol.SemiColon).Move();
            }

            return(varibles);
        }
Exemplo n.º 5
0
 public Method(Identifier name, VaribleType returnType, IEnumerable <Varible> arguments,
               IEnumerable <Varible> localVaribles, IEnumerable <Statement> statements) : base(name, returnType, arguments,
                                                                                               localVaribles, statements)
 {
 }
Exemplo n.º 6
0
 public Varible(VaribleType varibleType, Identifier identifier)
 {
     VaribleType = varibleType;
     Identifier  = identifier;
 }
Exemplo n.º 7
0
 public override int GetHashCode()
 {
     unchecked
     {
         return(((Identifier != null ? Identifier.GetHashCode() : 0) * 397) ^ (VaribleType != null ? VaribleType.GetHashCode() : 0));
     }
 }