Skip to content

Commit

Permalink
Mlrval arrayval from []Mlrval to []*Mlrval (#880)
Browse files Browse the repository at this point in the history
* to do
* Mlrval arrayval from []Mlrval to []*Mlrval
  • Loading branch information
johnkerl authored Jan 17, 2022
1 parent 4c6d43c commit 471a110
Show file tree
Hide file tree
Showing 20 changed files with 102 additions and 112 deletions.
2 changes: 1 addition & 1 deletion internal/pkg/bifs/cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func eq_b_aa(input1, input2 *mlrval.Mlrval) *mlrval.Mlrval {

// Same-length arrays: return false if any slot is not equal, else true.
for i := range a {
eq := BIF_equals(&a[i], &b[i])
eq := BIF_equals(a[i], b[i])
lib.InternalCodingErrorIf(eq.Type() != mlrval.MT_BOOL)
if eq.AcquireBoolValue() == false {
return mlrval.FALSE
Expand Down
39 changes: 20 additions & 19 deletions internal/pkg/bifs/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func depth_from_array(input1 *mlrval.Mlrval) *mlrval.Mlrval {
maxChildDepth := 0
arrayval := input1.AcquireArrayValue()
for _, child := range arrayval {
childDepth := BIF_depth(&child)
childDepth := BIF_depth(child)
lib.InternalCodingErrorIf(!childDepth.IsInt())
iChildDepth := int(childDepth.AcquireIntValue())
if iChildDepth > maxChildDepth {
Expand Down Expand Up @@ -99,9 +99,9 @@ func leafcount_from_array(input1 *mlrval.Mlrval) *mlrval.Mlrval {

childLeafCount := mlrval.FromInt(1)
if child.IsArray() {
childLeafCount = leafcount_from_array(&child)
childLeafCount = leafcount_from_array(child)
} else if child.IsMap() {
childLeafCount = leafcount_from_map(&child)
childLeafCount = leafcount_from_map(child)
}

lib.InternalCodingErrorIf(!childLeafCount.IsInt())
Expand Down Expand Up @@ -574,11 +574,11 @@ func BIF_splita(input1, input2 *mlrval.Mlrval) *mlrval.Mlrval {

fields := lib.SplitString(input1.AcquireStringValue(), fieldSeparator)

arrayval := make([]mlrval.Mlrval, len(fields))
arrayval := make([]*mlrval.Mlrval, len(fields))

for i, field := range fields {
value := mlrval.FromInferredType(field)
arrayval[i] = *value
arrayval[i] = value
}

return mlrval.FromArray(arrayval)
Expand All @@ -605,10 +605,10 @@ func BIF_splitax(input1, input2 *mlrval.Mlrval) *mlrval.Mlrval {
func bif_splitax_helper(input string, separator string) *mlrval.Mlrval {
fields := lib.SplitString(input, separator)

arrayval := make([]mlrval.Mlrval, len(fields))
arrayval := make([]*mlrval.Mlrval, len(fields))

for i, field := range fields {
arrayval[i] = *mlrval.FromString(field)
arrayval[i] = mlrval.FromString(field)
}

return mlrval.FromArray(arrayval)
Expand All @@ -619,19 +619,19 @@ func BIF_get_keys(input1 *mlrval.Mlrval) *mlrval.Mlrval {
if input1.IsMap() {
// TODO: make a ReferenceFrom with comments
mapval := input1.AcquireMapValue()
arrayval := make([]mlrval.Mlrval, mapval.FieldCount)
arrayval := make([]*mlrval.Mlrval, mapval.FieldCount)
i := 0
for pe := mapval.Head; pe != nil; pe = pe.Next {
arrayval[i] = *mlrval.FromString(pe.Key)
arrayval[i] = mlrval.FromString(pe.Key)
i++
}
return mlrval.FromArray(arrayval)

} else if input1.IsArray() {
inputarrayval := input1.AcquireArrayValue()
arrayval := make([]mlrval.Mlrval, len(inputarrayval))
arrayval := make([]*mlrval.Mlrval, len(inputarrayval))
for i := range inputarrayval {
arrayval[i] = *mlrval.FromInt(int(i + 1)) // Miller user-space indices are 1-up
arrayval[i] = mlrval.FromInt(int(i + 1)) // Miller user-space indices are 1-up
}
return mlrval.FromArray(arrayval)

Expand All @@ -644,19 +644,19 @@ func BIF_get_keys(input1 *mlrval.Mlrval) *mlrval.Mlrval {
func BIF_get_values(input1 *mlrval.Mlrval) *mlrval.Mlrval {
if input1.IsMap() {
mapval := input1.AcquireMapValue()
arrayval := make([]mlrval.Mlrval, mapval.FieldCount)
arrayval := make([]*mlrval.Mlrval, mapval.FieldCount)
i := 0
for pe := mapval.Head; pe != nil; pe = pe.Next {
arrayval[i] = *pe.Value.Copy()
arrayval[i] = pe.Value.Copy()
i++
}
return mlrval.FromArray(arrayval)

} else if input1.IsArray() {
inputarrayval := input1.AcquireArrayValue()
arrayval := make([]mlrval.Mlrval, len(inputarrayval))
arrayval := make([]*mlrval.Mlrval, len(inputarrayval))
for i, value := range inputarrayval {
arrayval[i] = *value.Copy()
arrayval[i] = value.Copy()
}
return mlrval.FromArray(arrayval)

Expand Down Expand Up @@ -743,10 +743,10 @@ func BIF_arrayify(input1 *mlrval.Mlrval) *mlrval.Mlrval {

if convertible {
mapval := input1.AcquireMapValue()
arrayval := make([]mlrval.Mlrval, input1.AcquireMapValue().FieldCount)
arrayval := make([]*mlrval.Mlrval, input1.AcquireMapValue().FieldCount)
i := 0
for pe := mapval.Head; pe != nil; pe = pe.Next {
arrayval[i] = *pe.Value.Copy()
arrayval[i] = pe.Value.Copy()
i++
}
return mlrval.FromArray(arrayval)
Expand All @@ -758,8 +758,9 @@ func BIF_arrayify(input1 *mlrval.Mlrval) *mlrval.Mlrval {
} else if input1.IsArray() {
// TODO: comment (or rethink) that this modifies its inputs!!
output := input1.Copy()
arrayval := output.AcquireArrayValue()
for i := range input1.AcquireArrayValue() {
output.AcquireArrayValue()[i] = *BIF_arrayify(&output.AcquireArrayValue()[i])
arrayval[i] = BIF_arrayify(arrayval[i])
}
return output

Expand Down Expand Up @@ -811,7 +812,7 @@ func BIF_json_stringify_binary(input1, input2 *mlrval.Mlrval) *mlrval.Mlrval {
}
}

func unaliasArrayIndex(array *[]mlrval.Mlrval, mindex int) (int, bool) {
func unaliasArrayIndex(array *[]*mlrval.Mlrval, mindex int) (int, bool) {
n := int(len(*array))
return unaliasArrayLengthIndex(n, mindex)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/bifs/collections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestBIF_depth(t *testing.T) {
assert.True(t, ok)
assert.Equal(t, 1, intval)

arrayval := make([]mlrval.Mlrval, 1)
arrayval[0] = *mlrval.FromString("value")
arrayval := make([]*mlrval.Mlrval, 1)
arrayval[0] = mlrval.FromString("value")
input1 = mlrval.FromArray(arrayval)
output = BIF_depth(input1)
intval, ok = output.GetIntValue()
Expand Down
14 changes: 7 additions & 7 deletions internal/pkg/dsl/cst/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func (node *RootNode) BuildArrayLiteralNode(
func (node *ArrayLiteralNode) Evaluate(
state *runtime.State,
) *mlrval.Mlrval {
mlrvals := make([]mlrval.Mlrval, len(node.evaluables))
mlrvals := make([]*mlrval.Mlrval, len(node.evaluables))
for i := range node.evaluables {
mlrvals[i] = *node.evaluables[i].Evaluate(state)
mlrvals[i] = node.evaluables[i].Evaluate(state)
}
return mlrval.FromArray(mlrvals)
}
Expand Down Expand Up @@ -204,7 +204,7 @@ func (node *ArraySliceAccessNode) Evaluate(
upperZindex, _ := mlrval.UnaliasArrayIndex(&array, upperIndex)

if lowerZindex > upperZindex {
return mlrval.FromArray(make([]mlrval.Mlrval, 0))
return mlrval.FromEmptyArray()
}

// Semantics: say x=[1,2,3,4,5]. Then x[3:10] is [3,4,5].
Expand All @@ -221,25 +221,25 @@ func (node *ArraySliceAccessNode) Evaluate(
if lowerZindex < 0 {
lowerZindex = 0
if lowerZindex > upperZindex {
return mlrval.FromArray(make([]mlrval.Mlrval, 0))
return mlrval.FromEmptyArray()
}
}
if upperZindex > n-1 {
upperZindex = n - 1
if lowerZindex > upperZindex {
return mlrval.FromArray(make([]mlrval.Mlrval, 0))
return mlrval.FromEmptyArray()
}
}

// Go slices have inclusive lower bound, exclusive upper bound.
// Miller slices have inclusive lower bound, inclusive upper bound.
var m = upperZindex - lowerZindex + 1

retval := make([]mlrval.Mlrval, m)
retval := make([]*mlrval.Mlrval, m)

di := 0
for si := lowerZindex; si <= upperZindex; si++ {
retval[di] = *array[si].Copy()
retval[di] = array[si].Copy()
di++
}

Expand Down
8 changes: 4 additions & 4 deletions internal/pkg/dsl/cst/for.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (node *ForLoopOneVariableNode) Execute(state *runtime.State) (*BlockExitPay
state.Stack.PushStackFrame()
defer state.Stack.PopStackFrame()
for _, element := range arrayval {
err := state.Stack.SetAtScope(node.indexVariable, &element)
err := state.Stack.SetAtScope(node.indexVariable, element)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -356,7 +356,7 @@ func (node *ForLoopTwoVariableNode) Execute(state *runtime.State) (*BlockExitPay
if err != nil {
return nil, err
}
err = state.Stack.SetAtScope(node.valueIndexVariable, &element)
err = state.Stack.SetAtScope(node.valueIndexVariable, element)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -573,7 +573,7 @@ func (node *ForLoopMultivariableNode) executeOuter(
return nil, err
}

blockExitPayload, err := node.executeOuter(&element, keyIndexVariables[1:], state)
blockExitPayload, err := node.executeOuter(element, keyIndexVariables[1:], state)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -663,7 +663,7 @@ func (node *ForLoopMultivariableNode) executeInner(
if err != nil {
return nil, err
}
err = state.Stack.SetAtScope(node.valueIndexVariable, &element)
err = state.Stack.SetAtScope(node.valueIndexVariable, element)
if err != nil {
return nil, err
}
Expand Down
39 changes: 20 additions & 19 deletions internal/pkg/dsl/cst/hofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ func selectArray(
udfCallsite := hofSpace.udfCallsite
argsArray := hofSpace.argsArray

outputArray := make([]mlrval.Mlrval, 0, len(inputArray))
outputArray := make([]*mlrval.Mlrval, 0, len(inputArray))

for i := range inputArray {
argsArray[0] = &inputArray[i]
argsArray[0] = inputArray[i]
mret := udfCallsite.EvaluateWithArguments(state, udfCallsite.udf, argsArray)
bret, ok := mret.GetBoolValue()
if !ok {
Expand All @@ -239,7 +239,7 @@ func selectArray(
os.Exit(1)
}
if bret {
outputArray = append(outputArray, *inputArray[i].Copy())
outputArray = append(outputArray, inputArray[i].Copy())
}
}
return mlrval.FromArray(outputArray)
Expand Down Expand Up @@ -315,13 +315,14 @@ func applyArray(
udfCallsite := hofSpace.udfCallsite
argsArray := hofSpace.argsArray

outputArray := make([]mlrval.Mlrval, len(inputArray))
outputArray := make([]*mlrval.Mlrval, len(inputArray))

for i := range inputArray {
argsArray[0] = &inputArray[i]
retval := *(udfCallsite.EvaluateWithArguments(state, udfCallsite.udf, argsArray))
isNonAbsentOrDie(&retval, "apply")
outputArray[i] = retval
argsArray[0] = inputArray[i]
outputArray[i] = isNonAbsentOrDie(
(udfCallsite.EvaluateWithArguments(state, udfCallsite.udf, argsArray)),
"apply",
)
}
return mlrval.FromArray(outputArray)
}
Expand Down Expand Up @@ -393,7 +394,7 @@ func reduceArray(

for i := 1; i < n; i++ {
argsArray[0] = accumulator
argsArray[1] = &inputArray[i]
argsArray[1] = inputArray[i]
accumulator = (udfCallsite.EvaluateWithArguments(state, udfCallsite.udf, argsArray))
isNonAbsentOrDie(accumulator, "apply")
}
Expand Down Expand Up @@ -470,7 +471,7 @@ func foldArray(

for i := range inputArray {
argsArray[0] = accumulator
argsArray[1] = &inputArray[i]
argsArray[1] = inputArray[i]
accumulator = (udfCallsite.EvaluateWithArguments(state, udfCallsite.udf, argsArray))
isNonAbsentOrDie(accumulator, "apply")
}
Expand Down Expand Up @@ -612,19 +613,19 @@ func sortA(
return output
}

func sortANumerical(array []mlrval.Mlrval, reverse bool) {
func sortANumerical(array []*mlrval.Mlrval, reverse bool) {
if !reverse {
sort.Slice(array, func(i, j int) bool {
return mlrval.LessThan(&array[i], &array[j])
return mlrval.LessThan(array[i], array[j])
})
} else {
sort.Slice(array, func(i, j int) bool {
return mlrval.GreaterThan(&array[i], &array[j])
return mlrval.GreaterThan(array[i], array[j])
})
}
}

func sortALexical(array []mlrval.Mlrval, reverse bool) {
func sortALexical(array []*mlrval.Mlrval, reverse bool) {
if !reverse {
sort.Slice(array, func(i, j int) bool {
return array[i].String() < array[j].String()
Expand All @@ -636,7 +637,7 @@ func sortALexical(array []mlrval.Mlrval, reverse bool) {
}
}

func sortACaseFold(array []mlrval.Mlrval, reverse bool) {
func sortACaseFold(array []*mlrval.Mlrval, reverse bool) {
if !reverse {
sort.Slice(array, func(i, j int) bool {
return strings.ToLower(array[i].String()) < strings.ToLower(array[j].String())
Expand Down Expand Up @@ -761,8 +762,8 @@ func sortAF(
outputArray := mlrval.CopyMlrvalArray(inputArray)

sort.Slice(outputArray, func(i, j int) bool {
argsArray[0] = &outputArray[i]
argsArray[1] = &outputArray[j]
argsArray[0] = outputArray[i]
argsArray[1] = outputArray[j]
// Call the user's comparator function.
mret := udfCallsite.EvaluateWithArguments(state, udfCallsite.udf, argsArray)
// Unpack the mlrval.Mlrval return value into a number.
Expand Down Expand Up @@ -865,7 +866,7 @@ func anyArray(

boolAny := false
for i := range inputArray {
argsArray[0] = &inputArray[i]
argsArray[0] = inputArray[i]
mret := udfCallsite.EvaluateWithArguments(state, udfCallsite.udf, argsArray)
bret, ok := mret.GetBoolValue()
if !ok {
Expand Down Expand Up @@ -957,7 +958,7 @@ func everyArray(

boolEvery := true
for i := range inputArray {
argsArray[0] = &inputArray[i]
argsArray[0] = inputArray[i]
mret := udfCallsite.EvaluateWithArguments(state, udfCallsite.udf, argsArray)
bret, ok := mret.GetBoolValue()
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/dsl/cst/lvalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ func (node *IndexedLvalueNode) Assign(
// This lets the user do '$y[ ["a", "b", "c"] ] = $x' in lieu of
// '$y["a"]["b"]["c"] = $x'.
if len(indices) == 1 && indices[0].IsArray() {
indices = mlrval.MakePointerArray(indices[0].GetArray())
indices = mlrval.CopyMlrvalArray(indices[0].GetArray())
}

return node.baseLvalue.AssignIndexed(rvalue, indices, state)
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/mlrval/mlrmap_accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func (mlrmap *Mlrmap) getWithMlrvalArrayIndex(index *Mlrval) (*Mlrval, error) {
array := index.arrayval
n := len(array)
for i, piece := range array {
next, err := current.GetWithMlrvalIndex(&piece)
next, err := current.GetWithMlrvalIndex(piece)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 471a110

Please sign in to comment.