views:

76

answers:

2

I wonder if it's possible to write a new language above Javascript VM so that you could embed your language - which is based on javascript - and it will work together with native javascript.

Example:

var person_exists = true; // native js

Animal eats banana // my language

console.log(person_exists) // native js

I've seen Cappuccino's syntax and they say it could be run without compilation in the browser.

But then I have seen Coffeescript's syntax, and that one has to be compiled first to javascript.

So is it possible to create a new language/javascript syntax on top of Javascript VM?

If yes, are there tutorials/books for how to do this?

+1  A: 

Sure, see GWT or Parenscript for some quite different examples.

Greg Hewgill
What does GWT has to do with this?
never_had_a_name
GWT compiles Java source code to Javascript for execution in the browser.
Greg Hewgill
Ok then GWT isn't for me I guess cause I want my source code to be run in browser without having to be compiled. Parenscript looks interesting.
never_had_a_name
Parenscript is compiled as well, and requires a running lisp.
itsmyown
+1  A: 

Processing.js is a script language interpreted by Javascript (interpreted, not compiled to JS as far as I know), you could take a look at that approach. Processing.js is specifically tailored to produce graphics onto a webpage, so it may not be exactly what you need, but it is an example of having two scripting languages on one website.

(Processing is a separate graphics language which was first run in a Java environment (as well as other implementations), before the Canvas element was conceived and another implementation based on Javascript was made.)

What it seems you want is 'Javascript with my own features'. This is only possible if you (or someone else) build a JS+yourfeatures interpreter on top of Javascript. I don't think that's a very plausible thing to do though. Processing.js's solution is probably a good middle ground, where you separate your own language from actual Javascript - that is, if your language and the regular JS can be separated.

<script language="javascript">
  var person_exists = true; // native js
  interpret("animal"); // your language interpreter. Interpret function looks through the DOM for a script tag with the "myscript" language and the 'animal' ID.
  console.log(person_exists) // native js
</script>
<script language="myscript" id="animal">
   Animal eats banana // your language
</script>

Still, I'd reccommend thinking about what you ''really'' want before skipping to the "I know, I'll develop my own language!" part. What's wrong with

Animal = {
   eats: function(fruit) {
       console.log('om nom nom nom ' + fruit);
   }
};

var person_exists = true;
Animal.eats(banana);
console.log(person_exists);

for example?

Cthulhu
That was just an example. There are other parts I want to wrap around a prettier code. And it's most for learning purpose and also for fun. Aren't there any books about how to do this and what tools you could use? I'm pretty blind here and don't know where to find more information except for reading your provided links.
never_had_a_name
Well, I guess there's plenty of books on general programming language development - http://stackoverflow.com/questions/1669/learning-to-write-a-compiler has a big list of resources. Those are aimed at compilers, but interpreters follow a similar process, usually - and there's always some crossover between the two.
Cthulhu