mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 13:06:59 -04:00
naming style (variable_to_string); nicer error messages for problems during dependency checking
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@67 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -72,7 +72,7 @@ ScriptValueP Context::eval(const Script& script, bool useScope) {
|
||||
// Get a variable
|
||||
case I_GET_VAR: {
|
||||
ScriptValueP value = variables[i.data].value;
|
||||
if (!value) throw ScriptError(_("Variable not set: ") + variableToString(i.data));
|
||||
if (!value) throw ScriptError(_("Variable not set: ") + variable_to_string(i.data));
|
||||
stack.push_back(value);
|
||||
break;
|
||||
}
|
||||
@@ -163,7 +163,7 @@ ScriptValueP Context::eval(const Script& script, bool useScope) {
|
||||
}
|
||||
|
||||
void Context::setVariable(const String& name, const ScriptValueP& value) {
|
||||
setVariable(stringToVariable(name), value);
|
||||
setVariable(string_to_variable(name), value);
|
||||
}
|
||||
|
||||
void Context::setVariable(int name, const ScriptValueP& value) {
|
||||
@@ -178,13 +178,13 @@ void Context::setVariable(int name, const ScriptValueP& value) {
|
||||
}
|
||||
|
||||
ScriptValueP Context::getVariable(const String& name) {
|
||||
ScriptValueP value = variables[stringToVariable(name)].value;
|
||||
ScriptValueP value = variables[string_to_variable(name)].value;
|
||||
if (!value) throw ScriptError(_("Variable not set: ") + name);
|
||||
return value;
|
||||
}
|
||||
|
||||
ScriptValueP Context::getVariableOpt(const String& name) {
|
||||
return variables[stringToVariable(name)].value;
|
||||
return variables[string_to_variable(name)].value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ class Context {
|
||||
Variable value; ///< Old value of that variable.
|
||||
};
|
||||
private:
|
||||
/// Variables, indexed by integer naem (using stringToVariable)
|
||||
/// Variables, indexed by integer naem (using string_to_variable)
|
||||
VectorIntMap<unsigned int, Variable> variables;
|
||||
/// Shadowed variable bindings
|
||||
vector<Binding> shadowed;
|
||||
|
||||
@@ -64,6 +64,20 @@ ScriptValueP unified(const ScriptValueP& a, const ScriptValueP& b) {
|
||||
else return new_intrusive2<DependencyUnion>(a,b);
|
||||
}
|
||||
|
||||
/// Behaves like script_nil, but with a name
|
||||
class ScriptMissingVariable : public ScriptValue {
|
||||
public:
|
||||
ScriptMissingVariable(const String& name) : name(name) {}
|
||||
virtual ScriptType type() const { return SCRIPT_NIL; }
|
||||
virtual String typeName() const { return _("missing variable '") + name + _("'"); }
|
||||
virtual operator String() const { return wxEmptyString; }
|
||||
virtual operator double() const { return 0.0; }
|
||||
virtual operator int() const { return 0; }
|
||||
virtual ScriptValueP eval(Context&) const { return script_nil; } // nil() == nil
|
||||
private:
|
||||
String name; ///< Name of the variable
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : Jump record
|
||||
|
||||
// Utility class: a jump that has been postponed
|
||||
@@ -245,7 +259,7 @@ ScriptValueP Context::dependencies(const Dependency& dep, const Script& script)
|
||||
// Get a variable (almost as normal)
|
||||
case I_GET_VAR: {
|
||||
ScriptValueP value = variables[i.data].value;
|
||||
if (!value) value = script_nil; // no errors here
|
||||
if (!value) value = new_intrusive1<ScriptMissingVariable>(variable_to_string(i.data)); // no errors here
|
||||
stack.push_back(value);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -355,7 +355,7 @@ void parseExpr(TokenIterator& input, Script& script, Precedence minPrec) {
|
||||
script.addInstruction(I_LOOP, 0xFFFF); // loop
|
||||
expectToken(input, _("do")); // do
|
||||
script.addInstruction(I_SET_VAR,
|
||||
stringToVariable(name.value)); // set name
|
||||
string_to_variable(name.value));// set name
|
||||
script.addInstruction(I_POP); // pop
|
||||
parseOper(input, script, PREC_SET, I_BINARY, I_ADD);// CCC; add
|
||||
script.addInstruction(I_JUMP, lblStart); // jump lbl_start
|
||||
@@ -373,7 +373,7 @@ void parseExpr(TokenIterator& input, Script& script, Precedence minPrec) {
|
||||
script.addInstruction(I_LOOP, 0xFFFF); // loop
|
||||
expectToken(input, _("do")); // do
|
||||
script.addInstruction(I_SET_VAR,
|
||||
stringToVariable(name.value)); // set name
|
||||
string_to_variable(name.value));// set name
|
||||
script.addInstruction(I_POP); // pop
|
||||
parseOper(input, script, PREC_SET, I_BINARY, I_ADD);// DDD; add
|
||||
script.addInstruction(I_JUMP, lblStart); // jump lbl_start
|
||||
@@ -391,7 +391,7 @@ void parseExpr(TokenIterator& input, Script& script, Precedence minPrec) {
|
||||
script.addInstruction(I_TERNARY, I_RGB);
|
||||
} else {
|
||||
// variable
|
||||
unsigned int var = stringToVariable(token.value);
|
||||
unsigned int var = string_to_variable(token.value);
|
||||
script.addInstruction(I_GET_VAR, var);
|
||||
}
|
||||
} else if (token == TOK_INT) {
|
||||
@@ -475,13 +475,13 @@ void parseOper(TokenIterator& input, Script& script, Precedence minPrec, Instruc
|
||||
while (t != _(")")) {
|
||||
if (input.peek(2) == _(":")) {
|
||||
// name: ...
|
||||
arguments.push_back(stringToVariable(t.value));
|
||||
arguments.push_back(string_to_variable(t.value));
|
||||
input.read(); // skip the name
|
||||
input.read(); // and the :
|
||||
parseOper(input, script, PREC_SEQ);
|
||||
} else {
|
||||
// implicit "input" argument
|
||||
arguments.push_back(stringToVariable(_("input")));
|
||||
arguments.push_back(string_to_variable(_("input")));
|
||||
parseOper(input, script, PREC_SEQ);
|
||||
}
|
||||
t = input.peek();
|
||||
|
||||
@@ -17,7 +17,7 @@ Variables variables;
|
||||
DECLARE_TYPEOF(Variables);
|
||||
|
||||
/// Return a unique name for a variable to allow for faster loopups
|
||||
unsigned int stringToVariable(const String& s) {
|
||||
unsigned int string_to_variable(const String& s) {
|
||||
map<String, unsigned int>::iterator it = variables.find(s);
|
||||
if (it == variables.end()) {
|
||||
unsigned int v = (unsigned int)variables.size();
|
||||
@@ -31,7 +31,7 @@ unsigned int stringToVariable(const String& s) {
|
||||
/// Get the name of a vaiable
|
||||
/** Warning: this function is slow, it should only be used for error messages and such.
|
||||
*/
|
||||
String variableToString(unsigned int v) {
|
||||
String variable_to_string(unsigned int v) {
|
||||
FOR_EACH(vi, variables) {
|
||||
if (vi.second == v) return vi.first;
|
||||
}
|
||||
@@ -159,7 +159,7 @@ String Script::dumpInstr(unsigned int pos, Instruction i) const {
|
||||
ret += "\t" + lexical_cast<String>(i.data);
|
||||
break;
|
||||
case I_GET_VAR: case I_SET_VAR: case I_NOP: // variable
|
||||
ret += "\t" + variableToString(i.data) + "\t$" + lexical_cast<String>(i.data);
|
||||
ret += "\t" + variable_to_string(i.data) + "\t$" + lexical_cast<String>(i.data);
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
|
||||
@@ -93,12 +93,12 @@ struct Instruction {
|
||||
// ----------------------------------------------------------------------------- : Variables
|
||||
|
||||
/// Return a unique name for a variable to allow for faster loopups
|
||||
unsigned int stringToVariable(const String& s);
|
||||
unsigned int string_to_variable(const String& s);
|
||||
|
||||
/// Get the name of a vaiable
|
||||
/** Warning: this function is slow, it should only be used for error messages and such.
|
||||
*/
|
||||
String variableToString(unsigned int v);
|
||||
String variable_to_string(unsigned int v);
|
||||
|
||||
// ----------------------------------------------------------------------------- : Script
|
||||
|
||||
|
||||
Reference in New Issue
Block a user