source: abuse/trunk/src/lisp/lisp_opt.cpp @ 484

Last change on this file since 484 was 484, checked in by Sam Hocevar, 10 years ago

lisp: populate LispSymbol? with symbol-related methods.

File size: 4.2 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *
5 *  This software was released into the Public Domain. As with most public
6 *  domain software, no warranty is made or implied by Crack dot Com or
7 *  Jonathan Clark.
8 */
9
10#include "config.h"
11
12#ifdef NO_LIBS
13#include "fakelib.h"
14#else
15#include "macs.h"
16#endif
17
18#include "lisp.h"
19#include "lisp_gc.h"
20
21void *true_symbol=NULL,*l_undefined,*list_symbol,*string_symbol,     // in lisp_init()
22     *quote_symbol,*backquote_symbol,*comma_symbol,*do_symbol,*in_symbol,*aref_symbol,
23     *colon_initial_contents,*colon_initial_element,*if_symbol,
24     *progn_symbol,*eq_symbol,*zero_symbol,*eq0_symbol,*car_symbol,*cdr_symbol,
25     *load_warning;
26
27
28void *if_1progn,*if_2progn,*if_12progn,*not_symbol;
29
30void *comp_optimize(void *list)
31{
32  void *return_val=list;
33  p_ref r1(list);
34  if (list)
35  {
36    if (CAR(list)==if_symbol)
37    {
38      void *eval1=lcar(lcdr(lcdr(list)));
39      p_ref r2(eval1);
40      void *eval2=lcar(lcdr(lcdr(lcdr(list))));
41      p_ref r3(eval2);
42
43      void *ret=NULL;
44      p_ref r1(ret);
45      if (lcar(list)==eq_symbol && (lcar(lcdr(list))==zero_symbol))  //  simplify (eq 0 x) -> (eq0 x)
46      {
47    push_onto_list(lcar(lcdr(lcdr(list))),ret);
48    push_onto_list(eq0_symbol,ret);
49    return_val=comp_optimize(ret);
50      } else if (lcar(list)==eq_symbol &&
51         (lcar(lcdr(lcdr(list)))==zero_symbol)) //simplify (eq x 0)-> (eq0 x)
52      {
53    push_onto_list(lcar(lcdr(list)),ret);
54    push_onto_list(eq0_symbol,ret);
55    return_val=comp_optimize(ret);
56      } else if (lcar(lcar(lcdr(list)))==not_symbol)  // simplify (if (not y) x z) -> (if y z x)
57      {
58    push_onto_list(lcar(lcdr(lcdr(list))),ret);
59    push_onto_list(lcar(lcdr(lcdr(lcdr(list)))),ret);
60    push_onto_list(lcar(lcdr(lcar(lcdr(list)))),ret);
61    push_onto_list(if_symbol,ret);
62    return_val=comp_optimize(ret);
63      }
64      else if (lcar(eval1)==progn_symbol && (eval2==NULL ||
65                         item_type(eval2)!=L_CONS_CELL))
66      {
67    push_onto_list(eval2,ret);
68    push_onto_list(lcdr(eval1),ret);
69    push_onto_list(lcar(lcdr(list)),ret);
70    push_onto_list(if_1progn,ret);
71    return_val=comp_optimize(ret);
72      } else if (lcar(eval1)==progn_symbol && lcar(eval2)==progn_symbol)
73      {
74    push_onto_list(lcdr(eval2),ret);
75    push_onto_list(lcdr(eval1),ret);
76    push_onto_list(lcar(lcdr(list)),ret);
77    push_onto_list(if_12progn,ret);
78    return_val=comp_optimize(ret);
79      } else if (lcar(eval2)==progn_symbol)
80      {
81    push_onto_list(lcdr(eval2),ret);
82    push_onto_list(eval1,ret);
83    push_onto_list(lcar(lcdr(list)),ret);
84    push_onto_list(if_2progn,ret);
85    return_val=comp_optimize(ret);
86      }
87
88    }
89  }
90  return return_val;
91}
92
93void l_comp_init()
94{
95    // This needs to be defined first
96    l_undefined = LispSymbol::FindOrCreate(":UNDEFINED");
97
98    // Collection problems result if we don't do this
99    ((LispSymbol *)l_undefined)->function = NULL;
100    ((LispSymbol *)l_undefined)->value = NULL;
101
102    true_symbol = LispSymbol::FindOrCreate("T");
103
104    list_symbol = LispSymbol::FindOrCreate("list");
105    string_symbol = LispSymbol::FindOrCreate("string");
106    quote_symbol = LispSymbol::FindOrCreate("quote");
107    backquote_symbol = LispSymbol::FindOrCreate("backquote");
108    comma_symbol = LispSymbol::FindOrCreate("comma");
109    in_symbol = LispSymbol::FindOrCreate("in");
110    do_symbol = LispSymbol::FindOrCreate("do");
111    aref_symbol = LispSymbol::FindOrCreate("aref");
112    colon_initial_contents = LispSymbol::FindOrCreate(":initial-contents");
113    colon_initial_element = LispSymbol::FindOrCreate(":initial-element");
114
115    if_1progn = LispSymbol::FindOrCreate("if-1progn");
116    if_2progn = LispSymbol::FindOrCreate("if-2progn");
117    if_12progn = LispSymbol::FindOrCreate("if-12progn");
118    if_symbol = LispSymbol::FindOrCreate("if");
119    progn_symbol = LispSymbol::FindOrCreate("progn");
120    not_symbol = LispSymbol::FindOrCreate("not");
121    eq_symbol = LispSymbol::FindOrCreate("eq");
122    zero_symbol = LispSymbol::FindOrCreate("0");
123    eq0_symbol = LispSymbol::FindOrCreate("eq0");
124    car_symbol = LispSymbol::FindOrCreate("car");
125    cdr_symbol = LispSymbol::FindOrCreate("cdr");
126    load_warning = LispSymbol::FindOrCreate("load_warning");
127}
128
Note: See TracBrowser for help on using the repository browser.