-1

WikiPedia says:

A compiler is a computer program (or set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code). The most common reason for wanting to transform source code is to create an executable program.

Would this be called a simple ruby to javascript compiler if I convert some ruby code into javascript code.

For a simple example consider this ruby code:

def hello_world
   return 1
end

So in javascript if I modify this code using regex and make it something like:

function hello_world()
{
   return 1;
}

and run this bit of code:

eval(codeString);

(I know this is a very small piece of code but I had to give a simple example)


If I am being too much stupid, can anyone explain what exactly a compiler is and how can I create one?

Faiz Ahmed
  • 1,083
  • 8
  • 16
  • 1
    What a compiler is? Exactly what wikipedia says. Where the "compiler" (translation from language A to language B can hardly be done with regex, even tchrist can't) part is in your example? How to create a compiler? Pretty broad... – Adriano Repetti Sep 08 '14 at 10:32
  • Take a look here: http://stackoverflow.com/questions/1669/learning-to-write-a-compiler As for your question: if you also `eval` the generated code, it's an interpreter (in your case it would be a ruby interpreter written in javascript). a compiler would write out `codeString` to a file. – mb21 Sep 08 '14 at 10:33
  • 1
    Learn computer science, establish theories, __then__ build a script engine, ___then___ think about building a compiler... My two cents – Unihedron Sep 08 '14 at 10:37
  • http://www.ethoberon.ethz.ch/WirthPubl/CBEAll.pdf seems to be an especially good introduction. – mb21 Sep 08 '14 at 10:49
  • What if I make my own syntax something like coffeescript. What would that be called and how can I create something like that??? – Faiz Ahmed Sep 08 '14 at 10:51
  • If you're looking for a transpiler for Ruby to JavaScript, take a look at Opal: http://opalrb.org/ – GDP2 Oct 31 '15 at 00:21

1 Answers1

2

What you're describing is a transpiler, which is a kind of compiler, yes.
You would certainly not create this with regex replacements though, as it's not always simply a matter of putting some brackets somewhere or adding a keyword. You would parse the Ruby source code into something like an AST, which expresses what the code is meant to do. You'd then compile this meaning back into Javascript code, which may or may not produce code which looks very different from the original Ruby source. That's essentially what a compiler does; typically the target language it compiles to is a lower-level language like machine code or byte code, but it may as well be another high level language like Javascript.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • What if I make my own syntax something like coffeescript. What would that be called and how can I create something like that??? Can I use javascript regex for that?? – Faiz Ahmed Sep 08 '14 at 10:56
  • That would be a Coffeescript compiler or Coffeescript to Javascript transpiler; what exactly you call it is a matter of personal choice. You'd create it exactly as I described above. The Coffeescript compiler is in fact largely written on Jison, a tool that makes creating parsers simpler. See http://coffeescript.org/documentation/docs/grammar.html, it's fantastic documentation. I'd suggest you write *some* simple parser by hand to get a feel for it, e.g. a JSON parser is pretty simple to implement. – deceze Sep 08 '14 at 11:00
  • What do you mean by writing a JSON parser?? ( I hope you don't mean reading a JSON file in your application ) Can you give me an example? – Faiz Ahmed Sep 08 '14 at 11:08
  • Yes, I mean writing a program that can read a JSON string and return a native object/array structure. There are plenty of such implementations in many languages you could look at. I believe that's a good exercise for starting with parsers. – deceze Sep 08 '14 at 11:12
  • Thanks but I have done that a million times. Do it everyday! :) – Faiz Ahmed Sep 08 '14 at 11:13
  • Just to clarify: I don't mean *call* `JSON.parse('...')`, I mean implement the `JSON.parse` function yourself. That's probably *not* something you do every day. – deceze Sep 08 '14 at 11:15
  • I was asking that JSON is written in special syntax, XML is written in special syntax. What if I write something in special syntax. How can I read that syntax from any programming language like javascript. – Faiz Ahmed Sep 08 '14 at 11:15
  • Ohh yes but same question how??? – Faiz Ahmed Sep 08 '14 at 11:16
  • Because what I think is only way and that is regex – Faiz Ahmed Sep 08 '14 at 11:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60817/discussion-between-faiz-ahmed-and-deceze). – Faiz Ahmed Sep 08 '14 at 11:18
  • Nobody is going to explain to you how to write a parser here, that's something University courses are taught on. That's why I'm telling you to look into something simple and existing like a JSON parser. Here's a Rison parser (close relative of JSON) I implemented for fun: https://github.com/deceze/Kunststube-Rison/blob/master/RisonDecoder.php – deceze Sep 08 '14 at 11:18
  • OK thanks for your time! I'll check that tick mark :) – Faiz Ahmed Sep 08 '14 at 11:20