forth

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2025 License: MPL-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Copyright 2024-2025 Blake Felt blake.w.felt@gmail.com

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

Copyright 2024-2025 Blake Felt blake.w.felt@gmail.com

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

Copyright 2024-2025 Blake Felt blake.w.felt@gmail.com

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

Copyright 2024-2025 Blake Felt blake.w.felt@gmail.com

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

Copyright 2024-2025 Blake Felt blake.w.felt@gmail.com

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

Copyright 2024-2025 Blake Felt blake.w.felt@gmail.com

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

Copyright 2024-2025 Blake Felt blake.w.felt@gmail.com

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

Copyright 2024-2025 Blake Felt blake.w.felt@gmail.com

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

Copyright 2024-2025 Blake Felt blake.w.felt@gmail.com

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

Copyright 2024-2025 Blake Felt blake.w.felt@gmail.com

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

Copyright 2024-2025 Blake Felt blake.w.felt@gmail.com

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

Copyright 2024-2025 Blake Felt blake.w.felt@gmail.com

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

Copyright 2024-2025 Blake Felt blake.w.felt@gmail.com

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

Index

Constants

View Source
const (
	UlpCompileTargetToken = iota
	UlpCompileTargetSubroutine
)

Variables

This section is empty.

Functions

func EntryError

func EntryError(entry *DictionaryEntry, format string, a ...interface{}) error

func JoinEntryError

func JoinEntryError(err error, entry *DictionaryEntry, format string, a ...interface{}) error

func PopError

func PopError(err error, entry *DictionaryEntry) error

func PrimitiveSetup

func PrimitiveSetup(vm *VirtualMachine) error

func PushError

func PushError(err error, entry *DictionaryEntry) error

Types

type Cell

type Cell interface {
	Execute(*VirtualMachine) error
	// Compile(*Ulp) (string, error) // The compiled string for the output assembly.
	AddToList(*Ulp) error
	// Build the assembly necessary to execute this cell.
	BuildExecution(*Ulp) (string, error)
	// The string reference for this cell, used for literals
	// and when the cell is stored as data.
	OutputReference(*Ulp) (string, error)
	// If this cell in some way refers to the input word, return true.
	IsRecursive(*WordForth) bool
}

A Cell is the smallest unit of address within Forth. Cells are what constitute words, stack entries, etc.

type CellAddress

type CellAddress struct {
	Entry     *DictionaryEntry // The dictionary entry with the address.
	Offset    int              // The offset within that entry.
	UpperByte bool             // If we're aligned to the upper byte.
}

A Cell representing an address in the dictionary. Used for pointers such as return addresses.

func (CellAddress) AddToList

func (c CellAddress) AddToList(u *Ulp) error

func (CellAddress) BuildExecution

func (c CellAddress) BuildExecution(u *Ulp) (string, error)

func (CellAddress) Execute

func (c CellAddress) Execute(vm *VirtualMachine) error

func (CellAddress) IsRecursive

func (c CellAddress) IsRecursive(check *WordForth) bool

func (CellAddress) OutputReference

func (c CellAddress) OutputReference(u *Ulp) (string, error)

func (CellAddress) String

func (c CellAddress) String() string

type CellBranch

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

A definite branch.

func (*CellBranch) AddToList

func (c *CellBranch) AddToList(u *Ulp) error

func (*CellBranch) BuildExecution

func (c *CellBranch) BuildExecution(u *Ulp) (string, error)

func (*CellBranch) Execute

func (c *CellBranch) Execute(vm *VirtualMachine) error

func (*CellBranch) IsRecursive

func (c *CellBranch) IsRecursive(check *WordForth) bool

func (*CellBranch) OutputReference

func (c *CellBranch) OutputReference(u *Ulp) (string, error)

func (*CellBranch) String

func (c *CellBranch) String() string

type CellBranch0

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

A conditional branch.

func (*CellBranch0) AddToList

func (c *CellBranch0) AddToList(u *Ulp) error

func (*CellBranch0) BuildExecution

func (c *CellBranch0) BuildExecution(u *Ulp) (string, error)

func (*CellBranch0) Execute

func (c *CellBranch0) Execute(vm *VirtualMachine) error

