inter

package
v0.0.0-...-9a7bae3 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

package inter constains lexer, parser and interpreter.

Index

Constants

View Source
const (
	VERSION   = "0.3.0"
	COPYRIGHT = "(c) Xavier Gandillot (aka xavier268) 2023"
)
View Source
const BOOL = 57349
View Source
const ERROR = 57350
View Source
const IDENT = 57347
View Source
const NUMBER = 57346
View Source
const SKIP = 57351
View Source
const STRING = 57348

Variables

View Source
var DEBUG int = 0

0 - off, 1 - on, 2 - on with trace

View Source
var ErrCannotPopENv = fmt.Errorf("cannot pop environement,  stack is empty")
View Source
var ErrDisplayTooManyArguments = fmt.Errorf("display: too many arguments")
View Source
var ErrEval = fmt.Errorf("cannot evaluate provided input")
View Source
var ErrInvalidArgumentListFormat = fmt.Errorf("invalid argument list format")
View Source
var ErrInvalidLambaFormals = fmt.Errorf("lambda: formals must be a  a list of symbols, possibly empty")
View Source
var ErrLoadArgument = fmt.Errorf("load expects a single, String, argument")
View Source
var ErrNaN = fmt.Errorf("term is not a number")
View Source
var ErrNotAList = fmt.Errorf("not a list")
View Source
var ErrParenthesisUnbalanced = fmt.Errorf("too many closing parenthesis")
View Source
var ErrVariableNotBound = fmt.Errorf("variable is not bound")
View Source
var ErrVariableUnassigned = fmt.Errorf("variable is bound but unassigned")
View Source
var NumberNaN = Number{1, 0}
View Source
var NumberOne = Number{1, 1}
View Source
var NumberZero = Number{0, 1}
View Source
var PAT_OPERATOR = regexp.MustCompile(`^[()'.]$`) // tell if an ident is an operator, once identified as IDENT

Functions

func Compare

func Compare(n, m Number) int

Compare n and m. -1 if n<m, 0 if n==m, 1 if n>m.

func IsKeyword

func IsKeyword(s string) bool

Is this string the name of a registered keyword ?

func IsLitteral

func IsLitteral(t Term) bool

func NewLexer

func NewLexer(input io.Reader, fileName string) *myLex

func Register

func Register(keyword string, evalArgFirst bool, primitive Primitive, help []string)

Register a keyword for a built-in primitive, typically during initialization.

func ToString

func ToString(t any) string

a convenience string function, that handles nil Term.

func Welcome

func Welcome() string

Prinţ welcome message.

Types

type Bool

type Bool struct {
	Value bool
}

func (Bool) IsBool

func (Bool) IsBool() bool

IsBool implements Term.

func (Bool) IsError

func (Bool) IsError() bool

IsError implements Term.

func (Bool) IsNumber

func (Bool) IsNumber() bool

IOsNumber implements Term.

func (Bool) IsPair

func (Bool) IsPair() bool

IsPair implements Term.

func (Bool) IsProcedure

func (Bool) IsProcedure() bool

IsProcedure implements Term.

func (Bool) IsString

func (Bool) IsString() bool

IsString implements Term.

func (Bool) IsSymbol

func (Bool) IsSymbol() bool

IsSymbol implements Term.

func (Bool) String

func (b Bool) String() string

type Environnement

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

func (*Environnement) Dump

func (s *Environnement) Dump()

Dump the current environnement and its ancestors

func (*Environnement) Fork

func (ev *Environnement) Fork() *Environnement

func (*Environnement) Get

func (ev *Environnement) Get(sym Symbol) (res Term, ok bool)

get symbol binding in environements, recursively. Ok if found.

func (*Environnement) Set

func (ev *Environnement) Set(sym Symbol, t Term)

Set symbol in current environement, creating it if needed

func (*Environnement) SetBang

func (ev *Environnement) SetBang(sym Symbol, t Term) error

Try to find the specified variable in one of the environements, and set this specific variable in this specific environement to the value. Variable is NEVER created if not found, but an error will be returned. A existing variable will NEVER be shadowed, but will be changed.

func (Environnement) String

func (s Environnement) String() string

type Error

type Error struct {
	Err     error
	Context Term
}

Error is a type to indicate an error has occurred.

func (Error) IsBool

func (Error) IsBool() bool

IsBool implements Term.

func (Error) IsError

func (Error) IsError() bool

IsError implements Term.

func (Error) IsNumber

func (Error) IsNumber() bool

IsNumber implements Term.

func (Error) IsPair

func (Error) IsPair() bool

IsPair implements Term.

func (Error) IsProcedure

func (Error) IsProcedure() bool

IsProcedure implements Term.

func (Error) IsString

func (Error) IsString() bool

IsString implements Term.

func (Error) IsSymbol

func (Error) IsSymbol() bool

IsSymbol implements Term.

func (Error) String

func (e Error) String() string

type Inter

type Inter struct {
	OnQuit func() // a function hook, called when the interpreter is quitting.
	// contains filtered or unexported fields
}

func NewInter

func NewInter() *Inter

func (*Inter) Eval

func (it *Inter) Eval(t Term) Term

Eval a Term, recursively, returning the result as a Term. An expression to be evaluated may be a literal, a variable reference, a special form, or a procedure call. Only the primitives or procedures that require it should fork the environement.

func (*Inter) PopEnv

func (it *Inter) PopEnv() error

func (*Inter) PushEnv

func (it *Inter) PushEnv()

func (*Inter) Repl

func (it *Inter) Repl()

func (Inter) String

func (it Inter) String() string

type Number

type Number struct {
	Num int
	Den int
}

