00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 #ifndef PLANG_CONTEXT_PRIV_H
00021 #define PLANG_CONTEXT_PRIV_H
00022 
00023 #include <plang/context.h>
00024 #include <plang/term.h>
00025 #include <stddef.h>
00026 
00027 #ifdef __cplusplus
00028 extern "C" {
00029 #endif
00030 
00033 #define P_CONTEXT_HASH_SIZE     511
00034 
00035 
00036 
00037 #define P_RESULT_TREE_CHANGE    ((p_goal_result)(P_RESULT_HALT + 5))
00038 
00039 
00040 #define P_RESULT_RETURN_BODY    ((p_goal_result)(P_RESULT_HALT + 6))
00041 
00042 typedef struct p_trail p_trail;
00043 
00044 struct p_path_list
00045 {
00046     char **paths;
00047     size_t num_paths;
00048     size_t max_paths;
00049 };
00050 
00051 typedef struct p_exec_node p_exec_node;
00052 typedef struct p_exec_fail_node p_exec_fail_node;
00053 typedef struct p_exec_clause_node p_exec_clause_node;
00054 typedef struct p_exec_catch_node p_exec_catch_node;
00055 typedef struct p_exec_pop_catch_node p_exec_pop_catch_node;
00056 typedef struct p_exec_pop_database_node p_exec_pop_database_node;
00057 typedef void (*p_exec_fail_func)
00058     (p_context *context, p_exec_fail_node *node);
00059 
00060 struct p_exec_node
00061 {
00062     p_term *goal;
00063     p_exec_node *success_node;
00064     p_exec_fail_node *cut_node;
00065     p_exec_fail_func fail_func;
00066 };
00067 struct p_exec_fail_node
00068 {
00069     p_exec_node parent;
00070     void *fail_marker;
00071     double confidence;
00072     p_exec_catch_node *catch_node;
00073     p_term *database;
00074 };
00075 struct p_exec_clause_node
00076 {
00077     p_exec_fail_node parent;
00078     p_term_clause_iter clause_iter;
00079 };
00080 struct p_exec_catch_node
00081 {
00082     p_exec_fail_node parent;
00083     p_exec_catch_node *catch_parent;
00084 };
00085 struct p_exec_pop_catch_node
00086 {
00087     p_exec_node parent;
00088     p_exec_catch_node *catch_node;
00089 };
00090 struct p_exec_pop_database_node
00091 {
00092     p_exec_node parent;
00093     p_term *database;
00094 };
00095 
00096 typedef void (*p_library_entry_func)(p_context *context);
00097 typedef struct p_library p_library;
00098 struct p_library
00099 {
00100     void *handle;
00101     p_library_entry_func shutdown_func;
00102     p_library *next;
00103 };
00104 
00105 struct p_context
00106 {
00107     p_term *nil_atom;
00108     p_term *prototype_atom;
00109     p_term *class_name_atom;
00110     p_term *dot_atom;
00111     p_term *clause_atom;
00112     p_term *dcg_atom;
00113     p_term *comma_atom;
00114     p_term *line_atom;
00115     p_term *if_atom;
00116     p_term *in_atom;
00117     p_term *slash_atom;
00118     p_term *true_atom;
00119     p_term *fail_atom;
00120     p_term *commit_atom;
00121     p_term *call_member_atom;
00122     p_term *call_args_atom;
00123     p_term *unify_atom;
00124     p_term *pop_catch_atom;
00125     p_term *pop_database_atom;
00126     p_term *atom_hash[P_CONTEXT_HASH_SIZE];
00127 
00128     p_trail *trail;
00129     int trail_top;
00130 
00131     int fail_on_unknown : 1;
00132     int debug : 1;
00133 
00134     int goal_active;
00135     void *goal_marker;
00136     p_exec_node *current_node;
00137     p_exec_fail_node *fail_node;
00138     p_exec_catch_node *catch_node;
00139     void *fail_marker;
00140     double confidence;
00141     p_term *database;
00142 
00143     int allow_test_goals;
00144     p_term *test_goal;
00145 
00146     struct p_path_list user_imports;
00147     struct p_path_list system_imports;
00148     struct p_path_list user_libs;
00149     struct p_path_list system_libs;
00150     struct p_path_list loaded_files;
00151 
00152     int unique_num;
00153 
00154     p_library *libraries;
00155 
00156     unsigned int random_seed;
00157 
00158     p_term **xregs;
00159     int num_xregs;
00160 };
00161 
00162 #define P_TRACE_SIZE 1020
00163 
00164 struct p_trail
00165 {
00166     void **bindings[P_TRACE_SIZE];
00167     p_trail *next;
00168 };
00169 
00170 int _p_context_record_in_trail(p_context *context, p_term *var);
00171 int _p_context_record_contents_in_trail(p_context *context, void **location, void *prev_value);
00172 
00173 void _p_context_basic_fail_func
00174     (p_context *context, p_exec_fail_node *node);
00175 void _p_context_clause_fail_func
00176     (p_context *context, p_exec_fail_node *node);
00177 void _p_context_init_fail_node
00178     (p_context *context, p_exec_fail_node *node,
00179      p_exec_fail_func fail_func);
00180 
00181 p_goal_result p_goal_call_from_parser(p_context *context, p_term *goal);
00182 
00183 p_goal_result _p_context_load_library(p_context *context, p_term *name, p_term **error);
00184 
00185 #define p_context_add_path(list,name)   \
00186     do { \
00187         if ((list).num_paths >= (list).max_paths) { \
00188             size_t new_max = (list).max_paths * 2; \
00189             if (new_max < 8) \
00190                 new_max = 8; \
00191             (list).paths = (char **)GC_REALLOC((list).paths, new_max * sizeof(char *)); \
00192             (list).max_paths = new_max; \
00193         } \
00194         (list).paths[((list).num_paths)++] = GC_STRDUP(name); \
00195     } while (0)
00196 
00197 
00198 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
00199 #define P_WIN32         1
00200 #define P_WIN32_CYGWIN  1
00201 #elif defined(_WIN32) || defined(WIN32)
00202 #define P_WIN32         1
00203 #define P_WIN32_NATIVE  1
00204 #endif
00205 
00208 #ifdef __cplusplus
00209 };
00210 #endif
00211 
00212 #endif