commit ba021fac9388ed554322c31bb2d9c972b70d487a
parent 5cea8cb72e00d0e339b3951bc27473975a098cf6
Author: Andres Navarro <canavarro82@gmail.com>
Date: Mon, 28 Feb 2011 12:54:08 -0300
Moved empty string and special tokens to the vm state
Diffstat:
5 files changed, 39 insertions(+), 40 deletions(-)
diff --git a/src/kstate.c b/src/kstate.c
@@ -12,6 +12,8 @@
#include "klisp.h"
#include "kstate.h"
+#include "kstring.h"
+#include "kpair.h"
#include "kmem.h"
/*
@@ -55,16 +57,26 @@ klisp_State *klisp_newstate (klisp_Alloc f, void *ud) {
/* TEMP: err */
/* do nothing for now */
- K->ssize = KS_ISSIZE;
- K->stop = 0; /* stack is empty */
- K->sbuf = (TValue *)s;
+ /* initialize strings */
+ /* Empty string */
+ /* TODO: make it uncollectible */
+ K->empty_string = kstring_new_empty(K);
/* initialize tokenizer */
+
+ /* WORKAROUND: for stdin line buffering & reading of EOF */
+ K->ktok_seen_eof = false;
+
ks_tbsize(K) = KS_ITBSIZE;
ks_tbidx(K) = 0; /* buffer is empty */
ks_tbuf(K) = (char *)b;
- /* XXX: For now just hardcode it to 8 spaces tab-stop */
+ /* Special Tokens */
+ K->ktok_lparen = kcons(K, ch2tv('('), KNIL);
+ K->ktok_rparen = kcons(K, ch2tv(')'), KNIL);
+ K->ktok_dot = kcons(K, ch2tv('.'), KNIL);
+
+ /* TEMP: For now just hardcode it to 8 spaces tab-stop */
K->ktok_source_info.tab_width = 8;
K->ktok_source_info.filename = "*STDIN*";
ktok_init(K);
@@ -74,6 +86,12 @@ klisp_State *klisp_newstate (klisp_Alloc f, void *ud) {
K->shared_dict = KNIL;
/* initialize writer */
+
+ /* initialize temp stack */
+ K->ssize = KS_ISSIZE;
+ K->stop = 0; /* stack is empty */
+ K->sbuf = (TValue *)s;
+
return K;
}
diff --git a/src/kstate.h b/src/kstate.h
@@ -58,8 +58,16 @@ struct klisp_State {
FILE *curr_out;
char *filename_in;
char *filename_out;
+
+ /* Strings */
+ TValue empty_string;
/* tokenizer */
+ /* special tokens, see ktoken.c for rationale */
+ TValue ktok_lparen;
+ TValue ktok_rparen;
+ TValue ktok_dot;
+
/* WORKAROUND for repl */
bool ktok_seen_eof;
ksource_info_t ktok_source_info;
diff --git a/src/kstring.c b/src/kstring.c
@@ -11,9 +11,6 @@
#include "kstate.h"
#include "kmem.h"
-/* TEMP: for now initialized in kstate.c */
-TValue kempty_string = KINERT_;
-
/* TEMP: this is for initializing the above value, for now, from ktoken.h */
TValue kstring_new_empty(klisp_State *K)
{
@@ -37,8 +34,7 @@ TValue kstring_new(klisp_State *K, const char *buf, uint32_t size)
String *new_str;
if (size == 0) {
- assert(ttisstring(kempty_string));
- return kempty_string;
+ return K->empty_string;
}
new_str = klispM_malloc(K, sizeof(String) + size + 1);
diff --git a/src/kstring.h b/src/kstring.h
@@ -11,14 +11,12 @@
#include "kstate.h"
/* TEMP: for now all strings are mutable */
+
TValue kstring_new_empty(klisp_State *K);
TValue kstring_new(klisp_State *K, const char *buf, uint32_t size);
#define kstring_buf(tv_) (((Symbol *) ((tv_).tv.v.gc))->b)
#define kstring_size(tv_) (((Symbol *) ((tv_).tv.v.gc))->size)
-/* The only empty string */
-/* TEMP: for now initialized in ktoken.c */
-TValue kempty_string;
#define kstring_is_empty(tv_) (kstring_size(tv_) == 0)
#endif
diff --git a/src/ktoken.c b/src/ktoken.c
@@ -107,10 +107,10 @@ kcharset ktok_delimiter, ktok_extended, ktok_subsequent;
#define ktok_is_subsequent(chi_) kcharset_contains(ktok_subsequent, chi_)
/*
-** Special Tokens
-*/
-
-/*
+** Special Tokens
+**
+** TEMP: defined in kstate.h
+**
** RATIONALE:
**
** Because a pair is not a token, they can be used to represent special tokens
@@ -123,16 +123,9 @@ kcharset ktok_delimiter, ktok_extended, ktok_subsequent;
** and easily classified (with switch(chvalue(kcar(tok)))).
**
*/
-TValue ktok_lparen, ktok_rparen, ktok_dot;
void ktok_init(klisp_State *K)
{
- assert(K->curr_in != NULL);
- assert(K->filename_in != NULL);
-
- /* WORKAROUND: for stdin line buffering & reading of EOF */
- K->ktok_seen_eof = false;
-
/* Character sets */
kcharset_fill(ktok_alphabetic, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz");
@@ -148,20 +141,6 @@ void ktok_init(klisp_State *K)
kcharset_union(ktok_subsequent, ktok_alphabetic);
kcharset_union(ktok_subsequent, ktok_numeric);
kcharset_union(ktok_subsequent, ktok_extended);
-
- /* Special Tokens */
- /* TODO: make them uncollectible */
- if (!ttispair(ktok_lparen)) {
- ktok_lparen = kcons(K, ch2tv('('), KNIL);
- ktok_rparen = kcons(K, ch2tv(')'), KNIL);
- ktok_dot = kcons(K, ch2tv('.'), KNIL);
- }
-
- /* Empty string */
- /* TEMP: for now initialize empty string here */
- /* TODO: make it uncollectible */
- if (!ttisstring(kempty_string))
- kempty_string = kstring_new_empty(K);
}
/*
@@ -284,14 +263,14 @@ TValue ktok_read_token (klisp_State *K)
return KEOF;
case '(':
ktok_getc(K);
- return ktok_lparen;
+ return K->ktok_lparen;
case ')':
ktok_getc(K);
- return ktok_rparen;
+ return K->ktok_rparen;
case '.':
ktok_getc(K);
if (ktok_check_delimiter(K))
- return ktok_dot;
+ return K->ktok_dot;
else {
ktok_error(K, "no delimiter found after dot");
/* avoid warning */