ok

package module
v0.0.0-...-6c3d1e9 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2022 License: MIT Imports: 10 Imported by: 0

README

ok

a toy interpreted lisp

repl

run the repl

go run github.com/jncornett/ok/cmd/repl

example session:

ok> (let concat (func a b (list a b)))
((a, b) => 9f90b94a128e848a86650d6ce75619c8)@func
ok> (concat
ok*   2
ok*   3)
[2@number 3@number]@array
ok> 

grammar

like other lisps, the grammar is exceedingly simple:

BlockNode = (StatementNode ";"?)* .
StatementNode = ("(" SexprNode ")") | ConstNode | TagNode .
SexprNode = TagNode StatementNode* .
TagNode = (<ident> | <op>) .
ConstNode = <string> | <number> .

Grammar can also be evaluated dynamically -- see cmd/ebnf/main.go.

builtins

core functionality is provided through builtins.

let assignment

syntax: todo

func function literals

syntax: todo

switch branching

syntax: todo

acknowledgements

  • participle - a unique, reflection-based parser generator.
  • liner - a read-evaluate-print looper.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Lexer = lexer.MustSimple([]lexer.Rule{
	{Name: "WS", Pattern: `\s+`},
	{Name: "Open", Pattern: `\(`},
	{Name: "Close", Pattern: `\)`},
	{Name: "String", Pattern: `"([^"]*)"`},
	{Name: "Number", Pattern: `\d+`},
	{Name: "Ident", Pattern: `[a-zA-Z_][a-zA-Z0-9_]*`},
	{Name: "Op", Pattern: `[-+*/%<>=!&|^]+`},
	{Name: "Semi", Pattern: `;`},
})
View Source
var Parser = participle.MustBuild(&BlockNode{}, participle.Lexer(Lexer), participle.Elide("WS"))

Functions

func REPL

func REPL(env *Env) error

Types

type Array

type Array []Value

func (Array) Bool

func (a Array) Bool() bool

func (Array) Nil

func (Array) Nil() bool

func (Array) String

func (a Array) String() string

func (Array) TypeName

func (Array) TypeName() string

type Assign

type Assign struct {
	Key   string
	Value Node
}

func (Assign) Eval

func (a Assign) Eval(env *Env) (Value, error)

func (Assign) String

func (a Assign) String() string

type Block

type Block []Node

func (Block) Eval

func (b Block) Eval(env *Env) (Value, error)

func (Block) String

func (b Block) String() string

type BlockNode

type BlockNode struct {
	Statements []StatementNode `parser:"(@@ ';'?)*"`
}

func (BlockNode) Parse

func (n BlockNode) Parse() Node

type Bool

type Bool bool

func (Bool) Bool

func (b Bool) Bool() bool

func (Bool) Nil

func (Bool) Nil() bool

func (Bool) String

func (b Bool) String() string

func (Bool) TypeName

func (Bool) TypeName() string

type Branch

type Branch struct {
	Cond Node
	Body Node
}

type Builtin

type Builtin func(env *Env, args ...Value) (Value, error)

func (Builtin) Bool

func (Builtin) Bool() bool

func (Builtin) Call

func (fn Builtin) Call(env *Env, args ...Value) (Value, error)

func (Builtin) Nil

func (Builtin) Nil() bool

func (Builtin) String

func (b Builtin) String() string

func (Builtin) TypeName

func (Builtin) TypeName() string

type Call

type Call struct {
	Callee Node
	Args   []Node
}

func (Call) Eval

func (c Call) Eval(env *Env) (Value, error)

func (Call) String

func (c Call) String() string

type Callable

type Callable interface {
	Call(env *Env, args ...Value) (Value, error)
}

type Const

type Const struct{ Value }

func (Const) Eval

func (c Const) Eval(env *Env) (Value, error)

type ConstNode

type ConstNode struct {
	String string `parser:"  @String"`
	Number string `parser:"| @Number"`
}

func (ConstNode) Parse

func (n ConstNode) Parse() Node

type Env

type Env []Scope

func DefaultEnv

func DefaultEnv() *Env

func (Env) Del

func (env Env) Del(key string)

func (Env) Get

func (env Env) Get(key string) (Value, bool)

func (*Env) Pop

func (env *Env) Pop()

func (*Env) Push

func (env *Env) Push(scope Scope)

func (Env) Set

func (env Env) Set(key string, val Value)

type Expander

type Expander interface {
	Expand(env *Env, args ...Node) (Node, error)
}

type Func

type Func struct {
	Params []string
	Code   Node
}

func (Func) Bool

func (Func) Bool() bool

func (Func) Call

func (f Func) Call(env *Env, args ...Value) (Value, error)

func (Func) Nil

func (Func) Nil() bool

func (Func) String

func (f Func) String() string

func (Func) TypeName

func (Func) TypeName() string

type Macro

type Macro func(env *Env, args ...Node) (Node, error)

func (Macro) Bool

func (Macro) Bool() bool

func (Macro) Expand

func (fn Macro) Expand(env *Env, args ...Node) (Node, error)

func (Macro) Nil

func (Macro) Nil() bool

func (Macro) String

func (m Macro) String() string

func (Macro) TypeName

func (Macro) TypeName() string

type Nil

type Nil struct{}

func (Nil) Bool

func (Nil) Bool() bool

func (Nil) Nil

func (Nil) Nil() bool

func (Nil) String

func (Nil) String() string

func (Nil) TypeName

func (Nil) TypeName() string

type Node

type Node interface {
	Eval(env *Env) (Value, error)
	String() string
}

func Parse

func Parse(file string, src io.Reader) (Node, error)

type Number

type Number int64

func (Number) Bool

func (n Number) Bool() bool

func (Number) Nil

func (Number) Nil() bool

func (Number) String

func (n Number) String() string

func (Number) TypeName

func (Number) TypeName() string

type Ref

type Ref string

func (Ref) Eval

func (r Ref) Eval(env *Env) (Value, error)

func (Ref) String

func (r Ref) String() string

type Scope

type Scope map[string]Value

func (Scope) Del

func (scope Scope) Del(key string)

func (Scope) Get

func (scope Scope) Get(key string) (Value, bool)

func (Scope) Set

func (scope Scope) Set(key string, val Value)

type SexprNode

type SexprNode struct {
	Tag  TagNode         `parser:"@@"`
	Args []StatementNode `parser:"@@*"`
}

func (SexprNode) Parse

func (n SexprNode) Parse() Node

type StatementNode

type StatementNode struct {
	Sexpr *SexprNode `parser:"  '(' @@ ')'"`
	Const *ConstNode `parser:"| @@"`
	Tag   *TagNode   `parser:"| @@"`
}

func (StatementNode) Parse

func (n StatementNode) Parse() Node

type String

type String string

func (String) Bool

func (s String) Bool() bool

func (String) Nil

func (String) Nil() bool

func (String) String

func (s String) String() string

func (String) TypeName

func (String) TypeName() string

type Switch

type Switch []Branch

func (Switch) Eval

func (sw Switch) Eval(env *Env) (Value, error)

func (Switch) String

func (sw Switch) String() string

type TagNode

type TagNode struct {
	Value string `parser:"@(Ident | Op)"`
}

type Value

type Value interface {
	TypeName() string
	String() string
	Bool() bool
	Nil() bool
}

func Eval

func Eval(file string, r io.Reader, env *Env) (Value, error)

func EvalString

func EvalString(file, src string, env *Env) (Value, error)

Directories

Path Synopsis
cmd
run

Jump to

Keyboard shortcuts

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