r/Compilers • • Oct 25 '26

Patterns

I've been making a programming language for a while now, and just came up with this idea for a feature i'm bout to implement. Patterns.

   pattern P {
    N;
    T(int, int);
    S {
        int x;
        int y;
    };
}
struct MS {
    int x;
}
class C {
    C() {}
    int y;
}
MS fits P(self) {
    N if (self->x == 0);
    T(self->x, -self->x) if (self->x < 10);
    S {
        x: self->x;
        y: 0;
    } if (self->x < 20);
    default N;
}
C fits P(self) {
    default N;
}
int main() {
    C c = C();
    c.y = 0;
    MS ms = {19};
    match (c) {
        P.N => `qout("Worked");
        default => `qout("Failed");
    }
    match (ms) {
        P.S {x, y} => `qout("Worked");
        default => `qout("Failed");
    }
    return 0;
} 

Basically, making user types have the ability to have custom behavior + extraction in match stmts, beyond just enums & integer types

8 Upvotes

33 comments sorted by

View all comments

Show parent comment

1

u/Far-Championship-883 Oct 30 '26

Yeah, c and c++ suck to fucking read

1

u/ApprehensivePipe1584 28d ago

It's just C style syntax that's just a preference of c/c++ style vs a modern rusty style

1

u/Far-Championship-883 28d ago

rust is also shit

1

u/ApprehensivePipe1584 28d ago

correct. I'm biased as a fan of C++, and C, and Zig, and despise Rust. I like C-Style syntax and am assuming you prefer a modern or like Haskell-like style?

1

u/Far-Championship-883 28d ago

no, i use a entirely different programming language

1

u/ApprehensivePipe1584 27d ago

Ok? Holy vaguepost. You prefer a different syntactical style to me. That's not even about the thing I'm posting about. Your complaining about the C style syntax i use as my general syntax, not even what the post is talking about.

1

u/Far-Championship-883 27d ago

your syntax is half the problem the other half is the absolutely ridiculous chain of inconsistency in the design. I had my AI bot take your code and implement it in the language I use for my systems level work.

// Your `pattern P` / `fits` / `match` example, ported to AILang.
// No new language feature needed: a tagged LinkagePool, one Function
// per `fits`, and Branch for `match`.


LibraryImport.Arena


// ---- pattern P { N; T(int, int); S { int x; int y; } } ----
// Variant tags
FixedPool.P {
    "N": Initialize=0
    "T": Initialize=1
    "S": Initialize=2
}


// Payload: one record holds every variant's fields
LinkagePool.PMatch {
    "tag": Initialize=0
    "t0":  Initialize=0     // T(int, _)
    "t1":  Initialize=0     // T(_, int)
    "x":   Initialize=0     // S { x }
    "y":   Initialize=0     // S { y }
}


// ---- struct MS { int x; }  /  class C { int y; } ----
LinkagePool.MS {
    "x": Initialize=0
}


LinkagePool.C {
    "y": Initialize=0
}


// ---- MS fits P(self) { ... } ----
// The condition comes first, and arms are tried top to bottom.
Function.MS_FitsP {
    Input:  self: LinkagePool.MS
    Output: LinkagePool.PMatch
    Body: {
        m = AllocateLinkage(LinkagePool.PMatch)
        x = self@x


        IfCondition EqualTo(x, 0) ThenBlock: {
            m@tag = P.N
            ReturnValue(m)
        }


        IfCondition LessThan(x, 10) ThenBlock: {
            m@tag = P.T
            m@t0  = x
            m@t1  = Subtract(0, x)
            ReturnValue(m)
        }


        IfCondition LessThan(x, 20) ThenBlock: {
            m@tag = P.S
            m@x   = x
            m@y   = 0
            ReturnValue(m)
        }


        // default N
        m@tag = P.N
        ReturnValue(m)
    }
}


// ---- C fits P(self) { default N; } ----
Function.C_FitsP {
    Input:  self: LinkagePool.C
    Output: LinkagePool.PMatch
    Body: {
        m = AllocateLinkage(LinkagePool.PMatch)
        m@tag = P.N
        ReturnValue(m)
    }
}


