Implemented structs

This commit is contained in:
2025-02-04 18:16:31 +01:00
parent 4609a814df
commit 03a3bd8f02
32 changed files with 267 additions and 63 deletions

33
src/core/CompileNew.go Normal file
View File

@ -0,0 +1,33 @@
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], int(typ.TotalSize()))
for _, register := range f.CPU.General {
if f.RegisterIsUsed(register) {
f.Register(asm.PUSH, register)
}
}
f.Call("mem.alloc")
for i := len(f.CPU.General) - 1; i >= 0; i-- {
register := f.CPU.General[i]
if f.RegisterIsUsed(register) {
f.Register(asm.POP, register)
}
}
return nil
}