parse comments as values

This commit is contained in:
Sami Samhuri 2011-04-20 18:45:50 -07:00
parent 3015167a30
commit 412f85a73b
9 changed files with 87 additions and 9 deletions

View file

@ -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

View file

36
comment.c Normal file
View file

@ -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));
}

19
comment.h Normal file
View file

@ -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

View file

@ -15,4 +15,4 @@
LakeDottedList *dlist_make(LakeList *head, LakeVal *tail);
char *dlist_repr(LakeDottedList *dlist);
#endif
#endif

4
eval.c
View file

@ -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);

5
lake.c
View file

@ -14,6 +14,7 @@
#include <stdio.h>
#include <string.h>
#include <sys/select.h>
#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("");

13
lake.h
View file

@ -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 <stdio.h>
@ -150,7 +160,6 @@ char *repr(LakeVal *val);
#include "list.h"
#include "dlist.h"
#include "fn.h"
#include "bootstrap.h"
#include "comment.h"
#endif

15
parse.c
View file

@ -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);