Implemented const keyword
This commit is contained in:
13
src/core/Constant.go
Normal file
13
src/core/Constant.go
Normal file
@ -0,0 +1,13 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"git.akyoto.dev/cli/q/src/fs"
|
||||
"git.akyoto.dev/cli/q/src/token"
|
||||
)
|
||||
|
||||
// Constant registers a single value to be accessible under a descriptive name.
|
||||
type Constant struct {
|
||||
Name string
|
||||
Value token.Token
|
||||
File *fs.File
|
||||
}
|
@ -86,11 +86,22 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
|
||||
|
||||
if node.Token.Kind == token.Period {
|
||||
left := node.Children[0]
|
||||
name := left.Token.Text(f.File.Bytes)
|
||||
variable := f.VariableByName(name)
|
||||
leftText := left.Token.Text(f.File.Bytes)
|
||||
right := node.Children[1]
|
||||
name = right.Token.Text(f.File.Bytes)
|
||||
field := variable.Type.(*types.Pointer).To.(*types.Struct).FieldByName(name)
|
||||
rightText := right.Token.Text(f.File.Bytes)
|
||||
variable := f.VariableByName(leftText)
|
||||
|
||||
if variable == nil {
|
||||
constant, isConst := f.Constants[f.Package+"."+leftText+"."+rightText]
|
||||
|
||||
if isConst {
|
||||
return f.TokenToRegister(constant.Value, register)
|
||||
}
|
||||
|
||||
return nil, errors.New(&errors.UnknownIdentifier{Name: leftText}, f.File, left.Token.Position)
|
||||
}
|
||||
|
||||
field := variable.Type.(*types.Pointer).To.(*types.Struct).FieldByName(rightText)
|
||||
f.MemoryRegister(asm.LOAD, asm.Memory{Base: variable.Register, Offset: int8(field.Offset), Length: byte(field.Type.Size())}, register)
|
||||
return field.Type, nil
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ type Function struct {
|
||||
OutputTypes []types.Type
|
||||
Functions map[string]*Function
|
||||
Structs map[string]*types.Struct
|
||||
Constants map[string]*Constant
|
||||
DLLs dll.List
|
||||
Err error
|
||||
deferred []func()
|
||||
|
Reference in New Issue
Block a user