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/ApprehensivePipe1584 Oct 29 '26

Well this is different from that i still don't understand what you mean. This is attaching custom behavior to pattern matching.

0

u/Far-Championship-883 Oct 29 '26

look at how hard your syntax is to READ

1

u/ApprehensivePipe1584 Oct 29 '26
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;
}

I don't find it too hard to read. everything in main is just normal C/C++ style syntax plus rust-style match, Patterns are just defining names like an enum + struct style tags, and fits statements are literally just
thing if (cond);
like math. you just put the name of the parameter that is being checked (self in these cases) which is a pointer to the type (MS*/C*) then you just do normal conditions. its like inline if statements. thing if cond, like cases blocks in math, which is a big left brace then value if thing,...
such as:
⎧ 1, if x > 0

f(x) = ⎨ 0, if x = 0

⎩ -1, if x < 0

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)
→ More replies