lexer

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package lexer provides a way to convert a Watson Representation into a sequence of Watson's instructions and vice versa. (where Watson Representation is a sequence of character that represents Watson's instructions).

The correspondence between characters and instructions depends on the lexer's mode. Each lexer has its own mode. The mode of a lexer is either `A` or `S`. The initial mode of a lexer is A unless otherwise specified.

The complete conversion table between instructions and their Watson Representations are as follows:

+-----------+--------------+--------------+
|Instruction|Watson        |Watson        |
|           |Representation|Representation|
|           |(mode = A)    |(mode = S)    |
+-----------+--------------+--------------+
|Inew       |B             |S             |
+-----------+--------------+--------------+
|Iinc       |u             |h             |
+-----------+--------------+--------------+
|Ishl       |b             |a             |
+-----------+--------------+--------------+
|Iadd       |a             |k             |
+-----------+--------------+--------------+
|Ineg       |A             |r             |
+-----------+--------------+--------------+
|Isht       |e             |A             |
+-----------+--------------+--------------+
|Itof       |i             |z             |
+-----------+--------------+--------------+
|Itou       |'             |i             |
+-----------+--------------+--------------+
|Finf       |q             |m             |
+-----------+--------------+--------------+
|Fnan       |t             |b             |
+-----------+--------------+--------------+
|Fneg       |p             |u             |
+-----------+--------------+--------------+
|Snew       |?             |$             |
+-----------+--------------+--------------+
|Sadd       |!             |-             |
+-----------+--------------+--------------+
|Onew       |~             |+             |
+-----------+--------------+--------------+
|Oadd       |M             |g             |
+-----------+--------------+--------------+
|Anew       |@             |v             |
+-----------+--------------+--------------+
|Aadd       |s             |?             |
+-----------+--------------+--------------+
|Bnew       |z             |^             |
+-----------+--------------+--------------+
|Bneg       |o             |!             |
+-----------+--------------+--------------+
|Nnew       |.             |y             |
+-----------+--------------+--------------+
|Gdup       |E             |/             |
+-----------+--------------+--------------+
|Gpop       |#             |e             |
+-----------+--------------+--------------+
|Gswp       |%             |:             |
+-----------+--------------+--------------+

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lexer

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

Lexer converts a Watson Representation into a sequence of `vm.Op`s. Each lexer has its state called mode. Its default mode is A, and whenever it yields the `Snew` instruction, it flips its mode.

Example: Consider the situation where the lexer tries to read the following string:

b?b$q

As described above, the lexer's initial mode is A. The lexer first hits 'b' and regards it as `Ishl`. Then it hits the character '?', where it changes its mode from A to S. More specifically, the lexer reads a character '?' and yields `Snew` since its current state is A. Then it changes its current state to S. After that, it hits 'b' again, but in this time the 'b' is interpreted differently from the previous lexing step. Since the current mode of the lexer is S, it regards 'b' as `Fnan` instead of `Ishl`. Then it hits '?', which is now interpreted as `Snew`, yields `Snew`, and changes its current mode to A. In the end, it hits 'q' and yields `Finf`, and it stops its lexing procedure.

func NewLexer

func NewLexer(r io.Reader, opts ...LexerOption) *Lexer

Creates a new Lexer that reads Watson Representation from r.

func (*Lexer) Mode

func (l *Lexer) Mode() Mode

Returns its current mode.

func (*Lexer) Next

func (l *Lexer) Next() (*Token, error)

Returns the next Op. This returns io.EOF if it hits on the end of the input.

type LexerOption

type LexerOption interface {
	// contains filtered or unexported methods
}

LexerOption configures a Lexer.

func WithFileName

func WithFileName(name string) LexerOption

WithFileName sets a file name of a lexer. File name is only used to generate error messages.

func WithInitialLexerMode

func WithInitialLexerMode(mode Mode) LexerOption

WithInitialLexerMode sets an initial mode of a lexer.

type Mode

type Mode int

Mode is an important concept that is unique to Watson. It determines the correspondence between Vm's instructions and their ASCII representation.

const (
	A Mode = iota // A, S are the modes of the lexer. See the overview for more details.
	S
)

type OpWriter

type OpWriter interface {
	Write(vm.Op) error
	Mode() Mode
}

OpWriter is an abstract interface that defines what the Unlexer does.

type SliceWriter

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

SliceWriter is a simple `lexer.OpWriter` that is intended to test anything that uses Unlexer.

func NewSliceWriter

func NewSliceWriter() *SliceWriter

NewSliceWriter creates a new `SliceWriter`.

func (*SliceWriter) Mode

func (s *SliceWriter) Mode() Mode

func (*SliceWriter) Ops

func (s *SliceWriter) Ops() []vm.Op

Returns the `vm.Op`s that was written by previous `Write`s.

func (*SliceWriter) Write

func (s *SliceWriter) Write(op vm.Op) error

type Token

type Token struct {
	Op       vm.Op
	FileName string
	Line     int
	Column   int
}

Token is a token yielded by Lexer.

type Unlexer

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

Unlexer converts a sequence of `vm.Op`s into a sequence of characters.

func NewUnlexer

func NewUnlexer(w io.Writer, opts ...UnlexerOption) *Unlexer

NewUnlexer returns a new Unlexer that writes to w.

func (*Unlexer) Mode

func (u *Unlexer) Mode() Mode

Mode returns the unlexer's current mode.

func (*Unlexer) Write

func (u *Unlexer) Write(op vm.Op) error

Write writes an Op to the underlying io.Writer.

type UnlexerOption

type UnlexerOption interface {
	// contains filtered or unexported methods
}

UnlexerOption configures the Unlexer.

func WithInitialUnlexerMode

func WithInitialUnlexerMode(m Mode) UnlexerOption

WithInitialUnlexerMode sets an initial mode of an Unlexer.

Jump to

Keyboard shortcuts

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