diff --git a/src/core/evaluateDelete.go b/src/core/evaluateDelete.go index a6beb3b..9c39426 100644 --- a/src/core/evaluateDelete.go +++ b/src/core/evaluateDelete.go @@ -4,7 +4,6 @@ import ( "git.urbach.dev/cli/q/src/errors" "git.urbach.dev/cli/q/src/expression" "git.urbach.dev/cli/q/src/ssa" - "git.urbach.dev/cli/q/src/token" "git.urbach.dev/cli/q/src/types" ) @@ -40,25 +39,11 @@ func (f *Function) evaluateDelete(expr *expression.Expression) (ssa.Value, error return call, nil case *types.Struct: - var ( - structure = value.(*ssa.Struct) - ptr = structure.Arguments[0] - length = structure.Arguments[1] - elementSize = types.Unwrap(ptr.Type()).(*types.Pointer).To.Size() - sizeInBytes ssa.Value - ) - - if elementSize == 1 { - sizeInBytes = length - } else { - elementSizeValue := f.Append(&ssa.Int{Int: elementSize}) - - sizeInBytes = f.Append(&ssa.BinaryOp{ - Op: token.Mul, - Left: length, - Right: elementSizeValue, - }) - } + structure := value.(*ssa.Struct) + ptr := structure.Arguments[0] + numElements := structure.Arguments[1] + elementSize := types.Unwrap(ptr.Type()).(*types.Pointer).To.Size() + sizeInBytes := f.multiplySize(numElements, elementSize) call := f.Append(&ssa.Call{ Func: &ssa.Function{ @@ -71,7 +56,7 @@ func (f *Function) evaluateDelete(expr *expression.Expression) (ssa.Value, error block := f.Block() block.Unidentify(ptr) - block.Unidentify(length) + block.Unidentify(numElements) return call, nil default: diff --git a/src/core/evaluateNew.go b/src/core/evaluateNew.go index cf431f0..180f73b 100644 --- a/src/core/evaluateNew.go +++ b/src/core/evaluateNew.go @@ -5,7 +5,6 @@ import ( "git.urbach.dev/cli/q/src/expression" "git.urbach.dev/cli/q/src/ssa" - "git.urbach.dev/cli/q/src/token" "git.urbach.dev/cli/q/src/types" ) @@ -35,19 +34,7 @@ func (f *Function) evaluateNew(expr *expression.Expression) (ssa.Value, error) { sliceType = f.Env.Slice(typ) elementSize := typ.Size() - - if elementSize == 1 { - sizeInBytes = numElements - } else { - elementSizeValue := f.Append(&ssa.Int{Int: elementSize}) - - sizeInBytes = f.Append(&ssa.BinaryOp{ - Op: token.Mul, - Left: numElements, - Right: elementSizeValue, - }) - } - + sizeInBytes = f.multiplySize(numElements, elementSize) mallocType = &types.Pointer{To: typ} } else { sizeInBytes = f.Append(&ssa.Int{Int: typ.Size()}) diff --git a/src/core/multiplySize.go b/src/core/multiplySize.go new file mode 100644 index 0000000..f05f0e2 --- /dev/null +++ b/src/core/multiplySize.go @@ -0,0 +1,21 @@ +package core + +import ( + "git.urbach.dev/cli/q/src/ssa" + "git.urbach.dev/cli/q/src/token" +) + +// multiplySize multiplies the `count` with the `size` if needed. +func (f *Function) multiplySize(count ssa.Value, size int) ssa.Value { + if size == 1 { + return count + } + + sizeValue := f.Append(&ssa.Int{Int: size}) + + return f.Append(&ssa.BinaryOp{ + Op: token.Mul, + Left: count, + Right: sizeValue, + }) +} \ No newline at end of file