From ca10456d3a0bc7414331e990c3dfeed2207a0b32 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Mon, 25 May 2015 18:13:52 +0000 Subject: [PATCH] more compiler code --- stix/lib/comp.c | 65 ++++++++++++++++++++++++++++----------------- stix/lib/main.c | 1 + stix/lib/stix-prv.h | 1 + stix/lib/stix.h | 21 ++++++++------- 4 files changed, 55 insertions(+), 33 deletions(-) diff --git a/stix/lib/comp.c b/stix/lib/comp.c index 1daaef0..bde642e 100644 --- a/stix/lib/comp.c +++ b/stix/lib/comp.c @@ -1834,28 +1834,38 @@ static stix_ssize_t find_class_level_variable (stix_t* stix, int index, const st stix_ucs_t hs; /* TODO: find it in other types of variables */ +/* THIS FUNCTION IS NOT COMPLETE */ pos = find_word_in_string(&stix->c->_class.vars[index], name); - if (pos <= -1) + super = stix->c->_class.super_oop; + if (pos >= 0) goto done; + + while (super != stix->_nil) { - super = stix->c->_class.super_oop; - while (super != stix->_nil) - { - v = ((stix_class_t*)super)->instvars; - hs.ptr = v->slot; - hs.len = STIX_OBJ_GET_SIZE(v); + STIX_ASSERT (STIX_CLASSOF(stix, super) == stix->_class); - pos = find_word_in_string(&hs, name); - if (pos <= -1) - { + v = ((stix_class_t*)super)->instvars; +/* TODO: v = ((stix_class_t*)super)->classvars; */ + hs.ptr = v->slot; + hs.len = STIX_OBJ_GET_SIZE(v); - - } - - super = ((stix_class_t*)super)->superclass; - } + pos = find_word_in_string(&hs, name); + super = ((stix_class_t*)super)->superclass; + if (pos >= 0) goto done; } return -1; + +done: + if (super != stix->_nil) + { + stix_oow_t spec; + + STIX_ASSERT (STIX_CLASSOF(stix, super) == stix->_class); + spec = STIX_OOP_TO_SMINT(((stix_class_t*)super)->spec); + pos += STIX_CLASS_SPEC_NAMED_INSTVAR(spec); + } + + return pos; } static int append_function_name (stix_t* stix, const stix_ucs_t* name) @@ -2592,24 +2602,23 @@ static int compile_stream (stix_t* stix) } else if (is_token_ksym(stix, KSYM_CLASS)) { + /* #class Selfclass(Superclass) { } */ GET_TOKEN (stix); if (compile_class_definition(stix) <= -1) return -1; } -/* +#if 0 else if (is_token_ksym(stix, KSYM_MAIN)) { + /* #main */ + /* TODO: implement this */ + } -*/ +#endif else { - /* TODO: error */ - stix_size_t i; - printf ("%d [", stix->c->tok.type); - for (i = 0; i < stix->c->tok.name.len; i++) - printf ("%c", stix->c->tok.name.ptr[i]); - printf ("]\n"); - GET_TOKEN (stix); + set_syntax_error(stix, STIX_SYNERR_DIRECTIVE, &stix->c->tok.loc, &stix->c->tok.name); + return -1; } } @@ -2622,6 +2631,7 @@ static void gc_compiler (stix_t* stix) { if (stix->c->_class.self_oop) stix->c->_class.self_oop = stix_moveoop (stix, stix->c->_class.self_oop); + if (stix->c->_class.super_oop) stix->c->_class.super_oop = stix_moveoop (stix, stix->c->_class.super_oop); } @@ -2631,6 +2641,8 @@ static void fini_compiler (stix_t* stix) { if (stix->c) { + stix_size_t i; + clear_io_names (stix); if (stix->c->tok.name.ptr) stix_freemem (stix, stix->c->tok.name.ptr); @@ -2638,6 +2650,11 @@ static void fini_compiler (stix_t* stix) if (stix->c->_class.name.ptr) stix_freemem (stix, stix->c->_class.name.ptr); if (stix->c->_class.supername.ptr) stix_freemem (stix, stix->c->_class.supername.ptr); + for (i = 0; i < STIX_COUNTOF(stix->c->_class.vars); i++) + { + if (stix->c->_class.vars[i].ptr) stix_freemem (stix, stix->c->_class.vars[i].ptr); + } + if (stix->c->fun.tmprs.ptr) stix_freemem (stix, stix->c->fun.tmprs.ptr); if (stix->c->fun.name.ptr) stix_freemem (stix, stix->c->fun.name.ptr); if (stix->c->fun.code.ptr) stix_freemem (stix, stix->c->fun.code.ptr); diff --git a/stix/lib/main.c b/stix/lib/main.c index 553c791..afd1a4a 100644 --- a/stix/lib/main.c +++ b/stix/lib/main.c @@ -201,6 +201,7 @@ static char* syntax_error_msg[] = "identifier expected", "integer expected", "primitive: expected", + "wrong directive", "wrong class modifier", "undefined class", "duplicate class", diff --git a/stix/lib/stix-prv.h b/stix/lib/stix-prv.h index c974333..e95776c 100644 --- a/stix/lib/stix-prv.h +++ b/stix/lib/stix-prv.h @@ -268,6 +268,7 @@ enum stix_synerrnum_t STIX_SYNERR_IDENT, /* identifier expected */ STIX_SYNERR_INTEGER, /* integer expected */ STIX_SYNERR_PRIMITIVE, /* primitive: expected */ + STIX_SYNERR_DIRECTIVE, /* wrong directive */ STIX_SYNERR_CLASSMOD, /* wrong class modifier */ STIX_SYNERR_CLASSUNDEF, /* undefined class */ STIX_SYNERR_CLASSDUP, /* duplicate class */ diff --git a/stix/lib/stix.h b/stix/lib/stix.h index 9e3241c..f359f4e 100644 --- a/stix/lib/stix.h +++ b/stix/lib/stix.h @@ -491,18 +491,21 @@ struct stix_class_t { STIX_OBJ_HEADER; - stix_oop_t spec; /* SmallInteger */ - stix_oop_t superclass; /* Another class */ - stix_oop_t subclasses; /* Array of subclasses */ - stix_oop_char_t name; /* Symbol */ - stix_oop_char_t instvars; /* String */ - stix_oop_char_t classvars; /* String */ + stix_oop_t spec; /* SmallInteger */ - stix_oop_oop_t instmthds; /* instance methods, MethodDictionary */ - stix_oop_oop_t classmthds; /* class methods, MethodDictionary */ + stix_oop_t superclass; /* Another class */ + stix_oop_t subclasses; /* Array of subclasses */ + + stix_oop_char_t name; /* Symbol */ + stix_oop_char_t instvars; /* String */ + stix_oop_char_t classvars; /* String */ + stix_oop_char_t classinstvars; /* String */ + + stix_oop_oop_t instfuns; /* instance methods, MethodDictionary */ + stix_oop_oop_t classfuns; /* class methods, MethodDictionary */ /* indexed part afterwards */ - stix_oop_t classvar[1]; /* most classes have no class variables. better to be 0 */ + stix_oop_t classvar[1]; /* most classes have no class variables. better to be 0 */ }; typedef struct stix_class_t stix_class_t; typedef struct stix_class_t* stix_oop_class_t;