Skip to content

Commit

Permalink
Refactor|libdeng2|ScriptedInfo: Use the semantic hint when evaluating…
Browse files Browse the repository at this point in the history
… values

Updated the test app with more examples of scriptability.
  • Loading branch information
skyjake committed May 10, 2013
1 parent 3d68efb commit 03babec
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
20 changes: 9 additions & 11 deletions doomsday/libdeng2/src/scriptsys/scriptedinfo.cpp
Expand Up @@ -26,6 +26,8 @@ namespace de {

DENG2_PIMPL(ScriptedInfo)
{
typedef Info::Element::Value InfoValue;

Info info; ///< Original full parsed contents.
QScopedPointer<Script> script; ///< Current script being executed.
Process process; ///< Execution context.
Expand Down Expand Up @@ -82,6 +84,7 @@ DENG2_PIMPL(ScriptedInfo)
}
}

// Script blocks are executed now.
if(block.blockType() == "script")
{
DENG2_ASSERT(block.find("script") != 0);
Expand All @@ -103,9 +106,9 @@ DENG2_PIMPL(ScriptedInfo)

foreach(Info::Element const *sub, block.contentsInOrder())
{
// Handle special elements.
if(sub->name() == "condition")
{
// Skip special elements.
continue;
}
if(sub->name() == ":" && !block.name().isEmpty())
Expand Down Expand Up @@ -138,11 +141,6 @@ DENG2_PIMPL(ScriptedInfo)
return varName;
}

static bool isEvaluatable(String const &value)
{
return value.startsWith('$');
}

Value *evaluate(String const &source)
{
script.reset(new Script(source));
Expand All @@ -151,13 +149,13 @@ DENG2_PIMPL(ScriptedInfo)
return process.context().evaluator().result().duplicate();
}

Value *makeValue(String const &rawValue)
Value *makeValue(InfoValue const &rawValue)
{
if(isEvaluatable(rawValue))
if(rawValue.flags.testFlag(InfoValue::Script))
{
return evaluate(rawValue.substr(1));
return evaluate(rawValue.text);
}
return new TextValue(rawValue);
return new TextValue(rawValue.text);
}

void processKey(Info::KeyElement const &key)
Expand All @@ -169,7 +167,7 @@ DENG2_PIMPL(ScriptedInfo)
void processList(Info::ListElement const &list)
{
ArrayValue* av = new ArrayValue;
foreach(QString v, list.values())
foreach(InfoValue const &v, list.values())
{
*av << makeValue(v);
}
Expand Down
33 changes: 24 additions & 9 deletions doomsday/tests/test_info/test_info.dei
Expand Up @@ -3,8 +3,16 @@
# - weight: normal bold light
# - style: normal italic

# Version module is available for embedded scripts.
script (import Version; print "Imported")
script {
# Version module is available for embedded scripts.
import Version

def joinArray(a)
result = ''
for i in a: result += i
return result
end
}

script "condition testing" condition "Version.OS == 'macx'" {
print "Running on %s!" % Version.OS
Expand All @@ -13,20 +21,27 @@ script "condition testing" condition "Version.OS == 'macx'" {
end
}

script test2 condition False { print "WILL NOT BE RUN" }

# Nameless groups are allowed.
group {
condition: False
script test2 condition True { print "WILL NOT BE RUN" }
}

group font {
condition: Version.OS == 'macx'

preferences < font.default, $ " 'font'+'.'+'title' " >

font default {
family: $ "Lucida " + 'Grande'
size: 16pt
family $= "'Lucida ' + ''Grande''"
size $: joinArray(['16', 'pt'])
weight = normal
style: normal
style: normal
}

# Identifiers are all case insensitive.
Font "Title" : font.default {
size: 24pt
weight: bold
Size: 24pt
Weight: bold
}
}

0 comments on commit 03babec

Please sign in to comment.