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

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