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

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

lisp: no LispSymbol? members are anonymous pointers any longer.

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
21LispObject *l_undefined;
22LispSymbol *true_symbol = NULL;
23
24void *list_symbol,*string_symbol,     // in lisp_init()
25     *quote_symbol,*backquote_symbol,*comma_symbol,*do_symbol,*in_symbol,*aref_symbol,
26     *colon_initial_contents,*colon_initial_element,*if_symbol,
27     *progn_symbol,*eq_symbol,*zero_symbol,*eq0_symbol,*car_symbol,*cdr_symbol,
28     *load_warning;
29
30
31void *if_1progn,*if_2progn,*if_12progn,*not_symbol;
32
33void *comp_optimize(void *list)
34{
35  void *return_val=list;
36  p_ref r1(list);
37  if (list)
38  {
39    if (CAR(list)==if_symbol)
40    {
41      void *eval1=lcar(lcdr(lcdr(list)));
42      p_ref r2(eval1);
43      void *eval2=lcar(lcdr(lcdr(lcdr(list))));
44      p_ref r3(eval2);
45
46      void *ret=NULL;
47      p_ref r1(ret);
48      if (lcar(list)==eq_symbol && (lcar(lcdr(list))==zero_symbol))  //  simplify (eq 0 x) -> (eq0 x)
49      {
50    push_onto_list(lcar(lcdr(lcdr(list))),ret);
51    push_onto_list(eq0_symbol,ret);
52    return_val=comp_optimize(ret);
53      } else if (lcar(list)==eq_symbol &&
54         (lcar(lcdr(lcdr(list)))==zero_symbol)) //simplify (eq x 0)-> (eq0 x)
55      {
56    push_onto_list(lcar(lcdr(list)),ret);
57    push_onto_list(eq0_symbol,ret);
58    return_val=comp_optimize(ret);
59      } else if (lcar(lcar(lcdr(list)))==not_symbol)  // simplify (if (not y) x z) -> (if y z x)
60      {
61    push_onto_list(lcar(lcdr(lcdr(list))),ret);
62    push_onto_list(lcar(lcdr(lcdr(lcdr(list)))),ret);
63    push_onto_list(lcar(lcdr(lcar(lcdr(list)))),ret);
64    push_onto_list(if_symbol,ret);
65    return_val=comp_optimize(ret);
66      }
67      else if (lcar(eval1)==progn_symbol && (eval2==NULL ||
68                         item_type(eval2)!=L_CONS_CELL))
69      {
70    push_onto_list(eval2,ret);
71    push_onto_list(lcdr(eval1),ret);
72    push_onto_list(lcar(lcdr(list)),ret);
73    push_onto_list(if_1progn,ret);
74    return_val=comp_optimize(ret);
75      } else if (lcar(eval1)==progn_symbol && lcar(eval2)==progn_symbol)
76      {
77    push_onto_list(lcdr(eval2),ret);
78    push_onto_list(lcdr(eval1),ret);
79    push_onto_list(lcar(lcdr(list)),ret);
80    push_onto_list(if_12progn,ret);
81    return_val=comp_optimize(ret);
82      } else if (lcar(eval2)==progn_symbol)
83      {
84    push_onto_list(lcdr(eval2),ret);
85    push_onto_list(eval1,ret);
86    push_onto_list(lcar(lcdr(list)),ret);
87    push_onto_list(if_2progn,ret);
88    return_val=comp_optimize(ret);
89      }
90
91    }
92  }
93  return return_val;
94}
95
96void l_comp_init()
97{
98    // This needs to be defined first
99    l_undefined = LispSymbol::FindOrCreate(":UNDEFINED");
100
101    // Collection problems result if we don't do this
102    ((LispSymbol *)l_undefined)->function = NULL;
103    ((LispSymbol *)l_undefined)->value = NULL;
104
105    true_symbol = LispSymbol::FindOrCreate("T");
106
107    list_symbol = LispSymbol::FindOrCreate("list");
108    string_symbol = LispSymbol::FindOrCreate("string");
109    quote_symbol = LispSymbol::FindOrCreate("quote");
110    backquote_symbol = LispSymbol::FindOrCreate("backquote");
111    comma_symbol = LispSymbol::FindOrCreate("comma");
112    in_symbol = LispSymbol::FindOrCreate("in");
113    do_symbol = LispSymbol::FindOrCreate("do");
114    aref_symbol = LispSymbol::FindOrCreate("aref");
115    colon_initial_contents = LispSymbol::FindOrCreate(":initial-contents");
116    colon_initial_element = LispSymbol::FindOrCreate(":initial-element");
117
118    if_1progn = LispSymbol::FindOrCreate("if-1progn");
119    if_2progn = LispSymbol::FindOrCreate("if-2progn");
120    if_12progn = LispSymbol::FindOrCreate("if-12progn");
121    if_symbol = LispSymbol::FindOrCreate("if");
122    progn_symbol = LispSymbol::FindOrCreate("progn");
123    not_symbol = LispSymbol::FindOrCreate("not");
124    eq_symbol = LispSymbol::FindOrCreate("eq");
125    zero_symbol = LispSymbol::FindOrCreate("0");
126    eq0_symbol = LispSymbol::FindOrCreate("eq0");
127    car_symbol = LispSymbol::FindOrCreate("car");
128    cdr_symbol = LispSymbol::FindOrCreate("cdr");
129    load_warning = LispSymbol::FindOrCreate("load_warning");
130}
131
Note: See TracBrowser for help on using the repository browser.