node

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package node is defines the abstract syntax tree (AST) node.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByteCode added in v1.3.0

func ByteCode(bc ByteCoder, cr compResult)

ByteCode compiles bc and appends the results in cr.

The evaluation result is left on the stack.

func ByteCodeNoStck added in v1.3.0

func ByteCodeNoStck(bc ByteCoder, cr compResult)

ByteCodeNoStck compiles bc and appends the results in cr.

The evaluation result is lost, code is expected to run for side effects.

func Graphviz added in v1.3.0

func Graphviz(top graphvizzer)

Graphviz writes dot format graphviz output of AST to stdout

print function for debugging calc. It prints an AST transforming it into a graphviz dotfile. One can convert the resulting file into an svg or other image formats.

% ./cmd --ast ../examples/euler_35.calc > x.dot
% gvpack -u x.dot > packed.dot
% dot -Tsvg packed.dot -o x.svg

func Loop added in v1.3.0

func Loop(r lineReader, p Parser, vm *vm.Type, doOut bool)

Loop is the repl-loop.

Types

type Assign added in v1.3.0

type Assign struct {
	VarRef Type // VarRef is variable reference
	Value  Type // Value is assigned value
}

func (Assign) Constant added in v1.5.0

func (a Assign) Constant() (value.Type, bool)

func (Assign) HasCall added in v1.5.0

func (a Assign) HasCall() bool

func (Assign) STRewrite added in v1.3.0

func (a Assign) STRewrite(symTbl SymTbl) Type

type Aton

type Aton struct{ Value Type }

Aton converts a string to a number type.

func (Aton) Constant added in v1.5.0

func (a Aton) Constant() (value.Type, bool)

func (Aton) HasCall added in v1.5.0

func (a Aton) HasCall() bool

func (Aton) STRewrite added in v1.3.0

func (a Aton) STRewrite(symTbl SymTbl) Type

type BinOp

type BinOp struct {
	Op    string // Op is the operator string
	Left  Type   // Left operand
	Right Type   // Right operand
}

BinOp is a binary operator of any kind, anything from "=", etc.

func (BinOp) Constant added in v1.5.0

func (b BinOp) Constant() (value.Type, bool)

func (BinOp) HasCall added in v1.5.0

func (b BinOp) HasCall() bool

func (BinOp) STRewrite added in v1.3.0

func (b BinOp) STRewrite(symTbl SymTbl) Type

type Block

type Block struct {
	Body []Type // Body is the block body
}

Block is a code block / sequence that was in '{', '}'.

func (Block) Constant added in v1.5.0

func (b Block) Constant() (value.Type, bool)

func (Block) HasCall added in v1.5.0

func (b Block) HasCall() bool

func (Block) STRewrite added in v1.3.0

func (b Block) STRewrite(symTbl SymTbl) Type

type Bool added in v1.3.0

type Bool bool

Bool is boolean literal.

func (Bool) Constant added in v1.5.0

func (b Bool) Constant() (value.Type, bool)

func (Bool) HasCall added in v1.5.0

func (b Bool) HasCall() bool

func (Bool) STRewrite added in v1.3.0

func (b Bool) STRewrite(_ SymTbl) Type

type ByteCoder added in v1.3.0

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

type Call

type Call struct {
	Name      Type // Variable referencing function
	Arguments List // Arguments passed to the function
}

Call is function call.

func (Call) Constant added in v1.5.0

func (c Call) Constant() (value.Type, bool)

func (Call) HasCall added in v1.5.0

func (c Call) HasCall() bool

func (Call) STRewrite added in v1.3.0

func (c Call) STRewrite(symTbl SymTbl) Type

type Closure added in v1.3.0

type Closure struct {
	Ix      int    // Ix is the index in the call frame
	VarName string // VarName is variable name
}

Closure variable reference.

func (Closure) Constant added in v1.5.0

func (c Closure) Constant() (value.Type, bool)

func (Closure) HasCall added in v1.5.0

func (c Closure) HasCall() bool

func (Closure) Name added in v1.4.0

func (c Closure) Name() string

func (Closure) STRewrite added in v1.3.0

func (c Closure) STRewrite(_ SymTbl) Type

type Constanter added in v1.5.0

type Constanter interface {
	Constant() (value.Type, bool) // ToValue converts a constant node to a value.
}

Constanter converts a constant node to a value.

A constant node is something that can be put in the data segment. Function literal can't be a constant node as they require code.

type Exit added in v1.3.0

type Exit struct{ Value Type }

Exit exits the interpreter with an os exit code.

func (Exit) Constant added in v1.5.0

func (e Exit) Constant() (value.Type, bool)

func (Exit) HasCall added in v1.5.0

func (e Exit) HasCall() bool

func (Exit) STRewrite added in v1.3.0

func (e Exit) STRewrite(symTbl SymTbl) Type

