2015-05-15 14:55:12 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
Copyright (c) 2014-2015 Chung, Hyung-Hwan. All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stix-prv.h"
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
#define TOKEN_NAME_ALIGN 256
|
2015-05-25 17:10:49 +00:00
|
|
|
#define CLASS_BUFFER_ALIGN 8 /* 256 */
|
2015-05-19 15:16:18 +00:00
|
|
|
|
2015-05-15 14:55:12 +00:00
|
|
|
#if 0
|
|
|
|
|
|
|
|
enum stix_send_target_t
|
|
|
|
{
|
|
|
|
STIX_SELF = 0,
|
|
|
|
STIX_SUPER = 1
|
|
|
|
};
|
|
|
|
typedef enum stix_send_target_t stix_send_target_t;
|
|
|
|
|
|
|
|
enum stix_stack_operand_t
|
|
|
|
{
|
|
|
|
STIX_RECEIVER_VARIABLE = 0,
|
|
|
|
STIX_TEMPORARY_LOCATION = 1,
|
|
|
|
STIX_LITERAL_CONSTANT = 2,
|
|
|
|
STIX_LITERAL_VARIABLE = 3
|
|
|
|
};
|
|
|
|
typedef enum stix_stack_operand_t stix_stack_operand_t;
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-21 17:07:55 +00:00
|
|
|
|
2015-05-22 15:09:45 +00:00
|
|
|
enum class_mod_t
|
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
CLASS_INDEXED = (1 << 0),
|
|
|
|
CLASS_EXTENDED = (1 << 1)
|
2015-05-22 15:09:45 +00:00
|
|
|
};
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
enum mth_mod_t
|
2015-05-22 15:09:45 +00:00
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
MTH_CLASS = (1 << 0)
|
2015-05-22 15:09:45 +00:00
|
|
|
};
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
enum var_type_t
|
|
|
|
{
|
|
|
|
VAR_INSTANCE,
|
|
|
|
VAR_CLASS,
|
|
|
|
VAR_CLASSINST
|
|
|
|
};
|
|
|
|
typedef enum var_type_t var_type_t;
|
|
|
|
|
|
|
|
struct var_info_t
|
|
|
|
{
|
|
|
|
var_type_t type;
|
|
|
|
stix_size_t pos;
|
|
|
|
stix_oop_class_t cls; /* useful if type is VAR_CLASS. note STIX_NULL indicates the self class. */
|
|
|
|
};
|
|
|
|
typedef struct var_info_t var_info_t;
|
|
|
|
|
2015-05-21 17:07:55 +00:00
|
|
|
static struct ksym_t
|
|
|
|
{
|
|
|
|
stix_oow_t len;
|
|
|
|
stix_uch_t str[11];
|
|
|
|
} ksyms[] = {
|
|
|
|
{ 4, { 'b','y','t','e' } },
|
|
|
|
{ 9, { 'c','h','a','r','a','c','t','e','r' } },
|
|
|
|
{ 5, { 'c','l','a','s','s' } },
|
|
|
|
{ 9, { 'c','l','a','s','s','i','n','s','t' } },
|
|
|
|
{ 3, { 'd','c','l' } },
|
|
|
|
{ 7, { 'd','e','c','l','a','r','e' } },
|
|
|
|
{ 5, { 'f','a','l','s','e' } },
|
|
|
|
{ 7, { 'i','n','c','l','u','d','e' } },
|
|
|
|
{ 4, { 'm','a','i','n' } },
|
2015-05-27 17:16:50 +00:00
|
|
|
{ 6, { 'm','e','t','h','o','d' } },
|
|
|
|
{ 3, { 'm','t','h' } },
|
2015-05-25 17:10:49 +00:00
|
|
|
{ 3, { 'n','i','l' } },
|
2015-05-21 17:07:55 +00:00
|
|
|
{ 7, { 'p','o','i','n','t','e','r' } },
|
2015-05-22 15:09:45 +00:00
|
|
|
{ 10, { 'p','r','i','m','i','t','i','v','e',':' } },
|
2015-05-21 17:07:55 +00:00
|
|
|
{ 4, { 's','e','l','f' } },
|
|
|
|
{ 5, { 's','u','p','e','r' } },
|
|
|
|
{ 11, { 't','h','i','s','C','o','n','t','e','x','t' } },
|
|
|
|
{ 4, { 't','r','u','e' } },
|
2015-05-22 15:09:45 +00:00
|
|
|
{ 4, { 'w','o','r','d' } },
|
|
|
|
{ 1, { '|' } },
|
|
|
|
{ 1, { '>' } },
|
|
|
|
{ 1, { '<' } }
|
2015-05-21 17:07:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum ksym_id_t
|
|
|
|
{
|
|
|
|
KSYM_BYTE,
|
|
|
|
KSYM_CHARACTER,
|
|
|
|
KSYM_CLASS,
|
|
|
|
KSYM_CLASSINST,
|
|
|
|
KSYM_DCL,
|
|
|
|
KSYM_DECLARE,
|
|
|
|
KSYM_FALSE,
|
|
|
|
KSYM_INCLUDE,
|
|
|
|
KSYM_MAIN,
|
2015-05-27 17:16:50 +00:00
|
|
|
KSYM_METHOD,
|
|
|
|
KSYM_MTH,
|
2015-05-25 17:10:49 +00:00
|
|
|
KSYM_NIL,
|
2015-05-21 17:07:55 +00:00
|
|
|
KSYM_POINTER,
|
2015-05-22 15:09:45 +00:00
|
|
|
KSYM_PRIMITIVE_COLON,
|
2015-05-21 17:07:55 +00:00
|
|
|
KSYM_SELF,
|
|
|
|
KSYM_SUPER,
|
|
|
|
KSYM_THIS_CONTEXT,
|
|
|
|
KSYM_TRUE,
|
2015-05-22 15:09:45 +00:00
|
|
|
KSYM_WORD,
|
|
|
|
|
|
|
|
KSYM_VBAR,
|
|
|
|
KSYM_GT,
|
|
|
|
KSYM_LT
|
2015-05-21 17:07:55 +00:00
|
|
|
};
|
|
|
|
typedef enum ksym_id_t ksym_id_t;
|
|
|
|
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static STIX_INLINE int is_spacechar (stix_uci_t c)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
/* TODO: handle other space unicode characters */
|
|
|
|
switch (c)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
case ' ':
|
|
|
|
case '\f': /* formfeed */
|
|
|
|
case '\n': /* linefeed */
|
|
|
|
case '\r': /* carriage return */
|
|
|
|
case '\t': /* horizon tab */
|
|
|
|
case '\v': /* vertical tab */
|
|
|
|
return 1;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static STIX_INLINE int is_alphachar (stix_uci_t c)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
/* TODO: support full unicode */
|
|
|
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
|
|
|
|
}
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static STIX_INLINE int is_digitchar (stix_uci_t c)
|
|
|
|
{
|
|
|
|
/* TODO: support full unicode */
|
|
|
|
return (c >= '0' && c <= '9');
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static STIX_INLINE int is_alnumchar (stix_uci_t c)
|
|
|
|
{
|
|
|
|
/* TODO: support full unicode */
|
|
|
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9');
|
|
|
|
}
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static STIX_INLINE int is_binselchar (stix_uci_t c)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
/*
|
2015-05-20 14:27:47 +00:00
|
|
|
* binary-selector-character :=
|
2015-05-15 14:55:12 +00:00
|
|
|
* '!' | '%' | '&' | '*' | '+' | ',' |
|
|
|
|
* '/' | '<' | '>' | '=' | '?' | '@' |
|
|
|
|
* '\' | '~' | '|' | '-'
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (c)
|
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
case '!':
|
|
|
|
case '%':
|
|
|
|
case '&':
|
|
|
|
case '*':
|
|
|
|
case '+':
|
|
|
|
case ',':
|
|
|
|
case '/':
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case '=':
|
|
|
|
case '?':
|
|
|
|
case '@':
|
|
|
|
case '\\':
|
|
|
|
case '|':
|
|
|
|
case '~':
|
|
|
|
case '-':
|
2015-05-15 14:55:12 +00:00
|
|
|
return 1;
|
2015-05-19 15:16:18 +00:00
|
|
|
|
2015-05-15 14:55:12 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static STIX_INLINE int is_closing_char (stix_uci_t c)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case '.':
|
|
|
|
case ']':
|
|
|
|
case ')':
|
|
|
|
case ';':
|
|
|
|
case '\"':
|
|
|
|
case '\'':
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
|
|
|
|
2015-05-25 17:10:49 +00:00
|
|
|
static STIX_INLINE int does_token_name_match (stix_t* stix, ksym_id_t id)
|
|
|
|
{
|
|
|
|
return stix->c->tok.name.len == ksyms[id].len &&
|
|
|
|
stix_equalchars(stix->c->tok.name.ptr, ksyms[id].str, ksyms[id].len);
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
static STIX_INLINE int is_token_symbol (stix_t* stix, ksym_id_t id)
|
2015-05-25 17:10:49 +00:00
|
|
|
{
|
|
|
|
return stix->c->tok.type == STIX_IOTOK_SYMLIT && does_token_name_match(stix, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static STIX_INLINE int is_token_ident (stix_t* stix, ksym_id_t id)
|
|
|
|
{
|
|
|
|
return stix->c->tok.type == STIX_IOTOK_IDENT && does_token_name_match(stix, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static STIX_INLINE int is_token_binsel (stix_t* stix, ksym_id_t id)
|
|
|
|
{
|
|
|
|
return stix->c->tok.type == STIX_IOTOK_BINSEL && does_token_name_match(stix, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static STIX_INLINE int is_token_keyword (stix_t* stix, ksym_id_t id)
|
|
|
|
{
|
|
|
|
return stix->c->tok.type == STIX_IOTOK_KEYWORD && does_token_name_match(stix, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int begin_include (stix_t* fsc);
|
|
|
|
static int end_include (stix_t* fsc);
|
|
|
|
|
|
|
|
static void set_syntax_error (stix_t* stix, stix_synerrnum_t num, const stix_ioloc_t* loc, const stix_ucs_t* tgt)
|
|
|
|
{
|
|
|
|
stix->errnum = STIX_ESYNTAX;
|
|
|
|
stix->c->synerr.num = num;
|
|
|
|
stix->c->synerr.loc = loc? *loc: stix->c->tok.loc;
|
|
|
|
if (tgt) stix->c->synerr.tgt = *tgt;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stix->c->synerr.tgt.ptr = STIX_NULL;
|
|
|
|
stix->c->synerr.tgt.len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int copy_string_to (stix_t* stix, const stix_ucs_t* src, stix_ucs_t* dst, stix_size_t* dst_capa, int append, stix_uch_t append_delim)
|
|
|
|
{
|
|
|
|
stix_size_t len, pos;
|
|
|
|
|
|
|
|
if (append)
|
|
|
|
{
|
|
|
|
pos = dst->len;
|
|
|
|
len = dst->len + src->len;
|
|
|
|
if (append_delim != '\0') len++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos = 0;
|
|
|
|
len = src->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > *dst_capa)
|
|
|
|
{
|
|
|
|
stix_uch_t* tmp;
|
|
|
|
stix_size_t capa;
|
|
|
|
|
|
|
|
capa = STIX_ALIGN(len, CLASS_BUFFER_ALIGN);
|
|
|
|
|
|
|
|
tmp = stix_reallocmem (stix, dst->ptr, STIX_SIZEOF(*tmp) * capa);
|
|
|
|
if (!tmp) return -1;
|
|
|
|
|
|
|
|
dst->ptr = tmp;
|
|
|
|
*dst_capa = capa;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (append && append_delim) dst->ptr[pos++] = append_delim;
|
|
|
|
stix_copychars (&dst->ptr[pos], src->ptr, src->len);
|
|
|
|
dst->len = len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static stix_ssize_t find_word_in_string (const stix_ucs_t* haystack, const stix_ucs_t* name)
|
|
|
|
{
|
|
|
|
/* this function is inefficient. but considering the typical number
|
|
|
|
* of arguments and temporary variables, the inefficiency can be
|
|
|
|
* ignored in my opinion. the overhead to maintain the reverse lookup
|
|
|
|
* table from a name to an index should be greater than this simple
|
|
|
|
* inefficient lookup */
|
|
|
|
|
|
|
|
stix_uch_t* t, * e;
|
|
|
|
stix_ssize_t index;
|
|
|
|
stix_size_t i;
|
|
|
|
|
|
|
|
t = haystack->ptr;
|
|
|
|
e = t + haystack->len;
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
while (t < e)
|
|
|
|
{
|
|
|
|
while (t < e && is_spacechar(*t)) t++;
|
|
|
|
|
|
|
|
for (i = 0; i < name->len; i++)
|
|
|
|
{
|
|
|
|
if (t >= e || name->ptr[i] != *t) goto unmatched;
|
|
|
|
t++;
|
|
|
|
}
|
|
|
|
if (t >= e || is_spacechar(*t)) return index;
|
|
|
|
|
|
|
|
unmatched:
|
|
|
|
while (t < e)
|
|
|
|
{
|
|
|
|
if (is_spacechar(*t))
|
|
|
|
{
|
|
|
|
t++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
t++;
|
|
|
|
}
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------
|
|
|
|
* Tokenizer
|
|
|
|
* --------------------------------------------------------------------- */
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
#define GET_CHAR(stix) \
|
|
|
|
do { if (get_char(stix) <= -1) return -1; } while (0)
|
|
|
|
|
|
|
|
#define GET_CHAR_TO(stix,c) \
|
|
|
|
do { \
|
|
|
|
if (get_char(stix) <= -1) return -1; \
|
|
|
|
c = (stix)->c->lxc.c; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
|
|
|
#define GET_TOKEN(stix) \
|
|
|
|
do { if (get_token(stix) <= -1) return -1; } while (0)
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
#define ADD_TOKEN_STR(stix,s,l) \
|
|
|
|
do { if (add_token_str(stix, s, l) <= -1) return -1; } while (0)
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
#define ADD_TOKEN_CHAR(stix,c) \
|
|
|
|
do { if (add_token_char(stix, c) <= -1) return -1; } while (0)
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static STIX_INLINE int add_token_str (stix_t* stix, const stix_uch_t* ptr, stix_size_t len)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-25 17:10:49 +00:00
|
|
|
stix_ucs_t tmp;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-25 17:10:49 +00:00
|
|
|
tmp.ptr = (stix_uch_t*)ptr;
|
|
|
|
tmp.len = len;
|
|
|
|
return copy_string_to (stix, &tmp, &stix->c->tok.name, &stix->c->tok.name_capa, 1, '\0');
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
|
|
|
|
static STIX_INLINE int add_token_char (stix_t* stix, stix_uch_t c)
|
|
|
|
{
|
2015-05-25 17:10:49 +00:00
|
|
|
stix_ucs_t tmp;
|
|
|
|
|
|
|
|
tmp.ptr = &c;
|
|
|
|
tmp.len = 1;
|
|
|
|
return copy_string_to (stix, &tmp, &stix->c->tok.name, &stix->c->tok.name_capa, 1, '\0');
|
2015-05-19 15:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static STIX_INLINE void unget_char (stix_t* stix, const stix_iolxc_t* c)
|
|
|
|
{
|
|
|
|
/* Make sure that the unget buffer is large enough */
|
|
|
|
STIX_ASSERT (stix->c->nungots < STIX_COUNTOF(stix->c->ungot));
|
|
|
|
stix->c->ungot[stix->c->nungots++] = *c;
|
|
|
|
}
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-17 05:02:30 +00:00
|
|
|
static int get_char (stix_t* stix)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
stix_ssize_t n;
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
if (stix->c->nungots > 0)
|
|
|
|
{
|
|
|
|
/* something in the unget buffer */
|
|
|
|
stix->c->lxc = stix->c->ungot[--stix->c->nungots];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-17 05:02:30 +00:00
|
|
|
if (stix->c->curinp->b.pos >= stix->c->curinp->b.len)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-17 05:02:30 +00:00
|
|
|
n = stix->c->impl (stix, STIX_IO_READ, stix->c->curinp);
|
2015-05-15 14:55:12 +00:00
|
|
|
if (n <= -1) return -1;
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
stix->c->curinp->lxc.c = STIX_UCI_EOF;
|
|
|
|
stix->c->curinp->lxc.l.line = stix->c->curinp->line;
|
|
|
|
stix->c->curinp->lxc.l.colm = stix->c->curinp->colm;
|
|
|
|
stix->c->curinp->lxc.l.file = stix->c->curinp->name;
|
2015-05-17 05:02:30 +00:00
|
|
|
stix->c->lxc = stix->c->curinp->lxc;
|
2015-05-19 15:16:18 +00:00
|
|
|
|
|
|
|
/* indicate that EOF has been read. lxc.c is also set to EOF. */
|
|
|
|
return 0;
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
|
|
|
|
2015-05-17 05:02:30 +00:00
|
|
|
stix->c->curinp->b.pos = 0;
|
|
|
|
stix->c->curinp->b.len = n;
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
if (stix->c->curinp->lxc.c == STIX_UCI_NL)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
/* if the previous charater was a newline,
|
|
|
|
* increment the line counter and reset column to 1.
|
|
|
|
* incrementing it line number here instead of
|
|
|
|
* updating inp->lxc causes the line number for
|
|
|
|
* TOK_EOF to be the same line as the lxc newline. */
|
2015-05-17 05:02:30 +00:00
|
|
|
stix->c->curinp->line++;
|
|
|
|
stix->c->curinp->colm = 1;
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
2015-05-17 05:02:30 +00:00
|
|
|
|
|
|
|
stix->c->curinp->lxc.c = stix->c->curinp->buf[stix->c->curinp->b.pos++];
|
2015-05-19 15:16:18 +00:00
|
|
|
stix->c->curinp->lxc.l.line = stix->c->curinp->line;
|
|
|
|
stix->c->curinp->lxc.l.colm = stix->c->curinp->colm++;
|
|
|
|
stix->c->curinp->lxc.l.file = stix->c->curinp->name;
|
2015-05-17 05:02:30 +00:00
|
|
|
stix->c->lxc = stix->c->curinp->lxc;
|
2015-05-19 15:16:18 +00:00
|
|
|
|
2015-05-17 05:02:30 +00:00
|
|
|
return 1; /* indicate that a normal character has been read */
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static int skip_spaces (stix_t* stix)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
while (is_spacechar(stix->c->curinp->lxc.c)) GET_CHAR (stix);
|
2015-05-15 14:55:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static int skip_comment (stix_t* stix)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
stix_uci_t c = stix->c->lxc.c;
|
|
|
|
stix_iolxc_t lc;
|
|
|
|
|
|
|
|
if (c == '"')
|
|
|
|
{
|
|
|
|
/* skip up to the closing " */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
GET_CHAR_TO (stix, c);
|
|
|
|
if (c == STIX_UCI_EOF)
|
|
|
|
{
|
|
|
|
/* unterminated comment */
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_CMTNC, &stix->c->lxc.l, STIX_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (c != '"');
|
|
|
|
|
|
|
|
if (c == '"') GET_CHAR (stix);
|
|
|
|
return 1; /* double-quoted comment */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle #! or ## */
|
|
|
|
if (c != '#') return 0; /* not a comment */
|
|
|
|
|
|
|
|
/* save the last character */
|
|
|
|
lc = stix->c->lxc;
|
|
|
|
/* read a new character */
|
|
|
|
GET_CHAR_TO (stix, c);
|
|
|
|
|
|
|
|
if (c == '!' || c == '#')
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
GET_CHAR_TO (stix, c);
|
|
|
|
if (c == STIX_UCI_EOF)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (c == STIX_UCI_NL)
|
|
|
|
{
|
|
|
|
GET_CHAR (stix);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (1);
|
|
|
|
|
|
|
|
return 1; /* single line comment led by ## or #! */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unget '#' */
|
|
|
|
unget_char (stix, &stix->c->lxc);
|
|
|
|
/* restore the previous state */
|
|
|
|
stix->c->lxc = lc;
|
|
|
|
|
2015-05-15 14:55:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static int get_ident (stix_t* stix)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
/*
|
2015-05-20 14:27:47 +00:00
|
|
|
* identifier := alpha-char (alpha-char | digit-char)*
|
|
|
|
* keyword := identifier ":"
|
2015-05-15 14:55:12 +00:00
|
|
|
*/
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
stix_uci_t c = stix->c->lxc.c;
|
|
|
|
stix->c->tok.type = STIX_IOTOK_IDENT;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
ADD_TOKEN_CHAR (stix, c);
|
|
|
|
GET_CHAR (stix);
|
|
|
|
c = stix->c->lxc.c;
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
while (is_alnumchar(c));
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
if (c == ':')
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
ADD_TOKEN_CHAR (stix, c);
|
|
|
|
stix->c->tok.type = STIX_IOTOK_KEYWORD;
|
|
|
|
GET_CHAR (stix);
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
2015-05-21 17:07:55 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* handle reserved words */
|
|
|
|
if (is_token_ident(stix, KSYM_SELF))
|
|
|
|
{
|
|
|
|
stix->c->tok.type = STIX_IOTOK_SELF;
|
|
|
|
}
|
|
|
|
else if (is_token_ident(stix, KSYM_SUPER))
|
|
|
|
{
|
|
|
|
stix->c->tok.type = STIX_IOTOK_SUPER;
|
|
|
|
}
|
2015-05-25 17:10:49 +00:00
|
|
|
else if (is_token_ident(stix, KSYM_NIL))
|
|
|
|
{
|
|
|
|
stix->c->tok.type = STIX_IOTOK_NIL;
|
|
|
|
}
|
2015-05-21 17:07:55 +00:00
|
|
|
else if (is_token_ident(stix, KSYM_TRUE))
|
|
|
|
{
|
|
|
|
stix->c->tok.type = STIX_IOTOK_TRUE;
|
|
|
|
}
|
|
|
|
else if (is_token_ident(stix, KSYM_FALSE))
|
|
|
|
{
|
|
|
|
stix->c->tok.type = STIX_IOTOK_FALSE;
|
|
|
|
}
|
|
|
|
else if (is_token_ident(stix, KSYM_THIS_CONTEXT))
|
|
|
|
{
|
|
|
|
stix->c->tok.type = STIX_IOTOK_THIS_CONTEXT;
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static int get_numlit (stix_t* stix, int negated)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
/*
|
2015-05-20 14:27:47 +00:00
|
|
|
* number-literal := number | ("-" number)
|
|
|
|
* number := integer | float | scaledDecimal
|
|
|
|
* integer := decimal-integer | radix-integer
|
|
|
|
* decimal-integer := digit-char+
|
|
|
|
* radix-integer := radix-specifier "r" radix-digit+
|
|
|
|
* radix-specifier := digit-char+
|
|
|
|
* radix-digit := digit-char | upper-alpha-char
|
|
|
|
*
|
|
|
|
* float := mantissa [exponentLetter exponent]
|
|
|
|
* mantissa := digit-char+ "." digit-char+
|
|
|
|
* exponent := ['-'] decimal-integer
|
|
|
|
* exponentLetter := 'e' | 'd' | 'q'
|
|
|
|
* scaledDecimal := scaledMantissa 's' [fractionalDigits]
|
|
|
|
* scaledMantissa := decimal-integer | mantissa
|
|
|
|
* fractionalDigits := decimal-integer
|
2015-05-15 14:55:12 +00:00
|
|
|
*/
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
stix_uci_t c = stix->c->lxc.c;
|
|
|
|
stix->c->tok.type = STIX_IOTOK_NUMLIT;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-20 14:27:47 +00:00
|
|
|
/*TODO: support a complex numeric literal */
|
2015-05-15 14:55:12 +00:00
|
|
|
do
|
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
ADD_TOKEN_CHAR(stix, c);
|
|
|
|
GET_CHAR (stix);
|
|
|
|
c = stix->c->lxc.c;
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
while (is_digitchar(c));
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
/* TODO; more */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static int get_charlit (stix_t* stix)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
/*
|
2015-05-20 14:27:47 +00:00
|
|
|
* character-literal := "$" character
|
|
|
|
* character := normal-character | "'"
|
2015-05-15 14:55:12 +00:00
|
|
|
*/
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
stix_uci_t c = stix->c->lxc.c; /* even a new-line or white space would be taken */
|
|
|
|
if (c == STIX_UCI_EOF)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
set_syntax_error (stix, STIX_SYNERR_CLTNT, &stix->c->lxc.l, STIX_NULL);
|
2015-05-15 14:55:12 +00:00
|
|
|
return -1;
|
2015-05-19 15:16:18 +00:00
|
|
|
}
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
stix->c->tok.type = STIX_IOTOK_CHRLIT;
|
|
|
|
ADD_TOKEN_CHAR(stix, c);
|
|
|
|
GET_CHAR (stix);
|
2015-05-15 14:55:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static int get_strlit (stix_t* stix)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
/*
|
2015-05-20 14:27:47 +00:00
|
|
|
* string-literal := single-quote string-character* single-quote
|
|
|
|
* string-character := normal-character | (single-quote single-quote)
|
|
|
|
* single-quote := "'"
|
|
|
|
* normal-character := character-except-single-quote
|
2015-05-15 14:55:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* TODO: C-like string */
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
stix_uci_t c = stix->c->lxc.c;
|
|
|
|
stix->c->tok.type = STIX_IOTOK_STRLIT;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
ADD_TOKEN_CHAR (stix, c);
|
2015-05-20 14:27:47 +00:00
|
|
|
GET_CHAR_TO (stix, c);
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
if (c == STIX_UCI_EOF)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
/* string not closed */
|
2015-05-20 14:27:47 +00:00
|
|
|
set_syntax_error (stix, STIX_SYNERR_STRNC, &stix->c->tok.loc /*&stix->c->lxc.l*/, STIX_NULL);
|
2015-05-15 14:55:12 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
while (c != '\'');
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-20 14:27:47 +00:00
|
|
|
GET_CHAR_TO (stix, c);
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
while (c == '\'');
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static int get_binsel (stix_t* stix)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
/*
|
2015-05-20 14:27:47 +00:00
|
|
|
* binary-selector := binary-selector-character+
|
2015-05-15 14:55:12 +00:00
|
|
|
*/
|
2015-05-19 15:16:18 +00:00
|
|
|
stix_uci_t oc;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
oc = stix->c->lxc.c;
|
|
|
|
ADD_TOKEN_CHAR (stix, oc);
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
GET_CHAR (stix);
|
|
|
|
/* special case if a minus is followed by a digit immediately */
|
|
|
|
if (oc == '-' && is_digitchar(stix->c->lxc.c)) return get_numlit (stix, 1);
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
/* up to 2 characters only */
|
2015-05-19 15:16:18 +00:00
|
|
|
if (is_binselchar (stix->c->lxc.c))
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
ADD_TOKEN_CHAR (stix, stix->c->lxc.c);
|
|
|
|
GET_CHAR (stix);
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* or up to any occurrences */
|
|
|
|
/*
|
2015-05-19 15:16:18 +00:00
|
|
|
while (is_binselchar(stix->c->lxc.c))
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
ADD_TOKEN_CHAR (stix, c);
|
|
|
|
GET_CHAR (stix);
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
stix->c->tok.type = STIX_IOTOK_BINSEL;
|
2015-05-15 14:55:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static int get_token (stix_t* stix)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
stix_uci_t c;
|
|
|
|
int n;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
retry:
|
|
|
|
do
|
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
if (skip_spaces(stix) <= -1) return -1;
|
|
|
|
if ((n = skip_comment(stix)) <= -1) return -1;
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
while (n >= 1);
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
/* clear the token resetting its location */
|
2015-05-19 15:16:18 +00:00
|
|
|
stix->c->tok.type = STIX_IOTOK_EOF; /* is it correct? */
|
|
|
|
stix->c->tok.name.len = 0;
|
|
|
|
|
|
|
|
stix->c->tok.loc = stix->c->lxc.l;
|
|
|
|
c = stix->c->lxc.c;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
switch (c)
|
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
case STIX_UCI_EOF:
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
static stix_uch_t _eof_str[] = { '<', 'E', 'O', 'F', '>' };
|
2015-05-15 14:55:12 +00:00
|
|
|
int n;
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
n = end_include (stix);
|
2015-05-15 14:55:12 +00:00
|
|
|
if (n <= -1) return -1;
|
|
|
|
if (n >= 1) goto retry;
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
stix->c->tok.type = STIX_IOTOK_EOF;
|
|
|
|
ADD_TOKEN_STR(stix, _eof_str, 5);
|
2015-05-15 14:55:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
case '$': /* character literal */
|
|
|
|
GET_CHAR (stix);
|
|
|
|
if (get_charlit(stix) <= -1) return -1;
|
2015-05-15 14:55:12 +00:00
|
|
|
break;
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
case '\'': /* string literal */
|
|
|
|
GET_CHAR (stix);
|
|
|
|
if (get_strlit(stix) <= -1) return -1;
|
2015-05-15 14:55:12 +00:00
|
|
|
break;
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
case ':':
|
|
|
|
stix->c->tok.type = STIX_IOTOK_COLON;
|
|
|
|
ADD_TOKEN_CHAR(stix, c);
|
|
|
|
GET_CHAR (stix);
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
c = stix->c->lxc.c;
|
|
|
|
if (c == '=')
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
stix->c->tok.type = STIX_IOTOK_ASSIGN;
|
|
|
|
ADD_TOKEN_CHAR(stix, c);
|
|
|
|
GET_CHAR (stix);
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
case '^':
|
|
|
|
stix->c->tok.type = STIX_IOTOK_RETURN;
|
|
|
|
goto single_char_token;
|
|
|
|
case '{': /* extension */
|
|
|
|
stix->c->tok.type = STIX_IOTOK_LBRACE;
|
|
|
|
goto single_char_token;
|
|
|
|
case '}': /* extension */
|
|
|
|
stix->c->tok.type = STIX_IOTOK_RBRACE;
|
|
|
|
goto single_char_token;
|
|
|
|
case '[':
|
|
|
|
stix->c->tok.type = STIX_IOTOK_LBRACK;
|
|
|
|
goto single_char_token;
|
|
|
|
case ']':
|
|
|
|
stix->c->tok.type = STIX_IOTOK_RBRACK;
|
|
|
|
goto single_char_token;
|
|
|
|
case '(':
|
|
|
|
stix->c->tok.type = STIX_IOTOK_LPAREN;
|
|
|
|
goto single_char_token;
|
|
|
|
case ')':
|
|
|
|
stix->c->tok.type = STIX_IOTOK_RPAREN;
|
|
|
|
goto single_char_token;
|
|
|
|
case '.':
|
|
|
|
stix->c->tok.type = STIX_IOTOK_PERIOD;
|
|
|
|
goto single_char_token;
|
|
|
|
case ';':
|
|
|
|
stix->c->tok.type = STIX_IOTOK_SEMICOLON;
|
|
|
|
goto single_char_token;
|
|
|
|
|
|
|
|
case '#':
|
2015-05-20 14:27:47 +00:00
|
|
|
/*
|
|
|
|
* The hash sign is not the part of the token name.
|
|
|
|
* ADD_TOKEN_CHAR(stix, c); */
|
2015-05-19 15:16:18 +00:00
|
|
|
GET_CHAR_TO (stix, c);
|
2015-05-15 14:55:12 +00:00
|
|
|
switch (c)
|
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
case STIX_UCI_EOF:
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_HLTNT, &stix->c->lxc.l, STIX_NULL);
|
2015-05-15 14:55:12 +00:00
|
|
|
return -1;
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
case '(':
|
2015-05-15 14:55:12 +00:00
|
|
|
/* #( */
|
2015-05-19 15:16:18 +00:00
|
|
|
ADD_TOKEN_CHAR(stix, c);
|
|
|
|
stix->c->tok.type = STIX_IOTOK_APAREN;
|
|
|
|
GET_CHAR (stix);
|
2015-05-15 14:55:12 +00:00
|
|
|
break;
|
|
|
|
|
2015-05-20 14:27:47 +00:00
|
|
|
case '[':
|
|
|
|
/* #[ - byte array literal */
|
|
|
|
ADD_TOKEN_CHAR(stix, c);
|
|
|
|
stix->c->tok.type = STIX_IOTOK_BPAREN;
|
|
|
|
GET_CHAR (stix);
|
|
|
|
break;
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
case '\'':
|
2015-05-20 14:27:47 +00:00
|
|
|
/* quoted symbol literal */
|
2015-05-19 15:16:18 +00:00
|
|
|
GET_CHAR (stix);
|
|
|
|
if (get_strlit(stix) <= -1) return -1;
|
|
|
|
stix->c->tok.type = STIX_IOTOK_SYMLIT;
|
2015-05-15 14:55:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-05-20 14:27:47 +00:00
|
|
|
/* symbol-literal := "#" symbol-body
|
|
|
|
* symbol-body := identifier | keyword+ | binary-selector | string-literal
|
|
|
|
*/
|
2015-05-19 15:16:18 +00:00
|
|
|
|
2015-05-20 14:27:47 +00:00
|
|
|
/* unquoted symbol literal */
|
2015-05-19 15:16:18 +00:00
|
|
|
if (is_binselchar(c))
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
ADD_TOKEN_CHAR (stix, c);
|
|
|
|
GET_CHAR_TO (stix, c);
|
|
|
|
}
|
|
|
|
while (is_binselchar(c));
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
else if (is_alphachar(c))
|
|
|
|
{
|
2015-05-19 16:26:52 +00:00
|
|
|
int colon_required = 0;
|
|
|
|
|
|
|
|
nextword:
|
2015-05-19 15:16:18 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
ADD_TOKEN_CHAR (stix, c);
|
|
|
|
GET_CHAR_TO (stix, c);
|
|
|
|
}
|
|
|
|
while (is_alnumchar(c));
|
|
|
|
|
|
|
|
if (c == ':')
|
|
|
|
{
|
|
|
|
ADD_TOKEN_CHAR (stix, c);
|
|
|
|
GET_CHAR_TO (stix, c);
|
|
|
|
|
2015-05-19 16:26:52 +00:00
|
|
|
if (is_alphachar(c))
|
|
|
|
{
|
|
|
|
colon_required =1;
|
|
|
|
goto nextword;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (colon_required)
|
|
|
|
{
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_CLNMS, &stix->c->lxc.l, STIX_NULL);
|
|
|
|
return -1;
|
2015-05-19 15:16:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
set_syntax_error (stix, STIX_SYNERR_HLTNT, &stix->c->lxc.l, STIX_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
stix->c->tok.type = STIX_IOTOK_SYMLIT;
|
2015-05-15 14:55:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-05-19 15:16:18 +00:00
|
|
|
if (is_alphachar(c))
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
if (get_ident (stix) <= -1) return -1;
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
else if (is_digitchar(c))
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
if (get_numlit (stix, 0) <= -1) return -1;
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
else if (is_binselchar(c))
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
/* binary selector */
|
2015-05-19 15:16:18 +00:00
|
|
|
if (get_binsel (stix) <= -1) return -1;
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
stix->c->ilchr = (stix_uch_t)c;
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_ILCHR, &stix->c->lxc.l, &stix->c->ilchr_ucs);
|
2015-05-15 14:55:12 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
2015-05-19 15:16:18 +00:00
|
|
|
|
|
|
|
single_char_token:
|
|
|
|
ADD_TOKEN_CHAR(stix, c);
|
|
|
|
GET_CHAR (stix);
|
|
|
|
break;
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
|
|
|
|
static void clear_io_names (stix_t* stix)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
stix_iolink_t* cur;
|
|
|
|
while (stix->c->io_names)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
cur = stix->c->io_names;
|
|
|
|
stix->c->io_names = cur->link;
|
|
|
|
stix_freemem (stix, cur);
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static const stix_uch_t* add_io_name (stix_t* stix, const stix_ucs_t* name)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
stix_iolink_t* link;
|
|
|
|
stix_uch_t* ptr;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
link = (stix_iolink_t*) stix_callocmem (stix, STIX_SIZEOF(*link) + STIX_SIZEOF(stix_uch_t) * (name->len + 1));
|
|
|
|
if (!link) return STIX_NULL;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
ptr = (stix_uch_t*)(link + 1);
|
2015-05-21 17:07:55 +00:00
|
|
|
|
|
|
|
stix_copychars (ptr, name->ptr, name->len);
|
|
|
|
ptr[name->len] = '\0';
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
link->link = stix->c->io_names;
|
|
|
|
stix->c->io_names = link;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static int begin_include (stix_t* stix)
|
|
|
|
{
|
|
|
|
stix_ioarg_t* arg;
|
|
|
|
const stix_uch_t* io_name;
|
|
|
|
|
|
|
|
io_name = add_io_name (stix, &stix->c->tok.name);
|
2015-05-20 14:27:47 +00:00
|
|
|
if (!io_name) return -1;
|
2015-05-19 15:16:18 +00:00
|
|
|
|
|
|
|
arg = (stix_ioarg_t*) stix_callocmem (stix, STIX_SIZEOF(*arg));
|
|
|
|
if (!arg) goto oops;
|
|
|
|
|
|
|
|
arg->name = io_name;
|
2015-05-15 14:55:12 +00:00
|
|
|
arg->line = 1;
|
|
|
|
arg->colm = 1;
|
2015-05-19 15:16:18 +00:00
|
|
|
arg->includer = stix->c->curinp;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
if (stix->c->impl (stix, STIX_IO_OPEN, arg) <= -1) goto oops;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
stix->c->curinp = arg;
|
|
|
|
/* stix->c->depth.incl++; */
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
/* read in the first character in the included file.
|
|
|
|
* so the next call to get_token() sees the character read
|
|
|
|
* from this file. */
|
2015-05-19 15:16:18 +00:00
|
|
|
if (get_char(stix) <= -1 || get_token(stix) <= -1)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
end_include (stix);
|
2015-05-15 14:55:12 +00:00
|
|
|
/* i don't jump to oops since i've called
|
2015-05-19 15:16:18 +00:00
|
|
|
* end_include() where stix->c->curinp/arg is freed. */
|
2015-05-15 14:55:12 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
oops:
|
2015-05-19 15:16:18 +00:00
|
|
|
if (arg) stix_freemem (stix, arg);
|
2015-05-15 14:55:12 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static int end_include (stix_t* stix)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
int x;
|
|
|
|
stix_ioarg_t* cur;
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
if (stix->c->curinp == &stix->c->arg) return 0; /* no include */
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
/* if it is an included file, close it and
|
|
|
|
* retry to read a character from an outer file */
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
x = stix->c->impl (stix, STIX_IO_CLOSE, stix->c->curinp);
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
/* if closing has failed, still destroy the
|
|
|
|
* sio structure first as normal and return
|
|
|
|
* the failure below. this way, the caller
|
2015-05-19 15:16:18 +00:00
|
|
|
* does not call STIX_IO_CLOSE on
|
|
|
|
* stix->c->curinp again. */
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
cur = stix->c->curinp;
|
|
|
|
stix->c->curinp = stix->c->curinp->includer;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
STIX_ASSERT (cur->name != STIX_NULL);
|
2015-05-19 15:16:18 +00:00
|
|
|
stix_freemem (stix, cur);
|
|
|
|
/* stix->parse.depth.incl--; */
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
if (x != 0)
|
|
|
|
{
|
|
|
|
/* the failure mentioned above is returned here */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
stix->c->lxc = stix->c->curinp->lxc;
|
2015-05-15 14:55:12 +00:00
|
|
|
return 1; /* ended the included file successfully */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-20 14:27:47 +00:00
|
|
|
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
#if 0
|
2015-05-15 14:55:12 +00:00
|
|
|
/* ---------------------------------------------------------------------
|
|
|
|
* Parser and Code Generator
|
|
|
|
* --------------------------------------------------------------------- */
|
|
|
|
#if 0
|
|
|
|
|
|
|
|
#define EMIT_CODE_TEST(fsc,high,low) \
|
2015-05-22 15:09:45 +00:00
|
|
|
do { if (emit_code_test(fsc,high,low) <= -1) return -1; } while (0)
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
#define EMIT_PUSH_RECEIVER_VARIABLE(fsc,pos) \
|
|
|
|
do { \
|
|
|
|
if (emit_stack_positional ( \
|
2015-05-22 15:09:45 +00:00
|
|
|
fsc, PUSH_RECEIVER_VARIABLE, pos) <= -1) return -1; \
|
2015-05-15 14:55:12 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define EMIT_PUSH_TEMPORARY_LOCATION(fsc,pos) \
|
|
|
|
do { \
|
|
|
|
if (emit_stack_positional ( \
|
2015-05-22 15:09:45 +00:00
|
|
|
fsc, PUSH_TEMPORARY_LOCATION, pos) <= -1) return -1; \
|
2015-05-15 14:55:12 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define EMIT_PUSH_LITERAL_CONSTANT(fsc,pos) \
|
|
|
|
do { \
|
|
|
|
if (emit_stack_positional ( \
|
2015-05-22 15:09:45 +00:00
|
|
|
fsc, PUSH_LITERAL_CONSTANT, pos) <= -1) return -1; \
|
2015-05-15 14:55:12 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
|
|
#define EMIT_PUSH_LITERAL_VARIABLE(fsc,pos) \
|
|
|
|
do { \
|
|
|
|
if (emit_stack_positional ( \
|
2015-05-22 15:09:45 +00:00
|
|
|
fsc, PUSH_LITERAL_VARIABLE, pos) <= -1) return -1; \
|
2015-05-15 14:55:12 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define EMIT_STORE_RECEIVER_VARIABLE(fsc,pos) \
|
|
|
|
do { \
|
|
|
|
if (emit_stack_positional ( \
|
2015-05-22 15:09:45 +00:00
|
|
|
fsc, STORE_RECEIVER_VARIABLE, pos) <= -1) return -1; \
|
2015-05-19 15:16:18 +00:00
|
|
|
} while (0)
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
#define EMIT_STORE_TEMPORARY_LOCATION(fsc,pos) \
|
|
|
|
do { \
|
|
|
|
if (emit_stack_positional ( \
|
2015-05-22 15:09:45 +00:00
|
|
|
fsc, STORE_TEMPORARY_LOCATION, pos) <= -1) return -1; \
|
2015-05-15 14:55:12 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
|
|
#define EMIT_POP_STACK_TOP(fsc) EMIT_CODE(fsc, POP_STACK_TOP)
|
|
|
|
#define EMIT_DUPLICATE_STACK_TOP(fsc) EMIT_CODE(fsc, DUPLICATE_STACK_TOP)
|
|
|
|
#define EMIT_PUSH_ACTIVE_CONTEXT(fsc) EMIT_CODE(fsc, PUSH_ACTIVE_CONTEXT)
|
|
|
|
#define EMIT_RETURN_FROM_MESSAGE(fsc) EMIT_CODE(fsc, RETURN_FROM_MESSAGE)
|
|
|
|
#define EMIT_RETURN_FROM_BLOCK(fsc) EMIT_CODE(fsc, RETURN_FROM_BLOCK)
|
|
|
|
|
|
|
|
#define EMIT_SEND_TO_SELF(fsc,nargs,selector) \
|
|
|
|
do { \
|
2015-05-22 15:09:45 +00:00
|
|
|
if (emit_send_to_self(fsc,nargs,selector) <= -1) return -1; \
|
2015-05-15 14:55:12 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define EMIT_SEND_TO_SUPER(fsc,nargs,selector) \
|
|
|
|
do { \
|
2015-05-22 15:09:45 +00:00
|
|
|
if (emit_send_to_super(fsc,nargs,selector) <= -1) return -1; \
|
2015-05-15 14:55:12 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define EMIT_DO_PRIMITIVE(fsc,no) \
|
2015-05-22 15:09:45 +00:00
|
|
|
do { if (emit_do_primitive(fsc,no) <= -1) return -1; } while(0)
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static STIX_INLINE int emit_code_test (
|
2015-05-17 05:02:30 +00:00
|
|
|
stix_t* fsc, const stix_uch_t* high, const stix_uch_t* low)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
wprintf (L"CODE: %s %s\n", high, low);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static STIX_INLINE int emit_code (stix_t* fsc, const stix_uint8_t* code, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if ((fsc->bcd.len + len) > STIX_COUNTOF(fsc->bcd.buf))
|
|
|
|
{
|
|
|
|
stix_seterrnum (fsc, STIX_FSC_EBCDTL, STIX_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) fsc->bcd.buf[fsc->bcd.len++] = code[i];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int emit_push_stack (stix_t* fsc, stix_stack_operand_t type, int pos)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 0-15 0000iiii Push Receiver Variable #iiii
|
|
|
|
* 16-31 0001iiii Push Temporary Location #iiii
|
|
|
|
* 32-63 001iiiii Push Literal Constant #iiiii
|
|
|
|
* 64-95 010iiiii Push Literal Variable #iiiii
|
|
|
|
* 128 10000000 jjkkkkkk Push (Receiver Variable, Temporary Location, Literal Constant, Literal Variable) [jj] #kkkkkk
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int bcds[] =
|
|
|
|
{
|
|
|
|
STIX_PUSH_RECEIVER_VARIABLE,
|
|
|
|
STIX_PUSH_TEMPORARY_LOCATION,
|
|
|
|
STIX_PUSH_LITERAL_CONSTANT,
|
|
|
|
STIX_PUSH_LITERAL_VARIABLE
|
|
|
|
};
|
|
|
|
static int bounds[] = { 0x0F, 0x0F, 0x1F, 0x1F };
|
|
|
|
|
|
|
|
stix_uint8_t code[2];
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
STIX_ASSERT (pos >= 0x0 && pos <= 0x3F); /* 0 to 63 */
|
|
|
|
STIX_ASSERT (type >= STIX_RECEIVER_VARIABLE && type <= STIX_LITERAL_VARIABLE);
|
|
|
|
|
|
|
|
if (pos <= bounds[type])
|
|
|
|
{
|
|
|
|
code[len++] = bcds[type] | pos;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
code[len++] = STIX_PUSH_EXTENDED;
|
|
|
|
code[len++] = (type << 6) | pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
return emit_code (fsc, code, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int emit_store_stack (stix_t* fsc, stix_stack_operand_t type, int pos)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 129 10000001 jjkkkkkk Store (Receiver Variable, Temporary Location, Illegal, Literal Variable) [jj] #kkkkkk
|
|
|
|
*/
|
|
|
|
|
|
|
|
stix_uint8_t code[2];
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
STIX_ASSERT (pos >= 0x0 && pos <= 0x3F); /* 0 to 63 */
|
|
|
|
STIX_ASSERT (type >= STIX_RECEIVER_VARIABLE && type <= STIX_LITERAL_VARIABLE);
|
|
|
|
|
|
|
|
code[len++] = STIX_STORE_EXTENDED;
|
|
|
|
code[len++] = (type << 6) | pos;
|
|
|
|
|
|
|
|
return emit_code (fsc, code, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int emit_pop_store_stack (stix_t* fsc, stix_stack_operand_t type, int pos)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 96-103 01100iii Pop and Store Receiver Variable #iii
|
|
|
|
* 104-111 01101iii Pop and Store Temporary Location #iii
|
|
|
|
* 129 10000001 jjkkkkkk Store (Receiver Variable, Temporary Location, Illegal, Literal Variable) [jj] #kkkkkk
|
|
|
|
* 130 10000010 jjkkkkkk Pop and Store (Receiver Variable, Temporary Location, Illegal, Literal Variable) [jj] #kkkkkk
|
|
|
|
*/
|
|
|
|
|
|
|
|
stix_uint8_t code[2];
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
static int bcds[] =
|
|
|
|
{
|
|
|
|
STIX_POP_STORE_RECEIVER_VARIABLE,
|
|
|
|
STIX_POP_STORE_TEMPORARY_LOCATION
|
|
|
|
};
|
|
|
|
|
|
|
|
STIX_ASSERT (pos >= 0x0 && pos <= 0x3F); /* 0 to 63 */
|
|
|
|
STIX_ASSERT (type >= STIX_RECEIVER_VARIABLE && type <= STIX_LITERAL_VARIABLE && type != STIX_LITERAL_CONSTANT);
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case STIX_RECEIVER_VARIABLE:
|
|
|
|
case STIX_TEMPORARY_LOCATION:
|
|
|
|
if (pos <= 0x07)
|
|
|
|
{
|
|
|
|
code[len++] = bcds[type] | pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fall through */
|
|
|
|
|
|
|
|
default:
|
|
|
|
code[len++] = STIX_POP_STORE_EXTENDED;
|
|
|
|
code[len++] = (type << 6) | pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return emit_code (fsc, code, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int emit_send_message (stix_t* fsc, stix_send_target_t target, int selector, int nargs)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 131 10000011 jjjkkkkk Send Literal Selector #kkkkk With jjj Arguments
|
|
|
|
* 132 10000100 jjjjjjjj kkkkkkkk Send Literal Selector #kkkkkkkk With jjjjjjjj Arguments
|
|
|
|
* 133 10000101 jjjkkkkk Send Literal Selector #kkkkk To Superclass With jjj Arguments
|
|
|
|
* 134 10000110 jjjjjjjj kkkkkkkk Send Literal Selector #kkkkkkkk To Superclass With jjjjjjjj Arguments
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct { int basic; int extended; } bcds[] =
|
|
|
|
{
|
|
|
|
{ STIX_SEND_TO_SELF, STIX_SEND_TO_SELF_EXTENDED },
|
|
|
|
{ STIX_SEND_TO_SUPER, STIX_SEND_TO_SUPER_EXTENDED }
|
|
|
|
};
|
|
|
|
|
|
|
|
stix_uint8_t code[3];
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
STIX_ASSERT (selector >= 0 && selector <= 0xFF);
|
|
|
|
STIX_ASSERT (nargs >= 0 && nargs <= 0xFF);
|
|
|
|
|
|
|
|
if (nargs <= 0x7 && selector <= 0x1F)
|
|
|
|
{
|
|
|
|
code[len++] = bcds[target].basic;
|
|
|
|
code[len++] = (nargs << 5) | selector;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
code[len++] = bcds[target].extended;
|
|
|
|
code[len++] = nargs;
|
|
|
|
code[len++] = selector;
|
|
|
|
}
|
|
|
|
|
|
|
|
return emit_code (fsc, code, len);
|
|
|
|
}
|
|
|
|
|
2015-05-22 15:09:45 +00:00
|
|
|
static int emit_do_primitive (stix_t* stix, int no)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
stix_uint8_t code[2];
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
STIX_ASSERT (no >= 0x0 && no <= 0xFF);
|
|
|
|
|
|
|
|
code[len++] = STIX_DO_PRIMITIVE;
|
|
|
|
code[len++] = no;
|
|
|
|
|
2015-05-22 15:09:45 +00:00
|
|
|
return emit_code (stix, code, len);
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static int __add_literal (stix_t* fsc, stix_word_t literal)
|
|
|
|
{
|
|
|
|
stix_word_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < fsc->literal_count; i++) {
|
|
|
|
/*
|
|
|
|
* it would remove redundancy of symbols and small integers.
|
|
|
|
* more complex redundacy check may be done somewhere else
|
|
|
|
* like in __add_string_literal.
|
|
|
|
*/
|
|
|
|
if (fsc->literals[i] == literal) return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fsc->literal_count >= STIX_COUNTOF(fsc->literals)) {
|
|
|
|
fsc->errnum = STIX_FSC_ERROR_TOO_MANY_LITERALS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fsc->literals[fsc->literal_count++] = literal;
|
|
|
|
return fsc->literal_count - 1;
|
|
|
|
}
|
|
|
|
|
2015-05-17 05:02:30 +00:00
|
|
|
static int __add_character_literal (stix_t* fsc, stix_uch_t ch)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
stix_word_t i, c, literal;
|
|
|
|
stix_vm_t* stx = fsc->stx;
|
|
|
|
|
|
|
|
for (i = 0; i < fsc->literal_count; i++) {
|
|
|
|
c = STIX_ISSMALLINT(fsc->literals[i])?
|
|
|
|
stx->class_smallinteger: STIX_CLASS (stx, fsc->literals[i]);
|
|
|
|
if (c != stx->class_character) continue;
|
|
|
|
|
|
|
|
if (ch == STIX_CHAR_AT(stx,fsc->literals[i],0)) return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
literal = stix_instantiate (
|
|
|
|
stx, stx->class_character, &ch, STIX_NULL, 0);
|
|
|
|
return __add_literal (fsc, literal);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __add_string_literal (
|
2015-05-17 05:02:30 +00:00
|
|
|
stix_t* fsc, const stix_uch_t* str, stix_word_t size)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
stix_word_t i, c, literal;
|
|
|
|
stix_vm_t* stx = fsc->stx;
|
|
|
|
|
|
|
|
for (i = 0; i < fsc->literal_count; i++) {
|
|
|
|
c = STIX_ISSMALLINT(fsc->literals[i])?
|
|
|
|
stx->class_smallinteger: STIX_CLASS (stx, fsc->literals[i]);
|
|
|
|
if (c != stx->class_string) continue;
|
|
|
|
|
|
|
|
if (stix_strxncmp (str, size,
|
|
|
|
STIX_DATA(stx,fsc->literals[i]),
|
|
|
|
STIX_SIZE(stx,fsc->literals[i])) == 0) return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
literal = stix_instantiate (
|
|
|
|
stx, stx->class_string, STIX_NULL, str, size);
|
|
|
|
return __add_literal (fsc, literal);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __add_symbol_literal (
|
2015-05-17 05:02:30 +00:00
|
|
|
stix_t* fsc, const stix_uch_t* str, stix_word_t size)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
stix_vm_t* stx = fsc->stx;
|
|
|
|
return __add_literal (fsc, stix_new_symbolx(stx, str, size));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int finish_method (stix_t* fsc)
|
|
|
|
{
|
|
|
|
stix_vm_t* stx = fsc->stx;
|
2015-05-26 16:31:47 +00:00
|
|
|
stix_oop_class_t class_obj;
|
2015-05-15 14:55:12 +00:00
|
|
|
stix_method_t* method_obj;
|
|
|
|
stix_word_t method, selector;
|
|
|
|
|
|
|
|
STIX_ASSERT (fsc->bcd.size != 0);
|
|
|
|
|
|
|
|
class_obj = (stix_class_t*) STIX_OBJPTR(stx, fsc->method_class);
|
|
|
|
|
|
|
|
if (class_obj->methods == stx->nil)
|
|
|
|
{
|
|
|
|
/* TODO: reconfigure method dictionary size */
|
|
|
|
class_obj->methods = stix_instantiate (
|
|
|
|
stx, stx->class_system_dictionary,
|
|
|
|
STIX_NULL, STIX_NULL, 64);
|
|
|
|
}
|
|
|
|
STIX_ASSERT (class_obj->methods != stx->nil);
|
|
|
|
|
|
|
|
selector = stix_new_symbolx (
|
|
|
|
stx, fsc->met.name.buf, fsc->method_name.size);
|
|
|
|
|
|
|
|
method = stix_instantiate(stx, stx->class_method,
|
|
|
|
STIX_NULL, fsc->literals, fsc->literal_count);
|
|
|
|
method_obj = (stix_method_t*)STIX_OBJPTR(stx, method);
|
|
|
|
|
|
|
|
/* TODO: text saving must be optional */
|
|
|
|
/*method_obj->text = stix_instantiate (
|
|
|
|
stx, stx->class_string, STIX_NULL,
|
|
|
|
fsc->text, stix_strlen(fsc->text));
|
|
|
|
*/
|
|
|
|
method_obj->selector = selector;
|
|
|
|
method_obj->bytecodes = stix_instantiate (
|
|
|
|
stx, stx->class_bytearray, STIX_NULL,
|
|
|
|
fsc->bcd.buf, fsc->bcd.size);
|
|
|
|
|
|
|
|
/* TODO: better way to store argument count & temporary count */
|
|
|
|
method_obj->tmpcount = STIX_TO_SMALLINT(fsc->met.tmpr.count - fsc->met.tmpr.nargs);
|
|
|
|
method_obj->argcount = STIX_TO_SMALLINT(fsc->met.tmpr.nargs);
|
|
|
|
|
|
|
|
stix_dict_put (stx, class_obj->methods, selector, method);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static int parse_block_statements (stix_t* fsc)
|
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
while (fsc->tok.type != STIX_IOTOK_RBRACK &&
|
2015-05-22 15:09:45 +00:00
|
|
|
fsc->tok.type != STIX_IOTOK_EOF)
|
|
|
|
{
|
|
|
|
if (parse_statement(fsc) <= -1) return -1;
|
2015-05-19 15:16:18 +00:00
|
|
|
if (fsc->tok.type != STIX_IOTOK_PERIOD) break;
|
2015-05-15 14:55:12 +00:00
|
|
|
GET_TOKEN (fsc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-22 15:09:45 +00:00
|
|
|
static int parse_basic_expression (stix_t* fsc, const stix_uch_t* ident)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
/*
|
2015-05-20 14:27:47 +00:00
|
|
|
* <basic expression> := <primary> [<messages> <cascaded messages>]
|
2015-05-15 14:55:12 +00:00
|
|
|
*/
|
|
|
|
int is_super;
|
|
|
|
|
|
|
|
if (parse_primary(fsc, ident, &is_super) == -1) return -1;
|
2015-05-22 15:09:45 +00:00
|
|
|
if (fsc->tok.type != STIX_IOTOK_EOF &&
|
|
|
|
fsc->tok.type != STIX_IOTOK_RBRACE &&
|
2015-05-19 15:16:18 +00:00
|
|
|
fsc->tok.type != STIX_IOTOK_PERIOD)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
if (parse_message_continuation(fsc, is_super) == -1) return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-22 15:09:45 +00:00
|
|
|
static int parse_assignment (stix_t* fsc, const stix_uch_t* target)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
/*
|
2015-05-20 14:27:47 +00:00
|
|
|
* <assignment> := <assignment target> assignmentOperator <expression>
|
2015-05-15 14:55:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
stix_word_t i;
|
|
|
|
stix_vm_t* stx = fsc->stx;
|
|
|
|
|
|
|
|
for (i = fsc->met.tmpr.nargs; i < fsc->met.tmpr.count; i++)
|
|
|
|
{
|
|
|
|
if (stix_strequal (target, fsc->met.tmpr.names[i]))
|
|
|
|
{
|
|
|
|
if (parse_expression(fsc) == -1) return -1;
|
|
|
|
EMIT_STORE_TEMPORARY_LOCATION (fsc, i);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stix_get_instance_variable_index (stx, fsc->method_class, target, &i) == 0)
|
|
|
|
{
|
|
|
|
if (parse_expression(fsc) == -1) return -1;
|
|
|
|
EMIT_STORE_RECEIVER_VARIABLE (fsc, i);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stix_lookup_class_variable (stx, fsc->method_class, target) != stx->nil)
|
|
|
|
{
|
|
|
|
if (parse_expression(fsc) == -1) return -1;
|
|
|
|
|
|
|
|
/* TODO */
|
|
|
|
EMIT_CODE_TEST (fsc, STIX_T("ASSIGN_CLASSVAR #"), target);
|
|
|
|
//EMIT_STORE_CLASS_VARIABLE (fsc, target);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: IMPLEMENT POOL DICTIONARIES */
|
|
|
|
|
|
|
|
/* TODO: IMPLEMENT GLOBLAS, but i don't like this idea */
|
|
|
|
|
|
|
|
fsc->errnum = STIX_FSC_ERROR_UNDECLARED_NAME;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-22 15:09:45 +00:00
|
|
|
static int parse_primary (stix_t* fsc, const stix_uch_t* ident, int* is_super)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
/*
|
2015-05-20 14:27:47 +00:00
|
|
|
* <primary> :=
|
2015-05-15 14:55:12 +00:00
|
|
|
* identifier | <literal> |
|
|
|
|
* <block constructor> | ( '('<expression>')' )
|
|
|
|
*/
|
|
|
|
|
|
|
|
stix_vm_t* stx = fsc->stx;
|
|
|
|
|
|
|
|
if (ident == STIX_NULL)
|
|
|
|
{
|
|
|
|
int pos;
|
|
|
|
stix_word_t literal;
|
|
|
|
|
|
|
|
*is_super = stix_false;
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
if (fsc->tok.type == STIX_IOTOK_IDENT)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
if (parse_primary_ident(fsc,
|
|
|
|
fsc->tok.name.buffer, is_super) == -1) return -1;
|
|
|
|
GET_TOKEN (fsc);
|
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
else if (fsc->tok.type == STIX_IOTOK_CHRLIT) {
|
2015-05-15 14:55:12 +00:00
|
|
|
pos = __add_character_literal(
|
|
|
|
fsc, fsc->tok.name.buffer[0]);
|
|
|
|
if (pos == -1) return -1;
|
|
|
|
EMIT_PUSH_LITERAL_CONSTANT (fsc, pos);
|
|
|
|
GET_TOKEN (fsc);
|
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
else if (fsc->tok.type == STIX_IOTOK_STRLIT) {
|
2015-05-15 14:55:12 +00:00
|
|
|
pos = __add_string_literal (fsc,
|
|
|
|
fsc->tok.name.buffer, fsc->tok.name.size);
|
|
|
|
if (pos == -1) return -1;
|
|
|
|
EMIT_PUSH_LITERAL_CONSTANT (fsc, pos);
|
|
|
|
GET_TOKEN (fsc);
|
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
else if (fsc->tok.type == STIX_IOTOK_NUMLIT)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
/* TODO: other types of numbers, negative numbers, etc */
|
|
|
|
stix_word_t tmp;
|
|
|
|
STIX_STRTOI (tmp, fsc->tok.name.buffer, STIX_NULL, 10);
|
|
|
|
literal = STIX_TO_SMALLINT(tmp);
|
|
|
|
pos = __add_literal(fsc, literal);
|
|
|
|
if (pos == -1) return -1;
|
|
|
|
EMIT_PUSH_LITERAL_CONSTANT (fsc, pos);
|
|
|
|
GET_TOKEN (fsc);
|
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
else if (fsc->tok.type == STIX_IOTOK_SYMLIT) {
|
2015-05-15 14:55:12 +00:00
|
|
|
pos = __add_symbol_literal (fsc,
|
|
|
|
fsc->tok.name.buffer, fsc->tok.name.size);
|
|
|
|
if (pos == -1) return -1;
|
|
|
|
EMIT_PUSH_LITERAL_CONSTANT (fsc, pos);
|
|
|
|
GET_TOKEN (fsc);
|
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
else if (fsc->tok.type == STIX_IOTOK_LBRACK) {
|
2015-05-15 14:55:12 +00:00
|
|
|
GET_TOKEN (fsc);
|
|
|
|
if (parse_block_constructor(fsc) == -1) return -1;
|
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
else if (fsc->tok.type == STIX_IOTOK_APAREN) {
|
2015-05-15 14:55:12 +00:00
|
|
|
/* TODO: array literal */
|
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
else if (fsc->tok.type == STIX_IOTOK_LPAREN) {
|
2015-05-15 14:55:12 +00:00
|
|
|
GET_TOKEN (fsc);
|
|
|
|
if (parse_expression(fsc) == -1) return -1;
|
2015-05-19 15:16:18 +00:00
|
|
|
if (fsc->tok.type != STIX_IOTOK_RPAREN) {
|
2015-05-15 14:55:12 +00:00
|
|
|
fsc->errnum = STIX_FSC_ERROR_NO_RPAREN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
GET_TOKEN (fsc);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fsc->errnum = STIX_FSC_ERROR_PRIMARY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/*if (parse_primary_ident(fsc, fsc->tok.name.buffer) == -1) return -1;*/
|
|
|
|
if (parse_primary_ident(fsc, ident, is_super) == -1) return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-22 15:09:45 +00:00
|
|
|
static int parse_primary_ident (stix_t* fsc, const stix_uch_t* ident, int* is_super)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
stix_word_t i;
|
|
|
|
stix_vm_t* stx = fsc->stx;
|
|
|
|
|
|
|
|
*is_super = stix_false;
|
|
|
|
|
|
|
|
if (stix_strequal(ident, STIX_T("self")))
|
|
|
|
{
|
|
|
|
EMIT_CODE (fsc, PUSH_RECEIVER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (stix_strequal(ident, STIX_T("super")))
|
|
|
|
{
|
|
|
|
*is_super = stix_true;
|
|
|
|
EMIT_CODE (fsc, PUSH_RECEIVER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (stix_strequal(ident, STIX_T("nil")))
|
|
|
|
{
|
|
|
|
EMIT_CODE (fsc, PUSH_NIL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (stix_strequal(ident, STIX_T("true")))
|
|
|
|
{
|
|
|
|
EMIT_CODE (fsc, PUSH_TRUE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (stix_strequal(ident, STIX_T("false")))
|
|
|
|
{
|
|
|
|
EMIT_CODE (fsc, PUSH_FALSE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Refer to parse_assignment for identifier lookup */
|
|
|
|
|
|
|
|
for (i = 0; i < fsc->met.tmpr.count; i++)
|
|
|
|
{
|
|
|
|
if (stix_strequal(ident, fsc->met.tmpr.names[i]))
|
|
|
|
{
|
|
|
|
EMIT_PUSH_TEMPORARY_LOCATION (fsc, i);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_instance_variable_index (
|
|
|
|
stx, fsc->method_class, ident, &i) == 0)
|
|
|
|
{
|
|
|
|
EMIT_PUSH_RECEIVER_VARIABLE (fsc, i);
|
|
|
|
return 0;
|
2015-05-20 14:27:47 +00:00
|
|
|
}
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
/* TODO: what is the best way to look up a class variable? */
|
|
|
|
/* 1. Use the class containing it and using its position */
|
|
|
|
/* 2. Use a primitive method after pushing the name as a symbol */
|
|
|
|
/* 3. Implement a vm instruction to do it */
|
|
|
|
/*
|
|
|
|
if (stix_lookup_class_variable (
|
|
|
|
stx, fsc->method_class, ident) != stx->nil) {
|
|
|
|
//EMIT_LOOKUP_CLASS_VARIABLE (fsc, ident);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* TODO: IMPLEMENT POOL DICTIONARIES */
|
|
|
|
|
|
|
|
/* TODO: IMPLEMENT GLOBLAS, but i don't like this idea */
|
|
|
|
|
|
|
|
fsc->errnum = STIX_FSC_ERROR_UNDECLARED_NAME;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_block_constructor (stix_t* fsc)
|
|
|
|
{
|
|
|
|
/*
|
2015-05-20 14:27:47 +00:00
|
|
|
* <block constructor> := '[' <block body> ']'
|
|
|
|
* <block body> := [<block argument>* '|']
|
2015-05-15 14:55:12 +00:00
|
|
|
* [<temporaries>] [<statements>]
|
2015-05-20 14:27:47 +00:00
|
|
|
* <block argument> := ':' identifier
|
2015-05-15 14:55:12 +00:00
|
|
|
*/
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
if (fsc->tok.type == STIX_IOTOK_COLON)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
GET_TOKEN (fsc);
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
if (fsc->tok.type != STIX_IOTOK_IDENT)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
fsc->errnum = STIX_FSC_ERROR_BLOCK_ARGUMENT_NAME;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO : store block arguments */
|
|
|
|
GET_TOKEN (fsc);
|
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
while (fsc->tok.type == STIX_IOTOK_COLON);
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
if (!is_vbar_tok(&fsc->tok))
|
|
|
|
{
|
|
|
|
fsc->errnum = STIX_FSC_ERROR_BLOCK_ARGUMENT_LIST;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (fsc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: create a block closure */
|
|
|
|
if (parse_method_temporaries(fsc) == -1) return -1;
|
|
|
|
if (parse_block_statements(fsc) == -1) return -1;
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
if (fsc->tok.type != STIX_IOTOK_RBRACK)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
fsc->errnum = STIX_FSC_ERROR_BLOCK_NOT_CLOSED;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (fsc);
|
|
|
|
|
|
|
|
/* TODO: do special treatment for block closures */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_message_continuation (
|
|
|
|
stix_t* fsc, int is_super)
|
|
|
|
{
|
|
|
|
/*
|
2015-05-20 14:27:47 +00:00
|
|
|
* <messages> :=
|
2015-05-15 14:55:12 +00:00
|
|
|
* (<unary message>+ <binary message>* [<keyword message>] ) |
|
|
|
|
* (<binary message>+ [<keyword message>] ) |
|
|
|
|
* <keyword message>
|
2015-05-20 14:27:47 +00:00
|
|
|
* <cascaded messages> := (';' <messages>)*
|
2015-05-15 14:55:12 +00:00
|
|
|
*/
|
|
|
|
if (parse_keyword_message(fsc, is_super) == -1) return -1;
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
while (fsc->tok.type == STIX_IOTOK_SEMICOLON)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
EMIT_CODE_TEST (fsc, STIX_T("DoSpecial(DUP_RECEIVER(CASCADE))"), STIX_T(""));
|
|
|
|
GET_TOKEN (fsc);
|
|
|
|
|
|
|
|
if (parse_keyword_message(fsc, stix_false) == -1) return -1;
|
|
|
|
EMIT_CODE_TEST (fsc, STIX_T("DoSpecial(POP_TOP)"), STIX_T(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_keyword_message (stix_t* fsc, int is_super)
|
|
|
|
{
|
|
|
|
/*
|
2015-05-20 14:27:47 +00:00
|
|
|
* <keyword message> := (keyword <keyword argument> )+
|
|
|
|
* <keyword argument> := <primary> <unary message>* <binary message>*
|
2015-05-15 14:55:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
stix_name_t name;
|
|
|
|
stix_word_t pos;
|
|
|
|
int is_super2;
|
|
|
|
int nargs = 0, n;
|
|
|
|
|
|
|
|
if (parse_binary_message (fsc, is_super) == -1) return -1;
|
2015-05-19 15:16:18 +00:00
|
|
|
if (fsc->tok.type != STIX_IOTOK_KEYWORD) return 0;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
if (stix_name_open(&name, 0) == STIX_NULL) {
|
|
|
|
fsc->errnum = STIX_FSC_ERROR_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (stix_name_adds(&name, fsc->tok.name.buffer) == -1)
|
|
|
|
{
|
|
|
|
fsc->errnum = STIX_FSC_ERROR_MEMORY;
|
|
|
|
stix_name_close (&name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (fsc);
|
|
|
|
if (parse_primary(fsc, STIX_NULL, &is_super2) == -1)
|
|
|
|
{
|
|
|
|
stix_name_close (&name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parse_binary_message(fsc, is_super2) == -1)
|
|
|
|
{
|
|
|
|
stix_name_close (&name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nargs++;
|
|
|
|
/* TODO: check if it has too many arguments.. */
|
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
while (fsc->tok.type == STIX_IOTOK_KEYWORD);
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
pos = __add_symbol_literal (fsc, name.buffer, name.size);
|
|
|
|
if (pos == -1)
|
|
|
|
{
|
|
|
|
stix_name_close (&name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = (is_super)? emit_send_to_super(fsc,nargs,pos):
|
|
|
|
emit_send_to_self(fsc,nargs,pos);
|
|
|
|
if (n == -1) {
|
|
|
|
stix_name_close (&name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
stix_name_close (&name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_binary_message (stix_t* fsc, int is_super)
|
|
|
|
{
|
|
|
|
/*
|
2015-05-20 14:27:47 +00:00
|
|
|
* <binary message> := binary-selector <binary argument>
|
|
|
|
* <binary argument> := <primary> <unary message>*
|
2015-05-15 14:55:12 +00:00
|
|
|
*/
|
|
|
|
stix_word_t pos;
|
|
|
|
int is_super2;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (parse_unary_message (fsc, is_super) == -1) return -1;
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
while (fsc->tok.type == STIX_IOTOK_BINSEL)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-17 05:02:30 +00:00
|
|
|
stix_uch_t* op = stix_tok_yield (&fsc->tok, 0);
|
2015-05-15 14:55:12 +00:00
|
|
|
if (op == STIX_NULL) {
|
|
|
|
fsc->errnum = STIX_FSC_ERROR_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (fsc);
|
|
|
|
if (parse_primary(fsc, STIX_NULL, &is_super2) == -1) {
|
|
|
|
stix_free (op);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parse_unary_message(fsc, is_super2) == -1) {
|
|
|
|
stix_free (op);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = __add_symbol_literal (fsc, op, stix_strlen(op));
|
|
|
|
if (pos == -1) {
|
|
|
|
stix_free (op);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = (is_super)?
|
|
|
|
emit_send_to_super(fsc,2,pos):
|
|
|
|
emit_send_to_self(fsc,2,pos);
|
|
|
|
if (n == -1) {
|
|
|
|
stix_free (op);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
stix_free (op);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_unary_message (stix_t* fsc, int is_super)
|
|
|
|
{
|
2015-05-20 14:27:47 +00:00
|
|
|
/* <unary message> := unarySelector */
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
stix_word_t pos;
|
|
|
|
int n;
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
while (fsc->tok.type == STIX_IOTOK_IDENT)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
pos = __add_symbol_literal (fsc,
|
|
|
|
fsc->tok.name.buffer, fsc->tok.name.size);
|
|
|
|
if (pos == -1) return -1;
|
|
|
|
|
|
|
|
n = (is_super)? emit_send_to_super (fsc, 0, pos):
|
|
|
|
emit_send_to_self (fsc, 0, pos);
|
|
|
|
if (n == -1) return -1;
|
|
|
|
|
|
|
|
GET_TOKEN (fsc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-05-20 15:24:37 +00:00
|
|
|
|
2015-05-25 17:10:49 +00:00
|
|
|
static STIX_INLINE int set_class_name (stix_t* stix, const stix_ucs_t* name)
|
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
return copy_string_to (stix, name, &stix->c->cls.name, &stix->c->cls.name_capa, 0, '\0');
|
2015-05-25 17:10:49 +00:00
|
|
|
}
|
2015-05-20 15:24:37 +00:00
|
|
|
|
2015-05-25 17:10:49 +00:00
|
|
|
static STIX_INLINE int set_superclass_name (stix_t* stix, const stix_ucs_t* name)
|
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
return copy_string_to (stix, name, &stix->c->cls.supername, &stix->c->cls.supername_capa, 0, '\0');
|
2015-05-25 17:10:49 +00:00
|
|
|
}
|
2015-05-20 15:24:37 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
static STIX_INLINE int append_class_level_variable (stix_t* stix, var_type_t index, const stix_ucs_t* name)
|
2015-05-25 17:10:49 +00:00
|
|
|
{
|
2015-05-26 16:31:47 +00:00
|
|
|
int n;
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
n = copy_string_to (stix, name, &stix->c->cls.vars[index], &stix->c->cls.vars_capa[index], 1, ' ');
|
2015-05-26 16:31:47 +00:00
|
|
|
|
|
|
|
if (n >= 0)
|
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->cls.var_count[index]++;
|
2015-05-28 15:20:27 +00:00
|
|
|
/* TODO: check if it exceeds STIX_MAX_NAMED_INSTVARS, STIX_MAX_CLASSVARS, STIX_MAX_CLASSINSTVARS */
|
2015-05-26 16:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
2015-05-25 17:10:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
static stix_ssize_t find_class_level_variable (stix_t* stix, stix_oop_class_t self, const stix_ucs_t* name, var_info_t* var)
|
2015-05-25 17:10:49 +00:00
|
|
|
{
|
|
|
|
stix_ssize_t pos;
|
|
|
|
stix_oop_t super;
|
|
|
|
stix_oop_char_t v;
|
2015-05-27 17:16:50 +00:00
|
|
|
stix_oop_char_t* vv;
|
2015-05-25 17:10:49 +00:00
|
|
|
stix_ucs_t hs;
|
2015-05-27 17:16:50 +00:00
|
|
|
int index;
|
|
|
|
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
STIX_ASSERT (STIX_CLASSOF(stix, self) == stix->_class);
|
|
|
|
|
|
|
|
/* NOTE the loop here assumes the right order of
|
|
|
|
* instvars
|
|
|
|
* classvars
|
|
|
|
* classinstvars
|
|
|
|
*/
|
|
|
|
vv = &self->instvars;
|
|
|
|
for (index = VAR_INSTANCE; index <= VAR_CLASSINST; index++)
|
|
|
|
{
|
|
|
|
v = vv[index];
|
|
|
|
hs.ptr = v->slot;
|
|
|
|
hs.len = STIX_OBJ_GET_SIZE(v);
|
2015-05-25 17:10:49 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
pos = find_word_in_string(&hs, name);
|
|
|
|
if (pos >= 0)
|
|
|
|
{
|
|
|
|
super = self->superclass;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
super = self->superclass;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* the class definition is not available yet */
|
|
|
|
for (index = VAR_INSTANCE; index <= VAR_CLASSINST; index++)
|
|
|
|
{
|
|
|
|
pos = find_word_in_string(&stix->c->cls.vars[index], name);
|
|
|
|
|
|
|
|
if (pos >= 0)
|
|
|
|
{
|
|
|
|
super = stix->c->cls.super_oop;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
super = stix->c->cls.super_oop;
|
|
|
|
}
|
2015-05-25 18:13:52 +00:00
|
|
|
|
|
|
|
while (super != stix->_nil)
|
2015-05-25 17:10:49 +00:00
|
|
|
{
|
2015-05-25 18:13:52 +00:00
|
|
|
STIX_ASSERT (STIX_CLASSOF(stix, super) == stix->_class);
|
2015-05-25 17:10:49 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
/* NOTE the loop here assumes the right order of
|
|
|
|
* instvars
|
|
|
|
* classvars
|
|
|
|
* classinstvars
|
|
|
|
*/
|
|
|
|
vv = &((stix_oop_class_t)super)->instvars;
|
|
|
|
for (index = VAR_INSTANCE; index <= VAR_CLASSINST; index++)
|
|
|
|
{
|
|
|
|
v = vv[index];
|
|
|
|
hs.ptr = v->slot;
|
|
|
|
hs.len = STIX_OBJ_GET_SIZE(v);
|
|
|
|
|
|
|
|
pos = find_word_in_string(&hs, name);
|
|
|
|
if (pos >= 0)
|
|
|
|
{
|
|
|
|
super = ((stix_oop_class_t)super)->superclass;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
2015-05-25 17:10:49 +00:00
|
|
|
|
2015-05-26 16:31:47 +00:00
|
|
|
super = ((stix_oop_class_t)super)->superclass;
|
2015-05-25 18:13:52 +00:00
|
|
|
}
|
2015-05-25 17:10:49 +00:00
|
|
|
|
2015-05-25 18:13:52 +00:00
|
|
|
return -1;
|
2015-05-25 17:10:49 +00:00
|
|
|
|
2015-05-25 18:13:52 +00:00
|
|
|
done:
|
2015-05-27 17:16:50 +00:00
|
|
|
/* 'self' may be STIX_NULL if STIX_NULL has been given for it.
|
|
|
|
* the caller must take good care when interpreting the meaning of
|
|
|
|
* this field */
|
|
|
|
var->cls = self;
|
|
|
|
|
2015-05-25 18:13:52 +00:00
|
|
|
if (super != stix->_nil)
|
|
|
|
{
|
|
|
|
stix_oow_t spec;
|
|
|
|
|
|
|
|
STIX_ASSERT (STIX_CLASSOF(stix, super) == stix->_class);
|
2015-05-27 17:16:50 +00:00
|
|
|
switch (index)
|
|
|
|
{
|
|
|
|
case VAR_INSTANCE:
|
|
|
|
/* each class has the number of named instance variables
|
|
|
|
* accumulated for inheritance. the position found in the
|
|
|
|
* local variable string can be adjusted by adding the
|
|
|
|
* number in the superclass */
|
|
|
|
spec = STIX_OOP_TO_SMINT(((stix_oop_class_t)super)->spec);
|
|
|
|
pos += STIX_CLASS_SPEC_NAMED_INSTVAR(spec);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VAR_CLASS:
|
|
|
|
/* no adjustment is needed.
|
|
|
|
* a class object is composed of three parts.
|
|
|
|
* fixed-part | classinst-variables | class-variabes
|
|
|
|
* the position returned here doesn't consider
|
|
|
|
* class instance variables that can be potentially
|
|
|
|
* placed before the class variables. */
|
|
|
|
var->cls = (stix_oop_class_t)super;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VAR_CLASSINST:
|
|
|
|
spec = STIX_OOP_TO_SMINT(((stix_oop_class_t)super)->selfspec);
|
|
|
|
pos += STIX_CLASS_SELFSPEC_CLASSINSTVAR(spec);
|
|
|
|
break;
|
|
|
|
}
|
2015-05-25 17:10:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
var->type = index;
|
|
|
|
var->pos = pos;
|
2015-05-25 18:13:52 +00:00
|
|
|
return pos;
|
2015-05-25 17:10:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
static int append_method_name (stix_t* stix, const stix_ucs_t* name)
|
2015-05-25 17:10:49 +00:00
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
/* method name segments are concatenated without any delimiters */
|
|
|
|
return copy_string_to (stix, name, &stix->c->mth.name, &stix->c->mth.name_capa, 1, '\0');
|
2015-05-25 17:10:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 17:41:11 +00:00
|
|
|
static stix_ssize_t find_method_name (stix_t* stix, stix_oop_class_t self, const stix_ucs_t* name)
|
|
|
|
{
|
|
|
|
/* TODO: .................... */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-25 17:10:49 +00:00
|
|
|
static int append_temporary (stix_t* stix, const stix_ucs_t* name)
|
|
|
|
{
|
|
|
|
/* temporary variable names are added to the string with leading
|
|
|
|
* space if it's not the first variable */
|
2015-05-27 17:16:50 +00:00
|
|
|
return copy_string_to (stix, name, &stix->c->mth.tmprs, &stix->c->mth.tmprs_capa, 1, ' ');
|
2015-05-25 17:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static STIX_INLINE stix_ssize_t find_temporary (stix_t* stix, const stix_ucs_t* name)
|
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
return find_word_in_string(&stix->c->mth.tmprs, name);
|
2015-05-25 17:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int compile_class_level_variables (stix_t* stix)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
var_type_t dcl_type = VAR_INSTANCE;
|
2015-05-20 15:24:37 +00:00
|
|
|
|
|
|
|
if (stix->c->tok.type == STIX_IOTOK_LPAREN)
|
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
/* process variable modifiers */
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
|
|
|
|
if (is_token_symbol(stix, KSYM_CLASS))
|
2015-05-20 15:24:37 +00:00
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
/* #dcl(#class) */
|
|
|
|
dcl_type = VAR_CLASS;
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
}
|
|
|
|
else if (is_token_symbol(stix, KSYM_CLASSINST))
|
|
|
|
{
|
|
|
|
/* #dcl(#classinst) */
|
|
|
|
dcl_type = VAR_CLASSINST;
|
2015-05-20 15:24:37 +00:00
|
|
|
GET_TOKEN (stix);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stix->c->tok.type != STIX_IOTOK_RPAREN)
|
|
|
|
{
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_RPAREN, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-05-20 15:52:45 +00:00
|
|
|
|
|
|
|
GET_TOKEN (stix);
|
2015-05-20 15:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (stix->c->tok.type == STIX_IOTOK_IDENT)
|
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
var_info_t var;
|
|
|
|
|
|
|
|
if (find_class_level_variable(stix, STIX_NULL, &stix->c->tok.name, &var) >= 0)
|
2015-05-25 17:10:49 +00:00
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
printf ("duplicate variable name type %d pos %lu\n", var.type, var.pos);
|
2015-05-25 17:10:49 +00:00
|
|
|
set_syntax_error (stix, STIX_SYNERR_VARNAMEDUP, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
if (append_class_level_variable(stix, dcl_type, &stix->c->tok.name) <= -1) return -1;
|
2015-05-20 15:24:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
}
|
|
|
|
while (1);
|
|
|
|
|
|
|
|
if (stix->c->tok.type != STIX_IOTOK_PERIOD)
|
|
|
|
{
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_PERIOD, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
static int compile_unary_method_name (stix_t* stix)
|
2015-05-22 15:09:45 +00:00
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
STIX_ASSERT (stix->c->mth.name.len == 0);
|
|
|
|
STIX_ASSERT (stix->c->mth.tmpr_nargs == 0);
|
2015-05-22 15:09:45 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
if (append_method_name (stix, &stix->c->tok.name) <= -1) return -1;
|
2015-05-22 15:09:45 +00:00
|
|
|
GET_TOKEN (stix);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
static int compile_binary_method_name (stix_t* stix)
|
2015-05-22 15:09:45 +00:00
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
STIX_ASSERT (stix->c->mth.name.len == 0);
|
|
|
|
STIX_ASSERT (stix->c->mth.tmpr_nargs == 0);
|
2015-05-22 15:09:45 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
if (append_method_name (stix, &stix->c->tok.name) <= -1) return -1;
|
2015-05-21 17:07:55 +00:00
|
|
|
GET_TOKEN (stix);
|
|
|
|
|
|
|
|
/* collect the argument name */
|
|
|
|
if (stix->c->tok.type != STIX_IOTOK_IDENT)
|
|
|
|
{
|
|
|
|
/* wrong argument name. identifier expected */
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_IDENT, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
STIX_ASSERT (stix->c->mth.tmpr_nargs == 0);
|
2015-05-21 17:07:55 +00:00
|
|
|
|
2015-05-27 17:41:11 +00:00
|
|
|
/* no duplication check is performed against class-level variable names.
|
|
|
|
* a duplcate name will shade a previsouly defined variable. */
|
2015-05-22 15:09:45 +00:00
|
|
|
if (append_temporary(stix, &stix->c->tok.name) <= -1) return -1;
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->mth.tmpr_nargs++;
|
2015-05-21 17:07:55 +00:00
|
|
|
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
static int compile_keyword_method_name (stix_t* stix)
|
2015-05-21 17:07:55 +00:00
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
STIX_ASSERT (stix->c->mth.name.len == 0);
|
|
|
|
STIX_ASSERT (stix->c->mth.tmpr_nargs == 0);
|
2015-05-21 17:07:55 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
if (append_method_name(stix, &stix->c->tok.name) <= -1) return -1;
|
2015-05-21 17:07:55 +00:00
|
|
|
|
|
|
|
GET_TOKEN (stix);
|
2015-05-22 15:09:45 +00:00
|
|
|
if (stix->c->tok.type != STIX_IOTOK_IDENT)
|
2015-05-21 17:07:55 +00:00
|
|
|
{
|
2015-05-22 15:09:45 +00:00
|
|
|
/* wrong argument name. identifier is expected */
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_IDENT, &stix->c->tok.loc, &stix->c->tok.name);
|
2015-05-21 17:07:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-22 15:09:45 +00:00
|
|
|
if (find_temporary(stix, &stix->c->tok.name) >= 0)
|
2015-05-21 17:07:55 +00:00
|
|
|
{
|
2015-05-25 17:10:49 +00:00
|
|
|
set_syntax_error (stix, STIX_SYNERR_ARGNAMEDUP, &stix->c->tok.loc, &stix->c->tok.name);
|
2015-05-21 17:07:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-22 15:09:45 +00:00
|
|
|
if (append_temporary(stix, &stix->c->tok.name) <= -1) return -1;
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->mth.tmpr_nargs++;
|
2015-05-21 17:07:55 +00:00
|
|
|
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
}
|
|
|
|
while (stix->c->tok.type == STIX_IOTOK_KEYWORD);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
static int compile_method_name (stix_t* stix)
|
2015-05-21 17:07:55 +00:00
|
|
|
{
|
|
|
|
/*
|
2015-05-27 17:16:50 +00:00
|
|
|
* method-name := unary-method-name | binary-method-name | keyword-method-name
|
|
|
|
* unary-method-name := unary-selector
|
|
|
|
* binary-method-name := binary-selector selector-argument
|
|
|
|
* keyword-method-name := (keyword selector-argument)+
|
2015-05-21 17:07:55 +00:00
|
|
|
* selector-argument := identifier
|
|
|
|
* unary-selector := identifier
|
|
|
|
*/
|
|
|
|
int n;
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
STIX_ASSERT (stix->c->mth.tmpr_count == 0);
|
2015-05-21 17:07:55 +00:00
|
|
|
|
2015-05-28 15:20:27 +00:00
|
|
|
stix->c->mth.name_loc = stix->c->tok.loc;
|
2015-05-21 17:07:55 +00:00
|
|
|
switch (stix->c->tok.type)
|
|
|
|
{
|
|
|
|
case STIX_IOTOK_IDENT:
|
2015-05-27 17:16:50 +00:00
|
|
|
n = compile_unary_method_name(stix);
|
2015-05-21 17:07:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STIX_IOTOK_BINSEL:
|
2015-05-27 17:16:50 +00:00
|
|
|
n = compile_binary_method_name(stix);
|
2015-05-21 17:07:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STIX_IOTOK_KEYWORD:
|
2015-05-27 17:16:50 +00:00
|
|
|
n = compile_keyword_method_name(stix);
|
2015-05-21 17:07:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-05-27 17:16:50 +00:00
|
|
|
/* illegal method name */
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_MTHNAME, &stix->c->tok.loc, &stix->c->tok.name);
|
2015-05-21 17:07:55 +00:00
|
|
|
n = -1;
|
|
|
|
}
|
|
|
|
|
2015-05-22 15:09:45 +00:00
|
|
|
if (n >= 0)
|
|
|
|
{
|
2015-05-27 17:41:11 +00:00
|
|
|
if (find_method_name(stix, stix->c->cls.self_oop, &stix->c->mth.name) >= 0)
|
|
|
|
{
|
2015-05-28 15:20:27 +00:00
|
|
|
set_syntax_error (stix, STIX_SYNERR_MTHNAMEDUP, &stix->c->mth.name_loc, &stix->c->mth.name);
|
2015-05-27 17:41:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-05-22 15:09:45 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 17:07:55 +00:00
|
|
|
/* the total number of temporaries is equal to the number of
|
|
|
|
* arguments after having processed the message pattern. it's because
|
|
|
|
* stix treats arguments the same as temporaries */
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->mth.tmpr_count = stix->c->mth.tmpr_nargs;
|
2015-05-21 17:07:55 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2015-05-28 15:20:27 +00:00
|
|
|
static int compile_method_temporaries (stix_t* stix)
|
2015-05-22 15:09:45 +00:00
|
|
|
{
|
|
|
|
/*
|
2015-05-27 17:16:50 +00:00
|
|
|
* method-temporaries := "|" variable-list "|"
|
2015-05-22 15:09:45 +00:00
|
|
|
* variable-list := identifier*
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!is_token_binsel(stix, KSYM_VBAR))
|
|
|
|
{
|
|
|
|
/* return without doing anything if | is not found.
|
|
|
|
* this is not an error condition */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
while (stix->c->tok.type == STIX_IOTOK_IDENT)
|
|
|
|
{
|
|
|
|
if (find_temporary(stix, &stix->c->tok.name) >= 0)
|
|
|
|
{
|
2015-05-25 17:10:49 +00:00
|
|
|
set_syntax_error (stix, STIX_SYNERR_TMPRNAMEDUP, &stix->c->tok.loc, &stix->c->tok.name);
|
2015-05-22 15:09:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (append_temporary(stix, &stix->c->tok.name) <= -1) return -1;
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->mth.tmpr_count++;
|
2015-05-22 15:09:45 +00:00
|
|
|
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_token_binsel(stix, KSYM_VBAR))
|
|
|
|
{
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_VBAR, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-28 15:20:27 +00:00
|
|
|
static int compile_method_primitive (stix_t* stix)
|
2015-05-22 15:09:45 +00:00
|
|
|
{
|
|
|
|
/*
|
2015-05-27 17:16:50 +00:00
|
|
|
* method-primitive := "<" "primitive:" integer ">"
|
2015-05-22 15:09:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!is_token_binsel(stix, KSYM_LT))
|
|
|
|
{
|
|
|
|
/* return if < is not seen. it is not an error condition */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
if (!is_token_keyword(stix, KSYM_PRIMITIVE_COLON))
|
|
|
|
{
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_PRIMITIVE, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* TODO: other modifiers?
|
|
|
|
* <primitive: 10>
|
|
|
|
* <some-other-modifier: xxxx>
|
|
|
|
*/
|
|
|
|
|
|
|
|
GET_TOKEN (stix); /* TODO: only integer */
|
|
|
|
if (stix->c->tok.type != STIX_IOTOK_NUMLIT)
|
|
|
|
{
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_INTEGER, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*TODO: more checks the validity of the primitive number */
|
|
|
|
#if 0
|
|
|
|
if (!stix_stristype(stix->tok.name.buffer, stix_isdigit))
|
|
|
|
{
|
|
|
|
stix->errnum = STIX_FSC_ERROR_PRIMITIVE_NUMBER;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
STIX_STRTOI (stix->c->mth.prim_no, stix->tok.name.buffer, STIX_NULL, 10);
|
2015-05-22 15:09:45 +00:00
|
|
|
if (prim_no < 0 || prim_no > 0xFF)
|
|
|
|
{
|
|
|
|
stix->errnum = STIX_FSC_ERROR_PRIMITIVE_NUMBER_RANGE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-25 17:10:49 +00:00
|
|
|
EMIT_DO_PRIMITIVE (stix, stix-.c->fun.prim_no);
|
2015-05-22 15:09:45 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
if (!is_token_binsel(stix, KSYM_GT))
|
|
|
|
{
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_GT, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
static int compile_class_method_expression (stix_t* stix)
|
2015-05-25 17:10:49 +00:00
|
|
|
{
|
|
|
|
/*
|
2015-05-27 17:16:50 +00:00
|
|
|
* method-expression := assignment-expression | basic-expression
|
|
|
|
* assignment-expression := identifier ":=" method-expression
|
2015-05-25 17:10:49 +00:00
|
|
|
* basic-expression := expression-primary (message cascaded-message)?
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (stix->tok.type == STIX_IOTOK_IDENT)
|
|
|
|
{
|
|
|
|
stix_uch_t* ident = stix_tok_yield (&stix->tok, 0);
|
|
|
|
if (ident == STIX_NULL)
|
|
|
|
{
|
|
|
|
stix->errnum = STIX_FSC_ERROR_MEMORY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
if (stix->tok.type == STIX_IOTOK_ASSIGN)
|
|
|
|
{
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
if (parse_assignment(stix, ident) <= -1)
|
|
|
|
{
|
|
|
|
stix_free (ident);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (parse_basic_expression(stix, ident) <= -1)
|
|
|
|
{
|
|
|
|
stix_free (ident);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-26 16:31:47 +00:00
|
|
|
stix_freemem (ident);
|
2015-05-25 17:10:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (parse_basic_expression(stix, STIX_NULL) <= -1) return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2015-05-22 15:09:45 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
static int compile_class_method_statement (stix_t* stix)
|
2015-05-22 15:09:45 +00:00
|
|
|
{
|
|
|
|
if (stix->c->tok.type == STIX_IOTOK_RETURN)
|
|
|
|
{
|
|
|
|
GET_TOKEN (stix);
|
2015-05-27 17:16:50 +00:00
|
|
|
if (compile_class_method_expression(stix) <= -1) return -1;
|
2015-05-22 15:09:45 +00:00
|
|
|
#if 0
|
|
|
|
EMIT_RETURN_FROM_MESSAGE (stix);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
if (compile_class_method_expression(stix) <= -1) return -1;
|
2015-05-22 15:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-28 15:20:27 +00:00
|
|
|
static int compile_method_statements (stix_t* stix)
|
2015-05-22 15:09:45 +00:00
|
|
|
{
|
|
|
|
/*
|
2015-05-27 17:16:50 +00:00
|
|
|
* method-statements := method-statement ("." | ("." method-statements))*
|
|
|
|
* method-statement := method-return | method-expression
|
|
|
|
* method-return := "^" method-expression
|
|
|
|
* method-expression := ...
|
2015-05-22 15:09:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (stix->c->tok.type != STIX_IOTOK_EOF &&
|
|
|
|
stix->c->tok.type != STIX_IOTOK_RBRACE)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
if (compile_class_method_statement(stix) <= -1) return -1;
|
2015-05-22 15:09:45 +00:00
|
|
|
|
|
|
|
if (stix->c->tok.type == STIX_IOTOK_PERIOD)
|
|
|
|
{
|
|
|
|
/* period after a statement */
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
if (stix->c->tok.type == STIX_IOTOK_EOF && stix->c->tok.type == STIX_IOTOK_RBRACE) break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (stix->c->tok.type == STIX_IOTOK_EOF && stix->c->tok.type == STIX_IOTOK_RBRACE) break;
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_PERIOD, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
EMIT_CODE (stix, RETURN_RECEIVER);
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
static int compile_class_method (stix_t* stix)
|
2015-05-20 15:24:37 +00:00
|
|
|
{
|
2015-05-21 17:07:55 +00:00
|
|
|
/* clear data required to compile a method */
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->mth.flags = 0;
|
|
|
|
stix->c->mth.name.len = 0;
|
2015-05-28 15:20:27 +00:00
|
|
|
STIX_MEMSET (&stix->c->mth.name_loc, 0, STIX_SIZEOF(stix->c->mth.name_loc));
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->mth.tmprs.len = 0;
|
|
|
|
stix->c->mth.tmpr_count = 0;
|
|
|
|
stix->c->mth.tmpr_nargs = 0;
|
|
|
|
stix->c->mth.code.len = 0;
|
|
|
|
|
|
|
|
if (stix->c->tok.type == STIX_IOTOK_LPAREN)
|
|
|
|
{
|
|
|
|
/* process method modifiers */
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
|
|
|
|
if (is_token_symbol(stix, KSYM_CLASS))
|
|
|
|
{
|
|
|
|
/* #method(#class) */
|
|
|
|
stix->c->mth.flags |= MTH_CLASS;
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stix->c->tok.type != STIX_IOTOK_RPAREN)
|
|
|
|
{
|
|
|
|
/* ) expected */
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_RPAREN, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-05-21 17:07:55 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
GET_TOKEN (stix);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (compile_method_name(stix) <= -1) return -1;
|
2015-05-21 17:07:55 +00:00
|
|
|
|
|
|
|
if (stix->c->tok.type != STIX_IOTOK_LBRACE)
|
|
|
|
{
|
|
|
|
/* { expected */
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_LBRACE, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
|
2015-05-28 15:20:27 +00:00
|
|
|
if (compile_method_temporaries(stix) <= -1 ||
|
|
|
|
compile_method_primitive(stix) <= -1 ||
|
|
|
|
compile_method_statements(stix) <= -1 /*||
|
2015-05-21 17:07:55 +00:00
|
|
|
finish_method(stix) <= -1*/) return -1;
|
|
|
|
|
|
|
|
if (stix->c->tok.type != STIX_IOTOK_RBRACE)
|
|
|
|
{
|
|
|
|
/* } expected */
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_RBRACE, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
|
2015-05-20 15:24:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-26 16:31:47 +00:00
|
|
|
static int make_defined_class (stix_t* stix)
|
|
|
|
{
|
|
|
|
/* this function make a class object with no functions/methods */
|
|
|
|
|
|
|
|
stix_oop_t tmp;
|
|
|
|
stix_oow_t spec, self_spec;
|
|
|
|
int just_made = 0;
|
|
|
|
|
2015-05-27 17:41:11 +00:00
|
|
|
spec = STIX_CLASS_SPEC_MAKE (stix->c->cls.var_count[VAR_INSTANCE],
|
2015-05-27 17:16:50 +00:00
|
|
|
((stix->c->cls.flags & CLASS_INDEXED)? 1: 0),
|
|
|
|
stix->c->cls.indexed_type);
|
2015-05-27 17:41:11 +00:00
|
|
|
self_spec = STIX_CLASS_SELFSPEC_MAKE(stix->c->cls.var_count[VAR_CLASS], stix->c->cls.var_count[VAR_CLASSINST]);
|
2015-05-26 16:31:47 +00:00
|
|
|
|
2015-05-28 15:20:27 +00:00
|
|
|
printf ("MAKING ... ");
|
2015-05-27 17:16:50 +00:00
|
|
|
print_ucs (&stix->c->cls.name);
|
2015-05-27 17:41:11 +00:00
|
|
|
printf (" instvars %d classvars %d classinstvars %d\n", (int)stix->c->cls.var_count[VAR_INSTANCE], (int)stix->c->cls.var_count[VAR_CLASS], (int)stix->c->cls.var_count[VAR_CLASSINST]);
|
2015-05-26 16:31:47 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
if (stix->c->cls.self_oop)
|
2015-05-26 16:31:47 +00:00
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
STIX_ASSERT (STIX_CLASSOF(stix, stix->c->cls.self_oop) == stix->_class);
|
|
|
|
STIX_ASSERT (STIX_OBJ_GET_FLAGS_KERNEL (stix->c->cls.self_oop) == 1);
|
2015-05-26 16:31:47 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
if (spec != STIX_OOP_TO_SMINT(stix->c->cls.self_oop->spec) ||
|
|
|
|
self_spec != STIX_OOP_TO_SMINT(stix->c->cls.self_oop->selfspec))
|
2015-05-26 16:31:47 +00:00
|
|
|
{
|
2015-05-27 17:41:11 +00:00
|
|
|
/* it conflicts with internal definition */
|
2015-05-28 15:20:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
printf (" CONFLICTING CLASS DEFINITION %lu %lu %lu %lu\n",
|
2015-05-26 16:31:47 +00:00
|
|
|
(unsigned long)spec, (unsigned long)self_spec,
|
2015-05-27 17:16:50 +00:00
|
|
|
(unsigned long)STIX_OOP_TO_SMINT(stix->c->cls.self_oop->spec), (unsigned long)STIX_OOP_TO_SMINT(stix->c->cls.self_oop->selfspec)
|
2015-05-26 16:31:47 +00:00
|
|
|
);
|
2015-05-28 15:20:27 +00:00
|
|
|
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_CLASSCONTRA, &stix->c->cls.name_loc, &stix->c->cls.name);
|
2015-05-26 16:31:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* the class variables and class instance variables are placed
|
|
|
|
* inside the class object after the fixed part. */
|
|
|
|
tmp = stix_instantiate (stix, stix->_class, STIX_NULL,
|
2015-05-27 17:41:11 +00:00
|
|
|
stix->c->cls.var_count[VAR_CLASSINST] + stix->c->cls.var_count[VAR_CLASS]);
|
2015-05-26 16:31:47 +00:00
|
|
|
if (!tmp) return -1;
|
|
|
|
|
|
|
|
just_made = 1;
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->cls.self_oop = (stix_oop_class_t)tmp;
|
2015-05-26 16:31:47 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
STIX_ASSERT (STIX_CLASSOF(stix, stix->c->cls.self_oop) == stix->_class);
|
2015-05-26 16:31:47 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->cls.self_oop->spec = STIX_OOP_FROM_SMINT(spec);
|
|
|
|
stix->c->cls.self_oop->selfspec = STIX_OOP_FROM_SMINT(self_spec);
|
2015-05-26 16:31:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
STIX_OBJ_SET_FLAGS_KERNEL (stix->c->cls.self_oop, 2);
|
2015-05-26 16:31:47 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
tmp = stix_makesymbol(stix, stix->c->cls.name.ptr, stix->c->cls.name.len);
|
2015-05-26 16:31:47 +00:00
|
|
|
if (!tmp) return -1;
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->cls.self_oop->name = (stix_oop_char_t)tmp;
|
2015-05-26 16:31:47 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
tmp = stix_makestring(stix, stix->c->cls.vars[0].ptr, stix->c->cls.vars[0].len);
|
2015-05-26 16:31:47 +00:00
|
|
|
if (!tmp) return -1;
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->cls.self_oop->instvars = (stix_oop_char_t)tmp;
|
2015-05-26 16:31:47 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
tmp = stix_makestring(stix, stix->c->cls.vars[1].ptr, stix->c->cls.vars[1].len);
|
2015-05-26 16:31:47 +00:00
|
|
|
if (!tmp) return -1;
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->cls.self_oop->classvars = (stix_oop_char_t)tmp;
|
2015-05-26 16:31:47 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
tmp = stix_makestring(stix, stix->c->cls.vars[2].ptr, stix->c->cls.vars[2].len);
|
2015-05-26 16:31:47 +00:00
|
|
|
if (!tmp) return -1;
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->cls.self_oop->classinstvars = (stix_oop_char_t)tmp;
|
2015-05-26 16:31:47 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
/* TODO: initialize more fields??? method_dictionary. */
|
2015-05-26 16:31:47 +00:00
|
|
|
|
|
|
|
if (just_made)
|
|
|
|
{
|
|
|
|
/* register the class to the system dictionary */
|
2015-05-27 17:16:50 +00:00
|
|
|
if (!stix_putatsysdic(stix, (stix_oop_t)stix->c->cls.self_oop->name, (stix_oop_t)stix->c->cls.self_oop)) return -1;
|
2015-05-26 16:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __compile_class_definition (stix_t* stix)
|
2015-05-20 15:24:37 +00:00
|
|
|
{
|
2015-05-22 15:09:45 +00:00
|
|
|
/*
|
|
|
|
* class-definition := #class class-modifier? "{" class-body "}"
|
2015-05-27 17:16:50 +00:00
|
|
|
* class-modifier := "(" (#byte | #character | #word | #pointer)? ")"
|
|
|
|
* class-body := variable-definition* method-definition*
|
2015-05-22 15:09:45 +00:00
|
|
|
*
|
|
|
|
* variable-definition := (#dcl | #declare) variable-modifier? variable-list "."
|
2015-05-27 17:16:50 +00:00
|
|
|
* variable-modifier := "(" (#class | #classinst)? ")"
|
2015-05-22 15:09:45 +00:00
|
|
|
* variable-list := identifier*
|
|
|
|
*
|
2015-05-27 17:16:50 +00:00
|
|
|
* method-definition := (#mth | #method) method-modifier? method-actual-definition
|
|
|
|
* method-modifier := "(" (#class | #instance)? ")"
|
|
|
|
* method-actual-definition := method-name "{" method-tempraries? method-primitive? method-statements* "}"
|
2015-05-20 15:24:37 +00:00
|
|
|
*/
|
2015-05-26 16:31:47 +00:00
|
|
|
stix_oop_association_t ass;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-20 15:24:37 +00:00
|
|
|
if (stix->c->tok.type == STIX_IOTOK_LPAREN)
|
|
|
|
{
|
|
|
|
/* process class modifiers */
|
2015-05-25 17:10:49 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
GET_TOKEN (stix);
|
2015-05-26 16:31:47 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
if (is_token_symbol(stix, KSYM_BYTE))
|
|
|
|
{
|
|
|
|
/* #class(#byte) */
|
|
|
|
stix->c->cls.flags |= CLASS_INDEXED;
|
|
|
|
stix->c->cls.indexed_type = STIX_OBJ_TYPE_BYTE;
|
2015-05-20 15:24:37 +00:00
|
|
|
GET_TOKEN (stix);
|
2015-05-27 17:16:50 +00:00
|
|
|
}
|
|
|
|
else if (is_token_symbol(stix, KSYM_CHARACTER))
|
|
|
|
{
|
|
|
|
/* #class(#character) */
|
|
|
|
stix->c->cls.flags |= CLASS_INDEXED;
|
|
|
|
stix->c->cls.indexed_type = STIX_OBJ_TYPE_CHAR;
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
}
|
|
|
|
else if (is_token_symbol(stix, KSYM_WORD))
|
|
|
|
{
|
|
|
|
/* #class(#word) */
|
|
|
|
stix->c->cls.flags |= CLASS_INDEXED;
|
|
|
|
stix->c->cls.indexed_type = STIX_OBJ_TYPE_WORD;
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
}
|
|
|
|
else if (is_token_symbol(stix, KSYM_POINTER))
|
|
|
|
{
|
|
|
|
/* #class(#pointer) */
|
|
|
|
stix->c->cls.flags |= CLASS_INDEXED;
|
|
|
|
stix->c->cls.indexed_type = STIX_OBJ_TYPE_OOP;
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
}
|
2015-05-20 15:24:37 +00:00
|
|
|
|
|
|
|
if (stix->c->tok.type != STIX_IOTOK_RPAREN)
|
|
|
|
{
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_RPAREN, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stix->c->tok.type != STIX_IOTOK_IDENT)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
|
|
|
/* class name expected. */
|
2015-05-20 15:24:37 +00:00
|
|
|
set_syntax_error (stix, STIX_SYNERR_IDENT, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-25 17:10:49 +00:00
|
|
|
/* copy the class name */
|
|
|
|
if (set_class_name(stix, &stix->c->tok.name) <= -1) return -1;
|
2015-05-28 15:20:27 +00:00
|
|
|
stix->c->cls.name_loc = stix->c->tok.loc;
|
2015-05-25 17:10:49 +00:00
|
|
|
GET_TOKEN (stix);
|
2015-05-20 15:24:37 +00:00
|
|
|
|
|
|
|
if (stix->c->tok.type == STIX_IOTOK_LPAREN)
|
|
|
|
{
|
2015-05-25 17:10:49 +00:00
|
|
|
printf ("DEFININING..\n");
|
|
|
|
{
|
|
|
|
int i;
|
2015-05-27 17:16:50 +00:00
|
|
|
for (i = 0; i < stix->c->cls.name.len; i++)
|
2015-05-25 17:10:49 +00:00
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
printf ("%c", stix->c->cls.name.ptr[i]);
|
2015-05-25 17:10:49 +00:00
|
|
|
}
|
|
|
|
printf ("\n");
|
|
|
|
}
|
|
|
|
int super_is_nil = 0;
|
|
|
|
|
2015-05-20 15:52:45 +00:00
|
|
|
/* superclass is specified. new class defintion.
|
2015-05-25 17:10:49 +00:00
|
|
|
* for example, #class Class(Stix)
|
2015-05-20 15:52:45 +00:00
|
|
|
*/
|
2015-05-25 17:10:49 +00:00
|
|
|
GET_TOKEN (stix); /* read superclass name */
|
2015-05-20 15:52:45 +00:00
|
|
|
|
|
|
|
/* TODO: multiple inheritance */
|
|
|
|
|
2015-05-25 17:10:49 +00:00
|
|
|
if (stix->c->tok.type == STIX_IOTOK_NIL)
|
|
|
|
{
|
|
|
|
super_is_nil = 1;
|
|
|
|
}
|
|
|
|
else if (stix->c->tok.type != STIX_IOTOK_IDENT)
|
2015-05-20 15:52:45 +00:00
|
|
|
{
|
|
|
|
/* superclass name expected */
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_IDENT, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-25 17:10:49 +00:00
|
|
|
if (set_superclass_name(stix, &stix->c->tok.name) <= -1) return -1;
|
2015-05-28 15:20:27 +00:00
|
|
|
stix->c->cls.supername_loc = stix->c->tok.loc;
|
2015-05-25 17:10:49 +00:00
|
|
|
|
2015-05-20 15:52:45 +00:00
|
|
|
GET_TOKEN (stix);
|
|
|
|
if (stix->c->tok.type != STIX_IOTOK_RPAREN)
|
|
|
|
{
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_RPAREN, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (stix);
|
2015-05-25 17:10:49 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
ass = (stix_oop_association_t)stix_lookupsysdic(stix, &stix->c->cls.name);
|
2015-05-26 16:31:47 +00:00
|
|
|
if (ass)
|
2015-05-25 17:10:49 +00:00
|
|
|
{
|
2015-05-26 16:31:47 +00:00
|
|
|
if (STIX_CLASSOF(stix, ass->value) != stix->_class ||
|
|
|
|
STIX_OBJ_GET_FLAGS_KERNEL(ass->value) > 1)
|
2015-05-25 17:10:49 +00:00
|
|
|
{
|
|
|
|
/* the object found with the name is not a class object
|
2015-05-26 16:31:47 +00:00
|
|
|
* or the the class object found is a fully defined kernel
|
2015-05-25 17:10:49 +00:00
|
|
|
* class object */
|
2015-05-28 15:20:27 +00:00
|
|
|
set_syntax_error (stix, STIX_SYNERR_CLASSDUP, &stix->c->cls.name_loc, &stix->c->cls.name);
|
2015-05-25 17:10:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-05-26 16:31:47 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->cls.self_oop = (stix_oop_class_t)ass->value;
|
2015-05-26 16:31:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no class of such a name is found. it's a new definition,
|
|
|
|
* which is normal for most new classes. */
|
2015-05-27 17:16:50 +00:00
|
|
|
STIX_ASSERT (stix->c->cls.self_oop == STIX_NULL);
|
2015-05-25 17:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (super_is_nil)
|
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->cls.super_oop = stix->_nil;
|
2015-05-25 17:10:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
ass = (stix_oop_association_t)stix_lookupsysdic(stix, &stix->c->cls.supername);
|
2015-05-26 16:31:47 +00:00
|
|
|
if (ass &&
|
|
|
|
STIX_CLASSOF(stix, ass->value) == stix->_class &&
|
|
|
|
STIX_OBJ_GET_FLAGS_KERNEL(ass->value) != 1)
|
|
|
|
{
|
|
|
|
/* the value found must be a class and it must not be
|
|
|
|
* an incomplete internal class object */
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->cls.super_oop = ass->value;
|
2015-05-26 16:31:47 +00:00
|
|
|
}
|
|
|
|
else
|
2015-05-25 17:10:49 +00:00
|
|
|
{
|
|
|
|
/* there is no object with such a name. or,
|
|
|
|
* the object found with the name is not a class object. or,
|
|
|
|
* the class object found is a internally defined kernel
|
|
|
|
* class object. */
|
2015-05-28 15:20:27 +00:00
|
|
|
set_syntax_error (stix, STIX_SYNERR_CLASSUNDEF, &stix->c->cls.supername_loc, &stix->c->cls.supername);
|
2015-05-25 17:10:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2015-05-20 15:52:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* extending class */
|
2015-05-27 17:16:50 +00:00
|
|
|
if (stix->c->cls.flags != 0)
|
2015-05-25 17:10:49 +00:00
|
|
|
{
|
|
|
|
/* the class definition specified with modifiers cannot extend
|
|
|
|
* an existing class. the superclass must be specified enclosed
|
|
|
|
* in parentheses. an opening parenthesis is expected to specify
|
|
|
|
* a superclass here. */
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_LPAREN, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->cls.flags |= CLASS_EXTENDED;
|
2015-05-25 17:10:49 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
ass = (stix_oop_association_t)stix_lookupsysdic(stix, &stix->c->cls.name);
|
2015-05-26 16:31:47 +00:00
|
|
|
if (ass &&
|
|
|
|
STIX_CLASSOF(stix, ass->value) != stix->_class &&
|
|
|
|
STIX_OBJ_GET_FLAGS_KERNEL(ass->value) != 1)
|
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->cls.self_oop = (stix_oop_class_t)ass->value;
|
2015-05-26 16:31:47 +00:00
|
|
|
}
|
|
|
|
else
|
2015-05-25 17:10:49 +00:00
|
|
|
{
|
|
|
|
/* only an existing class can be extended. */
|
2015-05-28 15:20:27 +00:00
|
|
|
set_syntax_error (stix, STIX_SYNERR_CLASSUNDEF, &stix->c->cls.name_loc, &stix->c->cls.name);
|
2015-05-25 17:10:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->cls.super_oop = stix->c->cls.self_oop->superclass;
|
2015-05-26 16:31:47 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
STIX_ASSERT ((stix_oop_t)stix->c->cls.super_oop == stix->_nil ||
|
|
|
|
STIX_CLASSOF(stix, stix->c->cls.super_oop) == stix->_class);
|
2015-05-20 15:24:37 +00:00
|
|
|
}
|
2015-05-20 15:52:45 +00:00
|
|
|
|
2015-05-20 15:24:37 +00:00
|
|
|
if (stix->c->tok.type != STIX_IOTOK_LBRACE)
|
|
|
|
{
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_LBRACE, &stix->c->tok.loc, &stix->c->tok.name);
|
2015-05-15 14:55:12 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
if (stix->c->cls.super_oop != stix->_nil)
|
2015-05-26 16:31:47 +00:00
|
|
|
{
|
|
|
|
/* adjust the instance variable count and the class instance variable
|
|
|
|
* count to include that of a superclass */
|
|
|
|
stix_oop_class_t c;
|
|
|
|
stix_oow_t spec, self_spec;
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
c = (stix_oop_class_t)stix->c->cls.super_oop;
|
2015-05-26 16:31:47 +00:00
|
|
|
spec = STIX_OOP_TO_SMINT(c->spec);
|
|
|
|
self_spec = STIX_OOP_TO_SMINT(c->selfspec);
|
2015-05-27 17:41:11 +00:00
|
|
|
stix->c->cls.var_count[VAR_INSTANCE] = STIX_CLASS_SPEC_NAMED_INSTVAR(spec);
|
|
|
|
stix->c->cls.var_count[VAR_CLASSINST] = STIX_CLASS_SELFSPEC_CLASSINSTVAR(self_spec);
|
2015-05-26 16:31:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 15:52:45 +00:00
|
|
|
GET_TOKEN (stix);
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
if (stix->c->cls.flags & CLASS_EXTENDED)
|
2015-05-26 16:31:47 +00:00
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
/* when a class is extended, a new variable cannot be added */
|
|
|
|
if (is_token_symbol(stix, KSYM_DCL) || is_token_symbol(stix, KSYM_DECLARE))
|
2015-05-20 15:24:37 +00:00
|
|
|
{
|
2015-05-26 16:31:47 +00:00
|
|
|
set_syntax_error (stix, STIX_SYNERR_DCLBANNED, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
2015-05-20 15:24:37 +00:00
|
|
|
}
|
2015-05-26 16:31:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* a new class including an internally defined class object */
|
2015-05-27 17:16:50 +00:00
|
|
|
while (is_token_symbol(stix, KSYM_DCL) || is_token_symbol(stix, KSYM_DECLARE))
|
2015-05-20 15:24:37 +00:00
|
|
|
{
|
2015-05-26 16:31:47 +00:00
|
|
|
/* variable definition. #dcl or #declare */
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
if (compile_class_level_variables(stix) <= -1) return -1;
|
2015-05-20 15:24:37 +00:00
|
|
|
}
|
2015-05-26 16:31:47 +00:00
|
|
|
|
|
|
|
if (make_defined_class(stix) <= -1) return -1;
|
2015-05-20 15:24:37 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
while (is_token_symbol(stix, KSYM_MTH) || is_token_symbol(stix, KSYM_METHOD))
|
2015-05-26 16:31:47 +00:00
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
/* method definition. #mth or #method */
|
2015-05-26 16:31:47 +00:00
|
|
|
GET_TOKEN (stix);
|
2015-05-27 17:16:50 +00:00
|
|
|
if (compile_class_method(stix) <= -1) return -1;
|
2015-05-26 16:31:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 15:24:37 +00:00
|
|
|
if (stix->c->tok.type != STIX_IOTOK_RBRACE)
|
|
|
|
{
|
|
|
|
set_syntax_error (stix, STIX_SYNERR_RBRACE, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
return 0;
|
2015-05-25 17:10:49 +00:00
|
|
|
}
|
2015-05-20 15:24:37 +00:00
|
|
|
|
2015-05-25 17:10:49 +00:00
|
|
|
static int compile_class_definition (stix_t* stix)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
stix_size_t i;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-25 17:10:49 +00:00
|
|
|
/* reset the structure to hold information about a class to be compiled */
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->cls.flags = 0;
|
2015-05-28 15:20:27 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->cls.name.len = 0;
|
|
|
|
stix->c->cls.supername.len = 0;
|
2015-05-28 15:20:27 +00:00
|
|
|
STIX_MEMSET (&stix->c->cls.name_loc, 0, STIX_SIZEOF(stix->c->cls.name_loc));
|
|
|
|
STIX_MEMSET (&stix->c->cls.supername_loc, 0, STIX_SIZEOF(stix->c->cls.supername_loc));
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
for (i = 0; i < STIX_COUNTOF(stix->c->cls.var_count); i++)
|
|
|
|
stix->c->cls.var_count[i] = 0;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->cls.self_oop = STIX_NULL;
|
|
|
|
stix->c->cls.super_oop = STIX_NULL;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-25 17:10:49 +00:00
|
|
|
/* do main compilation work */
|
2015-05-26 16:31:47 +00:00
|
|
|
n = __compile_class_definition (stix);
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-25 17:10:49 +00:00
|
|
|
/* reset these oops not to confuse gc_compiler() */
|
2015-05-27 17:16:50 +00:00
|
|
|
stix->c->cls.self_oop = STIX_NULL;
|
|
|
|
stix->c->cls.super_oop = STIX_NULL;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
2015-05-25 17:10:49 +00:00
|
|
|
return n;
|
2015-05-20 15:24:37 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static int compile_stream (stix_t* stix)
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-19 15:16:18 +00:00
|
|
|
GET_CHAR (stix);
|
|
|
|
GET_TOKEN (stix);
|
|
|
|
|
|
|
|
while (stix->c->tok.type != STIX_IOTOK_EOF)
|
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
if (is_token_symbol(stix, KSYM_INCLUDE))
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-20 14:27:47 +00:00
|
|
|
/* #include 'xxxx' */
|
2015-05-19 15:16:18 +00:00
|
|
|
GET_TOKEN (stix);
|
2015-05-20 14:27:47 +00:00
|
|
|
if (stix->c->tok.type != STIX_IOTOK_STRLIT)
|
|
|
|
{
|
2015-05-20 15:24:37 +00:00
|
|
|
set_syntax_error (stix, STIX_SYNERR_STRING, &stix->c->tok.loc, &stix->c->tok.name);
|
2015-05-20 14:27:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (begin_include(stix) <= -1) return -1;
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
2015-05-27 17:16:50 +00:00
|
|
|
else if (is_token_symbol(stix, KSYM_CLASS))
|
2015-05-15 14:55:12 +00:00
|
|
|
{
|
2015-05-25 18:13:52 +00:00
|
|
|
/* #class Selfclass(Superclass) { } */
|
2015-05-20 15:24:37 +00:00
|
|
|
GET_TOKEN (stix);
|
|
|
|
if (compile_class_definition(stix) <= -1) return -1;
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
2015-05-25 18:13:52 +00:00
|
|
|
#if 0
|
2015-05-27 17:16:50 +00:00
|
|
|
else if (is_token_symbol(stix, KSYM_MAIN))
|
2015-05-20 14:27:47 +00:00
|
|
|
{
|
2015-05-25 18:13:52 +00:00
|
|
|
/* #main */
|
|
|
|
/* TODO: implement this */
|
|
|
|
|
2015-05-20 14:27:47 +00:00
|
|
|
}
|
2015-05-25 18:13:52 +00:00
|
|
|
#endif
|
2015-05-20 14:27:47 +00:00
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2015-05-25 18:13:52 +00:00
|
|
|
set_syntax_error(stix, STIX_SYNERR_DIRECTIVE, &stix->c->tok.loc, &stix->c->tok.name);
|
|
|
|
return -1;
|
2015-05-20 14:27:47 +00:00
|
|
|
}
|
2015-05-15 14:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-19 15:16:18 +00:00
|
|
|
|
2015-05-25 17:10:49 +00:00
|
|
|
static void gc_compiler (stix_t* stix)
|
|
|
|
{
|
2015-05-26 16:31:47 +00:00
|
|
|
/* called when garbage collection is performed */
|
2015-05-25 17:10:49 +00:00
|
|
|
if (stix->c)
|
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
if (stix->c->cls.self_oop)
|
|
|
|
stix->c->cls.self_oop = (stix_oop_class_t)stix_moveoop (stix, (stix_oop_t)stix->c->cls.self_oop);
|
2015-05-25 18:13:52 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
if (stix->c->cls.super_oop)
|
|
|
|
stix->c->cls.super_oop = stix_moveoop (stix, stix->c->cls.super_oop);
|
2015-05-25 17:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
static void fini_compiler (stix_t* stix)
|
|
|
|
{
|
2015-05-26 16:31:47 +00:00
|
|
|
/* called before the stix object is closed */
|
2015-05-19 15:16:18 +00:00
|
|
|
if (stix->c)
|
|
|
|
{
|
2015-05-25 18:13:52 +00:00
|
|
|
stix_size_t i;
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
clear_io_names (stix);
|
2015-05-22 15:09:45 +00:00
|
|
|
|
|
|
|
if (stix->c->tok.name.ptr) stix_freemem (stix, stix->c->tok.name.ptr);
|
2015-05-25 17:10:49 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
if (stix->c->cls.name.ptr) stix_freemem (stix, stix->c->cls.name.ptr);
|
|
|
|
if (stix->c->cls.supername.ptr) stix_freemem (stix, stix->c->cls.supername.ptr);
|
2015-05-25 17:10:49 +00:00
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
for (i = 0; i < STIX_COUNTOF(stix->c->cls.vars); i++)
|
2015-05-25 18:13:52 +00:00
|
|
|
{
|
2015-05-27 17:16:50 +00:00
|
|
|
if (stix->c->cls.vars[i].ptr) stix_freemem (stix, stix->c->cls.vars[i].ptr);
|
2015-05-25 18:13:52 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 17:16:50 +00:00
|
|
|
if (stix->c->mth.tmprs.ptr) stix_freemem (stix, stix->c->mth.tmprs.ptr);
|
|
|
|
if (stix->c->mth.name.ptr) stix_freemem (stix, stix->c->mth.name.ptr);
|
|
|
|
if (stix->c->mth.code.ptr) stix_freemem (stix, stix->c->mth.code.ptr);
|
2015-05-22 15:09:45 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
stix_freemem (stix, stix->c);
|
|
|
|
stix->c = STIX_NULL;
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
int stix_compile (stix_t* stix, stix_ioimpl_t io)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (!io)
|
|
|
|
{
|
|
|
|
stix->errnum = STIX_EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
if (!stix->c)
|
|
|
|
{
|
|
|
|
stix_cb_t cb, * cbp;
|
2015-05-25 17:10:49 +00:00
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
STIX_MEMSET (&cb, 0, STIX_SIZEOF(cb));
|
2015-05-25 17:10:49 +00:00
|
|
|
cb.gc = gc_compiler;
|
2015-05-19 15:16:18 +00:00
|
|
|
cb.fini = fini_compiler;
|
|
|
|
cbp = stix_regcb (stix, &cb);
|
|
|
|
if (!cbp) return -1;
|
|
|
|
|
|
|
|
stix->c = stix_callocmem (stix, STIX_SIZEOF(*stix->c));
|
|
|
|
if (!stix->c)
|
|
|
|
{
|
|
|
|
stix_deregcb (stix, cbp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
stix->c->ilchr_ucs.ptr = &stix->c->ilchr;
|
|
|
|
stix->c->ilchr_ucs.len = 1;
|
|
|
|
}
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
stix->c->impl = io;
|
|
|
|
stix->c->arg.line = 1;
|
|
|
|
stix->c->arg.colm = 1;
|
|
|
|
stix->c->curinp = &stix->c->arg;
|
2015-05-19 15:16:18 +00:00
|
|
|
clear_io_names (stix);
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
/* open the top-level stream */
|
|
|
|
n = stix->c->impl (stix, STIX_IO_OPEN, stix->c->curinp);
|
|
|
|
if (n <= -1) return -1;
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
if (compile_stream (stix) <= -1) goto oops;
|
2015-05-15 14:55:12 +00:00
|
|
|
|
|
|
|
/* close the stream */
|
|
|
|
STIX_ASSERT (stix->c->curinp == &stix->c->arg);
|
|
|
|
stix->c->impl (stix, STIX_IO_CLOSE, stix->c->curinp);
|
2015-05-19 15:16:18 +00:00
|
|
|
|
2015-05-15 14:55:12 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
oops:
|
|
|
|
/* an error occurred and control has reached here
|
|
|
|
* probably, some included files might not have been
|
|
|
|
* closed. close them */
|
|
|
|
while (stix->c->curinp != &stix->c->arg)
|
|
|
|
{
|
|
|
|
stix_ioarg_t* prev;
|
|
|
|
|
|
|
|
/* nothing much to do about a close error */
|
|
|
|
stix->c->impl (stix, STIX_IO_CLOSE, stix->c->curinp);
|
|
|
|
|
|
|
|
prev = stix->c->curinp->includer;
|
|
|
|
STIX_ASSERT (stix->c->curinp->name != STIX_NULL);
|
|
|
|
STIX_MMGR_FREE (stix->mmgr, stix->c->curinp);
|
|
|
|
stix->c->curinp = prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
stix->c->impl (stix, STIX_IO_CLOSE, stix->c->curinp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-19 15:16:18 +00:00
|
|
|
void stix_getsynerr (stix_t* stix, stix_synerr_t* synerr)
|
|
|
|
{
|
|
|
|
STIX_ASSERT (stix->c != STIX_NULL);
|
|
|
|
if (synerr) *synerr = stix->c->synerr;
|
|
|
|
}
|