Simplified Evaluate function
This commit is contained in:
parent
3d294eb35c
commit
fbcadae268
14 changed files with 318 additions and 133 deletions
60
src/core/DotToRegister.go
Normal file
60
src/core/DotToRegister.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"git.urbach.dev/cli/q/src/asm"
|
||||
"git.urbach.dev/cli/q/src/cpu"
|
||||
"git.urbach.dev/cli/q/src/errors"
|
||||
"git.urbach.dev/cli/q/src/expression"
|
||||
"git.urbach.dev/cli/q/src/types"
|
||||
)
|
||||
|
||||
// DotToRegister moves a constant or a function address into the given register.
|
||||
func (f *Function) DotToRegister(node *expression.Expression, register cpu.Register) (types.Type, error) {
|
||||
left := node.Children[0]
|
||||
right := node.Children[1]
|
||||
leftText := left.Token.Text(f.File.Bytes)
|
||||
rightText := right.Token.Text(f.File.Bytes)
|
||||
variable := f.VariableByName(leftText)
|
||||
|
||||
if variable != nil {
|
||||
field := variable.Type.(*types.Pointer).To.(*types.Struct).FieldByName(rightText)
|
||||
|
||||
memory := asm.Memory{
|
||||
Base: variable.Register,
|
||||
Offset: int8(field.Offset),
|
||||
OffsetRegister: math.MaxUint8,
|
||||
Length: byte(field.Type.Size()),
|
||||
}
|
||||
|
||||
f.MemoryRegister(asm.LOAD, memory, register)
|
||||
return field.Type, nil
|
||||
}
|
||||
|
||||
constant, isConst := f.All.Constants[f.Package+"."+leftText+"."+rightText]
|
||||
|
||||
if isConst {
|
||||
number, err := ToNumber(constant.Token, constant.File)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
f.SaveRegister(register)
|
||||
f.RegisterNumber(asm.MOVE, register, number)
|
||||
return types.AnyInt, nil
|
||||
}
|
||||
|
||||
uniqueName := fmt.Sprintf("%s.%s", leftText, rightText)
|
||||
function, exists := f.All.Functions[uniqueName]
|
||||
|
||||
if exists {
|
||||
f.File.Imports[leftText].Used = true
|
||||
f.RegisterLabel(asm.MOVE, register, function.UniqueName)
|
||||
return types.AnyPointer, nil
|
||||
}
|
||||
|
||||
return nil, errors.New(&errors.UnknownIdentifier{Name: uniqueName}, f.File, left.Token.Position)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue