added some experimental code for namespace handling

This commit is contained in:
hyunghwan.chung 2015-07-12 17:14:21 +00:00
parent f2ec2d5fd2
commit f97eb3d72b
7 changed files with 117 additions and 40 deletions

View File

@ -241,6 +241,10 @@
{ {
} }
#class Namespace(Set)
{
}
#class MethodDictionary(Dictionary) #class MethodDictionary(Dictionary)
{ {

View File

@ -37,6 +37,7 @@
/* initial method dictionary size */ /* initial method dictionary size */
#define INSTANCE_METHOD_DICTIONARY_SIZE 256 /* TODO: choose the right size */ #define INSTANCE_METHOD_DICTIONARY_SIZE 256 /* TODO: choose the right size */
#define CLASS_METHOD_DICTIONARY_SIZE 128 /* TODO: choose the right size */ #define CLASS_METHOD_DICTIONARY_SIZE 128 /* TODO: choose the right size */
#define NAMESPACE_SIZE 128 /* TODO: choose the right size */
enum class_mod_t enum class_mod_t
{ {
@ -4313,63 +4314,105 @@ static int compile_class_definition (stix_t* stix, int extend)
return n; return n;
} }
static int add_namespace (stix_t* stix, stix_oop_set_t dic, const stix_ucs_t* name)
{
stix_size_t tmp_count = 0;
stix_oop_t sym;
stix_oop_set_t ns;
stix_pushtmp (stix, (stix_oop_t*)&dic); tmp_count++;
sym = stix_makesymbol (stix, name->ptr, name->len);
if (!sym) goto oops;
stix_pushtmp (stix, &sym); tmp_count++;
ns = stix_makedic (stix, stix->_namespace, NAMESPACE_SIZE);
if (!ns) goto oops;
/*stix_pushtmp (stix, &ns); tmp_count++;*/
if (!stix_putatdic (stix, dic, sym, (stix_oop_t)ns)) goto oops;
stix_poptmps (stix, tmp_count);
return 0;
oops:
stix_poptmps (stix, tmp_count);
return -1;
}
static int compile_namespace (stix_t* stix) static int compile_namespace (stix_t* stix)
{ {
/* the name sapce path must not contain any space between segments const stix_uch_t* ptr, * dot;
* #namespace Abc.Def .Hij stix_size_t len;
* Abc.Def comprises a valid name space. a period before Hij acts stix_ucs_t seg;
* as a terminator for the namespace declaration. Hij itself causes stix_oop_set_t dic;
* a syntax error */ stix_oop_association_t ass;
stix_iotok_t seg_tok; if (stix->c->tok.type != STIX_IOTOK_IDENT &&
stix->c->tok.type != STIX_IOTOK_IDENT_DOTTED)
if (stix->c->tok.type != STIX_IOTOK_IDENT)
{ {
set_syntax_error (stix, STIX_SYNERR_IDENT, &stix->c->tok.loc, &stix->c->tok.name); set_syntax_error (stix, STIX_SYNERR_IDENT, &stix->c->tok.loc, &stix->c->tok.name);
return -1; return -1;
} }
seg_tok = stix->c->tok; /* TODO: handle namespace attribute like dupok or missingok. #namespace(dupok) XXX. */
/*clone_nameseg_segment ().*/ dic = stix->sysdic;
ptr = stix->c->tok.name.ptr;
GET_TOKEN (stix); len = stix->c->tok.name.len;
while (stix->c->tok.type == STIX_IOTOK_PERIOD) while (1)
{ {
GET_TOKEN (stix); seg.ptr = (stix_uch_t*)ptr;
if (stix->c->tok.type != STIX_IOTOK_IDENT) goto ok;
if (stix->c->tok.loc.line == seg_tok.loc.line && dot = stix_findchar (ptr, len, '.');
stix->c->tok.loc.colm == seg_tok.loc.colm + seg_tok.name.len + 1) if (dot)
{ {
stix_oop_association_t ass; seg.len = dot - ptr;
ass = stix_lookupsysdic (stix, &seg_tok.name); ass = stix_lookupdic (stix, dic, &seg);
if (ass == STIX_NULL || STIX_CLASSOF(stix, ass->value) != stix->_namespace) if (ass && (STIX_CLASSOF(stix, ass->value) == stix->_namespace ||
(seg.ptr == stix->c->tok.name.ptr && ass->value == (stix_oop_t)stix->sysdic)))
{ {
/* unknown name space */ /* ok */
/*set_syntax_error (stix, STIX_SYNERR_UNKNOW*/ dic = (stix_oop_set_t)ass->value;
return -1;
}
seg_tok = stix->c->tok;
GET_TOKEN (stix);
} }
else else
{ {
goto ok; set_syntax_error (stix, STIX_SYNERR_NAMESPACE, &stix->c->tok.loc, &stix->c->tok.name);
return -1;
} }
} }
else
{
/* this is the last segment. */
seg.len = len;
ass = stix_lookupdic (stix, dic, &seg);
if (ass)
{
/* duplicate name space or conflicting name */
set_syntax_error (stix, STIX_SYNERR_NAMESPACEDUP, &stix->c->tok.loc, &stix->c->tok.name);
return -1;
}
if (add_namespace (stix, dic, &seg) <= -1) return -1;
break;
}
ptr = dot + 1;
len -= seg.len + 1;
}
GET_TOKEN (stix);
if (stix->c->tok.type != STIX_IOTOK_PERIOD)
{
set_syntax_error (stix, STIX_SYNERR_PERIOD, &stix->c->tok.loc, &stix->c->tok.name); set_syntax_error (stix, STIX_SYNERR_PERIOD, &stix->c->tok.loc, &stix->c->tok.name);
return -1; return -1;
}
ok: GET_TOKEN (stix);
/*
name = stix_makesymbol (stix, seg_tok.name);
ns = stix_instantiate (stix, stix->_namespace, XXXXXXXXXXX);
stix_putatdic (stix, dic, name, ns);
*/
return 0; return 0;
} }

View File

@ -226,6 +226,8 @@ static int ignite_3 (stix_t* stix)
{ 12, { 'S','m','a','l','l','I','n','t','e','g','e','r' } } { 12, { 'S','m','a','l','l','I','n','t','e','g','e','r' } }
}; };
static stix_uch_t stix_str[] = { 'S','t','a','x' };
stix_oow_t i; stix_oow_t i;
stix_oop_t sym; stix_oop_t sym;
stix_oop_t* stix_ptr; stix_oop_t* stix_ptr;
@ -243,6 +245,10 @@ static int ignite_3 (stix_t* stix)
stix_ptr++; stix_ptr++;
} }
sym = stix_makesymbol (stix, stix_str, 4);
if (!sym) return -1;
if (!stix_putatsysdic(stix, sym, (stix_oop_t)stix->sysdic)) return -1;
return 0; return 0;
} }

