From b55ab05d48240bd64986b1fd2ace9ea4540e8334 Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Wed, 3 Jun 2009 02:57:23 +0000 Subject: [PATCH] Added support for octal and hex character constants git-svn-id: http://picoc.googlecode.com/svn/trunk@326 21eae674-98b7-11dd-bd71-f92a316d2d60 --- lex.c | 15 +++++++++++++-- tests/26_character_constants.c | 10 ++++++++++ tests/26_character_constants.expect | 9 +++++++++ tests/Makefile | 3 ++- 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 tests/26_character_constants.c create mode 100644 tests/26_character_constants.expect diff --git a/lex.c b/lex.c index 025bf4f..4142f38 100644 --- a/lex.c +++ b/lex.c @@ -211,6 +211,17 @@ enum LexToken LexGetWord(struct LexState *Lexer, struct Value *Value) return TokenIdentifier; } +/* unescape a character from an octal character constant */ +unsigned char LexUnEscapeCharacterConstant(const char **From, const char *End, unsigned char FirstChar, int Base) +{ + unsigned char Total = GET_BASE_DIGIT(FirstChar); + int CCount; + for (CCount = 0; IS_BASE_DIGIT(**From, Base) && CCount < 2; CCount++, (*From)++) + Total = Total * Base + GET_BASE_DIGIT(**From); + + return Total; +} + /* unescape a character from a string or character constant */ unsigned char LexUnEscapeCharacter(const char **From, const char *End) { @@ -243,8 +254,8 @@ unsigned char LexUnEscapeCharacter(const char **From, const char *End) case 'r': return '\r'; case 't': return '\t'; case 'v': return '\v'; - /* case '0': XXX - implement octal character constants */ - /* case 'x': XXX - implement hex character constants */ + case '0': case '1': case '2': case '3': return LexUnEscapeCharacterConstant(From, End, ThisChar, 8); + case 'x': return LexUnEscapeCharacterConstant(From, End, '0', 16); default: return ThisChar; } } diff --git a/tests/26_character_constants.c b/tests/26_character_constants.c new file mode 100644 index 0000000..f4bb09f --- /dev/null +++ b/tests/26_character_constants.c @@ -0,0 +1,10 @@ +printf("%d\n", '\1'); +printf("%d\n", '\10'); +printf("%d\n", '\100'); +printf("%d\n", '\377'); +printf("%d\n", '\x01'); +printf("%d\n", '\x0e'); +printf("%d\n", '\x10'); +printf("%d\n", '\x40'); +printf("test \x407\n"); + diff --git a/tests/26_character_constants.expect b/tests/26_character_constants.expect new file mode 100644 index 0000000..a4fdf01 --- /dev/null +++ b/tests/26_character_constants.expect @@ -0,0 +1,9 @@ +1 +8 +64 +255 +1 +14 +16 +64 +test @7 diff --git a/tests/Makefile b/tests/Makefile index fa9272e..0f5bf2e 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -21,7 +21,8 @@ TESTS= 00_assignment.test \ 20_pointer_comparison.test \ 22_floating_point.test \ 23_type_coercion.test \ - 24_math_library.test + 24_math_library.test \ + 26_character_constants.test %.test: %.expect %.c @echo Test: $*...