00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include <stdio.h>
00048
00049
00050
00051
00052
00053 #include "cinterf.h"
00054 extern char *xsb_executable_full_path(char *);
00055 extern char *strip_names_from_path(char*, int);
00056
00057 int getline(s, lim)
00058 char s[];
00059 int lim;
00060 {
00061 int c=0, i;
00062 for (i=0; i<lim-1 && (c=getc(stdin))>=0 && c!='\n'; ++i)
00063 s[i] = c;
00064 if (c == '\n') {
00065 s[i] = c;
00066 ++i;
00067 }
00068 s[i] = '\0';
00069 return(i);
00070 }
00071
00072 void printpterm(prolog_term term)
00073 {
00074 int i;
00075
00076 if (is_var(term)) fprintf(stdout,"_%p", (void *)term);
00077 else if (is_int(term)) fprintf(stdout,"%d",p2c_int(term));
00078 else if (is_float(term)) fprintf(stdout,"%f",p2c_float(term));
00079 else if (is_nil(term)) fprintf(stdout,"[]");
00080 else if (is_string(term)) fprintf(stdout,"%s",p2c_string(term));
00081 else if (is_list(term)) {
00082 fprintf(stdout,"[");
00083 printpterm(p2p_car(term));
00084 term = p2p_cdr(term);
00085 while (is_list(term)) {
00086 fprintf(stdout,","),
00087 printpterm(p2p_car(term));
00088 term = p2p_cdr(term);
00089 }
00090 if (!is_nil(term)) {
00091 fprintf(stdout,"|");
00092 printpterm(term);
00093 }
00094 fprintf(stdout,"]");
00095 } else if (is_functor(term)) {
00096 fprintf(stdout,"%s",p2c_functor(term));
00097 if (p2c_arity(term) > 0) {
00098 fprintf(stdout,"(");
00099 printpterm(p2p_arg(term,1));
00100 for (i = 2; i <= p2c_arity(term); i++) {
00101 fprintf(stdout,",");
00102 printpterm(p2p_arg(term,i));
00103 }
00104 fprintf(stdout,")");
00105 }
00106 } else fprintf(stdout,"error, unrecognized type");
00107 }
00108
00109 int main(int argc, char *argv[])
00110 {
00111 int rcode;
00112 int myargc = 2;
00113 char *myargv[2];
00114 char query[256];
00115
00116
00117
00118
00119
00120
00121 myargv[0] = strip_names_from_path(xsb_executable_full_path(argv[0]), 3);
00122 myargv[1] = "-n";
00123
00124
00125 xsb_init(myargc,myargv);
00126
00127
00128 c2p_functor("consult",1,reg_term(1));
00129 c2p_string("ctest",p2p_arg(reg_term(1),1));
00130 if (xsb_command()) printf("Error consulting ctest.P.\n");
00131
00132 if (xsb_command_string("consult(basics)."))
00133 printf("Error (string) consulting basics.\n");
00134
00135
00136 c2p_functor("p",3,reg_term(1));
00137 c2p_int(300,p2p_arg(reg_term(1),1));
00138
00139 rcode = xsb_query();
00140
00141
00142 while (!rcode) {
00143 if (!(is_string(p2p_arg(reg_term(2),1)) &
00144 is_string(p2p_arg(reg_term(2),2))))
00145 printf("2nd and 3rd subfields must be atoms\n");
00146 else
00147 printf("Answer: %d, %s(%s), %s(%s)\n",
00148 p2c_int(p2p_arg(reg_term(1),1)),
00149 p2c_string(p2p_arg(reg_term(1),2)),
00150 xsb_var_string(1),
00151 p2c_string(p2p_arg(reg_term(1),3)),
00152 xsb_var_string(2)
00153 );
00154 rcode = xsb_next();
00155 }
00156
00157
00158
00159
00160 xsb_make_vars(3);
00161 xsb_set_var_int(300,1);
00162 rcode = xsb_query_string("p(X,Y,Z).");
00163
00164
00165 while (!rcode) {
00166 if (!(is_string(p2p_arg(reg_term(2),2)) &
00167 is_string(p2p_arg(reg_term(2),3))))
00168 printf("2nd and 3rd subfields must be atoms\n");
00169 else
00170 printf("Answer: %d, %s, %s\n",
00171 xsb_var_int(1),
00172 xsb_var_string(2),
00173 xsb_var_string(3)
00174 );
00175 rcode = xsb_next();
00176 }
00177
00178
00179 fprintf(stdout,"Enter Prolog queries (^d for eof to exit)\n");
00180 fprintf(stdout,"| ?- ");
00181 getline(query,255);
00182 while (query[0] != '\0') {
00183 rcode = xsb_query_string(query);
00184 while (!rcode) {
00185 printpterm(reg_term(2));
00186 fprintf(stdout,"\n");
00187 rcode = xsb_next();
00188 }
00189 fprintf(stdout,"| ?- ");
00190 getline(query,255);
00191 }
00192
00193
00194
00195 xsb_close();
00196 printf("cmain exit\n");
00197 return(0);
00198 }