Implemented basic support for function pointers
This commit is contained in:
@ -1,35 +1,53 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/asm"
|
||||
"git.akyoto.dev/cli/q/src/errors"
|
||||
"git.akyoto.dev/cli/q/src/expression"
|
||||
"git.akyoto.dev/cli/q/src/sizeof"
|
||||
"git.akyoto.dev/cli/q/src/token"
|
||||
"git.akyoto.dev/cli/q/src/types"
|
||||
)
|
||||
|
||||
// ExpressionToMemory puts the result of an expression into the specified memory address.
|
||||
func (f *Function) ExpressionToMemory(node *expression.Expression, memory asm.Memory) (*types.Type, error) {
|
||||
if node.IsLeaf() && node.Token.IsNumeric() {
|
||||
number, err := f.Number(node.Token)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if node.IsLeaf() {
|
||||
if node.Token.Kind == token.Identifier {
|
||||
f.MemoryLabel(asm.STORE, memory, fmt.Sprintf("%s.%s", f.Package, node.Token.Text(f.File.Bytes)))
|
||||
return types.Pointer, nil
|
||||
}
|
||||
|
||||
size := byte(sizeof.Signed(int64(number)))
|
||||
if node.Token.IsNumeric() {
|
||||
number, err := f.Number(node.Token)
|
||||
|
||||
if size != memory.Length {
|
||||
return nil, errors.New(&errors.NumberExceedsBounds{Number: number, ExpectedSize: memory.Length, Size: size}, f.File, node.Token.Position)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
size := byte(sizeof.Signed(int64(number)))
|
||||
|
||||
if size != memory.Length {
|
||||
return nil, errors.New(&errors.NumberExceedsBounds{Number: number, ExpectedSize: memory.Length, Size: size}, f.File, node.Token.Position)
|
||||
}
|
||||
|
||||
f.MemoryNumber(asm.STORE, memory, number)
|
||||
return types.Int, nil
|
||||
}
|
||||
|
||||
f.MemoryNumber(asm.STORE, memory, number)
|
||||
return types.Int, nil
|
||||
}
|
||||
|
||||
tmp := f.NewRegister()
|
||||
defer f.FreeRegister(tmp)
|
||||
typ, err := f.ExpressionToRegister(node, tmp)
|
||||
f.MemoryRegister(asm.STORE, memory, tmp)
|
||||
typ, register, isTemporary, err := f.Evaluate(node)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
f.MemoryRegister(asm.STORE, memory, register)
|
||||
|
||||
if isTemporary {
|
||||
f.FreeRegister(register)
|
||||
}
|
||||
|
||||
return typ, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user