aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlyssa Milburn2011-12-02 00:01:06 +0100
committerAlyssa Milburn2011-12-02 00:01:06 +0100
commit9b00b3d5b745801241a6d9b9e1383413e0c85ba6 (patch)
tree1411470db753add606d4a72a192d3e1815c4a0b2
parent446822369b671c96185cd721c972537d31aebf6c (diff)
downloadscummvm-rg350-9b00b3d5b745801241a6d9b9e1383413e0c85ba6.tar.gz
scummvm-rg350-9b00b3d5b745801241a6d9b9e1383413e0c85ba6.tar.bz2
scummvm-rg350-9b00b3d5b745801241a6d9b9e1383413e0c85ba6.zip
MOHAWK: LB list improvements, implement &= operator.
-rw-r--r--engines/mohawk/livingbooks_code.cpp73
-rw-r--r--engines/mohawk/livingbooks_code.h2
2 files changed, 59 insertions, 16 deletions
diff --git a/engines/mohawk/livingbooks_code.cpp b/engines/mohawk/livingbooks_code.cpp
index 60ace17205..86f5a06178 100644
--- a/engines/mohawk/livingbooks_code.cpp
+++ b/engines/mohawk/livingbooks_code.cpp
@@ -490,8 +490,8 @@ void LBCode::parseMain() {
break;
}
bool indexing = false;
- LBValue index;
- if (_currToken == kTokenListStart) {
+ Common::Array<LBValue> index;
+ while (_currToken == kTokenListStart) {
debugN("[");
nextToken();
parseStatement();
@@ -502,7 +502,7 @@ void LBCode::parseMain() {
if (!_stack.size())
error("index failed");
indexing = true;
- index = _stack.pop();
+ index.push_back(_stack.pop());
}
if (_currToken == kTokenAssign) {
debugN(" = ");
@@ -515,11 +515,29 @@ void LBCode::parseMain() {
val = getIndexedVar(varname, index);
else
val = &_vm->_variables[varname];
- if (val)
+ if (val) {
*val = _stack.pop();
+ _stack.push(*val);
+ } else
+ _stack.push(LBValue());
+ } else if (_currToken == kTokenAndEquals) {
+ debugN(" &= ");
+ nextToken();
+ parseStatement();
+ if (!_stack.size())
+ error("assignment failed");
+ LBValue *val;
+ if (indexing)
+ val = getIndexedVar(varname, index);
else
- *val = LBValue();
- _stack.push(*val);
+ val = &_vm->_variables[varname];
+ if (val) {
+ if (val->type != kLBValueString)
+ error("operator &= used on non-string");
+ val->string = val->string + _stack.pop().toString();
+ _stack.push(*val);
+ } else
+ _stack.push(LBValue());
} else {
if (indexing) {
LBValue *val = getIndexedVar(varname, index);
@@ -619,6 +637,28 @@ void LBCode::parseMain() {
nextToken();
break;
+ case kTokenListStart:
+ debugN("[");
+ nextToken();
+ {
+ Common::SharedPtr<LBList> list = Common::SharedPtr<LBList>(new LBList);
+ while (_currToken != kTokenListEnd) {
+ parseStatement();
+ if (!_stack.size())
+ error("unexpected empty stack during literal list evaluation");
+ list->array.push_back(_stack.pop());
+ if (_currToken == kTokenComma) {
+ debugN(", ");
+ nextToken();
+ } else if (_currToken != kTokenListEnd)
+ error("encountered unexpected token %02x during literal list", _currToken);
+ }
+ debugN("]");
+ nextToken();
+ _stack.push(list);
+ }
+ break;
+
case kTokenNot:
debugN("!");
nextToken();
@@ -659,15 +699,18 @@ void LBCode::parseMain() {
}
}
-LBValue *LBCode::getIndexedVar(Common::String varname, const LBValue &index) {
- LBValue &var = _vm->_variables[varname];
- if (var.type != kLBValueList)
- error("variable '%s' was indexed, but isn't a list", varname.c_str());
- if (index.type != kLBValueInteger)
- error("index wasn't an integer");
- if (index.integer < 1 || index.integer > (int)var.list->array.size())
- return NULL;
- return &var.list->array[index.integer - 1];
+LBValue *LBCode::getIndexedVar(Common::String varname, const Common::Array<LBValue> &index) {
+ LBValue *var = &_vm->_variables[varname];
+ for (uint i = 0; i < index.size(); i++) {
+ if (var->type != kLBValueList)
+ error("variable '%s' was indexed, but isn't a list after %d indexes", varname.c_str(), i);
+ if (index[i].type != kLBValueInteger)
+ error("index %d wasn't an integer", i);
+ if (index[i].integer < 1 || index[i].integer > (int)var->list->array.size())
+ return NULL;
+ var = &var->list->array[index[i].integer - 1];
+ }
+ return var;
}
LBItem *LBCode::resolveItem(const LBValue &value) {
diff --git a/engines/mohawk/livingbooks_code.h b/engines/mohawk/livingbooks_code.h
index bfe047bf23..ee61c2add2 100644
--- a/engines/mohawk/livingbooks_code.h
+++ b/engines/mohawk/livingbooks_code.h
@@ -232,7 +232,7 @@ protected:
void parseArithmetic2();
void parseMain();
- LBValue *getIndexedVar(Common::String varname, const LBValue &index);
+ LBValue *getIndexedVar(Common::String varname, const Common::Array<LBValue> &index);
LBItem *resolveItem(const LBValue &value);
Common::Array<LBValue> readParams();
Common::Rect getRectFromParams(const Common::Array<LBValue> &params);