lexer

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package lexer tokenises bc source into a stream of tokens.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Kind

type Kind int

Kind is the type of a token.

const (
	EOF     Kind = iota
	NEWLINE      // significant statement terminator
	NUMBER
	STRING
	IDENT

	// keywords
	KwIf
	KwElse
	KwWhile
	KwFor
	KwBreak
	KwContinue
	KwReturn
	KwDefine
	KwAuto
	KwPrint
	KwQuit
	KwHalt
	KwLimits
	KwWarranty
	KwLength
	KwSqrt
	KwScale // scale used as function: scale(expr)
	KwRead
	KwLast // "last" keyword

	// operators
	PLUS   // +
	MINUS  // -
	STAR   // *
	SLASH  // /
	PCT    // %
	CARET  // ^
	AND    // &&
	OR     // ||
	NOT    // !
	LT     // <
	LE     // <=
	GT     // >
	GE     // >=
	EQ     // ==
	NE     // !=
	ASSIGN // =
	ADDAS  // +=
	SUBAS  // -=
	MULAS  // *=
	DIVAS  // /=
	MODAS  // %=
	POWAS  // ^=
	INC    // ++
	DEC    // --

	// punctuation
	LPAREN // (
	RPAREN // )
	LBRACK // [
	RBRACK // ]
	LBRACE // {
	RBRACE // }
	COMMA  // ,
	SEMI   // ;
	DOT    // . (shorthand for last)
)

func (Kind) String

func (k Kind) String() string

type Lexer

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

Lexer tokenises bc source from a string.

func New

func New(src string) *Lexer

New creates a Lexer for the given source.

func (*Lexer) Next

func (l *Lexer) Next() Token

Next returns and consumes the next token.

func (*Lexer) Peek

func (l *Lexer) Peek() Token

Peek returns the next token without consuming it.

type Token

type Token struct {
	Kind Kind
	Text string // raw text (for NUMBER, STRING, IDENT)
	Line int
}

Token is a single lexical token.

func (Token) String

func (t Token) String() string

Jump to

Keyboard shortcuts

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