this would be a correct implementation of `and` an...
# announcements
m
this would be a correct implementation of
and
and
or
right? (not including combinations and association, eg
A and B or C
>
(A and B) or C
or
A and (B or C)
)
Copy code
fun peek(): Boolean = if (list.size != 0) peekMultiple() else peekSingle()

// private fun peekSingle omited

private fun peekMultiple(): Boolean {
    val listTmp = cloneList(list).iterator()
    if (list.all { it.type == operatorAnd }) {
        var matches = 0
        val max = list.size
        while (listTmp.hasNext()) {
            val stackTmp = listTmp.next().once!!
            if (stackTmp.peek()) {
                matches++
                stackTmp.pop()
            } else break
        }
        if (matches == max) return true
        return false
    } else if (list.all { it.type == operatorOr }) {
        while (listTmp.hasNext()) if (listTmp.next().once!!.peek()) return true
        return false
    }
    else return false // unsupported operator or combination of different operators
}

infix fun and(right: IsSequenceOnce): IsSequenceOnce {
    val stackTmp = parent.clone().IsSequenceOnce(this.value)
    if (this.list.size != 0) stackTmp.list.addAll(this.list) else stackTmp.list.add(Types().also { it.add(this, operatorAnd) })
    if (right.list.size != 0) stackTmp.list.addAll(right.list) else stackTmp.list.add(Types().also { it.add(right, operatorAnd) })
    return stackTmp
}

infix fun or(right: IsSequenceOnce): IsSequenceOnce {
    val stackTmp = parent.clone().IsSequenceOnce(this.value)
    if (this.list.size != 0) stackTmp.list.addAll(this.list) else stackTmp.list.add(Types().also { it.add(this, operatorOr) })
    if (right.list.size != 0) stackTmp.list.addAll(right.list) else stackTmp.list.add(Types().also { it.add(right, operatorOr) })
    return stackTmp
}
for example,
Copy code
val E = parseStream.IsSequenceOnce("E")
    val F = parseStream.IsSequenceOnce("F")
    val FE = F or E
    val EF = E and F
    println(EF.peek())
    println(FE.peek())
🤮 2
😱 2
s
May I suggest you take a look at
any
and
all
instead
r
What the hell are you doing
👍 1
p
Gentle reminder to be kind 🙂
@Matthew Good What is your end goal?
m
@pablisco to make a parser combinator
p
Ok, the infix should have a receiver I think
May have been missed from the copying
r
This whole code is actually inside a misnamed god class I think. Also please name your variables
Please name your variables, functions and classes correctly. I don’t understand what’s happening
p
peekMultiple
can probably be simplified as:
Copy code
private fun peekMultiple(): Boolean = 
  when(val tmp = cloneList(list)) {
    tmp.all { it.type == operatorAnd } -> tmp.all { 
      it.peek() 
    }
    tmp.all { it.type == operatorOr } -> tmp.all {
      it.once?.peek() ?: false 
    } 
    else -> false
  }
m
@pablisco ok, and
it.peek()
alone would not be enough due to the stack needing to be popped in order to correctly peek the next item in the list, aswell as needing a counter to ensure the number of peeks that are succesfull matches the total number of items in the list if for example an item does not match an item that
peek
checks against in the stack
p
What’s the type of
listTmp
?
m
@pablisco first of all,
listTmp
was
cloneList(list).iterator()
but since you eliminated that i can only assume you mean
list
or
tmp
in yours
tmp
is of type
MutableList<Types>
and
Types
is
Copy code
inner class Types {
    var once: IsSequenceOnce? = null
    var oneOrMany: IsSequenceOneOrMany? = null
    var zeroOrMany: IsSequenceZeroOrMany? = null
    var type: Int = 0

    fun type(): String? = when(type) {
        operatorOr -> "or"
        operatorAnd -> "and"
        else -> null
    }
    fun get(): Any? = when {
        once != null -> once
        oneOrMany != null -> oneOrMany
        zeroOrMany != null -> zeroOrMany
        else -> null
    }
    fun add(p: Any, type: Int) = when(p) {
        is IsSequenceOnce -> once = p
        is IsSequenceOneOrMany -> oneOrMany = p
        is IsSequenceZeroOrMany -> zeroOrMany = p
        else -> println("unsupported type")
    }.also { this.type = type }
}
p
I suggest you use a Sealed class for
Type
it would be easier to represent what you need
Also, if
listTmp
it’s a
MutableList
I don’t see a reason why you need to pop it…
Ah, maybe not, I missed the
listTmp.next().once!!
m
@pablisco the
pop
ultimately calls the
pop
of the
Parser
class
Copy code
/**
 * wrapper for [Stack.pop]
 * @return [tokenList].[pop()][Stack.pop] on success
 *
 * **null** on failure or if the [tokenList] is corrupted
 */
fun pop(): String? =
    if (tokenList.peek() == null) {
        // attempt to check for corruption
        val v = try {
            tokenList.pop()
        } catch (e: NoSuchElementException) {
            null
        }
        if (v != null) null // corruption has occured
        else null
    }
    else tokenList.pop()
p
Can you put as much as you have on a gist? to see it all together
m
in which each
IsSequence
including
Multi
provides
toString
,
peek
, and
pop
wrappers
as far as i can tell,
or
appears to be handling itself correctly however im not 100% certain
handling of
A or B and C and D or E or F and G
and such i have no idea how to implement the assiciation for
p
I don’t want to sound rude… however, is this for university or school coursework?
m
no, this is a hobby lol
p
Aright! I wasn’t sure what kind of project would need this 😅
As it was suggested, I would suggest by adding better names, try to extract code that is duplicated and make smaller functions
m
ok, ill try to do that later