parser

package
v0.0.0-...-8bb7ff2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2020 License: GPL-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package parser consumes tokens from the lexer, and generates the AST which is then walked to generate binary code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data struct {
	Node

	// Name is the name of the data-section
	Name string

	// Contents holds the string/byte data for the reference
	Contents []byte
}

Data holds a data-statement, which might look like either of these:

.foo DB "Steve"
.bar DB 0x030, 0x40, 0x90

func (Data) String

func (d Data) String() string

String outputs this Data structure as a string.

type Error

type Error struct {
	Node
	Value string
}

Error contains an error-message

func (Error) String

func (e Error) String() string

String outputs this Error structure as a string.

type Instruction

type Instruction struct {
	Node

	// Instruction holds the instruction we've found, as a string.
	Instruction string

	// Operands holds the operands for this instruction.
	//
	// Operands will include numbers, registers, and indrected registers.
	Operands []Operand
}

Instruction holds a parsed instruction.

For example "mov rax, rax".

func (Instruction) String

func (d Instruction) String() string

String outputs this Error structure as a string

type Label

type Label struct {
	Node

	// Name has the name of the instruction
	Name string
}

Label holds a label, as seen when it is defined.

For example ":foo" will define a label with name "foo".

func (Label) String

func (l Label) String() string

String outputs this Label structure as a string.

type Node

type Node interface {
	// Output this as a readable string
	String() string
}

Node is something we return from our parser.

type Operand

type Operand struct {
	// Token contains our parent token.
	token.Token

	// If we're operating upon memory-addresses we need to be
	// able to understand the size of the thing we're operating
	// upon.
	//
	// For example `inc byte ptr [rax]` will increment a byte,
	// or 8 bits.  We have different define sizes available to us:
	//
	//   byte -> 8 bits.
	//   word -> 16 bits.
	//  dword -> 32 bites.
	//  qword -> 64 bites.
	Size int

	// Is indirection used?
	//
	// i.e. `rax` has no indirection, but `[rax]` does.
	Indirection bool
}

Operand is used to hold the operand for an instruction.

Some instructions have zero operands (e.g. `nop`), others have one (e.g. `inc rax`), and finally we have several which take two operands (e.g. `mov rax, rbx`).

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser holds our state.

func New

func New(input string) *Parser

New creates a new Parser, which will parse the specified input program into a series of tokens, and then allow it to be parsed.

func (*Parser) Next

func (p *Parser) Next() Node

Next returns the stream of parsed "things" from the input source program.

The things we return include:

  • Instructions.
  • Label definitions.
  • Data references.

There might be more things in the future.

func (*Parser) TakeOneArgument

func (p *Parser) TakeOneArgument() ([]Operand, error)

TakeOneArgument reads the argument for a single-arg instruction.

Arguments may be a register-name, number, or a label-value.

func (*Parser) TakeTwoArguments

func (p *Parser) TakeTwoArguments() ([]Operand, error)

TakeTwoArguments handles fetching two arguments for an instruction.

Arguments may be register-names, numbers, or label-values

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL