Implemented Value interface
This commit is contained in:
@ -1,11 +0,0 @@
|
||||
package eval
|
||||
|
||||
type Kind uint8
|
||||
|
||||
const (
|
||||
Invalid Kind = iota // Invalid is an invalid value.
|
||||
Number // Number is an immediately encoded value stored together with the instruction.
|
||||
Register // Register is a CPU register.
|
||||
Memory // Memory is an area in the RAM.
|
||||
Label // Label is a reference to a name that can only be resolved once the program is fully compiled.
|
||||
)
|
17
src/eval/Label.go
Normal file
17
src/eval/Label.go
Normal file
@ -0,0 +1,17 @@
|
||||
package eval
|
||||
|
||||
import "git.urbach.dev/cli/q/src/types"
|
||||
|
||||
// Label is a named pointer to a code or data section.
|
||||
type Label struct {
|
||||
Typ types.Type
|
||||
Label string
|
||||
}
|
||||
|
||||
func (v Label) String() string {
|
||||
return "Label"
|
||||
}
|
||||
|
||||
func (v Label) Type() types.Type {
|
||||
return v.Typ
|
||||
}
|
20
src/eval/Memory.go
Normal file
20
src/eval/Memory.go
Normal file
@ -0,0 +1,20 @@
|
||||
package eval
|
||||
|
||||
import (
|
||||
"git.urbach.dev/cli/q/src/asm"
|
||||
"git.urbach.dev/cli/q/src/types"
|
||||
)
|
||||
|
||||
// Memory is a region in memory that can be addressed by an instruction.
|
||||
type Memory struct {
|
||||
Typ types.Type
|
||||
Memory asm.Memory
|
||||
}
|
||||
|
||||
func (v Memory) String() string {
|
||||
return "Memory"
|
||||
}
|
||||
|
||||
func (v Memory) Type() types.Type {
|
||||
return v.Typ
|
||||
}
|
17
src/eval/Number.go
Normal file
17
src/eval/Number.go
Normal file
@ -0,0 +1,17 @@
|
||||
package eval
|
||||
|
||||
import "git.urbach.dev/cli/q/src/types"
|
||||
|
||||
// Number is an immediate value that is stored next to the instruction.
|
||||
type Number struct {
|
||||
Typ types.Type
|
||||
Number int
|
||||
}
|
||||
|
||||
func (v Number) String() string {
|
||||
return "Number"
|
||||
}
|
||||
|
||||
func (v Number) Type() types.Type {
|
||||
return v.Typ
|
||||
}
|
21
src/eval/Register.go
Normal file
21
src/eval/Register.go
Normal file
@ -0,0 +1,21 @@
|
||||
package eval
|
||||
|
||||
import (
|
||||
"git.urbach.dev/cli/q/src/cpu"
|
||||
"git.urbach.dev/cli/q/src/types"
|
||||
)
|
||||
|
||||
// Register is a value that is stored inside a CPU register.
|
||||
type Register struct {
|
||||
Typ types.Type
|
||||
Alive uint8
|
||||
Register cpu.Register
|
||||
}
|
||||
|
||||
func (v Register) String() string {
|
||||
return "Register"
|
||||
}
|
||||
|
||||
func (v Register) Type() types.Type {
|
||||
return v.Typ
|
||||
}
|
@ -1,29 +1,22 @@
|
||||
package eval
|
||||
|
||||
import (
|
||||
"git.urbach.dev/cli/q/src/asm"
|
||||
"git.urbach.dev/cli/q/src/cpu"
|
||||
"git.urbach.dev/cli/q/src/types"
|
||||
)
|
||||
|
||||
// Value combines a register with its data type.
|
||||
type Value struct {
|
||||
Type types.Type
|
||||
Label string
|
||||
Number int
|
||||
Memory asm.Memory
|
||||
Register cpu.Register
|
||||
Alive uint8
|
||||
Kind Kind
|
||||
// Value abstracts all data storage types like immediates, registers and memory.
|
||||
type Value interface {
|
||||
String() string
|
||||
Type() types.Type
|
||||
}
|
||||
|
||||
// IsAlive returns true if the Value is still alive.
|
||||
func (v *Value) IsAlive() bool {
|
||||
// IsAlive returns true if the register value is still alive.
|
||||
func (v *Register) IsAlive() bool {
|
||||
return v.Alive > 0
|
||||
}
|
||||
|
||||
// Use reduces the lifetime counter by one.
|
||||
func (v *Value) Use() {
|
||||
func (v *Register) Use() {
|
||||
if v.Alive == 0 {
|
||||
panic("incorrect number of value use calls")
|
||||
}
|
||||
|
Reference in New Issue
Block a user