Support operations between strings and fields, support concatenating

strings.
This commit is contained in:
Tom MacWright 2012-06-26 12:14:13 -04:00
parent ea89af6ae2
commit 28ab872e72
6 changed files with 28 additions and 15 deletions

View File

@ -763,6 +763,7 @@ carto.Parser = function Parser(env) {
return new tree.Value(expressions);
}
},
// A sub-expression, contained by parenthensis
sub: function() {
var e;
@ -770,12 +771,15 @@ carto.Parser = function Parser(env) {
return e;
}
},
// This is a misnomer because it actually handles multiplication
// and division.
multiplication: function() {
var m, a, op, operation;
if (m = $(this.operand)) {
while ((op = ($('/') || $('*'))) && (a = $(this.operand))) {
operation = new tree.Operation(op, [operation || m, a], memo);
}
console.log(m);
return operation || m;
}
},
@ -793,9 +797,7 @@ carto.Parser = function Parser(env) {
// An operand is anything that can be part of an operation,
// such as a Color, or a Variable
operand: function() {
return $(this.sub) || $(this.entities.dimension) ||
$(this.entities.color) || $(this.entities.variable) ||
$(this.entities.call);
return $(this.sub) || $(this.entity);
},
// Expressions either represent mathematical operations,

View File

@ -4,8 +4,9 @@ tree.Field = function Field(content) {
this.value = content || '';
this.is = 'field';
};
tree.Field.prototype = {
toString: function(quotes) {
toString: function() {
return '[' + this.value + ']';
},
'eval': function() {

View File

@ -31,16 +31,18 @@ tree.Operation.prototype.eval = function(env) {
}
if (a instanceof tree.Quoted || b instanceof tree.Quoted) {
env.error({
message: 'One cannot add, subtract, divide, or multiply strings.',
index: this.index,
type: 'runtime',
filename: this.filename
});
return {
is: 'undefined',
value: 'undefined'
};
if (this.op === '*' || this.op === '/') {
env.error({
message: 'One cannot add, subtract, divide, or multiply strings.',
index: this.index,
type: 'runtime',
filename: this.filename
});
return {
is: 'undefined',
value: 'undefined'
};
}
}
return a.operate(this.op, b);

View File

@ -10,8 +10,14 @@ tree.Quoted.prototype = {
var xmlvalue = this.value.replace(/\'/g, ''');
return (quotes === true) ? "'" + xmlvalue + "'" : this.value;
},
'eval': function() {
return this;
},
operate: function(op, other) {
return new tree.Quoted(this.quote,
tree.operate(op, this.toString(), other.toString()));
}
};

View File

@ -166,6 +166,8 @@ tree.Reference.validValue = function(env, selector, value) {
}
return true;
}
} else if (tree.Reference.selector(selector).type == 'expression') {
return true;
} else {
if (tree.Reference.selector(selector).validate) {
var valid = false;

View File

@ -1,5 +1,5 @@
#world {
text-name: [NAME];
text-name: "hello " + [NAME];
text-size: 11;
text-face-name: "Georgia Regular", "Arial Italic";
}