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

Last change on this file since 524 was 524, checked in by Sam Hocevar, 11 years ago

core: Get rid of mostly useless headers, move endianness handling to
common.h (and rewrite functions so that they do not need the SDL headers)
and move a few functions out of sdlport's video.cpp. These functions
were in the original video.cpp (which reappears) and shouldn't be part
of the SDL port.

File size: 4.1 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com or
8 *  Jonathan Clark.
9 */
10
11#include "config.h"
12
13#ifdef NO_LIBS
14#include "fakelib.h"
15#endif
16
17#include "lisp.h"
18#include "lisp_gc.h"
19
20LObject *l_undefined;
21LSymbol *true_symbol = NULL, *list_symbol, *string_symbol, *quote_symbol,
22     *backquote_symbol, *comma_symbol, *do_symbol, *in_symbol, *aref_symbol,
23     *if_symbol, *progn_symbol, *car_symbol, *cdr_symbol;
24
25void *colon_initial_contents, *colon_initial_element,
26     *eq_symbol, *zero_symbol, *eq0_symbol, *load_warning;
27
28void *if_1progn,*if_2progn,*if_12progn,*not_symbol;
29
30void *comp_optimize(void *list)
31{
32  void *return_val=list;
33  PtrRef r1(list);
34  if (list)
35  {
36    if (CAR(list)==if_symbol)
37    {
38      void *eval1=lcar(lcdr(lcdr(list)));
39      PtrRef r2(eval1);
40      void *eval2=lcar(lcdr(lcdr(lcdr(list))));
41      PtrRef r3(eval2);
42
43      void *ret=NULL;
44      PtrRef r4(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 = LSymbol::FindOrCreate(":UNDEFINED");
97
98    // Collection problems result if we don't do this
99    ((LSymbol *)l_undefined)->function = NULL;
100    ((LSymbol *)l_undefined)->value = NULL;
101
102    true_symbol = LSymbol::FindOrCreate("T");
103
104    list_symbol = LSymbol::FindOrCreate("list");
105    string_symbol = LSymbol::FindOrCreate("string");
106    quote_symbol = LSymbol::FindOrCreate("quote");
107    backquote_symbol = LSymbol::FindOrCreate("backquote");
108    comma_symbol = LSymbol::FindOrCreate("comma");
109    in_symbol = LSymbol::FindOrCreate("in");
110    do_symbol = LSymbol::FindOrCreate("do");
111    aref_symbol = LSymbol::FindOrCreate("aref");
112    colon_initial_contents = LSymbol::FindOrCreate(":initial-contents");
113    colon_initial_element = LSymbol::FindOrCreate(":initial-element");
114
115    if_1progn = LSymbol::FindOrCreate("if-1progn");
116    if_2progn = LSymbol::FindOrCreate("if-2progn");
117    if_12progn = LSymbol::FindOrCreate("if-12progn");
118    if_symbol = LSymbol::FindOrCreate("if");
119    progn_symbol = LSymbol::FindOrCreate("progn");
120    not_symbol = LSymbol::FindOrCreate("not");
121    eq_symbol = LSymbol::FindOrCreate("eq");
122    zero_symbol = LSymbol::FindOrCreate("0");
123    eq0_symbol = LSymbol::FindOrCreate("eq0");
124    car_symbol = LSymbol::FindOrCreate("car");
125    cdr_symbol = LSymbol::FindOrCreate("cdr");
126    load_warning = LSymbol::FindOrCreate("load_warning");
127}
128
Note: See TracBrowser for help on using the repository browser.