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.
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.
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.
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.
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.