q/src/core/CompileNew.go

18 lines
507 B
Go

package core
import (
"git.akyoto.dev/cli/q/src/asm"
"git.akyoto.dev/cli/q/src/expression"
)
// CompileNew compiles a `new` function call which allocates a struct.
func (f *Function) CompileNew(root *expression.Expression) error {
parameters := root.Children[1:]
structName := parameters[0].Token.Text(f.File.Bytes)
typ := f.Types[structName]
f.SaveRegister(f.CPU.Input[0])
f.RegisterNumber(asm.MOVE, f.CPU.Input[0], typ.Size())
f.CallSafe(f.Functions["mem.alloc"], f.CPU.Input[:1])
return nil
}