Matthew Good
04/29/2019, 1:16 PMand 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))
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,
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())spand
04/29/2019, 1:20 PMany and all insteadribesg
04/29/2019, 1:21 PMpablisco
04/29/2019, 1:23 PMpablisco
04/29/2019, 1:25 PMMatthew Good
04/29/2019, 1:28 PMpablisco
04/29/2019, 1:28 PMpablisco
04/29/2019, 1:29 PMribesg
04/29/2019, 1:30 PMribesg
04/29/2019, 1:31 PMpablisco
04/29/2019, 1:41 PMpeekMultiple can probably be simplified as:
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
}Matthew Good
04/29/2019, 2:04 PMit.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 stackpablisco
04/29/2019, 2:06 PMlistTmp?Matthew Good
04/29/2019, 2:53 PMlistTmp 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 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 }
}pablisco
04/29/2019, 2:55 PMType it would be easier to represent what you needpablisco
04/29/2019, 2:55 PMlistTmp it’s a MutableList I don’t see a reason why you need to pop it…pablisco
04/29/2019, 2:57 PMlistTmp.next().once!!Matthew Good
04/29/2019, 2:57 PMpop ultimately calls the pop of the Parser class /**
* 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()pablisco
04/29/2019, 2:58 PMMatthew Good
04/29/2019, 2:58 PMIsSequence including Multi provides toString, peek, and pop wrappersMatthew Good
04/29/2019, 2:59 PMMatthew Good
04/29/2019, 3:01 PMor appears to be handling itself correctly however im not 100% certainMatthew Good
04/29/2019, 3:07 PMA or B and C and D or E or F and G and such i have no idea how to implement the assiciation forpablisco
04/29/2019, 3:10 PMMatthew Good
04/29/2019, 3:10 PMpablisco
04/29/2019, 3:10 PMpablisco
04/29/2019, 3:12 PMMatthew Good
04/29/2019, 3:18 PM