glib-ized

This commit is contained in:
Sami Samhuri 2011-04-18 22:36:07 -07:00
parent 0715d4a705
commit ec1c10f561
7 changed files with 35 additions and 35 deletions

4
env.c
View file

@ -31,9 +31,7 @@ Env *shared_env(void)
Env *env_make(Env *parent) Env *env_make(Env *parent)
{ {
Env *env; Env *env = g_malloc(sizeof(Env));
env = malloc(sizeof(Env));
if (!env) oom();
env->parent = parent; env->parent = parent;
env->bindings = g_hash_table_new(g_str_hash, g_str_equal); env->bindings = g_hash_table_new(g_str_hash, g_str_equal);
env->symbols = g_hash_table_new(g_str_hash, g_str_equal); env->symbols = g_hash_table_new(g_str_hash, g_str_equal);

3
int.c
View file

@ -7,6 +7,7 @@
* *
*/ */
#include <glib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "bool.h" #include "bool.h"
@ -14,7 +15,7 @@
static LakeInt *int_alloc(void) static LakeInt *int_alloc(void)
{ {
LakeInt *i = malloc(sizeof(LakeInt)); LakeInt *i = g_malloc(sizeof(LakeInt));
i->base.type = TYPE_INT; i->base.type = TYPE_INT;
i->base.size = sizeof(LakeInt); i->base.size = sizeof(LakeInt);
return i; return i;

11
lake.c
View file

@ -10,6 +10,7 @@
* *
*/ */
#include <glib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -103,7 +104,7 @@ char *repr(LakeVal *expr)
switch (expr->type) { switch (expr->type) {
case TYPE_NIL: case TYPE_NIL:
s = strdup("nil"); s = g_strdup("nil");
break; break;
case TYPE_SYM: case TYPE_SYM:
@ -112,7 +113,7 @@ char *repr(LakeVal *expr)
case TYPE_BOOL: case TYPE_BOOL:
printf("%s:%d TODO: repr for bools", __FILE__, __LINE__); printf("%s:%d TODO: repr for bools", __FILE__, __LINE__);
s = strdup("[bool]"); s = g_strdup("[bool]");
break; break;
case TYPE_INT: case TYPE_INT:
@ -122,9 +123,7 @@ char *repr(LakeVal *expr)
break; break;
case TYPE_STR: case TYPE_STR:
str = STR(expr); s = g_strdup_printf("\"%s\"", STR_S(STR(expr)));
s = malloc(STR_N(str) + 3);
sprintf(s, "\"%s\"", STR_S(str));
break; break;
case TYPE_LIST: case TYPE_LIST:
@ -133,7 +132,7 @@ char *repr(LakeVal *expr)
default: default:
printf("unrecognized value, type %d, size %Zu bytes", expr->type, expr->size); printf("unrecognized value, type %d, size %Zu bytes", expr->type, expr->size);
s = strdup("unrecognized value"); s = g_strdup("unrecognized value");
} }
return s; return s;

15
list.c
View file

@ -7,6 +7,7 @@
* *
*/ */
#include <glib.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "int.h" #include "int.h"
@ -18,7 +19,7 @@
static LakeList *list_alloc(void) static LakeList *list_alloc(void)
{ {
LakeList *list = malloc(sizeof(LakeList)); LakeList *list = g_malloc(sizeof(LakeList));
list->base.type = TYPE_LIST; list->base.type = TYPE_LIST;
list->base.size = sizeof(LakeList); list->base.size = sizeof(LakeList);
return list; return list;
@ -39,7 +40,7 @@ LakeList *list_make_with_cap(size_t cap)
LakeList *list = list_alloc(); LakeList *list = list_alloc();
list->cap = cap; list->cap = cap;
list->n = 0; list->n = 0;
list->vals = malloc(cap * sizeof(LakeVal *)); list->vals = g_malloc(cap * sizeof(LakeVal *));
return list; return list;
} }
@ -62,12 +63,12 @@ NILP list_grow(LakeList *list)
{ {
list->cap *= 2; list->cap *= 2;
LakeVal **new_vals; LakeVal **new_vals;
new_vals = malloc(list->cap * sizeof(LakeVal *)); new_vals = g_malloc(list->cap * sizeof(LakeVal *));
int i; int i;
for (i = 0; i < list->n; ++i) { for (i = 0; i < list->n; ++i) {
new_vals[i] = list->vals[i]; new_vals[i] = list->vals[i];
} }
free(list->vals); g_free(list->vals);
list->vals = new_vals; list->vals = new_vals;
return NIL; return NIL;
} }
@ -116,7 +117,7 @@ LakeStr *list_to_str(LakeList *list)
{ {
char *s = list_repr(list); char *s = list_repr(list);
LakeStr *str = str_from_c(s); LakeStr *str = str_from_c(s);
free(s); g_free(s);
return str; return str;
} }
@ -133,10 +134,10 @@ char *list_repr(LakeList *list)
len = strlen(s2); len = strlen(s2);
memcpy(s + n, s2, len); memcpy(s + n, s2, len);
n += len; n += len;
free(s2); g_free(s2);
if (i != LIST_N(list) - 1) s[n++] = ' '; if (i != LIST_N(list) - 1) s[n++] = ' ';
} }
s[n++] = ')'; s[n++] = ')';
s[n] = '\0'; s[n] = '\0';
return strdup(s); return g_strdup(s);
} }

