Пример #1
0
        public FormalArg Parse(TokenStream stream)
        {
            // Initialize the type value.
            string typeValue;

            // Check if the next token is a type
            if (TokenIdentifier.IsType(stream.Peek()))
            {
                // Capture argument type value.
                typeValue = stream.Next().Value;
            }
            else
            {
                // TODO: Better error lol.
                throw new Exception("Oops you need a type!");
            }

            // Create the argument's type.
            var type = new Type(typeValue);

            // Create the formal argument entity.
            var arg = new FormalArg(type);

            // Capture the argument's name.
            var name = stream.Next(TokenType.Identifier).Value;

            // Assign the argument's name.
            arg.SetName(name);

            return(arg);
        }
Пример #2
0
        public VarDeclareExpr Parse(TokenStream stream)
        {
            // Consume the type string.
            var typeValue = stream.Next().Value;

            // Create the type.
            var type = new Type(typeValue);

            // Create the variable declaration & link the type.
            var declaration = new VarDeclareExpr(type, null);

            // Consume the variable name.
            var name = stream.Next().Value;

            // Ensure captured name is not null nor empty.
            if (string.IsNullOrEmpty(name))
            {
                throw new Exception("Unexpected variable declaration identifier to be null or empty");
            }

            // Assign the name.
            declaration.SetName(name);

            // Peek next token for value.
            Token nextToken = stream.Peek();

            // Value is being assigned.
            if (nextToken.Type == TokenType.OperatorAssignment)
            {
                // Skip over the Assignment operator
                stream.Skip();

                // Parse value.
                Expr value = new ExprParser().Parse(stream);

                // Assign value.
                declaration.Value = value;
            }

            return(declaration);
        }