diff --git a/Makefile b/Makefile index a41d0bd..36accb3 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC = gcc CFLAGS := -Wall -g $(shell pkg-config --cflags glib-2.0) LFLAGS := $(shell pkg-config --libs glib-2.0) OBJS = lake.o env.o int.o string.o sym.o parse.o bool.o list.o eval.o \ - symtable.o fn.o dlist.o primitive.o + symtable.o fn.o dlist.o primitive.o comment.o all: lake diff --git a/bootstrap.h b/bootstrap.h deleted file mode 100644 index e69de29..0000000 diff --git a/comment.c b/comment.c new file mode 100644 index 0000000..2e64732 --- /dev/null +++ b/comment.c @@ -0,0 +1,36 @@ +/** + * comment.c + * Lake Scheme + * + * Copyright 2011 Sami Samhuri + * MIT License + * + */ + +#include "comment.h" +#include "lake.h" + +static LakeComment *comment_alloc(void) +{ + LakeComment *comment = g_malloc(sizeof(LakeComment)); + VAL(comment)->type = TYPE_COMM; + VAL(comment)->size = sizeof(LakeComment); + return comment; +} + +LakeComment *comment_make(LakeStr *text) +{ + LakeComment *comment = comment_alloc(); + comment->text = text; + return comment; +} + +LakeComment *comment_from_c(char *text) +{ + return comment_make(str_from_c(text)); +} + +char *comment_repr(LakeComment *comment) +{ + return g_strdup(STR_S(comment->text)); +} diff --git a/comment.h b/comment.h new file mode 100644 index 0000000..9839bd7 --- /dev/null +++ b/comment.h @@ -0,0 +1,19 @@ +/** + * comment.h + * Lake Scheme + * + * Copyright 2011 Sami Samhuri + * MIT License + * + */ + +#ifndef _LAKE_COMMENT_H +#define _LAKE_COMMENT_H 1 + +#include "lake.h" + +LakeComment *comment_make(LakeStr *text); +LakeComment *comment_from_c(char *text); +char *comment_repr(LakeComment *comment); + +#endif diff --git a/dlist.h b/dlist.h index e514932..9feb0aa 100644 --- a/dlist.h +++ b/dlist.h @@ -15,4 +15,4 @@ LakeDottedList *dlist_make(LakeList *head, LakeVal *tail); char *dlist_repr(LakeDottedList *dlist); -#endif \ No newline at end of file +#endif diff --git a/eval.c b/eval.c index d21558a..8d81e4b 100644 --- a/eval.c +++ b/eval.c @@ -221,6 +221,10 @@ LakeVal *eval(Env *env, LakeVal *expr) result = NULL; break; + case TYPE_COMM: + result = NULL; + break; + case TYPE_LIST: list = LIST(expr); diff --git a/lake.c b/lake.c index 0618b7e..f8510ea 100644 --- a/lake.c +++ b/lake.c @@ -14,6 +14,7 @@ #include #include #include +#include "comment.h" #include "env.h" #include "eval.h" #include "lake.h" @@ -144,6 +145,10 @@ char *repr(LakeVal *expr) s = fn_repr(FN(expr)); break; + case TYPE_COMM: + s = comment_repr(COMM(expr)); + break; + default: printf("error: unrecognized value, type %d, size %zu bytes", expr->type, expr->size); s = g_strdup(""); diff --git a/lake.h b/lake.h index 7e278f0..e0bef3a 100644 --- a/lake.h +++ b/lake.h @@ -25,6 +25,7 @@ typedef int LakeType; #define TYPE_DLIST 6 #define TYPE_PRIM 7 #define TYPE_FN 8 +#define TYPE_COMM 9 #define VAL(x) ((LakeVal *)x) #define SYM(x) ((LakeSym *)x) @@ -35,6 +36,7 @@ typedef int LakeType; #define DLIST(x) ((LakeDottedList *)x) #define PRIM(x) ((LakePrimitive *)x) #define FN(x) ((LakeFn *)x) +#define COMM(x) ((LakeComment *)x) struct lake_val { LakeType type; @@ -135,6 +137,14 @@ struct lake_fn { }; typedef struct lake_fn LakeFn; +struct lake_comment { + LakeVal base; + LakeStr *text; +}; +typedef struct lake_comment LakeComment; + +#define COMM_TEXT(x) (x->text) + char *repr(LakeVal *val); #include @@ -150,7 +160,6 @@ char *repr(LakeVal *val); #include "list.h" #include "dlist.h" #include "fn.h" - -#include "bootstrap.h" +#include "comment.h" #endif \ No newline at end of file diff --git a/parse.c b/parse.c index 69326b4..d910ec8 100644 --- a/parse.c +++ b/parse.c @@ -299,14 +299,19 @@ static gboolean is_not_newline(char c) return !is_newline(c); } -static void parse_comment(Ctx *ctx) +static LakeVal *parse_comment(Ctx *ctx) { - g_free(parse_while(ctx, is_not_newline)); + char *text = parse_while(ctx, is_not_newline); + LakeComment *comment = comment_from_c(text); + g_free(text); + return VAL(comment); } static LakeVal *_parse_expr(Ctx *ctx) { - LakeVal *result = NULL; + maybe_spaces(ctx); + + LakeVal *result; char c = peek(ctx); /* try to parse a number, if that fails parse a symbol */ if ((c >= '0' && c <= '9') || c == '-' || c == '+') { @@ -328,10 +333,10 @@ static LakeVal *_parse_expr(Ctx *ctx) result = parse_list(ctx); } else if (c == ';') { - parse_comment(ctx); + result = parse_comment(ctx); } else if (c == PARSE_EOF) { - /* noop */ + result = NULL; } else { ERR("unexpected char '%c'", c);