Allow <= and >= as overloadable operators and add in a string <= function (though some refactoring to do <, >, and => would be good) and a sorted to vec

This commit is contained in:
Nathan Braswell
2018-12-06 04:39:40 +00:00
parent d2011640f7
commit caafc6f4e4
3 changed files with 41 additions and 1 deletions

View File

@@ -66,7 +66,7 @@ scoped_identifier = scoped_identifier WS scope_op WS identifier | identifier ;
#Note that to prevent confilct with nested templates (T<A<B>>) right_shift is a nonterminal contructed as follows
right_shift = ">" ">" ;
overloadable_operator = "\+" | "-" | "\*" | "/" | "%" | "^" | "&" | "\|" | "~" | "!" | "," | "=" | "\+\+" | "--" | "<<" | "<" | ">" | right_shift | "==" | "!=" | "&&" | "\|\|" | "\+=" | "-=" | "/=" | "%=" | "^=" | "&=" | "\|=" | "\*=" | "<<=" | ">>=" | "->" | "\(" "\)" | "\[]" | "\[]=" ;
overloadable_operator = "\+" | "-" | "\*" | "/" | "%" | "^" | "&" | "\|" | "~" | "!" | "," | "=" | "\+\+" | "--" | "<<" | "<" | ">" | "<=" | ">=" | right_shift | "==" | "!=" | "&&" | "\|\|" | "\+=" | "-=" | "/=" | "%=" | "^=" | "&=" | "\|=" | "\*=" | "<<=" | ">>=" | "->" | "\(" "\)" | "\[]" | "\[]=" ;
func_identifier = identifier | identifier overloadable_operator ;
# allow omitting of return type (automatic void)

View File

@@ -206,6 +206,23 @@ obj str (Object, Serializable, Hashable) {
var str.construct(other) : str
return *this == str
}
fun operator<=(other: ref str): bool {
var l = 0
var r = 0
while (l < length() || r < other.length()) {
if l == length() {
return true
} else if r == other.length() {
return false
} else if (*this)[l] < other[r] {
return true
} else if (*this)[l] > other[r] {
return false
}
l++
r++
}
}
fun operator*(n: int): str {
var to_ret.construct(): str

View File

@@ -357,5 +357,28 @@ obj vec<T> (Object, Serializable) {
initial = func(data[i], initial)
return initial
}
fun sorted<U>(less_than: run(ref U, ref U): bool): vec<T> {
if size < 2 {
return *this
} else {
var left = slice(0, size/2).sorted(less_than)
var right = slice(size/2, -1).sorted(less_than)
var to_ret.construct(size): vec<T>
var l = 0
var r = 0
while (l < left.size || r < right.size) {
if l == left.size {
to_ret.add(right[r++])
} else if r == right.size {
to_ret.add(left[l++])
} else if less_than(left[l], right[r]) {
to_ret.add(left[l++])
} else {
to_ret.add(right[r++])
}
}
return to_ret
}
}
};