type FReader added in v1.4.0

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

func NewFReader added in v1.3.0

func NewFReader(fn string) FReader

func (FReader) Close added in v1.4.0

func (f FReader) Close() error

type Float

type Float float64

Float is float literal.

func (Float) Constant added in v1.5.0

func (f Float) Constant() (value.Type, bool)

func (Float) HasCall added in v1.5.0

func (f Float) HasCall() bool

func (Float) STRewrite added in v1.3.0

func (f Float) STRewrite(_ SymTbl) Type

type For added in v1.3.0

type For struct {
	VarRefs   List // VarRef is the variable references list
	Iterators List // Iterator is the iterator list
	Body      Type // Body is the loop body
}

For is a loop for iterators ans generators.

func (For) Constant added in v1.5.0

func (f For) Constant() (value.Type, bool)

func (For) HasCall added in v1.5.0

func (f For) HasCall() bool

func (For) STRewrite added in v1.3.0

func (f For) STRewrite(symTbl SymTbl) Type

type Function

type Function struct {
	Parameters List // Parameters of the function
	Body       Type // Body of the function
	LocalCnt   int  // count of local variables
}

Function is a function definition.

func (Function) Constant added in v1.5.0

func (f Function) Constant() (value.Type, bool)

func (Function) HasCall added in v1.5.0

func (f Function) HasCall() bool

func (Function) STRewrite added in v1.3.0

func (f Function) STRewrite(symTbl SymTbl) Type

type HasCaller added in v1.5.0

type HasCaller interface {
	HasCall() bool
}

HasCaller determines whether a node contains a call or not.

type If

type If struct {
	Condition Type // Condition is the condition for the if statement
	TrueCase  Type // TrueCase is executed if condition evaluates to true
}

If is a conditional construct without an else case.

func (If) Constant added in v1.5.0

func (i If) Constant() (value.Type, bool)

func (If) HasCall added in v1.5.0

func (i If) HasCall() bool

func (If) STRewrite added in v1.3.0

func (i If) STRewrite(symTbl SymTbl) Type

type IfElse

type IfElse struct {
	Condition Type // Condition is the condition for the if statement
	TrueCase  Type // TrueCase is executed if condition evaluates to true
	FalseCase Type // FalseCase is executed if condition evaluates to false
}

IfElse is a conditional construct.

func (IfElse) Constant added in v1.5.0

func (i IfElse) Constant() (value.Type, bool)

func (IfElse) HasCall added in v1.5.0

func (i IfElse) HasCall() bool

func (IfElse) STRewrite added in v1.3.0

func (i IfElse) STRewrite(symTbl SymTbl) Type

type IndexAt

type IndexAt struct {
	Ary Type // Ary is the indexed node
	At  Type // At is the index
}

func (IndexAt) Constant added in v1.5.0

func (u IndexAt) Constant() (value.Type, bool)

func (IndexAt) HasCall added in v1.5.0

func (u IndexAt) HasCall() bool

func (IndexAt) STRewrite added in v1.3.0

func (i IndexAt) STRewrite(symTbl SymTbl) Type

type IndexFromTo

type IndexFromTo struct {
	Ary  Type // Ary is the indexed node
	From Type // From is the start of the range
	To   Type // To is the end of the range
}

func (IndexFromTo) Constant added in v1.5.0

func (u IndexFromTo) Constant() (value.Type, bool)

func (IndexFromTo) HasCall added in v1.5.0

func (u IndexFromTo) HasCall() bool

func (IndexFromTo) STRewrite added in v1.3.0

func (i IndexFromTo) STRewrite(symTbl SymTbl) Type

type Int

type Int int

Int is integer literal.

func (Int) Constant added in v1.5.0

func (i Int) Constant() (value.Type, bool)

func (Int) HasCall added in v1.5.0

func (i Int) HasCall() bool

func (Int) STRewrite added in v1.3.0

func (i Int) STRewrite(_ SymTbl) Type

type Invalid

type Invalid struct{}

Invalid is an invalid AST node.

func (Invalid) ToValue added in v1.5.0

func (i Invalid) ToValue() (value.Type, bool)

type List

type List struct {
	Elems []Type // Elems are the parameters or arguments
}

List is a list of arguments or parameters depending on whether it's a function call or definition.

func (List) Constant added in v1.5.0

func (l List) Constant() (value.Type, bool)

func (List) HasCall added in v1.5.0

func (l List) HasCall() bool

func (List) STRewrite added in v1.3.0

func (l List) STRewrite(symTbl SymTbl) Type

type Local added in v1.3.0

type Local struct {
	Ix      int    // Ix is the index in the call frame
	VarName string // VarName is variable name
}

Local variable reference.

func (Local) Constant added in v1.5.0

func (l Local) Constant() (value.Type, bool)

func (Local) HasCall added in v1.5.0

func (l Local) HasCall() bool

