scummvm/engines/private/symbol.cpp

186 lines
4.6 KiB
C++
Raw Normal View History

2021-02-13 14:55:30 -03:00
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
2021-01-01 12:22:05 -03:00
#include "common/debug.h"
#include "private/grammar.h"
#include "private/grammar.tab.h"
2020-12-30 19:33:25 -03:00
namespace Private {
2021-01-01 12:22:05 -03:00
SymbolMap settings, variables, cursors, locations, rects;
ConstantList constants;
2021-02-07 14:08:28 -03:00
NameList variableList;
NameList locationList;
2021-01-12 20:49:12 -03:00
StringQueue stringToDefine;
RectQueue rectToDefine;
2021-01-01 12:22:05 -03:00
2021-01-10 20:44:27 -03:00
void defineSymbol(char *n, Common::Rect *r) {
Common::String *s = new Common::String(n);
stringToDefine.push(*s);
rectToDefine.push(r);
2021-01-01 12:22:05 -03:00
}
2021-01-10 20:44:27 -03:00
char *emalloc(unsigned n) {
char *p;
2020-12-30 15:41:29 -03:00
p = (char*)malloc(n);
assert(p != NULL);
return p;
2020-12-30 15:41:29 -03:00
}
2021-01-10 20:44:27 -03:00
void showSymbol(Symbol *s) {
if (s->type == NUM)
debug("%s %d",s->name->c_str(), s->u.val);
else if (s->type == STRING)
debug("%s %s", s->name->c_str(), s->u.str);
else if (s->type == NAME)
2021-01-09 16:11:30 -03:00
debug("%s %d",s->name->c_str(), s->type);
else
debug("%s %d", s->name->c_str(), s->type);
}
2021-01-10 15:38:10 -03:00
void setSymbol(Symbol *s, int v) {
s->u.val = v;
}
2021-01-10 20:44:27 -03:00
/* find s in symbol table symlist */
Symbol *lookup(Common::String s, SymbolMap symlist) {
2021-01-09 16:11:30 -03:00
Symbol *r = symlist.getVal(s);
return r;
2021-01-02 00:58:58 -03:00
}
2021-01-10 20:44:27 -03:00
/* lookup some name in some symbol table */
Symbol *lookupName(char *n) {
//debug("looking up %s", n);
Common::String *s = new Common::String(n);
2021-01-02 00:58:58 -03:00
if (settings.contains(*s))
return lookup(*s, settings);
2021-01-02 00:58:58 -03:00
else if (variables.contains(*s))
return lookup(*s, variables);
2021-01-02 00:58:58 -03:00
else if (cursors.contains(*s))
return lookup(*s, cursors);
2021-01-02 00:58:58 -03:00
else if (locations.contains(*s))
return lookup(*s, locations);
2021-01-02 00:58:58 -03:00
else if (rects.contains(*s))
return lookup(*s, rects);
else {
2021-01-16 15:30:00 -03:00
debug("WARNING: %s not defined", s->c_str());
return constant(STRING, 0, (char*) s->c_str());
}
2021-01-01 12:22:05 -03:00
}
2021-01-02 00:58:58 -03:00
2021-01-10 20:44:27 -03:00
void installAll(char *n) {
Common::String *s;
Common::Rect *r;
assert(stringToDefine.size() > 0);
while (!stringToDefine.empty()) {
s = new Common::String(stringToDefine.pop());
r = rectToDefine.pop();
//debug("name %s", s->c_str());
if (strcmp(n, "settings") == 0) {
assert(r == NULL);
install(s, STRING, 0, (char*) s->c_str(), r, &settings);
}
else if (strcmp(n, "variables") == 0) {
assert(r == NULL);
install(s, NAME, 0, NULL, r, &variables);
2021-01-12 20:49:12 -03:00
variableList.push_front(*s);
}
else if (strcmp(n, "cursors") == 0) {
assert(r == NULL);
install(s, NAME, 0, NULL, r, &cursors);
}
else if (strcmp(n, "locations") == 0) {
assert(r == NULL);
install(s, NAME, 0, NULL, r, &locations);
2021-02-07 14:08:28 -03:00
locationList.push_front(*s);
}
else if (strcmp(n, "rects") == 0) {
assert(r != NULL);
2021-01-09 16:11:30 -03:00
install(s, RECT, 0, NULL, r, &rects);
}
else
assert(0);
2021-01-01 12:22:05 -03:00
}
2021-01-01 12:22:05 -03:00
}
2021-01-10 20:44:27 -03:00
Symbol *constant(int t, int d, char *s) {
Symbol *sp;
Common::String *n = new Common::String("<constant>");
sp = (Symbol *)emalloc(sizeof(Symbol));
sp->name = n;
sp->type = t;
if (t == NUM || t == NAME)
sp->u.val = d;
else if (t == STRING)
sp->u.str = s;
else
assert(0);
constants.push_front(sp);
return sp;
}
2021-01-10 20:44:27 -03:00
/* install some symbol s in a symbol table */
Symbol *install(Common::String *n, int t, int d, char *s, Common::Rect *r, SymbolMap *symlist) {
Common::String *name = new Common::String(*n);
Symbol *sp;
sp = (Symbol *)emalloc(sizeof(Symbol));
sp->name = name;
sp->type = t;
if (t == NUM || t == NAME)
sp->u.val = d;
else if (t == STRING)
sp->u.str = s;
else if (t == RECT)
sp->u.rect = r;
else
assert(0);
symlist->setVal(*n, sp);
assert(symlist->size() > 0);
return sp;
}
2020-12-30 19:33:25 -03:00
}