qse/ase/test/stx/parser.c

217 lines
4.3 KiB
C
Raw Normal View History

2005-06-05 05:27:02 +00:00
#include <xp/stx/stx.h>
#ifdef _DOS
#include <stdio.h>
#define xp_printf printf
#else
#include <xp/bas/stdio.h>
#include <xp/bas/locale.h>
#endif
#include <xp/stx/parser.h>
2005-06-29 12:02:39 +00:00
#include <xp/stx/bootstrp.h>
#include <xp/stx/class.h>
2005-07-07 07:45:05 +00:00
#include <xp/stx/bytecode.h>
2005-09-11 15:15:35 +00:00
#include <xp/stx/interp.h>
2005-06-05 05:27:02 +00:00
2005-06-06 03:54:32 +00:00
#ifdef __linux
#include <mcheck.h>
#endif
2005-06-05 05:27:02 +00:00
struct ss_t
{
2005-06-08 16:05:41 +00:00
const xp_char_t* text;
2005-06-05 05:27:02 +00:00
xp_size_t index;
};
typedef struct ss_t ss_t;
2005-06-06 03:47:56 +00:00
int ss_func (int cmd, void* owner, void* arg)
2005-06-05 05:27:02 +00:00
{
2005-06-05 16:44:05 +00:00
if (cmd == XP_STX_PARSER_INPUT_OPEN) {
2005-06-06 03:47:56 +00:00
ss_t* ss = *(ss_t**)owner;
2005-06-08 16:05:41 +00:00
ss->text = (const xp_char_t*)arg;
2005-06-06 03:47:56 +00:00
ss->index = 0;
2005-06-05 16:44:05 +00:00
return 0;
}
else if (cmd == XP_STX_PARSER_INPUT_CLOSE) {
2005-06-06 03:54:32 +00:00
/*ss_t* ss = (ss_t*)owner; */
2005-06-05 16:44:05 +00:00
return 0;
}
else if (cmd == XP_STX_PARSER_INPUT_CONSUME) {
2005-06-06 03:47:56 +00:00
ss_t* ss = (ss_t*)owner;
xp_cint_t* c = (xp_cint_t*)arg;
2005-06-08 16:05:41 +00:00
if (ss->text[ss->index] == XP_CHAR('\0')) {
*c = XP_CHAR_EOF;
2005-06-06 03:47:56 +00:00
}
else *c = ss->text[ss->index++];
return 0;
2005-06-05 16:44:05 +00:00
}
2005-06-06 03:47:56 +00:00
else if (cmd == XP_STX_PARSER_INPUT_REWIND) {
return 0;
}
return -1;
}
struct stdio_t
{
XP_FILE* stdio;
};
typedef struct stdio_t stdio_t;
int stdio_func (int cmd, void* owner, void* arg)
{
if (cmd == XP_STX_PARSER_INPUT_OPEN) {
stdio_t* p = *(stdio_t**)owner;
p->stdio = xp_fopen ((const xp_char_t*)arg, XP_TEXT("r"));
if (p->stdio == XP_NULL) return -1;
return 0;
}
else if (cmd == XP_STX_PARSER_INPUT_CLOSE) {
stdio_t* p = (stdio_t*)owner;
xp_fclose (p->stdio);
return 0;
}
else if (cmd == XP_STX_PARSER_INPUT_CONSUME) {
stdio_t* p = (stdio_t*)owner;
xp_cint_t* c = (xp_cint_t*)arg;
2005-06-06 16:32:29 +00:00
xp_cint_t t = xp_fgetc (p->stdio);
if (t == XP_CHAR_EOF) {
if (xp_ferror (p->stdio)) return -1;
2005-06-08 16:05:41 +00:00
*c = XP_CHAR_EOF;
2005-06-06 03:47:56 +00:00
}
2005-06-06 16:32:29 +00:00
else *c = t;
2005-06-06 03:47:56 +00:00
return 0;
}
else if (cmd == XP_STX_PARSER_INPUT_REWIND) {
return 0;
}
return -1;
2005-06-05 05:27:02 +00:00
}
int xp_main (int argc, xp_char_t* argv[])
{
2005-06-29 12:02:39 +00:00
xp_stx_t stx;
2005-06-05 05:27:02 +00:00
xp_stx_parser_t parser;
2005-06-06 03:54:32 +00:00
#ifdef __linux
mtrace ();
#endif
/*
2005-06-05 05:27:02 +00:00
#ifndef _DOS
if (xp_setlocale () == -1) {
printf ("cannot set locale\n");
return -1;
}
#endif
2005-06-06 03:54:32 +00:00
*/
2005-06-29 12:02:39 +00:00
if (argc != 2) {
xp_printf (XP_TEXT("usage: %s class_name\n"), argv[0]);
return -1;
}
if (xp_stx_open (&stx, 10000) == XP_NULL) {
xp_printf (XP_TEXT("cannot open stx\n"));
return -1;
}
if (xp_stx_bootstrap(&stx) == -1) {
xp_stx_close (&stx);
xp_printf (XP_TEXT("cannot bootstrap\n"));
return -1;
}
2005-06-05 05:27:02 +00:00
2005-06-29 12:02:39 +00:00
if (xp_stx_parser_open(&parser, &stx) == XP_NULL) {
2005-06-05 05:27:02 +00:00
xp_printf (XP_TEXT("cannot open parser\n"));
return -1;
}
2005-06-29 12:02:39 +00:00
2005-06-05 16:44:05 +00:00
{
2005-06-06 03:47:56 +00:00
/*
ss_t ss;
parser.input_owner = (void*)&ss;
parser.input_func = ss_func;
xp_stx_parser_parse_method (&parser, 0,
XP_TEXT("isNil\n^true"));
*/
stdio_t stdio;
2005-06-29 12:02:39 +00:00
xp_word_t n = xp_stx_lookup_class (&stx, argv[1]);
2005-09-11 15:43:14 +00:00
xp_word_t m;
2005-06-29 12:02:39 +00:00
2005-06-06 03:47:56 +00:00
parser.input_owner = (void*)&stdio;
parser.input_func = stdio_func;
2005-06-29 12:02:39 +00:00
if (n == stx.nil) {
xp_printf (XP_TEXT("Cannot find class - %s\n"), argv[1]);
goto exit_program;
}
if (xp_stx_parser_parse_method (&parser, n,
2005-06-06 03:47:56 +00:00
(void*)XP_TEXT("test.st")) == -1) {
2005-06-12 12:33:31 +00:00
xp_printf (XP_TEXT("parser error <%s>\n"),
2005-07-07 07:45:05 +00:00
xp_stx_parser_error_string (&parser));
}
2005-09-13 11:15:41 +00:00
if (xp_stx_parser_parse_method (&parser, stx.class_symbol,
2005-09-13 12:10:23 +00:00
(void*)XP_TEXT("test1.st")) == -1) {
2005-09-13 11:15:41 +00:00
xp_printf (XP_TEXT("parser error <%s>\n"),
xp_stx_parser_error_string (&parser));
}
if (xp_stx_parser_parse_method (&parser, stx.class_symbol,
2005-09-13 12:10:23 +00:00
(void*)XP_TEXT("test2.st")) == -1) {
2005-09-13 11:15:41 +00:00
xp_printf (XP_TEXT("parser error <%s>\n"),
xp_stx_parser_error_string (&parser));
}
2005-09-30 12:19:00 +00:00
xp_printf (XP_TEXT("\n== Decoded Methods ==\n"));
2005-07-07 07:45:05 +00:00
if (xp_stx_decode(&stx, n) == -1) {
xp_printf (XP_TEXT("parser error <%s>\n"),
xp_stx_parser_error_string (&parser));
2005-06-06 03:47:56 +00:00
}
2005-09-11 15:15:35 +00:00
2005-09-30 12:19:00 +00:00
xp_printf (XP_TEXT("\n== Decoded Methods for Symbol ==\n"));
if (xp_stx_decode(&stx, stx.class_symbol) == -1) {
xp_printf (XP_TEXT("parser error <%s>\n"),
xp_stx_parser_error_string (&parser));
}
2005-09-11 15:43:14 +00:00
xp_printf (XP_TEXT("== Running the main method ==\n"));
m = xp_stx_lookup_method (&stx, n, XP_TEXT("main"));
if (m == stx.nil) {
xp_printf (XP_TEXT("cannot lookup method main\n"));
}
else {
2005-09-13 11:15:41 +00:00
xp_stx_interp (&stx, n, m);
2005-09-11 15:43:14 +00:00
}
2005-06-05 16:44:05 +00:00
}
2005-06-05 05:27:02 +00:00
2005-06-29 12:02:39 +00:00
exit_program:
2005-06-05 05:27:02 +00:00
xp_stx_parser_close (&parser);
2005-06-29 12:02:39 +00:00
xp_stx_close (&stx);
2005-06-05 05:27:02 +00:00
xp_printf (XP_TEXT("== End of program ==\n"));
2005-06-06 03:54:32 +00:00
#ifdef __linux
muntrace ();
#endif
2005-09-30 12:19:00 +00:00
/*
2005-06-06 15:46:48 +00:00
#ifdef __linux
2005-06-06 03:54:32 +00:00
{
char buf[1000];
snprintf (buf, sizeof(buf), "ls -l /proc/%u/fd", getpid());
system (buf);
}
2005-06-06 15:46:48 +00:00
#endif
2005-09-30 12:19:00 +00:00
*/
2005-06-05 05:27:02 +00:00
return 0;
}