15
parse.c
View file

@ -7,6 +7,7 @@
* *
*/ */
#include <glib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -68,7 +69,7 @@ static char ch(Ctx *ctx, char expected)
consume1(ctx); consume1(ctx);
return c; return c;
} }
char *msg = malloc(64); char *msg = g_malloc(64);
sprintf(msg, "parse error, expected '%c' got '%c'", expected, c); sprintf(msg, "parse error, expected '%c' got '%c'", expected, c);
die(msg); die(msg);
return 0; /* placate the compiler */ return 0; /* placate the compiler */
@ -113,7 +114,7 @@ static char *parse_while(Ctx *ctx, int (*is_valid)(char))
{ {
size_t n = 8; size_t n = 8;
size_t i = 0; size_t i = 0;
char *s = malloc(n); char *s = g_malloc(n);
char c; char c;
while ((c = peek(ctx)) != PARSE_EOF && is_valid(c)) { while ((c = peek(ctx)) != PARSE_EOF && is_valid(c)) {
s[i++] = c; s[i++] = c;
@ -121,7 +122,7 @@ static char *parse_while(Ctx *ctx, int (*is_valid)(char))
/* grow if necessary */ /* grow if necessary */
if (i >= n) { if (i >= n) {
n *= 2; n *= 2;
char *t = realloc(s, n); char *t = g_realloc(s, n);
if (!t) oom(); if (!t) oom();
s = t; s = t;
} }
@ -194,7 +195,7 @@ static LakeVal *parse_str(Ctx *ctx)
{ {
size_t n = 8; size_t n = 8;
size_t i = 0; size_t i = 0;
char *s = malloc(n); char *s = g_malloc(n);
char c; char c;
ch(ctx, '"'); ch(ctx, '"');
while ((c = peek(ctx)) != PARSE_EOF && c != '"') { while ((c = peek(ctx)) != PARSE_EOF && c != '"') {
@ -209,7 +210,7 @@ static LakeVal *parse_str(Ctx *ctx)
/* grow if necessary */ /* grow if necessary */
if (i >= n) { if (i >= n) {
n *= 2; n *= 2;
char *t = realloc(s, n); char *t = g_realloc(s, n);
if (!t) oom(); if (!t) oom();
s = t; s = t;
} }
@ -217,7 +218,7 @@ static LakeVal *parse_str(Ctx *ctx)
s[i] = '\0'; s[i] = '\0';
ch(ctx, '"'); ch(ctx, '"');
LakeStr *str = str_from_c(s); LakeStr *str = str_from_c(s);
free(s); g_free(s);
return VAL(str); return VAL(str);
} }
@ -244,7 +245,7 @@ static int is_not_newline(char c)
static void parse_comment(Ctx *ctx) static void parse_comment(Ctx *ctx)
{ {
free(parse_while(ctx, is_not_newline)); g_free(parse_while(ctx, is_not_newline));
} }
static LakeVal *_parse_expr(Ctx *ctx) static LakeVal *_parse_expr(Ctx *ctx)

View file

@ -16,7 +16,7 @@
static LakeStr *str_alloc(void) static LakeStr *str_alloc(void)
{ {
LakeStr *str = malloc(sizeof(LakeStr)); LakeStr *str = g_malloc(sizeof(LakeStr));
str->base.type = TYPE_STR; str->base.type = TYPE_STR;
str->base.size = sizeof(LakeStr); str->base.size = sizeof(LakeStr);
return str; return str;
@ -24,14 +24,14 @@ static LakeStr *str_alloc(void)
void str_free(LakeStr *str) void str_free(LakeStr *str)
{ {
free(str->s); g_free(str->s);
free(str); g_free(str);
} }
NILP str_set(LakeStr *str, char *s) NILP str_set(LakeStr *str, char *s)
{ {
str->n = strlen(s); str->n = strlen(s);
str->s = strdup(s); str->s = g_strdup(s);
return NIL; return NIL;
} }
@ -59,17 +59,17 @@ LakeStr *str_copy(LakeStr *str)
char *str_val(LakeStr *str) char *str_val(LakeStr *str)
{ {
return strdup(str->s); return g_strdup(str->s);
} }
LakeInt *str_cmp(LakeStr *a, LakeStr *b) LakeInt *str_cmp(LakeStr *a, LakeStr *b)
{ {
return int_from_c(strncmp(a->s, b->s, MIN(a->n, b->n))); return int_from_c(g_strcmp0(a->s, b->s));
} }
LakeSym *str_eq(LakeStr *a, LakeStr *b) LakeSym *str_eq(LakeStr *a, LakeStr *b)
{ {
return bool_from_int(strncmp(a->s, b->s, MIN(a->n, b->n)) == 0); return bool_from_int(g_strcmp0(a->s, b->s) == 0);
} }
LakeStr *str_to_str(LakeStr *str) LakeStr *str_to_str(LakeStr *str)

8
sym.c
View file

@ -20,7 +20,7 @@
static LakeSym *sym_alloc(void) static LakeSym *sym_alloc(void)
{ {
LakeSym *sym = malloc(sizeof(LakeSym)); LakeSym *sym = g_malloc(sizeof(LakeSym));
sym->base.type = TYPE_SYM; sym->base.type = TYPE_SYM;
sym->base.size = sizeof(LakeSym); sym->base.size = sizeof(LakeSym);
return sym; return sym;
@ -33,7 +33,7 @@ LakeSym *sym_intern(char *s)
if (!sym) { if (!sym) {
sym = sym_alloc(); sym = sym_alloc();
sym->n = strlen(s); sym->n = strlen(s);
sym->s = strdup(s); sym->s = g_strdup(s);
sym->hash = g_str_hash(s); sym->hash = g_str_hash(s);
g_hash_table_insert(symbols, sym->s, sym); g_hash_table_insert(symbols, sym->s, sym);
} }
@ -52,7 +52,7 @@ LakeSym *sym_from_str(LakeStr *str)
char *sym_repr(LakeSym *sym) char *sym_repr(LakeSym *sym)
{ {
return strdup(sym->s); return g_strdup(sym->s);
} }
unsigned long sym_val(LakeSym *sym) unsigned long sym_val(LakeSym *sym)
@ -62,5 +62,5 @@ unsigned long sym_val(LakeSym *sym)
LakeSym *sym_eq(LakeSym *a, LakeSym *b) LakeSym *sym_eq(LakeSym *a, LakeSym *b)
{ {
return bool_from_int(strncmp(a->s, b->s, MIN(a->n, b->n)) == 0); return bool_from_int(g_strcmp0(a->s, b->s) == 0);
} }