Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implementing and #16

Open
mgood7123 opened this issue Apr 26, 2019 · 2 comments
Open

implementing and #16

mgood7123 opened this issue Apr 26, 2019 · 2 comments

Comments

@mgood7123
Copy link

mgood7123 commented Apr 26, 2019

how would i design the and of a parser combinator so that i do not need to create an infinite number of classes for it
example (probably incorrect)

/*
val A = F1("1")
val B = F1("2")
val C = F1("3")

val C: F1AndF1 = A.and(B)
val D: F1AndF1 = B.and(D)

val E: F1AndF1AndF2AndF2 = C.and(D)

// ...

*/
class F1(left: Any) {
    // F1 And F1
    infix fun and(right: F1)  = F1AndF1(this, right)
}
class F1AndF1(left: F1, right: F1) {
    // F1AndF1 And F1AndF1
    infix fun and(right: F1AndF1)  = F1AndF1AndF2AndF2(this, right)
}
class F1AndF1AndF2AndF2(left: F1AndF1, right: F1AndF1) {
    // F1AndF1AndF2AndF2 And F1AndF1AndF2AndF2
    infix and(right: F1AndF1AndF2AndF2)  = F1AndF1AndF2AndF2AndF3AndF3AndF4AndF4(this, right)
}
// and so on for ever
@h0tk3y
Copy link
Owner

h0tk3y commented Jul 1, 2020

I couldn't find a way to do that preserving type safety. My and combinators are based on generated tuples and functions for adding another component, for each number of components (up to 16).

@mgood7123
Copy link
Author

it could be implemented as a binary tree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants