gisp

package module
v0.0.0-...-f667aca Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2024 License: MIT Imports: 10 Imported by: 2

README

gisp

My attempt to write a minimal Lisp interpreter (in Go)

Supported types

  • boolean (true, nil)
  • integer 64 bits
  • float 64 bits
  • string
  • symbol

Supported primitives

  • +, -, *, /, % : arithmetic operators

  • =, <, <=, >, >= : conditionals

  • quote

  • setq

  • let

  • not, or, and

  • if

  • while

  • begin

  • lambda

  • eval

  • list, first, last, nth, rest, find, append

  • print, println, format, readlines, sleep, rand

Examples

  • cmd/gisp : a REPL for gisp (can run single expressions, programs from file or expressions interactively)
  • cmd/turtle : a REPL for gisp with turtle abilities (it shows how to add new types and primitives to gisp)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEOF         = Error{/* contains filtered or unexported fields */}
	ErrInvalid     = Error{/* contains filtered or unexported fields */}
	ErrInvalidType = Error{/* contains filtered or unexported fields */}
	ErrMissing     = Error{/* contains filtered or unexported fields */}
	Verbose        = false

	True = Boolean{/* contains filtered or unexported fields */}
	Nil  = Boolean{/* contains filtered or unexported fields */}
)

Functions

func AddPrimitive

func AddPrimitive(name string, value Call)

AddPrimitive adds a new built-in/primitive method. Note that it can override existing primitives

func AsBool

func AsBool(o any, def bool) bool

AsBool converts the input object to a boolean, if possible or return the default value.

func AsFloat

func AsFloat(o any, def float64) float64

AsFloat converts the input object to a float, if possible or return the default value.

func AsInt

func AsInt(o any, def int64) int64

AsInt converts the input object to an integer, if possible or return the default value.

func AsString

func AsString(o any, def string) string

AsString returns the input representation for the input object, or the default.

func CallLambda

func CallLambda(l Lambda, env *Env, args []any) (ret any)

CallLambda call a lambda function, passing the local enviroment and some input parameters

func Eval

func Eval(env *Env, v any) any

Eval evaluates the current object

func Primitives

func Primitives() (l []string)

Primitives returns a list of primivives (names) Note that this doesn't return the math and conditional operators.

Types

type Boolean

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

Boolean is the boolean primitive object

func MakeBool

func MakeBool(v bool) Boolean

MakeBool creates a Boolean object from a bool

func (Boolean) Bool

func (o Boolean) Bool() bool

func (Boolean) Eq

func (o Boolean) Eq(v any) bool

func (Boolean) Geq

func (o Boolean) Geq(v any) bool

func (Boolean) Gt

func (o Boolean) Gt(v any) bool

func (Boolean) Leq

func (o Boolean) Leq(v any) bool

func (Boolean) Lt

func (o Boolean) Lt(v any) bool

func (Boolean) String

func (o Boolean) String() string

func (Boolean) Value

func (o Boolean) Value() any

type Call

type Call func(env *Env, args []any) any

Call is the signature for primitive/builtin methods

type CanBool

type CanBool interface {
	Bool() bool
}

CanBool is for objects that can cast to a boolean (true/false)

type CanCompare

type CanCompare interface {
	Eq(v any) bool
	Lt(v any) bool
	Leq(v any) bool
	Gt(v any) bool
	Geq(v any) bool
}

CanCompare is for objects that can compare with other objects (=, <, <=, >, >=)

type CanFloat

type CanFloat interface {
	Float() float64
}

CanFloat is for objects that can cast to a float (float64) value

type CanInt

type CanInt interface {
	Int() int64
}

CanInt is for objects that can cast to an integer (int64) value

type Cond

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

Cond is for conditional operators ( =, <, <=, >, >= )

func (Cond) String

func (o Cond) String() string

func (Cond) Value

func (o Cond) Value() any

type Env

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

Env stores the current environments (collection of variables)

func NewEnv

func NewEnv(prev *Env) *Env

NewEnv creates a new enviroment. The root environment should have prev=nil, local environment will link to the previous (parent) one.

func (*Env) Get

func (e *Env) Get(o any) any

Get tries to resolve to an existing variable or evaluate the input.

func (*Env) GetList

func (e *Env) GetList(l []any) (el []any)

Get resolves all input as variables or evaluates them.

func (*Env) GetValues

func (e *Env) GetValues(l []any) (el []any)

GetValues returns the primitive value for the input variables or evaluated objects.