func (*CellBranch0) IsRecursive

func (c *CellBranch0) IsRecursive(check *WordForth) bool

func (*CellBranch0) OutputReference

func (c *CellBranch0) OutputReference(u *Ulp) (string, error)

func (*CellBranch0) String

func (c *CellBranch0) String() string

type CellDestination

type CellDestination struct {
	Addr CellAddress // the address of this destination
	// contains filtered or unexported fields
}

A destination to branch to. Only used during compilation.

func (*CellDestination) AddToList

func (c *CellDestination) AddToList(u *Ulp) error

func (*CellDestination) BuildExecution

func (c *CellDestination) BuildExecution(u *Ulp) (string, error)

func (*CellDestination) Execute

func (c *CellDestination) Execute(vm *VirtualMachine) error

func (*CellDestination) IsRecursive

func (c *CellDestination) IsRecursive(check *WordForth) bool

func (*CellDestination) OutputReference

func (c *CellDestination) OutputReference(u *Ulp) (string, error)

func (*CellDestination) String

func (c *CellDestination) String() string

type CellLiteral

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

A Cell that places the underlying cell on the stack. Used for execution tokens.

func (CellLiteral) AddToList

func (c CellLiteral) AddToList(u *Ulp) error

func (CellLiteral) BuildExecution

func (c CellLiteral) BuildExecution(u *Ulp) (string, error)

func (CellLiteral) Execute

func (c CellLiteral) Execute(vm *VirtualMachine) error

func (CellLiteral) IsRecursive

func (c CellLiteral) IsRecursive(check *WordForth) bool

func (CellLiteral) OutputReference

func (c CellLiteral) OutputReference(u *Ulp) (string, error)

func (CellLiteral) String

func (c CellLiteral) String() string

type CellNumber

type CellNumber struct {
	Number uint16 // The unsigned number that this cell represents.
}

A Cell representing a number.

func GetCellNumber

func GetCellNumber(c Cell) (CellNumber, error)

func (CellNumber) AddToList

func (c CellNumber) AddToList(u *Ulp) error

func (CellNumber) BuildExecution

func (c CellNumber) BuildExecution(u *Ulp) (string, error)

func (CellNumber) Execute

func (c CellNumber) Execute(vm *VirtualMachine) error

func (CellNumber) IsRecursive

func (c CellNumber) IsRecursive(check *WordForth) bool

func (CellNumber) OutputReference

func (c CellNumber) OutputReference(u *Ulp) (string, error)

func (CellNumber) String

func (c CellNumber) String() string

type CellTailCall

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

Used during an optimization pass to add tail calls.

func (*CellTailCall) AddToList

func (c *CellTailCall) AddToList(u *Ulp) error

func (*CellTailCall) BuildExecution

func (c *CellTailCall) BuildExecution(u *Ulp) (string, error)

func (*CellTailCall) Execute

func (c *CellTailCall) Execute(vm *VirtualMachine) error

func (*CellTailCall) IsRecursive

func (c *CellTailCall) IsRecursive(check *WordForth) bool

func (*CellTailCall) OutputReference

func (c *CellTailCall) OutputReference(u *Ulp) (string, error)

func (*CellTailCall) String

func (c *CellTailCall) String() string

type Dictionary

type Dictionary struct {
	Entries []*DictionaryEntry
	// contains filtered or unexported fields
}

The Forth Dictionary. This architecture uses individual entries representing words rather than a flat cell structure.

func (*Dictionary) AddEntry

func (d *Dictionary) AddEntry(entry *DictionaryEntry) error

func (*Dictionary) FindName

func (d *Dictionary) FindName(name string) (*DictionaryEntry, error)

func (*Dictionary) LastForthWord

func (d *Dictionary) LastForthWord() (*WordForth, error)

func (*Dictionary) Setup

func (d *Dictionary) Setup(vm *VirtualMachine) error

Set up the empty dictionary.

type DictionaryEntry

type DictionaryEntry struct {
	Name      string
	NameLower string

	Word Word
	Flag Flag
	// contains filtered or unexported fields
}

A Dictionary entry. Contains the name, the word itself, and the flags.

func (*DictionaryEntry) AddToList

