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

Last change on this file since 481 was 481, checked in by Sam Hocevar, 7 years ago

Fuck the history, I'm renaming all .hpp files to .h for my own sanity.

File size: 3.9 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
93
94void l_comp_init()
95{
96  l_undefined=make_find_symbol(":UNDEFINED");  // this needs to be defined first
97  ((lisp_symbol *)l_undefined)->function=NULL;  // collection problems result if we don't do this
98  ((lisp_symbol *)l_undefined)->value=NULL;
99
100
101  true_symbol=make_find_symbol("T");
102
103
104  list_symbol=make_find_symbol("list");
105  string_symbol=make_find_symbol("string");
106  quote_symbol=make_find_symbol("quote");
107  backquote_symbol=make_find_symbol("backquote");
108  comma_symbol=make_find_symbol("comma");
109  in_symbol=make_find_symbol("in");
110  do_symbol=make_find_symbol("do");
111  aref_symbol=make_find_symbol("aref");
112  colon_initial_contents=make_find_symbol(":initial-contents");
113  colon_initial_element=make_find_symbol(":initial-element");
114
115  if_1progn=make_find_symbol("if-1progn");
116  if_2progn=make_find_symbol("if-2progn");
117  if_12progn=make_find_symbol("if-12progn");
118  if_symbol=make_find_symbol("if");
119  progn_symbol=make_find_symbol("progn");
120  not_symbol=make_find_symbol("not");
121  eq_symbol=make_find_symbol("eq");
122  zero_symbol=make_find_symbol("0");
123  eq0_symbol=make_find_symbol("eq0");
124  car_symbol=make_find_symbol("car");
125  cdr_symbol=make_find_symbol("cdr");
126  load_warning=make_find_symbol("load_warning");
127}
Note: See TracBrowser for help on using the repository browser.