示例#1
0
        private object ProcessArrayAccess(ArrayAccess arrayAccess)
        {
            object obj   = EvalExpression(arrayAccess.Exp);
            object index = EvalExpression(arrayAccess.Index);

            if (obj is Array) {
                Array array = (Array) obj;

                if (index is int) {
                    return array.GetValue((int) index);
                } else {
                    throw new TmplException("Index of array has to be integer", arrayAccess.Line, arrayAccess.Col);
                }
            } else {
                return ProcessMCall(obj, "get_Item", new object[] { index });
            }
        }
示例#2
0
        private Expression AtomExpression()
        {
            if (Current.TokenKind == TokenKind.StringStart) {
                return ReadString();
            } else if (Current.TokenKind == TokenKind.ID) {
                Token id = Consume();
                Expression exp = null;

                if (Current.TokenKind == TokenKind.LParen) {
                    Consume();
                    Expression[] args = ReadArguments();
                    Consume(TokenKind.RParen);

                    exp = new FCall(id.Line, id.Col, id.Data, args);
                } else {
                    exp = new Name(id.Line, id.Col, id.Data);
                }

                while (Current.TokenKind == TokenKind.Dot || Current.TokenKind == TokenKind.LBracket) {
                    if (Current.TokenKind == TokenKind.Dot) {
                        Consume();
                        Token field = Consume(TokenKind.ID);

                        if (Current.TokenKind == TokenKind.LParen) {
                            Consume();
                            Expression[] args = ReadArguments();
                            Consume(TokenKind.RParen);

                            exp = new MCall(field.Line, field.Col, exp, field.Data, args);
                        } else {
                            exp = new FieldAccess(field.Line, field.Col, exp, field.Data);
                        }
                    } else {
                        Token bracket = Current;
                        Consume();
                        Expression indexExp = TopExpression();
                        Consume(TokenKind.RBracket);

                        exp = new ArrayAccess(bracket.Line, bracket.Col, exp, indexExp);
                    }
                }

                return exp;
            } else if (Current.TokenKind == TokenKind.Integer) {
                int value = int.Parse(Current.Data);
                IntLiteral intLiteral = new IntLiteral(Current.Line, Current.Col, value);
                Consume();
                return intLiteral;
            } else if (Current.TokenKind == TokenKind.Double) {
                double value = double.Parse(Current.Data);
                DoubleLiteral dLiteral = new DoubleLiteral(Current.Line, Current.Col, value);
                Consume();
                return dLiteral;
            } else if (Current.TokenKind == TokenKind.LParen) {
                Consume();
                Expression exp = TopExpression();
                Consume(TokenKind.RParen);
                return exp;
            } else {
                throw new TmplException(string.Format("Invalid token in expression: " + Current.TokenKind + ". Was expecting ID or string.L{0}/C{1}", Current.Line, Current.Col), Current.Line, Current.Col);
            }
        }