scummvm/engines/private/symbol.cpp

162 lines
3.5 KiB
C++
Raw Normal View History

2021-01-01 12:22:05 -03:00
#include "common/debug.h"
#include "grammar.h"
#include "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;
StringQueue stringToDefine;
RectQueue rectToDefine;
2021-01-01 12:22:05 -03:00
void define(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
}
2020-12-30 15:41:29 -03:00
char *emalloc(unsigned n) /* check return from malloc */
{
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
}
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)
debug("%s",s->name->c_str());
else
debug("%s %d", s->name->c_str(), s->type);
}
2021-01-01 12:22:05 -03:00
Symbol *lookup(Common::String s, SymbolMap symlist) /* find s in symbol table symlist */
{
//debug("looking up %s", s.c_str());
return symlist.getVal(s);
2021-01-02 00:58:58 -03:00
}
Symbol *lookupName(char *n) /* install s in some symbol table */
{
//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 {
debug("WARNING: %s not defined", n);
return addconstant(NAME, 0, n);
}
2021-01-01 12:22:05 -03:00
}
2021-01-02 00:58:58 -03:00
2021-01-01 12:22:05 -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);
}
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);
}
else if (strcmp(n, "rects") == 0) {
assert(r != NULL);
install(s, NAME, 0, NULL, r, &rects);
}
else
assert(0);
2021-01-01 12:22:05 -03:00
}
2021-01-01 12:22:05 -03:00
}
Symbol *addconstant(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-01 12:22:05 -03:00
Symbol *install(Common::String *n, int t, int d, char *s, Common::Rect *r, SymbolMap *symlist) /* install s in symbol table */
{
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
}