func (d *DictionaryEntry) AddToList(u *Ulp) error

func (*DictionaryEntry) BodyLabel

func (d *DictionaryEntry) BodyLabel() string

func (*DictionaryEntry) ClearVisited

func (d *DictionaryEntry) ClearVisited()

func (DictionaryEntry) Details

func (d DictionaryEntry) Details() string

func (DictionaryEntry) String

func (d DictionaryEntry) String() string

type DictionaryEntryError

type DictionaryEntryError struct {
	Entry  *DictionaryEntry
	Format string
	Args   []interface{}
}

func (DictionaryEntryError) Error

func (e DictionaryEntryError) Error() string

type Flag

type Flag struct {
	Hidden     bool // This word is hidden from being found.
	Immediate  bool // This word should be executed immediately.
	Data       bool // This Forth word is data and should not be optimized.
	GlobalData bool // This data word should be marked as .global in the output.
	// contains filtered or unexported fields
}

Flags associated with each word

type Optimizer

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

func (*Optimizer) Optimize

func (o *Optimizer) Optimize() error

type ParseArea

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

The input parse area.

func (*ParseArea) Fill

func (p *ParseArea) Fill(bytes []byte) error

func (*ParseArea) Restore

func (p *ParseArea) Restore() error

func (*ParseArea) Save

func (p *ParseArea) Save() error

func (*ParseArea) Setup

func (p *ParseArea) Setup() error

Set up the parse area.

func (*ParseArea) Word

func (p *ParseArea) Word(delimiter byte) ([]byte, error)

type PrimitiveGo

type PrimitiveGo func(*VirtualMachine, *DictionaryEntry) error

The Go code for a primitive Word.

type PrimitiveUlp

type PrimitiveUlp struct {
	Asm  []string
	Next TokenNextType
}

The ULP assembly for a primitive Word that uses token threading. type PrimitiveUlp []string

type PrimitiveUlpSrt

type PrimitiveUlpSrt struct {
	Asm             []string // the assembly, not including NEXT if standard
	NonStandardNext bool     // this word uses a nonstandard NEXT ending
}

The ULP assembly for a primitive Word that uses subroutine threading

type Stack

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

The structure for stacks.

func (*Stack) Depth

func (s *Stack) Depth() int

Get the current depth of the stack.

func (*Stack) Pop

func (s *Stack) Pop() (Cell, error)

Pop a cell from the stack.

func (*Stack) PopNumber

func (s *Stack) PopNumber() (uint16, error)

Pop a number from the stack.

func (*Stack) Push

func (s *Stack) Push(c Cell) error

Push a cell onto the stack.

func (*Stack) Reset

func (s *Stack) Reset()

func (*Stack) SetDepth

func (s *Stack) SetDepth(depth int) error

Set the stack depth. Must not be greater than current depth.

func (*Stack) Setup

func (s *Stack) Setup() error

Set up the stack.

func (Stack) String

func (s Stack) String() string

type State

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

func (*State) Get

func (m *State) Get() (StateType, error)

func (*State) Set

func (m *State) Set(stateType StateType) error

func (*State) Setup

func (m *State) Setup(vm *VirtualMachine) error

type StateType

type StateType int

The execution state.

const (
	StateInterpret StateType = iota // Interpret parsed words.
	StateCompile                    // Compile parsed words to Forth.
	StateExit                       // Exit the virtual machine.
	StateUnknown                    // Unknown what state the vm is supposed to be in.
)

func (StateType) String

func (m StateType) String() string

type TokenNextType

type TokenNextType int
const (
	// update the constants in 11_misc.f if this list changes
	TokenNextNonstandard TokenNextType = iota
	TokenNextNormal
	TokenNextSkipR2
	TokenNextSkipLoad
)

type Ulp

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

func (*Ulp) BuildAssembly

func (u *Ulp) BuildAssembly(vm *VirtualMachine, word string) (string, error)

Build the assembly using the word passed in as the main function. Note that the virtual machine will be unusable after this.

func (*Ulp) BuildAssemblySrt

func (u *Ulp) BuildAssemblySrt(vm *VirtualMachine, word string) (string, error)

type UlpCompileTarget

type UlpCompileTarget int

The target that we are cross-compiling.

