mirror of
https://github.com/samsonjs/lake.git
synced 2026-03-25 08:55:49 +00:00
- add booleans, dotted lists, functions - proper lexical scope - special forms: define, set!, and, or, quote, lambda - added some basic list manipulations - print functions and dotted lists - removed nil - more robust in general
66 lines
1.1 KiB
C
66 lines
1.1 KiB
C
/**
|
|
* sym.c
|
|
* Lake Scheme
|
|
*
|
|
* Copyright 2011 Sami Samhuri
|
|
* MIT License
|
|
*
|
|
*/
|
|
|
|
#include <glib.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "env.h"
|
|
#include "lake.h"
|
|
#include "string.h"
|
|
#include "sym.h"
|
|
|
|
static GHashTable *_symbols;
|
|
|
|
static LakeSym *sym_alloc(void)
|
|
{
|
|
LakeSym *sym = g_malloc(sizeof(LakeSym));
|
|
VAL(sym)->type = TYPE_SYM;
|
|
VAL(sym)->size = sizeof(LakeSym);
|
|
return sym;
|
|
}
|
|
|
|
LakeSym *sym_intern(char *s)
|
|
{
|
|
if (!_symbols) _symbols = g_hash_table_new(g_str_hash, g_str_equal);
|
|
LakeSym *sym = g_hash_table_lookup(_symbols, s);
|
|
if (!sym) {
|
|
sym = sym_alloc();
|
|
sym->n = strlen(s);
|
|
sym->s = g_strdup(s);
|
|
sym->hash = g_str_hash(s);
|
|
g_hash_table_insert(_symbols, sym->s, sym);
|
|
}
|
|
return sym;
|
|
}
|
|
|
|
LakeStr *sym_to_str(LakeSym *sym)
|
|
{
|
|
return str_from_c(sym->s);
|
|
}
|
|
|
|
LakeSym *sym_from_str(LakeStr *str)
|
|
{
|
|
return sym_intern(str->s);
|
|
}
|
|
|
|
char *sym_repr(LakeSym *sym)
|
|
{
|
|
return g_strdup(sym->s);
|
|
}
|
|
|
|
unsigned long sym_val(LakeSym *sym)
|
|
{
|
|
return sym->hash;
|
|
}
|
|
|
|
LakeBool *sym_eq(LakeSym *a, LakeSym *b)
|
|
{
|
|
return bool_from_int(g_strcmp0(a->s, b->s) == 0);
|
|
}
|