Add parser support for default parameters.
Add parser support for default parameters.
1.1 --- a/src/AST.h Sun Jul 29 18:19:12 2012 -0700
1.2 +++ b/src/AST.h Sun Jul 29 19:24:55 2012 -0700
1.3 @@ -203,14 +203,17 @@
1.4 bool reference_;
1.5 bool const_;
1.6 ExpressionList *dimensions_;
1.7 + Expression *initializer_;
1.8
1.9 public:
1.10 - Parameter(Variable *var, Handle<String> tag, bool reference, bool isConst, ExpressionList *dimensions)
1.11 + Parameter(Variable *var, Handle<String> tag, bool reference, bool isConst,
1.12 + ExpressionList *dimensions, Expression *initializer)
1.13 : variable_(var),
1.14 tag_(tag),
1.15 reference_(reference),
1.16 const_(isConst),
1.17 - dimensions_(dimensions)
1.18 + dimensions_(dimensions),
1.19 + initializer_(initializer)
1.20 {
1.21 }
1.22
1.23 @@ -229,6 +232,9 @@
1.24 bool isConst() const {
1.25 return const_;
1.26 }
1.27 + Expression *initializer() const {
1.28 + return initializer_;
1.29 + }
1.30 };
1.31
1.32 class NameProxy : public Expression
2.1 --- a/src/Parser.cpp Sun Jul 29 18:19:12 2012 -0700
2.2 +++ b/src/Parser.cpp Sun Jul 29 19:24:55 2012 -0700
2.3 @@ -967,6 +967,7 @@
2.4
2.5 if (!expect(TOK_LPAREN))
2.6 return NULL;
2.7 +
2.8 if (!match(TOK_RPAREN)) {
2.9 while (true) {
2.10 bool isConst = match(TOK_CONST);
2.11 @@ -1022,7 +1023,14 @@
2.12 return NULL;
2.13 }
2.14
2.15 - Parameter *parameter = new (pool_) Parameter(arg, tag, ref, isConst, postDimensions);
2.16 + Expression *expr = NULL;
2.17 + if (match(TOK_ASSIGN)) {
2.18 + if ((expr = expression()) == NULL)
2.19 + return NULL;
2.20 + }
2.21 +
2.22 + Parameter *parameter = new (pool_) Parameter(arg, tag, ref, isConst, postDimensions,
2.23 + expr);
2.24 if (!parameters->append(parameter))
2.25 return NULL;
2.26
3.1 --- a/src/Scanner.cpp Sun Jul 29 18:19:12 2012 -0700
3.2 +++ b/src/Scanner.cpp Sun Jul 29 19:24:55 2012 -0700
3.3 @@ -369,10 +369,12 @@
3.4 if (!literal_.append(c))
3.5 return TOK_ERROR;
3.6 }
3.7 -
3.8 +
3.9 if (!literal_.append('\0'))
3.10 return TOK_ERROR;
3.11
3.12 + if (!strcmp(literal(), "_"))
3.13 + return TOK_UNDERSCORE;
3.14 if (!strcmp(literal(), "new"))
3.15 return TOK_NEW;
3.16 if (!strcmp(literal(), "for"))
4.1 --- a/src/Token.h Sun Jul 29 18:19:12 2012 -0700
4.2 +++ b/src/Token.h Sun Jul 29 19:24:55 2012 -0700
4.3 @@ -1,19 +1,20 @@
4.4 -/*
4.5 +/* vim: set ts=4 sw=4 tw=99 et:
4.6 + *
4.7 * Copyright (C) 2012 David Anderson
4.8 *
4.9 - * This file is part of JITCraft.
4.10 + * This file is part of SourcePawn.
4.11 *
4.12 - * JITCraft is free software: you can redistribute it and/or modify it under
4.13 + * SourcePawn is free software: you can redistribute it and/or modify it under
4.14 * the terms of the GNU General Public License as published by the Free
4.15 * Software Foundation, either version 3 of the License, or (at your option)
4.16 * any later version.
4.17 *
4.18 - * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
4.19 + * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
4.20 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
4.21 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
4.22 *
4.23 * You should have received a copy of the GNU General Public License along with
4.24 - * JITCraft. If not, see http://www.gnu.org/licenses/.
4.25 + * SourcePawn. If not, see http://www.gnu.org/licenses/.
4.26 */
4.27 #ifndef _include_jitcraft_token_h_
4.28 #define _include_jitcraft_token_h_
4.29 @@ -110,6 +111,7 @@
4.30 _(TILDE, "~") \
4.31 _(QMARK, "?") \
4.32 _(COLON, ":") \
4.33 + _(UNDERSCORE, "_") \
4.34 _(EOF, "<end-of-file>")
4.35
4.36 enum TokenKind