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

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

style: remove trailing spaces, fix copyright statements.

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