func (Local) Name added in v1.4.0

func (l Local) Name() string

func (Local) STRewrite added in v1.3.0

func (l Local) STRewrite(_ SymTbl) Type

type Name

type Name string

Variable name.

func (Name) Constant added in v1.5.0

func (n Name) Constant() (value.Type, bool)

func (Name) HasCall added in v1.5.0

func (n Name) HasCall() bool

func (Name) Name added in v1.4.0

func (n Name) Name() string

func (Name) STRewrite added in v1.3.0

func (n Name) STRewrite(symTbl SymTbl) Type

type Namer added in v1.4.0

type Namer interface {
	Name() string
}

type Parser added in v1.3.0

type Parser interface {
	Parse(string) ([]Type, ParserError)
}

type ParserError added in v1.4.0

type ParserError = *combinator.Error

type RLReader added in v1.4.0

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

func NewRLReader added in v1.3.0

func NewRLReader() RLReader

func (RLReader) Close added in v1.4.0

func (rl RLReader) Close() error

type Read

type Read struct{}

Read reads a string from stdin.

func (Read) Constant added in v1.5.0

func (r Read) Constant() (value.Type, bool)

func (Read) HasCall added in v1.5.0

func (r Read) HasCall() bool

func (Read) STRewrite added in v1.3.0

func (r Read) STRewrite(_ SymTbl) Type

type Return

type Return struct {
	Target Type // Target is the returned value
}

Return is a return statement.

func (Return) Constant added in v1.5.0

func (r Return) Constant() (value.Type, bool)

func (Return) HasCall added in v1.5.0

func (r Return) HasCall() bool

func (Return) STRewrite added in v1.3.0

func (r Return) STRewrite(symTbl SymTbl) Type

type STRewriter added in v1.3.0

type STRewriter interface {
	STRewrite(symTbl SymTbl) Type
}

STRewriter is a recursive node transformation that resolves local and closure variable names to indices into the call frame

It is not idempotent. It is supposed to be called after the parser produced the AST and before evaluation. Global variables are untouched. Local variables are changed from Name nodes to either Local or Closure nodes.

type String

type String string

String is string literal.

func (String) Constant added in v1.5.0

func (s String) Constant() (value.Type, bool)

func (String) HasCall added in v1.5.0

func (s String) HasCall() bool

func (String) STRewrite added in v1.3.0

func (s String) STRewrite(_ SymTbl) Type

type SymTbl added in v1.3.0

type SymTbl []map[string]int

type Toa added in v1.3.0

type Toa struct{ Value Type }

Toa converts a valye to a string.

func (Toa) Constant added in v1.5.0

func (t Toa) Constant() (value.Type, bool)

func (Toa) HasCall added in v1.5.0

func (t Toa) HasCall() bool

func (Toa) STRewrite added in v1.3.0

func (t Toa) STRewrite(symTbl SymTbl) Type

type Type

type Type interface {
	STRewriter

	ByteCoder
	Constanter
	HasCaller
	// contains filtered or unexported methods
}

Type is AST node type.

type UnOp

type UnOp struct {
	Op     string // Op is the operator string
	Target Type   // Target is the operand
}

UnOp is a unary operator of any kind, ie. '-'.

func (UnOp) Constant added in v1.5.0

func (u UnOp) Constant() (value.Type, bool)

func (UnOp) HasCall added in v1.5.0

func (u UnOp) HasCall() bool

func (UnOp) STRewrite added in v1.3.0

func (u UnOp) STRewrite(symTbl SymTbl) Type

type While

type While struct {
	Condition Type // Condition is the condition for the loop
	Body      Type // Body is the loop body
}

While is a loop construct.

func (While) Constant added in v1.5.0

func (w While) Constant() (value.Type, bool)

func (While) HasCall added in v1.5.0

func (w While) HasCall() bool

func (While) STRewrite added in v1.3.0

func (w While) STRewrite(symTbl SymTbl) Type

type Write

type Write struct{ Value Type }

Write writes a value to stdout.

func (Write) Constant added in v1.5.0

func (w Write) Constant() (value.Type, bool)

func (Write) HasCall added in v1.5.0

func (w Write) HasCall() bool

func (Write) STRewrite added in v1.3.0

func (w Write) STRewrite(symTbl SymTbl) Type

type Yield added in v1.3.0

type Yield struct {
	Target Type // Target is the yielded value
}

Yield statement.

func (Yield) Constant added in v1.5.0

func (y Yield) Constant() (value.Type, bool)

func (Yield) HasCall added in v1.5.0

func (y Yield) HasCall() bool

func (Yield) STRewrite added in v1.3.0

func (y Yield) STRewrite(symTbl SymTbl) Type

Directories

Path Synopsis
package bc provides the data that is passed to calls in the bytecoder.
package bc provides the data that is passed to calls in the bytecoder.

Jump to

Keyboard shortcuts

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