source: golgotha/src/i4/lisp/lisp.hh @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 5.5 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#ifndef LI_LISP_HH
10#define LI_LISP_HH
11
12#include "lisp/li_types.hh"
13#include "lisp/li_optr.hh"
14
15class i4_status_class;
16
17// this is used for debugging... it doesn't evaluate o
18void lip(li_object *o);
19
20// this is the function added into the system, arguments are evaluated
21li_object *li_print(li_object *o, li_environment *env=0);
22
23li_object *li_get_expression(char *&s, li_environment *env);
24
25li_object *li_eval(li_object *expression, li_environment *env=0);
26li_object *li_call(char *fun_name, li_object *params=0, li_environment *env=0);
27li_object *li_call(li_symbol *fun_name, li_object *params=0, li_environment *env=0);
28
29// if env is 0, global function is set
30void li_add_function(li_symbol *sym, li_function_type fun, li_environment *env=0);
31void li_add_function(char *sym_name, li_function_type fun, li_environment *env=0);
32
33li_symbol *li_find_symbol(const char *name);    // if symbol doesn't exsist, 0 is returned
34li_symbol *li_get_symbol(const char *name);     // if symbol doesn't exsist, it is created
35
36// if cache_to is 0, then the symbol is found and stored there, otherwise cache_to is returned
37li_symbol *li_get_symbol(char *name, li_symbol *&cache_to);
38
39
40inline li_object *li_get_value(li_symbol *sym, li_environment *env=0)
41{
42  if (env)
43    return env->value(sym);
44  else return sym->value();
45}
46
47inline li_object *li_get_value(char *sym, li_environment *env=0)
48{ return li_get_value(li_get_symbol(sym),env); }
49
50inline void li_set_value(li_symbol *sym, li_object *value, li_environment *env=0)
51{
52  if (env)
53    env->set_value(sym, value);
54  else sym->set_value(value); 
55}
56
57inline void li_set_value(char *sym, li_object *value, li_environment *env=0)
58{ li_set_value(li_get_symbol(sym),value,env); }
59 
60
61// gets a function for a symbol.  Checks to see if it has a local value in the environment
62// first
63li_object *li_get_fun(li_symbol *sym, li_environment *env);
64
65li_object *li_get_fun(char *sym, li_environment *env);
66 
67// return 0 if type doesn't exsist
68li_object *li_new(char *type_name, li_object *params=0, li_environment *env=0); 
69li_object *li_new(int type, li_object *params=0, li_environment *env=0); 
70
71
72// CAR comes from old LISP machines which stands for Contents of the Address Register
73// though lisp machines aren't used anymore, the term has stuck around.
74inline li_object *li_car(li_object *o, li_environment *env) { return li_list::get(o,env)->data(); }
75
76// CDR = Contents of the Destination Register.  or better know by "C" people as ->next
77inline li_object *li_cdr(li_object *o, li_environment *env) { return li_list::get(o,env)->next(); }
78
79i4_bool    li_get_bool(li_object *o, li_environment *env);   // return i4_T if o is 'T,  i4_F if it is 'F or nil
80float      li_get_float(li_object *o, li_environment *env);  // will convert int to float
81int        li_get_int(li_object *o, li_environment *env);    // will convert float to int
82char      *li_get_string(li_object *o, li_environment *env);
83i4_bool    li_is_number(li_object *o, li_environment *env);  // returns true if item is LI_INT or LI_FLOAT
84
85// these function return the 1st, 2nd...  item in a list first=li_cdr(o), second=li_cdr(li_cdr(o))
86li_object  *li_first(li_object *o, li_environment *env);
87li_object  *li_second(li_object *o, li_environment *env);
88li_object  *li_third(li_object *o, li_environment *env);
89li_object  *li_fourth(li_object *o, li_environment *env);
90li_object  *li_fifth(li_object *o, li_environment *env);
91li_object  *li_nth(li_object *o, int x, li_environment *env);
92
93// li_make_list takes a variable @ of arguments (null terminated) and return a list of
94// add the members : example li_make_list(new li_int(4), new li_string("hello"), 0);
95li_list   *li_make_list(li_object *first, ...);
96int        li_length(li_object *o, li_environment *env);     // returns the length of the list
97
98// this version of load take a list of li_strings which are loaded sequentially
99// return result of last evaluated expression
100li_object *li_load(li_object *name, li_environment *env, i4_status_class *status);
101li_object *li_load(li_object *name, li_environment *env=0);
102
103// this loads a single file and returns result of last evaluated expression
104li_object *li_load(char *filename, li_environment *env=0, i4_status_class *status=0);
105
106// loads from a file pointer, reads entire buffer
107li_object *li_load(i4_file_class *fp, li_environment *env=0, i4_status_class *status=0);
108
109// reads an expression from i4_debug and prints the result of evaluation to i4_debug
110li_object *li_read_eval(li_object *o, li_environment *env=0);
111
112// simply add an item to the begining of a list
113inline void li_push(li_list *&l, li_object *o) { l=new li_list(o, l); }
114
115// global symbols
116extern li_symbol *li_nil,
117  *li_true_sym,
118  *li_quote,
119  *li_backquote,
120  *li_comma,
121  *li_function_symbol;
122
123
124class li_symbol_ref
125{
126  char *name;
127  li_symbol *sym;
128public:
129  li_symbol *get() { if (!sym) sym=li_get_symbol(name); return sym; }
130  i4_bool operator==(const li_symbol *&b) { return (get() == b); }
131
132  li_symbol_ref(char *sym_name) : name(sym_name) { sym=0; }
133};
134
135#endif
Note: See TracBrowser for help on using the repository browser.