Matt's thoughts

Matt's thoughts

Matt Benic  //  Game & mobile dev gun for hire and environmental worrier.

Dec 31 / 4:15pm

Fun with initialization order

Here's something I meant to post ages ago, when I was doing J2ME to Flash conversions, and got caught out by assumptions about the evaluation order of values passed to a method or to initialize an array. It's one of those really subtle issues that had me scratching my head to figure out what had gone wrong in a piece of code.

Consider the following (pseudo)code:

Now what would you expect the output to be? I had unwittingly assumed the result would consistently be:
arr: [1, 2, 3]
val1: 1, val2: 2, val3: 3

As it turns out, the answer varies from language to language (which is why I got snagged by code working 'correctly' in one language but 'breaking' in another):

Java:

Result:
arr: [1, 2, 3]
val1: 1, val2: 2, val3: 3
 
Actionscript:

Result:
arr: [3, 2, 1]
val1: 3, val2: 2, val3: 1
 
Some others:
C++ undefined:
C# guaranteed to be right to left:
Filed under  //  actionscript   code   java  

Comments (0)

May 21 / 1:53pm

Some useful regular expression patterns when porting code between languages

I'm currently moving a lot of code from Java to Actionscript, and regex has been a lifesaver. Here are some of the patterns that proved invaluable, and that would work on most c-style languages. The Actionscript replacements are obviously specific to that language, but changing them for a different output language really would be trivial:

Find non-parameter variable declarations and assignments
Note: this will find returns as well, see the next pattern
(\w+)(\s)(\w+)(;|\s=)

Flash replacement:
var$2$3:$1$4

Will replace:
    int myInt;
    boolean myBool = true;
With:
    var myInt:int;
    var myBool:boolean = true;   

Find resulting returns in Flash
This will match all the wonky return statements resulting from the previous replacement.
var(\s)(\w+):return;

Flash replacement:
return$1$2;

Will replace:
    var true:return;
With:
    return true;

Find parameter variable declarations
(\w+)(\s)(\w+)(,|\))

Flash replacement:
$3:$1$4

Will replace:
    public int foo(int intParam, boolean boolParam) {
With:
    public int foo(intParam:int, boolParam:boolean) {

Find function signatures with parameters in c/java format
(\w+)(\s)(\w+\([\s?\w\s\w+,?]*\))(\s?\{)

Flash replacement:
function$2$3:$1$4

Will replace:
    public int foo(int intParam, boolean boolParam) {
With:
    public function foo(int intParam, boolean boolParam):int {


Find function signatures with parameters in Actionscript format
(\w+)(\s)(\w+\([\s?\w+:\w+,?]*\))(\s?\{)

Flash replacement:
function$2$3:$1$4

Will replace:
    public int foo(intParam:int, boolParam:int) {
With:
    public function foo(intParam:int, boolParam:int):int {

Find numeric types
(,|;|\(|\)|=|\s)(int|long|short|float|double|byte)(,|;|\(|\)|=|\s)

Flash replacement:
Number

Will replace:
    var myInt:int;
    long myLong;
    float foo(byte bar) {
With:
    var myInt:Number;
    Number myLong;
    Number foo(Number bar) {

Filed under  //  code   flash   j2me   java  

Comments (2)