func (*Env) Put

func (e *Env) Put(o, value any) any

Put update a variable with the same name, starting from the local environment. If the variable doesn't already exist, it will be created in the global environment.

func (*Env) PutLocal

func (e *Env) PutLocal(o, value any) any

PutLocal creates or update a variable in the local environment

type Error

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

Error is a primitive object that maps errors

func MakeError

func MakeError(e error) Error

MakeList creates an Error object from a go error

func (Error) Error

func (o Error) Error() string

func (Error) String

func (o Error) String() string

func (Error) Value

func (o Error) Value() any

type Float

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

Float is the floating point primitive type (float64)

func MakeFloat

func MakeFloat[T float32 | float64](v T) Float

MakeFloat creates a Float object from a float64

func (Float) Bool

func (o Float) Bool() bool

func (Float) Eq

func (o Float) Eq(v any) bool

func (Float) Float

func (o Float) Float() float64

func (Float) Geq

func (o Float) Geq(v any) bool

func (Float) Gt

func (o Float) Gt(v any) bool

func (Float) Int

func (o Float) Int() int64

func (Float) Leq

func (o Float) Leq(v any) bool

func (Float) Lt

func (o Float) Lt(v any) bool

func (Float) String

func (o Float) String() string

func (Float) Value

func (o Float) Value() any

type Integer

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

Integer is the integer primitive type (int64)

func MakeInt

func MakeInt[T int8 | int | int16 | int64](v T) Integer

MakeInt creates an Integer object from an int

func (Integer) Bool

func (o Integer) Bool() bool

func (Integer) Eq

func (o Integer) Eq(v any) bool

func (Integer) Float

func (o Integer) Float() float64

func (Integer) Geq

func (o Integer) Geq(v any) bool

func (Integer) Gt

func (o Integer) Gt(v any) bool

func (Integer) Int

func (o Integer) Int() int64

func (Integer) Leq

func (o Integer) Leq(v any) bool

func (Integer) Lt

func (o Integer) Lt(v any) bool

func (Integer) String

func (o Integer) String() string

func (Integer) Value

func (o Integer) Value() any

type Lambda

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

Lambda is the anonymous function type

func (Lambda) Arg

func (o Lambda) Arg(i int) any

func (Lambda) String

func (o Lambda) String() string

func (Lambda) Value

func (o Lambda) Value() any

type List

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

List is the list type

func MakeList

func MakeList(items ...any) List

MakeList creates a List object from a list of objects

func (List) Bool

func (o List) Bool() bool

func (List) Item

func (o List) Item(i int) any

func (List) Items

func (o List) Items() []any

func (List) String

func (o List) String() string

func (List) Value

func (o List) Value() any

type Object

type Object interface {
	String() string
	Value() any
}

Object is the interface for all gisp objects.

type Op

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

Op is for math operators ( +, -, *, / )

func (Op) String

func (o Op) String() string

func (Op) Value

func (o Op) Value() any

type Parser

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

Parser can parse a gisp object or program

func NewParser

func NewParser(r io.Reader) *Parser

NewParser creates a new Parser object that can parse the input Reader

func (*Parser) Parse

func (p *Parser) Parse() (l []any, err error)

Parse parses the input from the Reader until EOF and returns a list of objects

func (*Parser) ParseOne

func (p *Parser) ParseOne() (l []any, err error)

ParseOne parses one object from the input

func (*Parser) SepNext

func (p *Parser) SepNext() bool

SepNext checks if the next character to parse is a separator between gisp objects

type Quoted

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

Quoted is for quoted symbols

func (Quoted) String

func (o Quoted) String() string

func (Quoted) Value

func (o Quoted) Value() any

type String

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

String is the string primitive type

func MakeString

func MakeString(v string) String

MakeString creates a String object from a string

func (String) Bool

func (o String) Bool() bool

func (String) Eq

func (o String) Eq(v any) bool

func (String) Geq

func (o String) Geq(v any) bool

func (String) Gt

func (o String) Gt(v any) bool

func (String) Leq

func (o String) Leq(v any) bool

func (String) Lt

func (o String) Lt(v any) bool

func (String) String

func (o String) String() string

func (String) Value

func (o String) Value() any

type Symbol

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

Symbol is the symbol atom

func (Symbol) String

func (o Symbol) String() string

func (Symbol) Value

func (o Symbol) Value() any

Directories

Path Synopsis
cmd
gisp Module
turtle Module

Jump to

Keyboard shortcuts

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