Example #1
0
        private void EvalCreateList()
        {
            // Create new array variable
            bool result = EvalExpression();

            Debug.Assert(result);
            Variable size = VarStack.Pop();

            VarStack.Push(Variable.CreateList(size.ToInteger()));
        }
Example #2
0
        internal bool ParseLiteral([NotNullWhen(true)] out Variable?variable, bool allowLineBreak = false)
#endif
        {
            variable = null;
            bool negate = false;

            Token token = allowLineBreak ? Lexer.GetNextSkipNewLines() : Lexer.GetNext();

            if (token.Type == TokenType.LeftBracket)
            {
                // List size
                if (!ParseLiteral(out variable))
                {
                    return(false);
                }
                variable = Variable.CreateList(variable.ToInteger());
                // Parse right bracket
                token = Lexer.GetNext();
                if (token.Type != TokenType.RightBracket)
                {
                    Error(ErrorCode.ExpectedRightBracket, token);
                    NextLine();
                    return(false);
                }
                return(true);
            }
            if (token.Type == TokenType.LeftBrace)
            {
                // List initializers
                List <Variable> list = new();
                do
                {
                    if (!ParseLiteral(out variable, true))
                    {
                        return(false);
                    }
                    list.Add(variable);
                    token = Lexer.GetNextSkipNewLines();
                } while (token.Type == TokenType.Comma);
                // Parse right brace
                if (token.Type != TokenType.RightBrace)
                {
                    Error(ErrorCode.ExpectedRightBrace, token);
                    NextLine();
                    return(false);
                }
                variable = new Variable(list);
                return(true);
            }
            if (token.Type == TokenType.Minus || token.Type == TokenType.Plus)
            {
                negate = (token.Type == TokenType.Minus);
                token  = Lexer.GetNext();
            }
            if (token.IsLiteral)
            {
                variable = new Variable(token);
                if (negate)
                {
                    variable = variable.Negate();
                }
                return(true);
            }
            Error(ErrorCode.ExpectedLiteral, token);
            return(false);
        }