View File

@ -214,7 +214,9 @@ static char* syntax_error_msg[] =
"too many block arguments", "too many block arguments",
"too large block", "too large block",
"wrong primitive number", "wrong primitive number",
"#include error" "#include error",
"wrong namespace name",
"duplicate namespace name"
}; };
stix_uch_t str_stix[] = { 'S', 't', 'i', 'x' }; stix_uch_t str_stix[] = { 'S', 't', 'i', 'x' };

View File

@ -328,7 +328,9 @@ enum stix_synerrnum_t
STIX_SYNERR_BLKARGFLOOD, /* too many block arguments */ STIX_SYNERR_BLKARGFLOOD, /* too many block arguments */
STIX_SYNERR_BLKFLOOD, /* too large block */ STIX_SYNERR_BLKFLOOD, /* too large block */
STIX_SYNERR_PRIMNO, /* wrong primitive number */ STIX_SYNERR_PRIMNO, /* wrong primitive number */
STIX_SYNERR_INCLUDE /* #include error */ STIX_SYNERR_INCLUDE, /* #include error */
STIX_SYNERR_NAMESPACE, /* wrong namespace name */
STIX_SYNERR_NAMESPACEDUP /* duplicate namespace name */
}; };
typedef enum stix_synerrnum_t stix_synerrnum_t; typedef enum stix_synerrnum_t stix_synerrnum_t;
@ -845,6 +847,12 @@ void stix_copychars (
stix_size_t len stix_size_t len
); );
stix_uch_t* stix_findchar (
const stix_uch_t* ptr,
stix_size_t len,
stix_uch_t c
);
/* ========================================================================= */ /* ========================================================================= */
/* gc.c */ /* gc.c */
/* ========================================================================= */ /* ========================================================================= */

View File

@ -217,6 +217,20 @@ void stix_copychars (stix_uch_t* dst, const stix_uch_t* src, stix_size_t len)
for (i = 0; i < len; i++) dst[i] = src[i]; for (i = 0; i < len; i++) dst[i] = src[i];
} }
stix_uch_t* stix_findchar (const stix_uch_t* ptr, stix_size_t len, stix_uch_t c)
{
const stix_uch_t* end;
end = ptr + len;
while (ptr < end)
{
if (*ptr == c) return (stix_uch_t*)ptr;
ptr++;
}
return STIX_NULL;
}
void* stix_allocmem (stix_t* stix, stix_size_t size) void* stix_allocmem (stix_t* stix, stix_size_t size)
{ {
void* ptr; void* ptr;