Skip to content

Commit

Permalink
Add super mammouth
Browse files Browse the repository at this point in the history
  • Loading branch information
btwael committed Aug 25, 2014
1 parent 02605e9 commit 3df1dda
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 22 deletions.
19 changes: 14 additions & 5 deletions extras/mammouth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3876,16 +3876,19 @@ require["./helpers"] = (function() {var exports = {}, module = {exports: exports
return module.exports;})();
require["./rewriter"] = (function() {var exports = {}, module = {exports: exports};// Generated by CoffeeScript 1.7.1
(function() {
var helpers, nodes;
var SuperMammouth, helpers, nodes;

nodes = require('./nodes');

helpers = require('./helpers');

SuperMammouth = "\nfunction mammouth($func, $arg1, $arg2){if ( $func == '+' ){if((is_string($arg1) && is_numeric($arg2))||(is_string($arg2) && is_numeric($arg1))) {return $arg1.$arg2;} else {return $arg1+$arg2;}}}\n";

exports.rewrite = function(tree, context) {
var ADD, IdCounter, UseSuperMammouth, compile, doc, php, _i, _len;
var ADD, AddSuperMammouth, IdCounter, UseSuperMammouth, code, compile, doc, php, _i, _len;
IdCounter = new helpers.IdCounter;
UseSuperMammouth = false;
AddSuperMammouth = false;
php = '';
ADD = function(string) {
return php += string;
Expand Down Expand Up @@ -4065,7 +4068,7 @@ require["./rewriter"] = (function() {var exports = {}, module = {exports: export
r = compile(element.left) + '.' + compile(element.right);
} else if (element.operator === '+') {
UseSuperMammouth = true;
r = '$Mammouth("+", ' + compile(element.left) + ', ' + compile(element.right) + ')';
r = 'mammouth("+", ' + compile(element.left) + ', ' + compile(element.right) + ')';
} else {
r = compile(element.left) + ' ' + element.operator + ' ' + compile(element.right);
}
Expand Down Expand Up @@ -4106,7 +4109,7 @@ require["./rewriter"] = (function() {var exports = {}, module = {exports: export
r = 'isset(' + compile(element.expression) + ')';
return r;
case 'In':
r = '$Mammouth("in_array", ' + compile(element.left) + ', ' + compile(element.right) + ')';
r = 'mammouth("in_array", ' + compile(element.left) + ', ' + compile(element.right) + ')';
return r;
case 'Echo':
return 'echo ' + compile(element.value);
Expand Down Expand Up @@ -4355,7 +4358,13 @@ require["./rewriter"] = (function() {var exports = {}, module = {exports: export
ADD(doc.toPHP());
break;
case 'MammouthBlock':
ADD('<?php' + compile(doc.body) + '?>');
code = compile(doc.body);
if (UseSuperMammouth && !AddSuperMammouth) {
code = SuperMammouth + code;
AddSuperMammouth = true;
UseSuperMammouth = false;
}
ADD('<?php' + code + '?>');
}
}
return php;
Expand Down
2 changes: 1 addition & 1 deletion extras/mammouth.min.js

Large diffs are not rendered by default.

19 changes: 14 additions & 5 deletions lib/rewriter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mammouth",
"version": "2.0.0",
"version": "2.0.1",
"description": "Unfancy PHP",
"author": {
"name": "Wael Amine Boutglay",
Expand Down
2 changes: 1 addition & 1 deletion src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ lexer.addRule RegularExpression.MammouthStart, ->

lastIsIdentifier = false
ShouldCloseCall = false
captureTypeCasting =false
captureTypeCasting = false
tokenStack = []
return '{{'

Expand Down
15 changes: 12 additions & 3 deletions src/rewriter.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
nodes = require './nodes'
helpers = require './helpers'

SuperMammouth = "\nfunction mammouth($func, $arg1, $arg2){if ( $func == '+' ){if((is_string($arg1) && is_numeric($arg2))||(is_string($arg2) && is_numeric($arg1))) {return $arg1.$arg2;} else {return $arg1+$arg2;}}}\n"

exports.rewrite = (tree, context) ->
IdCounter = new helpers.IdCounter
UseSuperMammouth = false
AddSuperMammouth = false
php = ''
ADD = (string) ->
php += string
Expand Down Expand Up @@ -138,7 +141,7 @@ exports.rewrite = (tree, context) ->
r = compile(element.left) + '.' + compile(element.right)
else if element.operator is '+'
UseSuperMammouth = true
r = '$Mammouth("+", ' + compile(element.left) + ', ' + compile(element.right) + ')'
r = 'mammouth("+", ' + compile(element.left) + ', ' + compile(element.right) + ')'
else
r = compile(element.left) + ' ' + element.operator + ' ' + compile(element.right)
return r
Expand Down Expand Up @@ -174,7 +177,7 @@ exports.rewrite = (tree, context) ->
r = 'isset(' + compile(element.expression) + ')'
return r
when 'In'
r = '$Mammouth("in_array", ' + compile(element.left) + ', ' + compile(element.right) + ')'
r = 'mammouth("in_array", ' + compile(element.left) + ', ' + compile(element.right) + ')'
return r

# Simple Statements
Expand Down Expand Up @@ -395,5 +398,11 @@ exports.rewrite = (tree, context) ->
for doc in tree
switch doc.type
when 'PlainBlock' then ADD doc.toPHP()
when 'MammouthBlock' then ADD '<?php' + compile(doc.body) + '?>'
when 'MammouthBlock'
code = compile(doc.body)
if UseSuperMammouth and not AddSuperMammouth
code = SuperMammouth + code
AddSuperMammouth = true
UseSuperMammouth = false
ADD '<?php' + code + '?>'
return php
7 changes: 1 addition & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ function heredoc (f) {
v = heredoc(function() {
/*
{{
`sdfsdfsdfdsfsdsdfssdfssf
dsfsdfs
dd
fsdfsdfsd`
'123'+123
}}
*/
})
Expand Down

0 comments on commit 3df1dda

Please sign in to comment.