at operator return -1 when fails in lists and arrays; insert operators (<< and >>) changes che container if it is a variable
This commit is contained in:
+10
-20
@@ -33,7 +33,7 @@ func newAppendTerm(tk *scan.Token) (inst *scan.Term) {
|
||||
}
|
||||
}
|
||||
|
||||
func prependToList(opTerm *scan.Term, leftValue, rightValue any) (result any, err error) {
|
||||
func prependToList(opTerm kern.Term, leftValue, rightValue any) (result any, err error) {
|
||||
if a, ok := rightValue.(*array.ArrayType); ok {
|
||||
var it kern.Iterator
|
||||
if it, ok = leftValue.(kern.Iterator); !ok {
|
||||
@@ -53,6 +53,10 @@ func prependToList(opTerm *scan.Term, leftValue, rightValue any) (result any, er
|
||||
for _, item := range *a {
|
||||
newArray = append(newArray, item)
|
||||
}
|
||||
if opTerm.GetRightChild().IsVar() {
|
||||
// Update the original array if the right operand is a variable
|
||||
*a = newArray
|
||||
}
|
||||
result = &newArray
|
||||
} else if ll, ok := rightValue.(*list.LinkedList); ok {
|
||||
var it kern.Iterator
|
||||
@@ -78,7 +82,7 @@ func evalPrepend(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func appendToList(opTerm *scan.Term, leftValue, rightValue any) (result any, err error) {
|
||||
func appendToList(opTerm kern.Term, leftValue, rightValue any) (result any, err error) {
|
||||
if a, ok := leftValue.(*array.ArrayType); ok {
|
||||
var it kern.Iterator
|
||||
if it, ok = rightValue.(kern.Iterator); !ok {
|
||||
@@ -98,6 +102,10 @@ func appendToList(opTerm *scan.Term, leftValue, rightValue any) (result any, err
|
||||
for node := ls.FirstNode(); node != nil; node = node.Next() {
|
||||
newArray = append(newArray, node.Data())
|
||||
}
|
||||
if opTerm.GetLeftChild().IsVar() {
|
||||
// Update the original array if the left operand is a variable
|
||||
*a = newArray
|
||||
}
|
||||
result = &newArray
|
||||
} else if ll, ok := leftValue.(*list.LinkedList); ok {
|
||||
var it kern.Iterator
|
||||
@@ -124,24 +132,6 @@ func evalAppend(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// // func evalAssignAppend(ctx ExprContext, self *term) (v any, err error) {
|
||||
// // var leftValue, rightValue any
|
||||
|
||||
// // if leftValue, rightValue, err = self.evalInfix(ctx); err != nil {
|
||||
// // return
|
||||
// // }
|
||||
|
||||
// // if IsArray(leftValue) {
|
||||
// // list, _ := leftValue.(*ListType)
|
||||
// // newList := append(*list, rightValue)
|
||||
// // v = &newList
|
||||
// // if
|
||||
// // } else {
|
||||
// // err = self.errIncompatibleTypes(leftValue, rightValue)
|
||||
// // }
|
||||
// // return
|
||||
// // }
|
||||
|
||||
// init
|
||||
func init() {
|
||||
scan.RegisterTermConstructor(scan.SymPlusGreater, newPrependTerm)
|
||||
|
||||
Reference in New Issue
Block a user