add string type

This commit is contained in:
Yuichi Nishiwaki 2013-10-21 08:48:55 +09:00
parent cefa9a9885
commit 5e43fe58fe
6 changed files with 60 additions and 12 deletions

View File

@ -1,8 +1,6 @@
#ifndef PORT_H__
#define PORT_H__
#include "picrin.h"
enum pic_port_flag {
PIC_PORT_IN,
PIC_PORT_OUT,

14
include/picrin/string.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef STRING_H__
#define STRING_H__
struct pic_string {
PIC_OBJECT_HEADER
const char *str;
size_t len;
};
#define pic_str_ptr(v) ((struct pic_string *)v.u.data)
pic_value pic_str_new_cstr(pic_state *, const char *);
#endif

View File

@ -28,7 +28,8 @@ enum pic_tt {
PIC_TT_PAIR,
PIC_TT_SYMBOL,
PIC_TT_PROC,
PIC_TT_PORT
PIC_TT_PORT,
PIC_TT_STRING
};
#define PIC_OBJECT_HEADER \

View File

@ -4,6 +4,7 @@
#include "picrin/gc.h"
#include "picrin/irep.h"
#include "picrin/proc.h"
#include "picrin/string.h"
#if GC_DEBUG
# include <stdio.h>
@ -148,7 +149,15 @@ gc_mark_object(pic_state *pic, struct pic_object *obj)
case PIC_TT_PROC: {
break;
}
default:
case PIC_TT_PORT: {
break;
}
case PIC_TT_STRING: {
}
case PIC_TT_NIL:
case PIC_TT_BOOL:
case PIC_TT_FLOAT:
case PIC_TT_UNDEF:
pic_abort(pic, "logic flaw");
}
}
@ -233,16 +242,16 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj)
break;
}
case PIC_TT_PROC: {
struct pic_proc *proc;
proc = (struct pic_proc *)obj;
/* free irep */
pic_free(pic, proc->u.irep->code);
pic_free(pic, proc->u.irep);
break;
}
default:
case PIC_TT_STRING: {
pic_free(pic, (void*)((struct pic_string *)obj)->str);
break;
}
case PIC_TT_NIL:
case PIC_TT_BOOL:
case PIC_TT_FLOAT:
case PIC_TT_UNDEF:
pic_abort(pic, "logic flaw");
}
}

View File

@ -4,6 +4,7 @@
#include "picrin.h"
#include "picrin/proc.h"
#include "picrin/port.h"
#include "picrin/string.h"
static void write_pair(pic_state *pic, struct pic_pair *pair);
@ -40,6 +41,9 @@ write(pic_state *pic, pic_value obj)
case PIC_TT_PORT:
printf("#<port %p>", pic_port_ptr(obj));
break;
case PIC_TT_STRING:
printf("\"%s\"", pic_str_ptr(obj)->str);
break;
}
}

22
src/string.c Normal file
View File

@ -0,0 +1,22 @@
#include <string.h>
#include "picrin.h"
#include "picrin/string.h"
pic_value
pic_str_new_cstr(pic_state *pic, const char *cstr)
{
struct pic_string *str;
size_t len;
char *new_str;
len = strlen(cstr);
new_str = (char *)pic_alloc(pic, len + 1);
strncpy(new_str, cstr, len + 1);
str = (struct pic_string *)pic_obj_alloc(pic, sizeof(struct pic_string), PIC_TT_STRING);
str->len = len;
str->str = new_str;
return pic_obj_value(str);
}