krfkeith / la-pe

Automatically exported from code.google.com/p/la-pe
0 stars 0 forks source link

How does Lape Unions work? #3

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I can't find out how Unions work for anything.

Would also like to know if External should be working yet?
I see you have it as a keyword, tho it doesn't work for me.

Would also like to see inline added. =)

Original issue reported on code.google.com by Dgby...@gmail.com on 8 Jun 2011 at 6:06

GoogleCodeExporter commented 9 years ago
Will you be adding internal or external classes? soon?

Original comment by Dgby...@gmail.com on 8 Jun 2011 at 6:08

GoogleCodeExporter commented 9 years ago
Unions are a mix between c unions and pascal variant records. What it means is 
that certain fields point to the same memory location.

type
  rgb = union
    Color: Int32;
    ColorRGB: packed record
      R, G, B, A: UInt8;
    end;
  end;

var
  Color: rgb;
begin
  Color.Color := 1971210;
  WriteLn('r: ' + IntToStr(Color.ColorRGB.R) + ', g: ' + IntToStr(Color.ColorRGB.G) + ', b: ' + IntToStr(Color.ColorRGB.B));

  Color.ColorRGB.R := 255;
  Color.ColorRGB.G := 255;
  Color.ColorRGB.B := 255;
  WriteLn(Color.Color);
end;

In this case, Color and ColorRGB point to the same location (so R is the first 
byte of Color, G is the second and so on).

I'm sorry if I got your hopes up with the external keyword, but (for now) it is 
used to distinguish imported from script functions. Like so:

procedure test;
begin
  WriteLn('s');
end;
var
  proc: procedure; //This can either be an imported method or a method defined in the script, the compiler will decide how to invoke
  procI: external procedure; //This is an imported method
  procS: private procedure; //This is a method defined in the script
begin
  proc := Test; //No @
  proc();
end;

Inline would be a nice extra, but is not of high priority. The same goes for 
internal and external classes. I think I've got it pretty much figured out in 
my head about how I want to implement the base, but I want to do some other 
things first. I want to do the magic functions like ToString, Write, WriteLn 
and SetArrayLength first (and perhaps labels). After that, you've got a 
reasonable set of features which need some extensive testing.

Original comment by niels....@gmail.com on 8 Jun 2011 at 10:33

GoogleCodeExporter commented 9 years ago
SetArrayLength? That's the same thing as SetLength in PascalScript.

Original comment by Dgby...@gmail.com on 9 Jun 2011 at 3:31

GoogleCodeExporter commented 9 years ago
Ah yes, I'll call it that, too :)

Original comment by niels....@gmail.com on 9 Jun 2011 at 8:46

GoogleCodeExporter commented 9 years ago
Also unions might be a little broken.

type
  test = union
    W: UInt16;
    BB: packed record
      B1, B2: UInt8;
    end;
  end;

In the above W = B2B1

Original comment by Dgby...@gmail.com on 9 Jun 2011 at 1:05

GoogleCodeExporter commented 9 years ago
I think they are working correctly? What is the rest of your testcode? Are you 
familiar with endianness 
(http://wiki.freepascal.org/Writing_portable_code_regarding_the_processor_archit
ecture#Endianness)?

I really appreciate you're testing :)

Original comment by niels....@gmail.com on 9 Jun 2011 at 3:28

GoogleCodeExporter commented 9 years ago
Hmmm, thanks for the link =)

Why didn't you do unions like FreePascal does?

type
  test = record
    case boolean of
      true: (W: Word);
      false: (B: array[0..1] of Byte)
  end;

Original comment by Dgby...@gmail.com on 9 Jun 2011 at 4:57

GoogleCodeExporter commented 9 years ago
I don't like the way that looks. Plus, this was just an easy adjustment of the 
record parsing/implementation. ;)

Original comment by niels....@gmail.com on 9 Jun 2011 at 5:46

GoogleCodeExporter commented 9 years ago
Ah k. =)

Btw all my testing is done in Simba.

You never get on IRC =(

Original comment by Dgby...@gmail.com on 9 Jun 2011 at 5:51