numbers are rational numbers Num & Den are always prime among them, Num is always > 0, except to represent NaN with 1/0.

func NumberFromString

func NumberFromString(s string) (n Number, err error)

func (Number) Div

func (n Number) Div(m Number) Number

func (Number) IsBool

func (Number) IsBool() bool

IsBool implements Term.

func (Number) IsError

func (Number) IsError() bool

IsError implements Term.

func (Number) IsNumber

func (Number) IsNumber() bool

IsNumber implements Term.

func (Number) IsPair

func (Number) IsPair() bool

IsPair implements Term.

func (Number) IsProcedure

func (Number) IsProcedure() bool

IsProcedure implements Term.

func (Number) IsString

func (Number) IsString() bool

IsString implements Term.

func (Number) IsSymbol

func (Number) IsSymbol() bool

IsSymbol implements Term.

func (Number) Minus

func (n Number) Minus(m Number) Number

func (Number) Mul

func (n Number) Mul(m Number) Number

func (Number) Normalize

func (n Number) Normalize() Number

Normalize the internal representation of a number. 0/x is normalized as 0/1, NaN is normalized 1/0 Den is always > 0

func (Number) Plus

func (n Number) Plus(m Number) Number

func (Number) String

func (n Number) String() string

type Pair

type Pair struct {
	Car, Cdr Term
}

func (Pair) IsBool

func (Pair) IsBool() bool

IsBool implements Term.

func (Pair) IsError

func (Pair) IsError() bool

IsError implements Term.

func (Pair) IsNumber

func (Pair) IsNumber() bool

IsNumber implements Term.

func (Pair) IsPair

func (Pair) IsPair() bool

IsPair implements Term.

func (Pair) IsProcedure

func (Pair) IsProcedure() bool

IsProcedure implements Term.

func (Pair) IsString

func (Pair) IsString() bool

IsString implements Term.

func (Pair) IsSymbol

func (Pair) IsSymbol() bool

IsSymbol implements Term.

func (Pair) String

func (t Pair) String() string

type Primitive

type Primitive func(it *Inter, args Term) Term

All primitive, built in functions or macros must have this signature. They will be called with args containing the cdr of the form ( keyword .... ). detailed format depends on the function.

type Procedure

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

A procedure is a Term that cannot be entered directly by the user, but is created by a lambda application. All procedures evaluate their arguments. Doc says : Procedures are created by evaluating lambda expressions (see Lambda Expressions); the lambda may either be explicit or may be implicit as in a “procedure define” (see Definitions).

func (Procedure) IsBool

func (Procedure) IsBool() bool

IsBool implements Term.

func (Procedure) IsError

func (Procedure) IsError() bool

IsError implements Term.

func (Procedure) IsNumber

func (Procedure) IsNumber() bool

IsNumber implements Term.

func (Procedure) IsPair

func (Procedure) IsPair() bool

IsPair implements Term.

func (Procedure) IsProcedure

func (Procedure) IsProcedure() bool

IsProcedure implements Term.

func (Procedure) IsString

func (Procedure) IsString() bool

IsString implements Term.

func (Procedure) IsSymbol

func (Procedure) IsSymbol() bool

IsSymbol implements Term.

func (Procedure) String

func (p Procedure) String() string

String implements Term.

type String

type String struct {
	Value string
}

func (String) IsBool

func (String) IsBool() bool

IsBool implements Term.

func (String) IsError

func (String) IsError() bool

IsError implements Term.

func (String) IsNumber

func (String) IsNumber() bool

IsNumber implements Term.

func (String) IsPair

func (String) IsPair() bool

IsPair implements Term.

func (String) IsProcedure

func (String) IsProcedure() bool

IsProcedure implements Term.

func (String) IsString

func (String) IsString() bool

IsString implements Term.

func (String) IsSymbol

func (String) IsSymbol() bool

IsSymbol implements Term.

func (String) String

func (t String) String() string

default is to printed quoted string

type Symbol

type Symbol struct {
	Value string
}

func (Symbol) IsBool

func (Symbol) IsBool() bool

IsBool implements Term.

func (Symbol) IsError

func (Symbol) IsError() bool

IsError implements Term.

func (Symbol) IsNumber

func (Symbol) IsNumber() bool

IsNumber implements Term.

func (Symbol) IsPair

func (Symbol) IsPair() bool

IsPair implements Term.

func (Symbol) IsProcedure

func (Symbol) IsProcedure() bool

IsProcedure implements Term.

func (Symbol) IsString

func (Symbol) IsString() bool

IsString implements Term.

func (Symbol) IsSymbol

func (Symbol) IsSymbol() bool

IsSymbol implements Term.

func (Symbol) String

func (t Symbol) String() string

type Term

type Term interface {
	String() string
	IsPair() bool
	IsSymbol() bool
	IsString() bool
	IsNumber() bool
	IsBool() bool
	IsProcedure() bool
	IsError() bool
}

func MustParse1String

func MustParse1String(input string, sourcename string) Term

func MustParseNString

func MustParseNString(input string, sourcename string) []Term

func Parse1

func Parse1(rdr io.Reader, sourcename string) (Term, error)

Return the LAST parsed Term.

func Parse1File

func Parse1File(filename string) (Term, error)

func Parse1String

func Parse1String(input string, sourcename string) (Term, error)

func ParseN

func ParseN(rdr io.Reader, sourcename string) ([]Term, error)

Return a (golang) list of parsed Terms.

func ParseNFile

func ParseNFile(filename string) ([]Term, error)

func ParseNString

func ParseNString(input string, sourcename string) ([]Term, error)

Jump to

Keyboard shortcuts

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