/* #define OLD */ #include #include "coroutin.h" #ifndef OLD #include "expr.h" #endif #include "prolog.h" /* #define TRACE */ /* #define expr(n) struct \ { \ int type; \ int val; \ int arite; \ param[n]; \ } */ #ifndef OLD /* #define print_expr print */ #endif #ifdef OLD struct s_expr { int type; int val; char *str; /* void *adr; */ struct s_expr **adr; int arite; struct s_expr *param[1]; }; typedef struct s_expr *expr; struct s_expr expr_nil = { 1, 0, "", NULL, 0, {NULL}}; struct s_expr *nil = &expr_nil; #endif #define TYPE_VAR 0 #define TYPE_ATOM 1 #define TYPE_CONS 2 #define TYPE_SYMBOL 3 #ifdef OLD #define size_expr(x) (sizeof(struct s_expr)+((x)-1)*sizeof(struct s_expr *)) #define type(x) ((x)->type) #define val(x) ((x)->val) #define str(x) ((x)->str) #define adr(x) ((x)->adr) #define arite(x) ((x)->arite) #define param(x,i) ((x)->param[i]) #define is_var(x) ((x)->type == TYPE_VAR) #define adr_var(x) ((x)->adr) /* ((x)->param[0]) */ #define val_var(x) (*(adr_var(x))) struct s_expr *mk_var (struct s_expr **adr) { struct s_expr *var; var = malloc (size_expr(0)); if (var == NULL) { printf ("Insufficient memory to allocate variable\n"); exit (-1); } var->type = TYPE_VAR; var->val = 0; var->str = ""; var->arite = 0; var->adr = adr; return var; } struct s_expr *symbol (char *s) { struct s_expr *x; x = malloc (size_expr(0)); if (x == NULL) { printf ("Insufficient memory to allocate symbol\n"); exit (-1); } x->type = TYPE_SYMBOL; x->val = 0; x->str = s; x->adr = NULL; x->arite = 0; return x; } struct s_expr *cons (struct s_expr *x, struct s_expr *y) { struct s_expr *c; c = malloc (size_expr(2)); if (c == NULL) { printf ("Insufficient memory to allocate cons\n"); exit (-1); } c->type = TYPE_CONS; c->val = 0; c->str = ""; c->adr = NULL; c->arite = 2; c->param[0] = x; c->param[1] = y; return c; } #define UNDEF NULL #else /* int arite (expr x) { if (atom(x)) return 0; return 2; } */ #define arite(x) (atom(x) ? 0 : 2) #define param(x,i) ((i==0) ? car(x) : cdr(x)) #define type(x) (atom(x) ? TYPE_ATOM : TYPE_CONS) int val (expr x) { if (atom(x)) return x; return 0; } /* #define VAR 0x7FFC */ int is_var (expr x) { /* #ifdef TRACE printf ("\nx = "); print_expr (x); #endif */ if (atom(x)) return 0; if (atom(car(x))) return (car(x) == VAR); return 0; } #ifdef VAR_VAL #define val_var cdr #else #define val_var(x) (*(expr *)(cdr(x))) #endif /* #define set_val_var rplacd */ /* #define UNDEF 0x7FFD */ #define ANY 0x7FFB #define NDF 0x7FFE /* #define print_expr print */ /* #ifdef VAR_VAL #define mk_var(adr) (cons (VAR, *(adr))) #else #define mk_var(adr) (cons (VAR, expr_int ((int) adr))) #endif */ #endif int is_const (expr x) { int i; if (is_var(x)) return 0; for (i=0; itype, x->val, x->str, x->adr); if (x->adr && x->type == TYPE_VAR) { printf (" {"); if (*(x->adr)) print_expr (*(x->adr)); printf ("}"); } for (i=0; iarite; i++) print_expr (x->param[i]); printf ("] "); } #else #ifdef OLDPRINT print_expr (expr x) { if (is_var(x)) { printf ("{"); print_expr (val_var(x)); printf ("}"); } else if (atom(x)) print (x); else { printf ("*"); print_expr (car(x)); printf (" "); print_expr (cdr(x)); } } #else print_expr (expr x) { expr p; if (is_var(x)) print_expr (val_var(x)); else if (atom(x)) print(x); else { print_expr (car(x)); if (cdr(x)) { printf ("("); for (p=cdr(x); p; p=cdr(p)) { print_expr (car(p)); if (cdr(p)) printf (","); } printf (")"); } } } #endif #endif #define MAX_NEW_CONS 50 pl_printexpr_1 (struct coroutine *k, expr x) { print_expr (x); } pl_printstring_1 (struct coroutine *k, char *s) { printf ("%s", s); } /* #include "append.c" */ test_append (struct coroutine *k) { /* append (x, y, [a,b,c]) */ expr x, y, _x, _y, abc; #ifndef OLD expr r, t1, t2, t3; begin_decl (); decl_loc (x); decl_loc (y); decl_loc (_x); decl_loc (_y); decl_loc (abc); decl_loc (t1); decl_loc (t2); decl_loc (t3); #endif x = UNDEF; /* undefined */ y = UNDEF; _x = mk_var (&x); _y = mk_var (&y); #ifdef OLD abc = cons (symbol("a"), cons (symbol("b"), cons (symbol("c"), nil))); #else /* abc = cons (111, cons (222, cons (333, 0))); */ t1 = cons (333, 0); t2 = cons (222, t1); abc = cons (111, t2); #endif append (k, _x, _y, abc); printf ("\nx = "); #if 1 print_expr (x); #else print_expr (val_var(_x)); #endif printf (" ; y = "); #if 1 print_expr (y); #else print_expr (val_var(_y)); #endif printf (" .\n"); #ifndef OLD free_expr (); #endif } #define N_CONS 500 maincr (void *p, struct coroutine *c1) { struct coroutine calling[1]; /* expr tab_cons [N_CONS] [2]; char tab_status [N_CONS]; int ptrcons; recup_item tab_recup[400]; int ptr_recup; int n_decl; */ struct param_expr_info px; expr buf_cons [N_CONS] [2]; char buf_status [N_CONS]; /* int ptrcons; */ recup_item buf_recup[400]; /* int ptr_recup; */ /* int n_decl; */ char *(buf_symbol[N_SYMBOL]); memcpy (calling, c1, sizeof(calling)); px.pe_tab_cons = buf_cons; px.pe_tab_status = buf_status; px.pe_n_cons = N_CONS; px.pe_tab_recup = buf_recup; px.pe_n_recup = N_RECUP; px.pe_tab_symbol = buf_symbol; px.pe_n_symbol = N_SYMBOL; init_expr (&px); /* init_expr (tab_recup, sizeof(tab_recup)/sizeof(tab_recup[0]), &ptr_recup, &n_decl, &tab_cons, N_CONS, tab_status, &ptrcons); */ /* test_append (calling); */ pl_goal_0 (calling); end (calling); } /* #include "coroutin.h" */ main () { int stack [3000]; int maincr (); struct param_scheduler p; p.stack_size = sizeof(stack)-STACK_BOTTOM*sizeof(int); scheduler (maincr, &p, stack, p.stack_size, 0); } #if 0 main () { int stack [6000 + STACK_BOTTOM]; int maincr (); struct param_scheduler p; /* test_coroutine (); */ /* new_scheduler (testalt, 0); */ /* scheduler (testalt, 0); */ p.stack_size = sizeof(stack)-STACK_BOTTOM*sizeof(int); scheduler (maincr, &p, stack, sizeof(stack)-STACK_BOTTOM*sizeof(int)); } #endif