Skip to content

Commit

Permalink
Introduce ruby-like string concatentation
Browse files Browse the repository at this point in the history
- parse \r and \n in strings
- remove 'str'+'str' concatentation
  • Loading branch information
marcelklehr committed May 30, 2012
1 parent 6eb4031 commit 717d5e0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
18 changes: 17 additions & 1 deletion lib/ferrite/scope.js
Expand Up @@ -82,7 +82,23 @@ Scope.prototype.execute = function execute( node ) {
return Primal.Float(this.env, node.value);

case NODE_STR:
return Primal.String(this.env, node.value);
var scope = this;
var str = node.value;
str = str.replace('\\r', "\r");
str = str.replace('\\n', "\n");
str = str.replace('\\\r', "\\r");
str = str.replace('\\\n', "\\n");

str = '%'+str; // add padding
str = str.replace(/[^\\]#{(.*?)}/gi, function(str, expression, offset) {
var opcode = [], error_offs = [];
var code = '('+expression+')';
if((err_count = __parse( code, error_offs, [], opcode ) ) > 0) throw MagnetError('Syntax Error: String concatentation failed. (Unexpected '+code[error_offs[0]]+')', node.offset+offset+2+error_offs[0], scope);
return str[0]+scope.env.cast('string', scope.env.resolve(scope.execute(opcode[0].value))).getVal();
});
str = str.substr(1); // remove padding
str = str.replace('\\#{', "#{");
return Primal.String(this.env, str);


case NODE_OBJ:
Expand Down
7 changes: 0 additions & 7 deletions lib/ferrite/type_string.js
Expand Up @@ -15,13 +15,6 @@ Primal.protoString = function(env) {
return Primal.Boolean(env, bool);
});

// +
object.properties['+'] = Primal.NativeFunction(env, function(precScope, obj) {
var str;
str = env.cast('string', this).getVal() + env.cast('string', env.resolve(obj)).getVal();
return Primal.String(env, str);
});

// *
object.properties['*'] = Primal.NativeFunction(env, function(precScope, obj) {
var count = env.cast('integer', env.resolve(obj)).getVal();
Expand Down
4 changes: 2 additions & 2 deletions lib/grammar.par
Expand Up @@ -41,7 +41,7 @@
'/' [* %match = %offset; *]
'\*' [* %match = %offset; *]
'#' [* %match = %offset; *]
'(\'([^\']|\\\')*\')|("([^"]|\\")*")' String [* %match = %match.substr( 1, %match.length - 2 ); %match = %match.replace( /''/g, "\'" ); *]
'(\'([^\']|\\\')*\')|("([^"]|\\")*")' String [* %match = { string: %match.substr( 1, %match.length - 2 ).replace('\\'+%match[0], %match[0]), offset: %offset}; *]
'[0-9]+' Integer [* %match = parseInt(%match); *]
'[0-9]+\.[0-9]+' Float [* %match = parseFloat(%match); *]
"true|false" Boolean [* %match = (%match == 'true'); *]
Expand Down Expand Up @@ -102,7 +102,7 @@ NegExp: '-' value [* %% = new Node( NODE_OP, OP_NEG, [%2] ); *]
value: Integer [* %% = new Node( NODE_INT, %1 ); *]
| Float [* %% = new Node( NODE_FLOAT, %1 ); *]
| Boolean [* %% = new Node( NODE_BOOL, %1 ); *]
| String [* %% = new Node( NODE_STR, %1 ); *]
| String [* %% = new Node( NODE_STR, %1.string, [], %1.offset ); *]
| Nil
| function-call
| variable [* %% = new Node( NODE_VAR, %1); *]
Expand Down
20 changes: 7 additions & 13 deletions sample/test.mg
Expand Up @@ -95,7 +95,7 @@ poped = list.pop()
list.unshift(shifted)
list.push(poped)
if list.#(0) == 1 && list.#(8) == 9 {
puts 'lists work'
puts 'Lists work.'
}

/**
Expand Down Expand Up @@ -128,19 +128,13 @@ if i == 10 {

'Hello'.split().each({char :: puts char})

puts "5+5=#{ 5+5 }"
puts "5*5=#{ 5*5 }"
puts "5/5=#{ 5/5 }"
puts "(5+5)/5=#{ (5+5)/5 }"

fork = 'Hello'.fork()
fork.#("+") = {"G'Day"}
puts fork // should output "Hello"
puts fork+" World" // should output "G'Day"

puts "5+5="+(5+5)
puts "5*5="+(5*5)
puts "5/5="+(5/5)
puts "(5+5)/5="+((5+5)/5)

test = 5+" times"
puts '5+" times" = '+test
test = "#{5} times"
puts '"\#{5} times" = #{ test }'

/* crazycccc should be a regular function */
crazy = {'hello'}.copy()
Expand Down

0 comments on commit 717d5e0

Please sign in to comment.