0

How would one write swizzling as a defined behaviour in a programming language? (swizzling members like matrices and vectors in GLSL) So if I wanted to make a programming language that would allow the definition of swizzling on some members, what would be a good way to do it? So for example I could do this:

struct
{
    swizzable
    {
        float x, float y, float z, float w
    }
}

But this is missing a lot. For example it does not define that what sould it return when I swizzle more or less elements or assign to a subset or just the elements backwards. Like in GLSL I can do v.xyz to create a Vec3 from a Vec4 called v. Or I could assign a subset of members: v.zyx = ... in any order.

So this swizzable substruct is not a solution (or at least too limited). Another way would be to return an array of swizzled members and an implicit cast (with a constructor) would generate the wanted element:

struct Vec2
{
    swizzable { float x, float y }
    Vec2(float[2] elements)
    { x = elements[0]; y = elements[1]; }
}

struct Vec3
{
    swizzable { float x, float y, float z }
}

So if I accessed a Vec3's x and y via swizzling, I would get a float[2] and because I have a constructor for Vec2, I can assign this array to it (and implicitly instantiating a vec2).

This looks like a better solution but still: How could one do better?

Edit: Sorry I didn't specify the question: I want to implement a programming language that supports this kind of thing.

Peter Lenkefi
  • 1,306
  • 11
  • 29
  • I'm not sure what you're asking here. Are you asking how to design a programming language that allows this sort of thing? Or are you asking how to implement this behavior in some specific programming language? Or are you asking how to implement it in a programming language, but you don't have a particular programming language in mind? – Tanner Swett Jul 21 '16 at 19:27
  • @TannerSwett The first one. Sorry that I didn't specify this. I want to create a programming language that supports this. – Peter Lenkefi Jul 21 '16 at 19:34
  • That's a hard question to answer, because the way that you'll implement this feature depends on how you're implementing all of your other features. – Tanner Swett Jul 21 '16 at 19:44

1 Answers1

0

I'm not sure how to give a good, detailed answer, so here is just one idea.

If I understand right, swizzling is mainly a syntactic convenience. The page https://www.opengl.org/wiki/GLSL_Optimizations gives the following example of swizzling:

gl_FragColor = mycolor.xyzw * constantList.xxxy + constantList.yyyx;

This could simply be syntactic shorthand for something like:

gl_FragColor = Vector(mycolor.x, mycolor.y, mycolor.z, mycolor.w)
    * Vector(constantList.x, constantList.x, constantList.x, constantList.y)
    + Vector(constantList.y, constantList.y, constantList.y, constantList.x);

So, one step may be to figure out how to parse the shorter syntax and interpret it as meaning something similar to the longer syntax.

I don't see why it would be necessary to declare the struct as anything more complicated than struct myStruct { float x, float y, float z, float w }. The language itself should be able to handle all the details of how to implement this swizzling.

Tanner Swett
  • 3,241
  • 1
  • 26
  • 32
  • How would the language know what the swizzling result is from the same struct at different number of elements? (vector 2 -> vec2, 3-> vec3, matrix -> 4 mat2, 9-> mat3, ....) – Peter Lenkefi Jul 21 '16 at 20:03
  • Couldn't you say that the swizzling result is always a vector, where the length of the vector is equal to the number of members that you swizzled? – Tanner Swett Jul 22 '16 at 11:22
  • Yeah I guess I'll just stick to that. Not too flexible tho. – Peter Lenkefi Jul 23 '16 at 08:38
  • I don't know what you mean by "not too flexible". What's something you want to be able to do, that you wouldn't be able to do with my idea? – Tanner Swett Jul 25 '16 at 13:17