summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKeith Packard <keithp@keithp.com>2018-01-03 14:53:48 -0800
committerKeith Packard <keithp@keithp.com>2018-01-03 14:53:48 -0800
commitfccb5105b79d5b9e2ed052ce5459028015c01741 (patch)
tree657941b0abca3b3d6325ae27bff220a25e671c66 /src
parentfc63968f90e3fab12e63d973a4ee7f16d80d765f (diff)
altos/scheme: Add support for hex, octal and binary constants
Signed-off-by: Keith Packard <keithp@keithp.com>
Diffstat (limited to 'src')
-rw-r--r--src/scheme/ao_scheme_read.c54
-rw-r--r--src/scheme/ao_scheme_read.h3
2 files changed, 44 insertions, 13 deletions
diff --git a/src/scheme/ao_scheme_read.c b/src/scheme/ao_scheme_read.c
index e93466fc..9174de5e 100644
--- a/src/scheme/ao_scheme_read.c
+++ b/src/scheme/ao_scheme_read.c
@@ -83,12 +83,12 @@ static const uint16_t lex_classes[128] = {
PRINTABLE, /* > */
PRINTABLE, /* ? */
PRINTABLE, /* @ */
- PRINTABLE, /* A */
- PRINTABLE, /* B */
- PRINTABLE, /* C */
- PRINTABLE, /* D */
- PRINTABLE|FLOATC, /* E */
- PRINTABLE, /* F */
+ PRINTABLE|HEX_LETTER, /* A */
+ PRINTABLE|HEX_LETTER, /* B */
+ PRINTABLE|HEX_LETTER, /* C */
+ PRINTABLE|HEX_LETTER, /* D */
+ PRINTABLE|FLOATC|HEX_LETTER,/* E */
+ PRINTABLE|HEX_LETTER, /* F */
PRINTABLE, /* G */
PRINTABLE, /* H */
PRINTABLE, /* I */
@@ -115,12 +115,12 @@ static const uint16_t lex_classes[128] = {
PRINTABLE, /* ^ */
PRINTABLE, /* _ */
PRINTABLE|SPECIAL_QUASI, /* ` */
- PRINTABLE, /* a */
- PRINTABLE, /* b */
- PRINTABLE, /* c */
- PRINTABLE, /* d */
- PRINTABLE|FLOATC, /* e */
- PRINTABLE, /* f */
+ PRINTABLE|HEX_LETTER, /* a */
+ PRINTABLE|HEX_LETTER, /* b */
+ PRINTABLE|HEX_LETTER, /* c */
+ PRINTABLE|HEX_LETTER, /* d */
+ PRINTABLE|FLOATC|HEX_LETTER,/* e */
+ PRINTABLE|HEX_LETTER, /* f */
PRINTABLE, /* g */
PRINTABLE, /* h */
PRINTABLE, /* i */
@@ -285,6 +285,30 @@ static const struct namedfloat namedfloats[] = {
#endif
static int
+parse_int(int base)
+{
+ int cval;
+ int c;
+
+ token_int = 0;
+ for (;;) {
+ c = lexc();
+ if ((lex_class & HEX_DIGIT) == 0) {
+ lex_unget(c);
+ end_token();
+ return NUM;
+ }
+ add_token(c);
+ if ('0' <= c && c <= '9')
+ cval = c - '0';
+ else
+ cval = (c | ('a' - 'A')) - 'a' + 10;
+ token_int = token_int * base + cval;
+ }
+ return NUM;
+}
+
+static int
_lex(void)
{
int c;
@@ -387,6 +411,12 @@ _lex(void)
continue;
}
return NUM;
+ case 'x':
+ return parse_int(16);
+ case 'o':
+ return parse_int(8);
+ case 'b':
+ return parse_int(2);
}
}
if (lex_class & STRINGC) {
diff --git a/src/scheme/ao_scheme_read.h b/src/scheme/ao_scheme_read.h
index 1aa11a3a..d0b9b36a 100644
--- a/src/scheme/ao_scheme_read.h
+++ b/src/scheme/ao_scheme_read.h
@@ -65,10 +65,11 @@
# define IGNORE 0x0200 /* \0 - ' ' */
# define BACKSLASH 0x0400 /* \ */
# define STRINGC 0x0800 /* " */
-# define POUND 0x1000 /* # */
+# define HEX_LETTER 0x1000 /* a-f A-F */
# define NOTNAME (STRINGC|COMMENT|ENDOFFILE|WHITE|SPECIAL)
# define INTEGER (DIGIT|SIGN)
# define NUMBER (INTEGER|FLOATC)
+# define HEX_DIGIT (DIGIT|HEX_LETTER)
#endif /* _AO_SCHEME_READ_H_ */