Added hex character constants within strings.

This commit is contained in:
Martin Haye 2016-01-29 08:27:00 -08:00
parent 951d03c4d3
commit 10db0b073a
4 changed files with 27 additions and 7 deletions

View File

@ -26,6 +26,7 @@ void emit_def(char *name, int is_bytecode);
int emit_data(int vartype, int consttype, long constval, int constsize); int emit_data(int vartype, int consttype, long constval, int constsize);
void emit_codetag(int tag); void emit_codetag(int tag);
void emit_const(int cval); void emit_const(int cval);
void emit_conststr(long conststr, int strsize);
void emit_lb(void); void emit_lb(void);
void emit_lw(void); void emit_lw(void);
void emit_llb(int index); void emit_llb(int index);

View File

@ -75,6 +75,18 @@ void parse_error(char *errormsg)
fprintf(stderr, "^\nError: %s\n", errormsg); fprintf(stderr, "^\nError: %s\n", errormsg);
exit(1); exit(1);
} }
int hexdigit(char ch)
{
ch = toupper(ch);
if (ch >= '0' && ch <= '9')
return ch - '0';
else if (ch >= 'A' && ch <= 'F')
return ch - 'A' + 10;
else
return -1;
}
t_token scan(void) t_token scan(void)
{ {
prevtoken = scantoken; prevtoken = scantoken;
@ -152,12 +164,8 @@ t_token scan(void)
constval = 0; constval = 0;
while (scanpos++) while (scanpos++)
{ {
if (*scanpos >= '0' && *scanpos <= '9') if (hexdigit(*scanpos) >= 0)
constval = constval * 16 + *scanpos - '0'; constval = constval * 16 + hexdigit(*scanpos);
else if (*scanpos >= 'A' && *scanpos <= 'F')
constval = constval * 16 + *scanpos - 'A' + 10;
else if (*scanpos >= 'a' && *scanpos <= 'f')
constval = constval * 16 + *scanpos - 'a' + 10;
else else
break; break;
} }
@ -216,6 +224,7 @@ t_token scan(void)
else if (scanpos[0] == '\"') else if (scanpos[0] == '\"')
{ {
char *scanshift; char *scanshift;
int scanoffset;
/* /*
* String constant. * String constant.
*/ */
@ -225,6 +234,7 @@ t_token scan(void)
{ {
if (*scanpos == '\\') if (*scanpos == '\\')
{ {
scanoffset = 1;
switch (scanpos[1]) switch (scanpos[1])
{ {
case 'n': case 'n':
@ -248,12 +258,20 @@ t_token scan(void)
case '0': case '0':
*scanpos = '\0'; *scanpos = '\0';
break; break;
case '$':
if (hexdigit(scanpos[2]) < 0 || hexdigit(scanpos[3]) < 0) {
parse_error("Bad string constant");
return (-1);
}
*scanpos = hexdigit(scanpos[2]) * 16 + hexdigit(scanpos[3]);
scanoffset = 3;
break;
default: default:
parse_error("Bad string constant"); parse_error("Bad string constant");
return (-1); return (-1);
} }
for (scanshift = scanpos + 1; *scanshift; scanshift++) for (scanshift = scanpos + 1; *scanshift; scanshift++)
scanshift[0] = scanshift[1]; scanshift[0] = scanshift[scanoffset];
} }
scanpos++; scanpos++;
} }

View File

@ -125,4 +125,5 @@ puti(mystruc)
putln putln
puts(@constr); puti(constval); putln puts(@constr); puti(constval); putln
puts("Hello from in-line string!\n") puts("Hello from in-line string!\n")
puts("Hi \$41pple.\n");
done done