add reader struct
This commit is contained in:
parent
bf9db30059
commit
e3fc2d5009
|
@ -94,8 +94,7 @@ typedef struct {
|
|||
xhash macros;
|
||||
pic_value libs;
|
||||
|
||||
bool rfcase;
|
||||
xhash rlabels;
|
||||
struct pic_reader *reader;
|
||||
|
||||
jmp_buf *jmp;
|
||||
struct pic_error *err;
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
/**
|
||||
* See Copyright Notice in picrin.h
|
||||
*/
|
||||
|
||||
#ifndef PICRIN_READ_H__
|
||||
#define PICRIN_READ_H__
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum pic_typecase {
|
||||
PIC_CASE_DEFAULT,
|
||||
PIC_CASE_FOLD,
|
||||
};
|
||||
|
||||
struct pic_reader {
|
||||
short typecase;
|
||||
xhash labels;
|
||||
};
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
15
src/read.c
15
src/read.c
|
@ -6,6 +6,7 @@
|
|||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include "picrin.h"
|
||||
#include "picrin/read.h"
|
||||
#include "picrin/error.h"
|
||||
#include "picrin/pair.h"
|
||||
#include "picrin/string.h"
|
||||
|
@ -134,13 +135,13 @@ read_directive(pic_state *pic, struct pic_port *port, int c)
|
|||
switch (peek(port)) {
|
||||
case 'n':
|
||||
if (expect(port, "no-fold-case")) {
|
||||
pic->rfcase = false;
|
||||
pic->reader->typecase = PIC_CASE_DEFAULT;
|
||||
return pic_undef_value();
|
||||
}
|
||||
break;
|
||||
case 'f':
|
||||
if (expect(port, "fold-case")) {
|
||||
pic->rfcase = true;
|
||||
pic->reader->typecase = PIC_CASE_FOLD;
|
||||
return pic_undef_value();
|
||||
}
|
||||
break;
|
||||
|
@ -203,7 +204,7 @@ read_symbol(pic_state *pic, struct pic_port *port, int c)
|
|||
if (len != 0) {
|
||||
c = next(port);
|
||||
}
|
||||
if (pic->rfcase) {
|
||||
if (pic->reader->typecase == PIC_CASE_FOLD) {
|
||||
c = tolower(c);
|
||||
}
|
||||
len += 1;
|
||||
|
@ -579,7 +580,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i)
|
|||
|
||||
val = pic_cons(pic, pic_none_value(), pic_none_value());
|
||||
|
||||
xh_put_int(&pic->rlabels, i, &val);
|
||||
xh_put_int(&pic->reader->labels, i, &val);
|
||||
|
||||
tmp = read(pic, port, c);
|
||||
pic_pair_ptr(val)->car = pic_car(pic, tmp);
|
||||
|
@ -602,7 +603,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i)
|
|||
|
||||
val = pic_obj_value(pic_vec_new(pic, 0));
|
||||
|
||||
xh_put_int(&pic->rlabels, i, &val);
|
||||
xh_put_int(&pic->reader->labels, i, &val);
|
||||
|
||||
tmp = pic_vec_ptr(read(pic, port, c));
|
||||
SWAP(pic_value *, tmp->data, pic_vec_ptr(val)->data);
|
||||
|
@ -617,7 +618,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i)
|
|||
{
|
||||
val = read(pic, port, c);
|
||||
|
||||
xh_put_int(&pic->rlabels, i, &val);
|
||||
xh_put_int(&pic->reader->labels, i, &val);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
@ -631,7 +632,7 @@ read_label_ref(pic_state *pic, struct pic_port *port, int i)
|
|||
|
||||
UNUSED(port);
|
||||
|
||||
e = xh_get_int(&pic->rlabels, i);
|
||||
e = xh_get_int(&pic->reader->labels, i);
|
||||
if (! e) {
|
||||
read_error(pic, "label of given index not defined");
|
||||
}
|
||||
|
|
11
src/state.c
11
src/state.c
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include "picrin.h"
|
||||
#include "picrin/gc.h"
|
||||
#include "picrin/read.h"
|
||||
#include "picrin/proc.h"
|
||||
#include "picrin/macro.h"
|
||||
#include "picrin/cont.h"
|
||||
|
@ -59,8 +60,9 @@ pic_open(int argc, char *argv[], char **envp)
|
|||
pic->lib = NULL;
|
||||
|
||||
/* reader */
|
||||
pic->rfcase = false;
|
||||
xh_init_int(&pic->rlabels, sizeof(pic_value));
|
||||
pic->reader = malloc(sizeof(struct pic_reader));
|
||||
pic->reader->typecase = PIC_CASE_DEFAULT;
|
||||
xh_init_int(&pic->reader->labels, sizeof(pic_value));
|
||||
|
||||
/* error handling */
|
||||
pic->jmp = NULL;
|
||||
|
@ -177,12 +179,15 @@ pic_close(pic_state *pic)
|
|||
free(pic->stbase);
|
||||
free(pic->cibase);
|
||||
|
||||
/* free reader struct */
|
||||
xh_destroy(&pic->reader->labels);
|
||||
free(pic->reader);
|
||||
|
||||
/* free global stacks */
|
||||
free(pic->try_jmps);
|
||||
xh_destroy(&pic->syms);
|
||||
xh_destroy(&pic->globals);
|
||||
xh_destroy(&pic->macros);
|
||||
xh_destroy(&pic->rlabels);
|
||||
|
||||
/* free GC arena */
|
||||
free(pic->arena);
|
||||
|
|
Loading…
Reference in New Issue