core: Support keywords.

* display.c (display): Handle keyword.
* mes.c (type_t): Add KEYWORD.
  (eq_p): Handle it.
  (make_keyword): New function.
* reader.c (lookup): Use it.
* type.c (keyword_p): New function.
* NEWS: Update.
This commit is contained in:
Jan Nieuwenhuizen 2016-12-16 23:30:33 +01:00
parent 885f48757a
commit 46a617f16e
6 changed files with 40 additions and 2 deletions

2
NEWS
View file

@ -16,6 +16,8 @@ Please send Mes bug reports to janneke@gnu.org.
The C-reader needs only support reading of words and lists The C-reader needs only support reading of words and lists
(s-expressions), line-comments. Quoting, characters, strings, (s-expressions), line-comments. Quoting, characters, strings,
block-comments are all handled by the Scheme reader later. block-comments are all handled by the Scheme reader later.
** Language
*** Keywords are supported.
* Changes in 0.3 since 0.2 * Changes in 0.3 since 0.2
** Core ** Core
*** Number-based rather than pointer-based cells. *** Number-based rather than pointer-based cells.

View file

@ -127,6 +127,8 @@ display_helper (FILE* f, SCM x, bool cont, char const *sep, bool quote)
break; break;
} }
case BROKEN_HEART: fprintf (f, "<3"); break; case BROKEN_HEART: fprintf (f, "<3"); break;
case KEYWORD:
fprintf (f, "#:");
default: default:
if (STRING (x)) if (STRING (x))
{ {

16
mes.c
View file

@ -44,7 +44,7 @@ int MAX_ARENA_SIZE = 20000000;
int GC_SAFETY = 100; int GC_SAFETY = 100;
typedef int SCM; typedef int SCM;
enum type_t {CHAR, FUNCTION, MACRO, NUMBER, PAIR, SPECIAL, STRING, SYMBOL, REF, VALUES, VECTOR, BROKEN_HEART}; enum type_t {CHAR, FUNCTION, KEYWORD, MACRO, NUMBER, PAIR, SPECIAL, STRING, SYMBOL, REF, VALUES, VECTOR, BROKEN_HEART};
typedef SCM (*function0_t) (void); typedef SCM (*function0_t) (void);
typedef SCM (*function1_t) (SCM); typedef SCM (*function1_t) (SCM);
typedef SCM (*function2_t) (SCM, SCM); typedef SCM (*function2_t) (SCM, SCM);
@ -250,6 +250,8 @@ SCM
eq_p (SCM x, SCM y) eq_p (SCM x, SCM y)
{ {
return (x == y return (x == y
|| ((TYPE (x) == KEYWORD && TYPE (y) == KEYWORD
&& STRING (x) == STRING (y)))
|| (TYPE (x) == CHAR && TYPE (y) == CHAR || (TYPE (x) == CHAR && TYPE (y) == CHAR
&& VALUE (x) == VALUE (y)) && VALUE (x) == VALUE (y))
|| (TYPE (x) == NUMBER && TYPE (y) == NUMBER || (TYPE (x) == NUMBER && TYPE (y) == NUMBER
@ -676,6 +678,15 @@ make_function (SCM name, SCM id, SCM arity)
return make_cell (tmp_num3, name, tmp_num4); return make_cell (tmp_num3, name, tmp_num4);
} }
SCM
make_keyword (SCM s)
{
SCM x = internal_lookup_symbol (s);
x = x ? x : internal_make_symbol (s);
g_cells[tmp_num].value = KEYWORD;
return make_cell (tmp_num, STRING (x), 0);
}
SCM SCM
make_macro (SCM name, SCM x) make_macro (SCM name, SCM x)
{ {
@ -932,7 +943,8 @@ gc_loop (SCM scan)
{ {
while (scan < g_free.value) while (scan < g_free.value)
{ {
if (NTYPE (scan) == MACRO if (NTYPE (scan) == KEYWORD
|| NTYPE (scan) == MACRO
|| NTYPE (scan) == PAIR || NTYPE (scan) == PAIR
|| NTYPE (scan) == REF || NTYPE (scan) == REF
|| scan == 1 // null || scan == 1 // null

View file

@ -224,6 +224,8 @@ lookup (SCM s, SCM a)
if (p == cell_nil) return make_number (n * sign); if (p == cell_nil) return make_number (n * sign);
} }
if (VALUE (car (s)) == '#' && VALUE (cadr (s)) == ':') return make_keyword (cddr (s));
SCM x = internal_lookup_symbol (s); SCM x = internal_lookup_symbol (s);
if (x) return x; if (x) return x;

View file

@ -110,3 +110,17 @@ symbol_to_string (SCM x)
assert (TYPE (x) == SYMBOL); assert (TYPE (x) == SYMBOL);
return make_string (STRING (x)); return make_string (STRING (x));
} }
SCM
keyword_to_symbol (SCM x)
{
assert (TYPE (x) == KEYWORD);
return make_symbol (STRING (x));
}
SCM
symbol_to_keyword (SCM x)
{
assert (TYPE (x) == SYMBOL);
return make_keyword (STRING (x));
}

6
type.c
View file

@ -26,6 +26,12 @@ char_p (SCM x)
return TYPE (x) == CHAR ? cell_t : cell_f; return TYPE (x) == CHAR ? cell_t : cell_f;
} }
SCM
keyword_p (SCM x)
{
return TYPE (x) == KEYWORD ? cell_t : cell_f;
}
SCM SCM
macro_p (SCM x) macro_p (SCM x)
{ {