Added support for octal and hex character constants

git-svn-id: http://picoc.googlecode.com/svn/trunk@326 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2009-06-03 02:57:23 +00:00
parent 637e224fff
commit b55ab05d48
4 changed files with 34 additions and 3 deletions

15
lex.c
View file

@ -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;
}
}

View file

@ -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");

View file

@ -0,0 +1,9 @@
1
8
64
255
1
14
16
64
test @7

View file

@ -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: $*...