Author Topic: Help writing a C# translator  (Read 1091 times)


  • Member
  • **
  • Posts: 79
  • Karma: +0/-0
    • View Profile
Help writing a C# translator
« on: Tue, Oct 26, 2010 »
I'll like to write a translator, from Actionscript to C#.

I need some help about how to handle the tranlation process in order to extend it easily when the translator doesn´t work in certain circumstances and make it work for other languages(objective C).

 The translator convert "var c:int = 0" to "int c = 0" very well, but cant process "var c:Number = 0". So you can add your case to the stack in order to translate "var c:Number = 0.5;" to "float c = 0.5F;"

Any ideas?


Richard Kain

  • Active Member
  • ***
  • Posts: 231
  • Karma: +0/-0
    • View Profile
Re: Help writing a C# translator
« Reply #1 on: Tue, Oct 26, 2010 »
Well, instead of converting Number variables to floats, you could instead convert them into doubles. Double variables in C# do not require that you append "f" to the end of the value designation.

"var c:Number = 0.0" should translate cleanly into "double c = 0.0"


  • Active Member
  • ***
  • Posts: 159
  • Karma: +0/-0
  • Herper of Derps
    • View Profile
Re: Help writing a C# translator
« Reply #2 on: Tue, Oct 26, 2010 »
Objective-C is too different from....well, I don't think you want to write one Uber-Converter that handles it too. Dunno what language you're using, but my choice if I just want to go from one language to another and get that particular task done is to use a bunch of regular expressions to change syntax and pull out information

In the example where a type descriptor exists in one language but not another (Number is changed to float, not as easy as int <-> int), you could use a regex written for each such case.

Off the top of my head (PHP style. Modify for Perl, JS, etc):

Code: [Select]
match: '/var (\w+):Number = ([0-9\.]+);/'
replacement: '/float $1 = $2f;/'

Regular expressions can be intimidating to learn at first and often look like gobbledygook but they're really powerful things. To briefly explain that one:

- it starts looking for the literal word var
- matches a space, then any number of word characters which means A-Z, a-z, 0-9 and underscore. Basically, a valid variable name. Captures it for later use
- This must be immediately followed by :Number =
- then it matches any length of 0-9 or periods and captures that too (this would capture invalid syntax, like 0.1.44....153.2.1, so only feed it code you already know is valid)
- The replacement just sticks the first match ($1, the var name) and $2 (its value) into the syntax of the other language

So if I just wanted to accomplish going from one particular language to another and they were very similar, I would write a lot of these. It's great for trivial syntactical changes, but not for semantic differences - i.e. when the target language accomplishes tasks in completely different ways. That captured information, though, can be pulled out into arrays and you can write various loops and logic blocks to figure out the more complicated changes.

Some pretty decent information on regexes:

Information caters heavily to Javascript. If you want to try your hand at them in PHP, the relevant functions are preg_replace, preg_match, preg_match_all.

Or there might be a lib in your language of choice

Of course, don't forget to head to Google and poke around for what tools might exist for your particular conversion needs ;) There might be proper parsers out there rather negating this messy kludge