Documentation ¶
Index ¶
- func AssembleProgram(lines []*Instruction) ([100]int, error)
- func IsValidAddress(address int) bool
- type AddressRef
- type AddressRegistry
- type AddressType
- type EvaluatedInstruction
- type Instruction
- func ADD(address *AddressRef) *Instruction
- func BRA(address *AddressRef) *Instruction
- func BRP(address *AddressRef) *Instruction
- func BRZ(address *AddressRef) *Instruction
- func CreateInstruction(opcode Opcode, address *AddressRef) *Instruction
- func DAT(address *AddressRef) *Instruction
- func HLT() *Instruction
- func INP() *Instruction
- func LDA(address *AddressRef) *Instruction
- func OUT() *Instruction
- func STA(address *AddressRef) *Instruction
- func SUB(address *AddressRef) *Instruction
- type InstructionType
- type Opcode
- type Parser
- type Token
- type TokenType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssembleProgram ¶
func AssembleProgram(lines []*Instruction) ([100]int, error)
Assembles the given /evaluated/ program into LMC machine code.
func IsValidAddress ¶
Establishes whether the given address is valid, noting that LMC address are only 3 digits.
Types ¶
type AddressRef ¶
type AddressRef struct { Type AddressType `json:"type"` Address int `json:"address"` Value int `json:"value"` }
An address reference.
func CreateAddressRef ¶
func CreateAddressRef(address int) *AddressRef
Creates an address reference, given a concrete address.
func CreateDataLabel ¶
func CreateDataLabel(value int) *AddressRef
Creates an address reference for the use of data labels.
func CreatePositionLabel ¶
func CreatePositionLabel() *AddressRef
Creates an address reference for the use of position labels.
func (*AddressRef) Apply ¶
func (r *AddressRef) Apply(i *Instruction) *Instruction
Applies the label to an instruction to create a labeled instruction.
type AddressRegistry ¶
type AddressRegistry map[string]*AddressRef
func (AddressRegistry) GetMapping ¶
func (r AddressRegistry) GetMapping(labelName string) *AddressRef
type AddressType ¶
type AddressType int
The type of an address reference, used for checking whether additional processing needs to happen later on.
const ( ADDR_NORMAL AddressType = iota ADDR_LABEL_POSITION ADDR_LABEL_DATA )
func (AddressType) IsLabel ¶
func (t AddressType) IsLabel() bool
Establishes whether the address type is a label type, position or data.
type EvaluatedInstruction ¶
Represents an evaluated instruction.
func CreateEvaluatedInstruction ¶
func CreateEvaluatedInstruction(opcode Opcode, address int) *EvaluatedInstruction
func DisassembleInstruction ¶
func DisassembleInstruction(instruction int) (*EvaluatedInstruction, error)
Disassemble machine code into the form of an /evaluated/ instruction.
func (*EvaluatedInstruction) Compile ¶
func (i *EvaluatedInstruction) Compile() int
Compiles the instruction.
type Instruction ¶
type Instruction struct { Type InstructionType `json:"type"` Opcode Opcode `json:"opcode"` AddressRef *AddressRef `json:"ref"` // Labeled Label *AddressRef `json:"label"` }
Represents un-evaluated instructions.
func CreateInstruction ¶
func CreateInstruction(opcode Opcode, address *AddressRef) *Instruction
func (*Instruction) Evaluate ¶
func (i *Instruction) Evaluate() (*EvaluatedInstruction, error)
Evaluates an instruction. NOTE: The address reference should have /already/ been evaluated
type InstructionType ¶
type InstructionType int
The type of an instruction, used for establishing whether the label of an instruction needs to be evaluated when assembling.
const ( INST_NORMAL InstructionType = iota INST_LABELED )
type Opcode ¶
type Opcode int
Represents the operation code of an instruction, specifying what operation to perform.
const ( // This is not actually an operation code, but rather a psuedo // Opcode for DAT instructions. OP_DAT Opcode = -1 // The HLT operation code will stop the program when run. OP_HLT Opcode = 0 // The ADD operation code will add the given argument to the // current value on the accumulator, the result will be stored // on the accumulator. OP_ADD Opcode = 1 // The SUB operation code will subtract the given argument from // the current value on the accumulator, the result will be stored // on the accumulator. OP_SUB Opcode = 2 // The STA operation code will store the value on the accumulator // to the address provided. OP_STA Opcode = 3 // The LDA operation code will load the value from the provided // address into the accumulator. OP_LDA Opcode = 5 // The BRA operation code will branch to the provided address. OP_BRA Opcode = 6 // The BRZ operation code will branch to the provided address, // given that the value on the accumulator is zero. OP_BRZ Opcode = 7 // The BRP operation code will branch to the provided address, // given that the value on the accumulator is positive. OP_BRP Opcode = 8 // The INP_OUT operation code performs two operations depending // on the provided address. // If the address is 1, the program will ask for input to be stored // in the accumulator. // If the address is 2, the program will output the value stored in // the accumulator. OP_INP_OUT Opcode = 9 )
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
func CreateParser ¶
func CreateParser() *Parser
func (*Parser) Assemble ¶
func (p *Parser) Assemble() ([]*Instruction, error)