Code parsing

Miscellaneous Forums/General Discussion/Code parsing

Is there anything out there which can parse and translate from one type of code to another?

I am writing an app for my mobile phone and find the Java code very tedious to enter. so I want to essentially write code in a simplified form and have a parser convert it to the Java equivalent ..

I am spending toooo much time trying to keep the compiler happy at the moment. If I forget just one ";" somewhere the compiler complains about unexpected source code so I end up trying to find where the missing ; is supposed to go

my simple code
for x=1 to 10
 if x=4
   drawimage blah,5,2
  else
    print "hello"
  end
next

if myval=0 cls


Java equivalent
for (int x=1;x<11;x++){
  if (x=4){
    image(blah,5,2);
  }else{
    println ("hello");
  }
}

if (myval==0) {background(0);}


so what your looking for is a BASIC to Java converter?

I think a crash course in Regular Expressions may just be what you need.
I find them extremely useful for these kind of purposes.

Basically create a set of Regex patterns for each possible language construct in both languages you want to convert between.
Use named grouping to extract the useful parts from language A and plug it into the patterns for language B.

For example, the for-next loop could be expressed in both languages as shown below.

// BlitzMax Code
For n:Int = 0 to 50
  If( n = 4 ) Then
    DrawImage( blah, 5, 2)
  Else
    Print("foo")
  End If
Next

// BlitzMax Pattern
"for (?<vname>\S+):(?<vtype>\S+) = (?<vsval>\S+) to {?<veval>\S+) (Step (?<vstep>\S+))? (?<content>.+) next"



// Java Code
for(int n = 0; n < 50; n+=1)
{
  if( n == 4 )
     image( blah, 5, 2);
  else
     println("foo");
}

// Java Pattern
"for( (?<vtype>\S+) (?<vname>\S+) = (?<vsval>\S+); (?<vname>\S+) < {?<veval>\S+); (?<vname>\S+) += (?<vstep>\S+)){ (?<content>.+) }"


Basicaly you loop through the basic code and look for the patterns that you defined. This process is a recursive one, because many of the values used in your code may themselves be complete expressions, which in turn contain more expressions etc.

Note that this is just an example, it is by no means a valid and workable regex pattern. There are a host of tings you need to keep in mind. The biggest problem is the source language (basic in this case). My example makes a lot of assumptions about the way you type it. Are you using SuperStrict style coding? or is it very liberal (eg: are you using strict type definitions or not?)?

For example, this will be easy to convert to java:
For n:Int = 0 to 50
 ...
Next


This will not:
For n = 0 to 50
 ...
Next


The reason is that the converter does not know what data type 'n' should be declared as.

Java is very strict about it's type definitions. If you don't reflect that in your basic code, then the parser has to guess at what it is you intended it to be. This causes numerous errors. Some may be easy to find as they generate java compiler errors. But others may be more insidious.

So the question is, is it really worth the trouble? If you already have to take great care in the way you type your basic code, it may very well be easier to just type java to begin with.

A better Java equivalent:
for (int x=1;x<11;x++)
  if (x.equals(4))
    image(blah,5,2);
  else
    println ("hello");

if (myval.equals(0))
  background(0);
So the question is, is it really worth the trouble?
The answer is "Not really". Get a better Java IDE (like Eclipse or IntelliJ) instead.