Implemented basic support for function pointers
This commit is contained in:
@ -29,6 +29,10 @@ func (f *Function) CompileCall(root *expression.Expression) (*Function, error) {
|
||||
if name == "syscall" {
|
||||
return nil, f.CompileSyscall(root)
|
||||
}
|
||||
|
||||
if name == "store" {
|
||||
return nil, f.CompileMemoryStore(root)
|
||||
}
|
||||
} else {
|
||||
pkg = nameNode.Children[0].Token.Text(f.File.Bytes)
|
||||
name = nameNode.Children[1].Token.Text(f.File.Bytes)
|
||||
|
33
src/core/CompileMemoryStore.go
Normal file
33
src/core/CompileMemoryStore.go
Normal file
@ -0,0 +1,33 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/asm"
|
||||
"git.akyoto.dev/cli/q/src/errors"
|
||||
"git.akyoto.dev/cli/q/src/expression"
|
||||
)
|
||||
|
||||
// CompileMemoryStore ...
|
||||
func (f *Function) CompileMemoryStore(root *expression.Expression) error {
|
||||
parameters := root.Children[1:]
|
||||
name := parameters[0].Token.Text(f.File.Bytes)
|
||||
numBytes, _ := f.Number(parameters[1].Token)
|
||||
value := parameters[2]
|
||||
variable := f.VariableByName(name)
|
||||
|
||||
if variable == nil {
|
||||
return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, parameters[0].Token.Position)
|
||||
}
|
||||
|
||||
defer f.UseVariable(variable)
|
||||
|
||||
memory := asm.Memory{
|
||||
Base: variable.Register,
|
||||
OffsetRegister: math.MaxUint8,
|
||||
Length: byte(numBytes),
|
||||
}
|
||||
|
||||
_, err := f.ExpressionToMemory(value, memory)
|
||||
return err
|
||||
}
|
@ -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