type UlpCompileType

type UlpCompileType int

The type of information that we are currently cross-compiling.

const (
	UlpCompileProgram UlpCompileType = iota
	UlpCompileData
)

type VMNumber

type VMNumber struct {
	Word WordForth
}

func (*VMNumber) Get

func (n *VMNumber) Get() (uint16, error)

func (*VMNumber) Set

func (n *VMNumber) Set(num uint16) error

func (*VMNumber) Setup

func (n *VMNumber) Setup(vm *VirtualMachine, name string, shared bool) error

type VirtualMachine

type VirtualMachine struct {
	Dictionary       Dictionary   // The Forth dictionary.
	Stack            Stack        // The data stack.
	ReturnStack      Stack        // The return stack.
	ControlFlowStack Stack        // The control flow stack.
	DoStack          Stack        // A stack for compiling DO loops.
	ParseArea        ParseArea    // The input parse area.
	State            VMNumber     // The execution state for the virtual machine. Convert to type State when using.
	IP               *CellAddress // The interpreter pointer.
	Base             VMNumber     // The number base.
	Out              io.Writer    // The output for the vm.
	// contains filtered or unexported fields
}

The Forth virtual machine.

func (*VirtualMachine) BuiltinEsp32

func (vm *VirtualMachine) BuiltinEsp32() error

BuiltinEsp32 runs the files intended for accessing esp32 hardware.

func (*VirtualMachine) Execute

func (vm *VirtualMachine) Execute(b []byte) error

Execute the given bytes.

func (*VirtualMachine) ExecuteFile

func (vm *VirtualMachine) ExecuteFile(f fs.File) error

func (*VirtualMachine) ReplClose added in v0.0.4

func (vm *VirtualMachine) ReplClose() error

Close the Repl.

func (*VirtualMachine) ReplRun added in v0.0.4

func (vm *VirtualMachine) ReplRun() error

Run the read-eval-print loop.

func (*VirtualMachine) ReplSetup added in v0.0.4

func (vm *VirtualMachine) ReplSetup() error

Set up the Repl. Should be closed with ReplClose().

func (*VirtualMachine) Reset

func (vm *VirtualMachine) Reset() error

func (*VirtualMachine) Setup

func (vm *VirtualMachine) Setup() error

Set up the virtual machine.

type Word

type Word interface {
	Execute(*VirtualMachine) error // Execute the word.
	AddToList(*Ulp) error
	BuildAssembly(*Ulp) (string, error)
	IsRecursive(*WordForth) bool
}

An executable Forth Word.

type WordForth

type WordForth struct {
	Cells []Cell           // The cells that constitute this word.
	Entry *DictionaryEntry // The associated dictionary entry.
}

A Word built using other Forth words/numbers.

func (*WordForth) AddToList

func (w *WordForth) AddToList(u *Ulp) error

func (*WordForth) BuildAssembly

func (w *WordForth) BuildAssembly(u *Ulp) (string, error)

func (*WordForth) Execute

func (w *WordForth) Execute(vm *VirtualMachine) error

func (*WordForth) ExecuteOffset

func (w *WordForth) ExecuteOffset(vm *VirtualMachine, offset int) error

func (*WordForth) IsRecursive

func (w *WordForth) IsRecursive(check *WordForth) bool

type WordPrimitive

type WordPrimitive struct {
	Go     PrimitiveGo      // The Go function to be executed.
	Ulp    PrimitiveUlp     // The ULP assembly to be compiled.
	UlpSrt PrimitiveUlpSrt  // The ULP assembly using subroutine threading to be compiled.
	Entry  *DictionaryEntry // The associated dictionary entry.
}

A Word defined using Go and ULP assembly.

func (*WordPrimitive) AddToList

func (w *WordPrimitive) AddToList(u *Ulp) error

func (*WordPrimitive) BuildAssembly

func (w *WordPrimitive) BuildAssembly(u *Ulp) (string, error)

func (*WordPrimitive) Execute

func (w *WordPrimitive) Execute(vm *VirtualMachine) error

func (*WordPrimitive) IsRecursive

func (w *WordPrimitive) IsRecursive(check *WordForth) bool

Jump to

Keyboard shortcuts

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