// ---- int main() ----
SubRoutine.Main {
    Arena_Init()


    c = AllocateLinkage(LinkagePool.C)
    c@y = 0


    ms = AllocateLinkage(LinkagePool.MS)
    ms@x = 19


    // match (c) { P.N => ...; default => ...; }
    pc  = C_FitsP(c)
    tag = pc@tag
    Branch tag {
        Case P.N: {
            PrintMessage("Worked\n")
        }
        Default: {
            PrintMessage("Failed\n")
        }
    }


    // match (ms) { P.S {x, y} => ...; default => ...; }
    pm  = MS_FitsP(ms)
    tag = pm@tag
    Branch tag {
        Case P.S: {
            x = pm@x            // destructuring is just field reads
            y = pm@y
            PrintMessage("Worked\n")
        }
        Default: {
            PrintMessage("Failed\n")
        }
    }


    FreeLinkage(pc, LinkagePool.PMatch)
    FreeLinkage(pm, LinkagePool.PMatch)
    FreeLinkage(c,  LinkagePool.C)
    FreeLinkage(ms, LinkagePool.MS)
    Exit(0)
}


RunTask(Main)

1

u/ApprehensivePipe1584 27d ago

> your syntax is half the problem the other half is the absolutely ridiculous chain of inconsistency in the design. I had my AI bot take your code and implement it in the language I use for my systems level work.

so you don't even code, and are complaining I am making a c style language. my syntax is entierly consistent, all of my compile-time-only format-shaped types are somewhat mathmatical, and if i made typos in the on-the-spot typing up of a proof-of-concept for a feature i have not even added yet, boo hoo.

LibraryImport.Arena


// ---- pattern P { N; T(int, int); S { int x; int y; } } ----
// Variant tags
FixedPool.P {
    "N": Initialize=0
    "T": Initialize=1
    "S": Initialize=2
}


// Payload: one record holds every variant's fields
LinkagePool.PMatch {
    "tag": Initialize=0
    "t0":  Initialize=0     // T(int, _)
    "t1":  Initialize=0     // T(_, int)
    "x":   Initialize=0     // S { x }
    "y":   Initialize=0     // S { y }
}


// ---- struct MS { int x; }  /  class C { int y; } ----
LinkagePool.MS {
    "x": Initialize=0
}


LinkagePool.C {
    "y": Initialize=0
}


// ---- MS fits P(self) { ... } ----
// The condition comes first, and arms are tried top to bottom.
Function.MS_FitsP {
    Input:  self: LinkagePool.MS
    Output: LinkagePool.PMatch
    Body: {
        m = AllocateLinkage(LinkagePool.PMatch)
        x = self@x


        IfCondition EqualTo(x, 0) ThenBlock: {
            m@tag = P.N
            ReturnValue(m)
        }


        IfCondition LessThan(x, 10) ThenBlock: {
            m@tag = P.T
            m@t0  = x
            m@t1  = Subtract(0, x)
            ReturnValue(m)
        }


        IfCondition LessThan(x, 20) ThenBlock: {
            m@tag = P.S
            m@x   = x
            m@y   = 0
            ReturnValue(m)
        }


        // default N
        m@tag = P.N
        ReturnValue(m)
    }
}


// ---- C fits P(self) { default N; } ----
Function.C_FitsP {
    Input:  self: LinkagePool.C
    Output: LinkagePool.PMatch
    Body: {
        m = AllocateLinkage(LinkagePool.PMatch)
        m@tag = P.N
        ReturnValue(m)
    }
}


// ---- int main() ----
SubRoutine.Main {
    Arena_Init()


    c = AllocateLinkage(LinkagePool.C)
    c@y = 0


    ms = AllocateLinkage(LinkagePool.MS)
    ms@x = 19


    // match (c) { P.N => ...; default => ...; }
    pc  = C_FitsP(c)
    tag = pc@tag
    Branch tag {
        Case P.N: {
            PrintMessage("Worked\n")
        }
        Default: {
            PrintMessage("Failed\n")
        }
    }


    // match (ms) { P.S {x, y} => ...; default => ...; }
    pm  = MS_FitsP(ms)
    tag = pm@tag
    Branch tag {
        Case P.S: {
            x = pm@x            // destructuring is just field reads
            y = pm@y
            PrintMessage("Worked\n")
        }
        Default: {
            PrintMessage("Failed\n")
        }
    }


    FreeLinkage(pc, LinkagePool.PMatch)
    FreeLinkage(pm, LinkagePool.PMatch)
    FreeLinkage(c,  LinkagePool.C)
    FreeLinkage(ms, LinkagePool.MS)
    Exit(0)
}


