enhanced newline handling

This commit is contained in:
hyunghwan.chung 2015-06-11 12:09:10 +00:00
parent 469210bd80
commit af4d0596fb
6 changed files with 221 additions and 121 deletions

View File

@ -394,6 +394,7 @@ static STIX_INLINE void unget_char (stix_t* stix, const stix_iolxc_t* c)
static int get_char (stix_t* stix)
{
stix_ssize_t n;
stix_uci_t lc, ec;
if (stix->c->nungots > 0)
{
@ -402,13 +403,25 @@ static int get_char (stix_t* stix)
return 0;
}
if (stix->c->curinp->b.state == -1)
{
stix->c->curinp->b.state = 0;
return -1;
}
else if (stix->c->curinp->b.state == 1)
{
stix->c->curinp->b.state = 0;
goto return_eof;
}
if (stix->c->curinp->b.pos >= stix->c->curinp->b.len)
{
n = stix->c->impl (stix, STIX_IO_READ, stix->c->curinp);
if (n <= -1) return -1;
if (n == 0)
{
return_eof:
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;
@ -434,7 +447,43 @@ static int get_char (stix_t* stix)
stix->c->curinp->colm = 1;
}
stix->c->curinp->lxc.c = stix->c->curinp->buf[stix->c->curinp->b.pos++];
lc = stix->c->curinp->buf[stix->c->curinp->b.pos++];
if (lc == '\n' || lc == '\r')
{
/* handle common newline conventions.
* LF+CR
* CR+LF
* LF
* CR
*/
if (stix->c->curinp->b.pos >= stix->c->curinp->b.len)
{
n = stix->c->impl (stix, STIX_IO_READ, stix->c->curinp);
if (n <= -1)
{
stix->c->curinp->b.state = -1;
goto done;
}
else if (n == 0)
{
stix->c->curinp->b.state = 1;
goto done;
}
else
{
stix->c->curinp->b.pos = 0;
stix->c->curinp->b.len = n;
}
}
ec = (lc == '\n')? '\r': '\n';
if (stix->c->curinp->buf[stix->c->curinp->b.pos] == ec) stix->c->curinp->b.pos++;
done:
lc = STIX_UCI_NL;
}
stix->c->curinp->lxc.c = lc;
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;
@ -957,7 +1006,7 @@ static int begin_include (stix_t* stix)
{
end_include (stix);
/* i don't jump to oops since i've called
* end_include() where stix->c->curinp/arg is freed. */
* end_include() which frees stix->c->curinp/arg */
return -1;
}
@ -1686,6 +1735,7 @@ static int compile_method_primitive (stix_t* stix)
static int get_variable_info (stix_t* stix, const stix_ucs_t* name, const stix_ioloc_t* name_loc, var_info_t* var)
{
stix_ssize_t index;
STIX_MEMSET (var, 0, STIX_SIZEOF(*var));
index = find_temporary_variable (stix, name);
@ -1754,7 +1804,7 @@ static int get_variable_info (stix_t* stix, const stix_ucs_t* name, const stix_i
}
}
if (index > MAX_CODE_INDEX)
if (var->pos > MAX_CODE_INDEX)
{
/* the assignee is not usable because its index is too large
* to be expressed in byte-codes. */
@ -3102,8 +3152,12 @@ int stix_compile (stix_t* stix, stix_ioimpl_t io)
}
stix->c->impl = io;
STIX_MEMSET (&stix->c->arg, 0, STIX_SIZEOF(stix->c->arg));
stix->c->arg.line = 1;
stix->c->arg.colm = 1;
stix->c->nungots = 0;
stix->c->curinp = &stix->c->arg;
clear_io_names (stix);

99
stix/lib/debug.c Normal file
View File

@ -0,0 +1,99 @@
/*
* $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 WAfRRANTIES
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"
void dump_symbol_table (stix_t* stix)
{
stix_oow_t i, j;
stix_oop_char_t symbol;
printf ("--------------------------------------------\n");
printf ("Stix Symbol Table %lu\n", (unsigned long int)STIX_OBJ_GET_SIZE(stix->symtab->bucket));
printf ("--------------------------------------------\n");
for (i = 0; i < STIX_OBJ_GET_SIZE(stix->symtab->bucket); i++)
{
symbol = (stix_oop_char_t)stix->symtab->bucket->slot[i];
if ((stix_oop_t)symbol != stix->_nil)
{
printf (" %lu [", (unsigned long int)i);
for (j = 0; j < STIX_OBJ_GET_SIZE(symbol); j++)
{
printf ("%c", symbol->slot[j]);
}
printf ("]\n");
}
}
printf ("--------------------------------------------\n");
}
void dump_dictionary (stix_t* stix, stix_oop_set_t dic, const char* title)
{
stix_oow_t i, j;
stix_oop_association_t ass;
printf ("--------------------------------------------\n");
printf ("%s %lu\n", title, (unsigned long int)STIX_OBJ_GET_SIZE(dic->bucket));
printf ("--------------------------------------------\n");
for (i = 0; i < STIX_OBJ_GET_SIZE(dic->bucket); i++)
{
ass = (stix_oop_association_t)dic->bucket->slot[i];
if ((stix_oop_t)ass != stix->_nil)
{
printf (" %lu [", (unsigned long int)i);
for (j = 0; j < STIX_OBJ_GET_SIZE(ass->key); j++)
{
printf ("%c", ((stix_oop_char_t)ass->key)->slot[j]);
}
printf ("]\n");
}
}
printf ("--------------------------------------------\n");
}
void print_ucs (const stix_ucs_t* name)
{
stix_size_t i;
for (i = 0; i < name->len; i++) printf ("%c", name->ptr[i]);
}
void dump_object (stix_t* stix, stix_oop_t oop, const char* title)
{
stix_oop_class_t c;
stix_ucs_t s;
printf ("%s: ", title);
printf ("%p instance of ", oop);
c = (stix_oop_class_t)STIX_CLASSOF(stix, oop);
s.ptr = ((stix_oop_char_t)c->name)->slot;
s.len = STIX_OBJ_GET_SIZE(c->name);
print_ucs (&s);
printf ("\n");
}

View File

@ -274,20 +274,6 @@ TODO: overcome this problem
(stix)->active_context->slot[sp] = v; \
} while (0)
static void dump_object (stix_t* stix, stix_oop_t oop, const char* title)
{
stix_oop_class_t c;
stix_ucs_t s;
printf ("%s: ", title);
printf ("%p instance of ", oop);
c = STIX_CLASSOF(stix, oop);
s.ptr = ((stix_oop_char_t)c->name)->slot;
s.len = STIX_OBJ_GET_SIZE(c->name);
print_ucs (&s);
printf ("\n");
}
static int execute_primitive (stix_t* stix, int prim_no, stix_ooi_t nargs, stix_ooi_t* xsp)
{
/* a primitive handler must pop off all arguments and the receiver and

View File

@ -157,61 +157,6 @@ static stix_ssize_t input_handler (stix_t* stix, stix_iocmd_t cmd, stix_ioarg_t*
}
}
static void dump_symbol_table (stix_t* stix)
{
stix_oow_t i, j;
stix_oop_char_t symbol;
printf ("--------------------------------------------\n");
printf ("Stix Symbol Table %lu\n", (unsigned long int)STIX_OBJ_GET_SIZE(stix->symtab->bucket));
printf ("--------------------------------------------\n");
for (i = 0; i < STIX_OBJ_GET_SIZE(stix->symtab->bucket); i++)
{
symbol = (stix_oop_char_t)stix->symtab->bucket->slot[i];
if ((stix_oop_t)symbol != stix->_nil)
{
printf (" %lu [", (unsigned long int)i);
for (j = 0; j < STIX_OBJ_GET_SIZE(symbol); j++)
{
printf ("%c", symbol->slot[j]);
}
printf ("]\n");
}
}
printf ("--------------------------------------------\n");
}
void dump_dictionary (stix_t* stix, stix_oop_set_t dic, const char* title)
{
stix_oow_t i, j;
stix_oop_association_t ass;
printf ("--------------------------------------------\n");
printf ("%s %lu\n", title, (unsigned long int)STIX_OBJ_GET_SIZE(dic->bucket));
printf ("--------------------------------------------\n");
for (i = 0; i < STIX_OBJ_GET_SIZE(dic->bucket); i++)
{
ass = (stix_oop_association_t)dic->bucket->slot[i];
if ((stix_oop_t)ass != stix->_nil)
{
printf (" %lu [", (unsigned long int)i);
for (j = 0; j < STIX_OBJ_GET_SIZE(ass->key); j++)
{
printf ("%c", ((stix_oop_char_t)ass->key)->slot[j]);
}
printf ("]\n");
}
}
printf ("--------------------------------------------\n");
}
void print_ucs (const stix_ucs_t* name)
{
stix_size_t i;
for (i = 0; i < name->len; i++) printf ("%c", name->ptr[i]);
}
static char* syntax_error_msg[] =
{
"no error",
@ -262,6 +207,7 @@ int main (int argc, char* argv[])
xtn_t* xtn;
stix_ucs_t objname;
stix_ucs_t mthname;
int i;
printf ("Stix 1.0.0 - max named %lu max indexed %lu max class %lu max classinst %lu\n",
(unsigned long int)STIX_MAX_NAMED_INSTVARS,
@ -282,9 +228,9 @@ printf ("%ld\n", (long int)STIX_OOP_TO_SMINT(k));
}
#if !defined(macintosh)
if (argc != 2)
if (argc < 2)
{
fprintf (stderr, "Usage: %s filename\n", argv[0]);
fprintf (stderr, "Usage: %s filename ...\n", argv[0]);
return -1;
}
#endif
@ -354,58 +300,66 @@ printf ("%p\n", a);
}
xtn = stix_getxtn (stix);
#if !defined(macintosh)
xtn->source_path = argv[1];
#else
#if defined(macintosh)
i = 20;
xtn->source_path = "test.st";
goto compile;
#endif
if (stix_compile (stix, input_handler) <= -1)
for (i = 1; i < argc; i++)
{
if (stix->errnum == STIX_ESYNTAX)
xtn->source_path = argv[i];
compile:
if (stix_compile (stix, input_handler) <= -1)
{
stix_synerr_t synerr;
stix_bch_t bcs[1024]; /* TODO: right buffer size */
stix_size_t bcslen, ucslen;
stix_getsynerr (stix, &synerr);
printf ("ERROR: ");
if (synerr.loc.file)
if (stix->errnum == STIX_ESYNTAX)
{
bcslen = STIX_COUNTOF(bcs);
ucslen = ~(stix_size_t)0;
if (stix_ucstoutf8 (synerr.loc.file, &ucslen, bcs, &bcslen) >= 0)
stix_synerr_t synerr;
stix_bch_t bcs[1024]; /* TODO: right buffer size */
stix_size_t bcslen, ucslen;
stix_getsynerr (stix, &synerr);
printf ("ERROR: ");
if (synerr.loc.file)
{
printf ("%.*s ", (int)bcslen, bcs);
}
}
printf ("syntax error at line %lu column %lu - %s",
(unsigned long int)synerr.loc.line, (unsigned long int)synerr.loc.colm,
syntax_error_msg[synerr.num]);
if (synerr.tgt.len > 0)
{
bcslen = STIX_COUNTOF(bcs);
ucslen = synerr.tgt.len;
if (stix_ucstoutf8 (synerr.tgt.ptr, &ucslen, bcs, &bcslen) >= 0)
{
printf (" [%.*s]", (int)bcslen, bcs);
bcslen = STIX_COUNTOF(bcs);
ucslen = ~(stix_size_t)0;
if (stix_ucstoutf8 (synerr.loc.file, &ucslen, bcs, &bcslen) >= 0)
{
printf ("%.*s ", (int)bcslen, bcs);
}
}
printf ("syntax error at line %lu column %lu - %s",
(unsigned long int)synerr.loc.line, (unsigned long int)synerr.loc.colm,
syntax_error_msg[synerr.num]);
if (synerr.tgt.len > 0)
{
bcslen = STIX_COUNTOF(bcs);
ucslen = synerr.tgt.len;
if (stix_ucstoutf8 (synerr.tgt.ptr, &ucslen, bcs, &bcslen) >= 0)
{
printf (" [%.*s]", (int)bcslen, bcs);
}
}
printf ("\n");
}
printf ("\n");
else
{
printf ("ERROR: cannot compile code - %d\n", stix_geterrnum(stix));
}
stix_close (stix);
return -1;
}
else
{
printf ("ERROR: cannot compile code - %d\n", stix_geterrnum(stix));
}
stix_close (stix);
return -1;
}
/* objname.ptr = str_stix;
objname.len = 4;*/
objname.ptr = str_my_object;

