source: abuse/trunk/src/parse.cpp @ 555

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

ps3: make everything compile on the PS3. Of course, nothing links yet
because so much support is missing.

File size: 3.3 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, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <ctype.h>
18
19#include "parse.h"
20
21char *ttype[]={ "END","NUMBER","STRING","WORD","OPERATOR","LEFT BRACE","RIGHT BRACE",
22            "LEFT PAREN","RIGHT_PAREN","ASSIGNMENT","COMMA"};
23
24void match_right(char *&s)
25{
26  while (1)
27  {
28    switch (token_type(s))
29    {
30      case sLEFT_PAREN : next_token(s); match_right(s); break;
31      case sRIGHT_PAREN : next_token(s); return ; break;
32      default :
33        next_token(s);
34    }
35  }
36}
37
38
39void expect(int thing, int type, char *where)
40{
41  if (thing!=type)
42  {
43    printf("Expecting %s (not %s) at %s\n",ttype[type],ttype[thing],where);
44    exit(1);
45  }
46}
47
48
49int token_type(char *s);
50
51
52void skip_space(char *&s)
53{
54  while (*s && (*s==' ' || *s=='\n' || *s=='\t' || *s=='\r')) s++;
55
56  if (*s=='/' && s[1]=='*')
57  {
58    s+=2;
59    while (*s && (*s!='*' || s[1]!='/')) s++;
60    s+=2;
61    skip_space(s);
62  }
63}
64
65
66int get_token(char *&s, char *buffer)  // returns token type
67{
68  // skip any starting spaces
69  skip_space(s);
70
71  switch (*s)
72  {
73    case 0    : *buffer=0; return sEND;                                  break;
74    case '{ '  : *(buffer++)=*(s++); *buffer=0; return sLEFT_BRACE;       break;
75    case '}'  : *(buffer++)=*(s++); *buffer=0; return sRIGHT_BRACE;      break;
76    case '('  : *(buffer++)=*(s++); *buffer=0; return sLEFT_PAREN;       break;
77    case ')'  : *(buffer++)=*(s++); *buffer=0; return sRIGHT_PAREN;      break;
78    case '='  : *(buffer++)=*(s++); *buffer=0; return sASSIGNMENT;       break;
79    case ','  : *(buffer++)=*(s++); *buffer=0; return sCOMMA;            break;
80    case '+' :
81    case '-' :
82    case '*' :
83    case '/' : *(buffer++)=*(s++); *buffer=0; return sOPERATOR;          break;
84    default :
85    {
86      if (isdigit(*s))
87      {
88    while (isdigit(*s))
89    *(buffer++)=*(s++);
90    *buffer=0;
91    return sNUMBER;
92      } else if (*s=='"')
93      {
94    while (*s=='"')
95    {
96      s++;
97      while (*s && *s!='"')
98        *(buffer++)=*(s++);
99         if (*s) s++;
100      skip_space(s);
101    }
102    *buffer=0;
103    return sSTRING;
104      } else
105      {
106    *(buffer++)=*(s++);      // take the first character, no matter what it is
107                             // because nobody else will
108    while (*s && isalnum(*s) || *s=='_' || *s=='.')
109    *(buffer++)=*(s++);
110    *buffer=0;
111    return sWORD;
112      }
113    }
114  }
115}
116
117
118int token_type(char *s)
119{
120  char tmp[100];
121  return get_token(s,tmp);
122}
123
124
125void next_token(char *&s)
126{
127  char tmp[100];
128  get_token(s,tmp);
129}
130
131int get_number(char *&s)
132{
133  char tmp[100];
134
135  if (get_token(s,tmp)==sOPERATOR)
136  {
137    if (tmp[0]=='-')
138    {
139      expect(get_token(s,tmp),sNUMBER,s);
140      return -atoi(tmp);
141    }
142    else expect(sWORD,sNUMBER,s);
143  }
144  return atoi(tmp);
145}
146
147
148void get_filename(char *&s, char *buffer)
149{
150  char *b=buffer;
151  skip_space(s);
152  while (*s && *s!=' ' && *s!='\n' && *s!='\t' && *s!='\r' && *s!=')' && *s!='(')
153    *(b++)=*(s++);
154  *b=0;
155}
156
157
158
159
160
161
162
163
164
165
166
167
168
Note: See TracBrowser for help on using the repository browser.