RunTask(Main)LibraryImport.Arena


// ---- pattern P { N; T(int, int); S { int x; int y; } } ----
// Variant tags
FixedPool.P {
    "N": Initialize=0
    "T": Initialize=1
    "S": Initialize=2
}


// Payload: one record holds every variant's fields
LinkagePool.PMatch {
    "tag": Initialize=0
    "t0":  Initialize=0     // T(int, _)
    "t1":  Initialize=0     // T(_, int)
    "x":   Initialize=0     // S { x }
    "y":   Initialize=0     // S { y }
}


// ---- struct MS { int x; }  /  class C { int y; } ----
LinkagePool.MS {
    "x": Initialize=0
}


LinkagePool.C {
    "y": Initialize=0
}


// ---- MS fits P(self) { ... } ----
// The condition comes first, and arms are tried top to bottom.
Function.MS_FitsP {
    Input:  self: LinkagePool.MS
    Output: LinkagePool.PMatch
    Body: {
        m = AllocateLinkage(LinkagePool.PMatch)
        x = self@x


        IfCondition EqualTo(x, 0) ThenBlock: {
            m@tag = P.N
            ReturnValue(m)
        }


        IfCondition LessThan(x, 10) ThenBlock: {
            m@tag = P.T
            m@t0  = x
            m@t1  = Subtract(0, x)
            ReturnValue(m)
        }


        IfCondition LessThan(x, 20) ThenBlock: {
            m@tag = P.S
            m@x   = x
            m@y   = 0
            ReturnValue(m)
        }


        // default N
        m@tag = P.N
        ReturnValue(m)
    }
}


// ---- C fits P(self) { default N; } ----
Function.C_FitsP {
    Input:  self: LinkagePool.C
    Output: LinkagePool.PMatch
    Body: {
        m = AllocateLinkage(LinkagePool.PMatch)
        m@tag = P.N
        ReturnValue(m)
    }
}


// ---- int main() ----
SubRoutine.Main {
    Arena_Init()


    c = AllocateLinkage(LinkagePool.C)
    c@y = 0


    ms = AllocateLinkage(LinkagePool.MS)
    ms@x = 19


    // match (c) { P.N => ...; default => ...; }
    pc  = C_FitsP(c)
    tag = pc@tag
    Branch tag {
        Case P.N: {
            PrintMessage("Worked\n")
        }
        Default: {
            PrintMessage("Failed\n")
        }
    }


    // match (ms) { P.S {x, y} => ...; default => ...; }
    pm  = MS_FitsP(ms)
    tag = pm@tag
    Branch tag {
        Case P.S: {
            x = pm@x            // destructuring is just field reads
            y = pm@y
            PrintMessage("Worked\n")
        }
        Default: {
            PrintMessage("Failed\n")
        }
    }


    FreeLinkage(pc, LinkagePool.PMatch)
    FreeLinkage(pm, LinkagePool.PMatch)
    FreeLinkage(c,  LinkagePool.C)
    FreeLinkage(ms, LinkagePool.MS)
    Exit(0)
}


RunTask(Main) this looks so much worse, and is not the purpose. My language could also do this, but this looks significantly worse. This is like complaining operator overloading exists by saying "my language can already do this with x.add(y)"

1

u/Far-Championship-883 24d ago

Yes a man with no programming knowledge works on a custom bare metal x86_64 compiler with performance competitive with GCC -03, sure buddy, think what you like. why would I waste my time on your bad idea, i handed it to a bot, and it did precisely what i asked it to.

1

u/ApprehensivePipe1584 23d ago

So you didn't code it. I'm assuming it's all AI generated considering it's called AILang. Which implies it's... AI. Sorry you don't like the syntax because... you have complaints against all syntactical styles. That's your opinion. My lang's C style. You don't like C style. Not once have you actually talked about my idea or given a single reason on why it's bad or negative, you have exclusively complained about my syntax in general being c style, which i did not ask your opinion about, and things it literally doesn't do or whatever it is designed to make you not do. In your above example you needed to manually call fits. Which you don't need to do for this. That's the whole purpose. To destructure and call fits for you.

→ More replies

1

u/ApprehensivePipe1584 27d ago

you have a general disliking towards c style syntax. That's not the question i asked. Just saying "your syntax is absolutely ridiculous and inconsistent" without even looking at any other syntax.