Phillip Senn
Phillip Senn

Reputation: 47605

Using begin and end in JavaScript

Can I somehow change the JavaScript language such that the word end equates to the } symbol and begin equates to {?

Upvotes: 4

Views: 4289

Answers (3)

GitaarLAB
GitaarLAB

Reputation: 14645

Have a look at META II.

...make a lot of compilers - and it's all going to be easy. No Ajax, Active X, DLLs, SOs, ASP, CGI, Java, plugins, modules, XML, cookies, PHP, Perl, Python, magic shell operations, world wide standard du jour, or intergalactic domination plans are necessary - just plain JavaScript in frames. Also you will be able to move the compiler you build off these web pages and into your programming language of choice by cutting and pasting. After all a compiler is just a program that reads text and writes text or binary.

Just as an idea.


Alternatively, you might want to use the folloing (dubious) coding-style (to get familiar with ES's grammar) in the beginning:

function name(args){//begin
  // your code here
}//end

while(L--){//begin
  // your code here
}//end

Upvotes: 2

icktoofay
icktoofay

Reputation: 129011

No, you can't. The best you could do is have a script that modifies another script with an invalid type and changes it to a valid type so you'd get the effect, kind of.

<script type="text/x-algolscript">
    function hello() begin
        alert("Hello, world!");
    end
    hello();
</script>
<script type="text/javascript">
    Array.prototype.forEach.call(document.getElementsByTagName('script'), function(script) {
        if(script.type === 'text/x-algolscript') {
            var oldParent = script.parentNode;
            var oldNext = script.nextSibling;
            oldParent.removeChild(script);
            script.textContent = script.textContent.replace(/\bbegin\b/g, '{').replace(/\bend\b/g, '}');
            script.type = 'text/javascript';
            oldParent.insertBefore(script, oldNext);
        }
    });
</script>

This is context-insensitive, however, and will gladly change your strings and such.

Bonus: Minified and more browser-compatible version:

!function(s,i,t,e,l,p,o,n){for(l=s.length;i<l;i++)((e=s[i]).type==='text/x-algolscript')&&t.push(e);for(i=0;i<t.length;i++)o=(e=t[i]).parentNode,n=e.nextSibling,o.removeChild(e),p='textContent',e[p]||(p='innerText'),e[p]=e[p].replace(/\bbegin\b/,'{').replace(/\bend\b/,'}'),e.type='text/javascript',o.insertBefore(e,n)}(document.getElementsByTagName('script'),0,[]);

Upvotes: 6

Andrey
Andrey

Reputation: 60065

You can write your own transcompiler that will parse our Pascalesque Javascript and output traditional Javascript. You can either just replace the words begin/end with brackets or use something more advanced like Jison.

Upvotes: 5

Related Questions