Add hex literal support.
Add hex literal support.
1.1 --- a/src/Parser.cpp Sat Sep 01 17:10:56 2012 -0700
1.2 +++ b/src/Parser.cpp Sat Sep 01 17:11:06 2012 -0700
1.3 @@ -143,6 +143,13 @@
1.4 return new (pool_) FloatLiteral(scanner_.begin(), value);
1.5 }
1.6
1.7 + case TOK_HEX_LITERAL:
1.8 + {
1.9 + scanner_.scan();
1.10 + int value = strtol(scanner_.literal(), NULL, 16);
1.11 + return new (pool_) IntegerLiteral(scanner_.begin(), value);
1.12 + }
1.13 +
1.14 case TOK_INTEGER_LITERAL:
1.15 {
1.16 scanner_.scan();
2.1 --- a/src/Scanner.cpp Sat Sep 01 17:10:56 2012 -0700
2.2 +++ b/src/Scanner.cpp Sat Sep 01 17:11:06 2012 -0700
2.3 @@ -278,6 +278,27 @@
2.4 }
2.5
2.6 TokenKind
2.7 +Scanner::hexLiteral()
2.8 +{
2.9 + literal_.clear();
2.10 +
2.11 + char c;
2.12 + for (;;) {
2.13 + c = getChar();
2.14 + if (!IsHexDigit(c))
2.15 + break;
2.16 + if (!literal_.append(c))
2.17 + return TOK_ERROR;
2.18 + }
2.19 +
2.20 + putChar(c);
2.21 +
2.22 + if (!literal_.append('\0'))
2.23 + return TOK_ERROR;
2.24 + return TOK_HEX_LITERAL;
2.25 +}
2.26 +
2.27 +TokenKind
2.28 Scanner::numberLiteral(char first)
2.29 {
2.30 literal_.clear();
2.31 @@ -293,6 +314,14 @@
2.32 return TOK_ERROR;
2.33 }
2.34
2.35 + // Detect a hexadecimal string.
2.36 + if (literal_.length() == 1 &&
2.37 + literal_[0] == '0' &&
2.38 + (c == 'x' || c == 'X'))
2.39 + {
2.40 + return hexLiteral();
2.41 + }
2.42 +
2.43 if (c != '.') {
2.44 putChar(c);
2.45 if (!literal_.append('\0'))
3.1 --- a/src/Scanner.h Sat Sep 01 17:10:56 2012 -0700
3.2 +++ b/src/Scanner.h Sat Sep 01 17:11:06 2012 -0700
3.3 @@ -52,6 +52,7 @@
3.4 void calcNewLine(char c);
3.5 int readEscapeCode();
3.6 TokenKind stringLiteral();
3.7 + TokenKind hexLiteral();
3.8 TokenKind numberLiteral(char first);
3.9 TokenKind identifier(char first);
3.10
4.1 --- a/src/Token.h Sat Sep 01 17:10:56 2012 -0700
4.2 +++ b/src/Token.h Sat Sep 01 17:11:06 2012 -0700
4.3 @@ -52,6 +52,7 @@
4.4 _(RETURN, "'return'") \
4.5 _(NAME, "<name>") \
4.6 _(INTEGER_LITERAL, "<integer>") \
4.7 + _(HEX_LITERAL, "<hex>") \
4.8 _(FLOAT_LITERAL, "<float>") \
4.9 _(STRING_LITERAL, "<string>") \
4.10 _(IF, "<if>") \