View File

@ -165,11 +165,12 @@ struct stix_iolxc_t
};
typedef struct stix_iolxc_t stix_iolxc_t;
/*
enum stix_ioarg_flag_t
{
STIX_IO_INCLUDED = (1 << 0)
};
typedef enum stix_ioarg_flag_t stix_ioarg_flag_t;
typedef enum stix_ioarg_flag_t stix_ioarg_flag_t; */
typedef struct stix_ioarg_t stix_ioarg_t;
struct stix_ioarg_t
@ -204,7 +205,7 @@ struct stix_ioarg_t
/*----------- from here down, internal use only -------------------*/
struct
{
int pos, len;
int pos, len, state;
} b;
unsigned long line;
@ -317,9 +318,6 @@ struct stix_synerr_t
typedef struct stix_synerr_t stix_synerr_t;
typedef enum stix_code_id_t stix_code_id_t;
struct stix_code_t
{
stix_uint8_t* ptr;
@ -739,6 +737,16 @@ void stix_getsynerr (
stix_synerr_t* synerr
);
/* TODO: remove debugging functions */
/* ========================================================================= */
/* debug.c */
/* ========================================================================= */
void dump_symbol_table (stix_t* stix);
void dump_dictionary (stix_t* stix, stix_oop_set_t dic, const char* title);
void print_ucs (const stix_ucs_t* name);
void dump_object (stix_t* stix, stix_oop_t oop, const char* title);
#if defined(__cplusplus)
}
#endif

View File

@ -72,7 +72,6 @@ typedef struct stix_ucs_t stix_ucs_t;
#define STIX_UCI_EOF ((stix_uci_t)-1)
#define STIX_UCI_NL ((stix_uci_t)'\n')
#define STIX_SIZEOF(x) (sizeof(x))
#define STIX_COUNTOF(x) (sizeof(x) / sizeof(x[0]))