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