scrl

package module
v0.0.0-...-506a7df Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2023 License: MIT Imports: 9 Imported by: 0

README

scrl

scrl is a scripting language implemented and embedded in Go.

It's designed to complement the host language by adding a convenient meta level on top that's completely under the users control.

setup

The REPL may be started from a shell like this:

$ go run main/scrl.go
scrl v1
  

types

type-of may be used to get the type of an expression.

  type-of 1
  
[Int]
  type-of Int

[Meta]
  type-of Meta

[Meta]
booleans

Booleans have one of two values, T or F.

  or T F

[T]

All values have boolean representations; many are unconditionally T, 0 and empty sequences are F.

  and "foo" 42

[42]

Logical operatos are short-circuiting.

  and T say "hello"

hello
[]
  or T say "hello"

[T]
integers
  + 1 2
  
[3]
symbols

New symbols may be created using '... or by interning strings.

  = sym "foo" 'foo

[T]
strings

New strings may be created using "...".

"foo"
  
["foo"]
pairs

New pairs may be created using :.

 1:2
  
[1:2]
deques

Deques are double ended queues of values. New deques may be created using [...].

  [3 2 1]
  
[[3 2 1]]
sets

Sets are ordered collections of values. New sets may be created using {...}.

 {3 2 1}
  
[{1 2 3}]

flow control

if may be used to branch on a condition.

  if F "foo"

[]
``

`else` may be used to evaluate code when the condition is `F`.

if F "foo" else "bar"

["bar"]


## debugging
`trace` may be used to toggle VM tracing.

```
  trace

[T]
  1 2 3

3 Push 1
5 Push 2
7 Push 3
9 Stop
[T 1 2 3]
```

## benchmarking
`bench` may be used to measure elapsed time for specified number of repetitions.

```
  bench 2 sleep milliseconds 500
  
[1.001917973s]
```

```
 fun fib (n) 
   if < n 2 n else + fib - n 1 fib - n 2

 bench 100 fib 20

[180.872334ms]
```

```
 fun fib(n a b)
   if > n 1 fib - n 1 b + a b else if = n 0 a else b

 bench 10000 fib 70 0 1

[78.846962ms]
```

Documentation

Index

Constants

View Source
const (
	Lt = Order(-1)
	Eq = Order(0)
	Gt = Order(1)
)
View Source
const (
	VERSION = 1
)

Variables

This section is empty.

Functions

func REPL

func REPL(vm *Vm, env Env, stack *Stack)

func ReadBody

func ReadBody(vm *Vm, in *bufio.Reader, out *Forms, pos *Pos, closingChar rune) error

func ReadDeque

func ReadDeque(vm *Vm, in *bufio.Reader, out *Forms, pos *Pos) error

func ReadForm

func ReadForm(vm *Vm, in *bufio.Reader, out *Forms, pos *Pos) error

func ReadForms

func ReadForms(vm *Vm, in *bufio.Reader, out *Forms, pos *Pos) error

func ReadId

func ReadId(vm *Vm, in *bufio.Reader, out *Forms, pos *Pos) error

func ReadInt

func ReadInt(vm *Vm, in *bufio.Reader, out *Forms, pos *Pos) error

func ReadList

func ReadList(vm *Vm, in *bufio.Reader, out *Forms, pos *Pos) error

func ReadPair

func ReadPair(vm *Vm, in *bufio.Reader, out *Forms, pos *Pos) error

func ReadQuote

func ReadQuote(vm *Vm, in *bufio.Reader, out *Forms, pos *Pos) error

func ReadSet

func ReadSet(vm *Vm, in *bufio.Reader, out *Forms, pos *Pos) error

func ReadStr

func ReadStr(vm *Vm, in *bufio.Reader, out *Forms, pos *Pos) error

func SkipWhitespace

func SkipWhitespace(vm *Vm, in *bufio.Reader, pos *Pos) error

func ValCompare

func ValCompare(l, r Val) int

Types

type AbcLibT

type AbcLibT struct {
	BasicLib
	BoolType   BoolType
	DequeType  DequeType
	IntType    IntType
	ExprType   ExprType
	MacroType  MacroType
	MetaType   BasicType
	PairType   PairType
	FunType    FunType
	FunArgType FunArgType
	SetType    SetType
	StrType    StrType
	SymType    SymType
	TimeType   TimeType
}
var AbcLib AbcLibT

func (*AbcLibT) Init

func (self *AbcLibT) Init(name string) *AbcLibT

type AndOp

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

func NewAndOp

func NewAndOp(pos Pos, falsePc Pc) *AndOp

func (AndOp) Dump

func (self AndOp) Dump(out io.Writer) error

type BasicEnv

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

func NewEnv

func NewEnv(parent Env) *BasicEnv

func (*BasicEnv) Bind

func (self *BasicEnv) Bind(id string, v Val)

func (BasicEnv) Each

func (self BasicEnv) Each(f EnvEachFunc)

func (BasicEnv) Find

func (self BasicEnv) Find(id string) *Val

func (*BasicEnv) Import

func (self *BasicEnv) Import(source Env, names ...string) error

func (*BasicEnv) Init

func (self *BasicEnv) Init(parent Env) *BasicEnv

type BasicForm

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

func (*BasicForm) Init

func (self *BasicForm) Init(pos Pos)

func (BasicForm) Pos

func (self BasicForm) Pos() Pos

type BasicLib

type BasicLib struct {
	BasicEnv
	// contains filtered or unexported fields
}

func (*BasicLib) BindFun

func (self *BasicLib) BindFun(name string, args FunArgs, retType Type, body FunBody)

func (*BasicLib) BindMacro

func (self *BasicLib) BindMacro(name string, body MacroBody)

func (*BasicLib) BindType

func (self *BasicLib) BindType(t Type, name string)

func (*BasicLib) Init

func (self *BasicLib) Init(name string) *BasicLib

func (BasicLib) Name

func (self BasicLib) Name() string

type BasicType

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

func (BasicType) Compare

func (_ BasicType) Compare(l, r Val) int

func (BasicType) Dump

func (_ BasicType) Dump(v Val, out io.Writer) error

func (BasicType) Emit

func (_ BasicType) Emit(v Val, args *Forms, vm *Vm, env Env, pos Pos) error

func (BasicType) Eq

func (_ BasicType) Eq(l, r Val) bool

func (*BasicType) Init

func (self *BasicType) Init(name string)

func (BasicType) IsTrue

func (_ BasicType) IsTrue(_ Val) bool

func (*BasicType) Name

func (self *BasicType) Name() string

func (*BasicType) String

func (self *BasicType) String() string

func (BasicType) Write

func (_ BasicType) Write(v Val, out io.Writer) error

type BenchOpT

type BenchOpT struct{}
var BenchOp BenchOpT

func (BenchOpT) Dump

func (_ BenchOpT) Dump(out io.Writer) error

type BoolType

type BoolType struct {
	BasicType
}

func (BoolType) Dump

func (_ BoolType) Dump(v Val, out io.Writer) error

func (BoolType) IsTrue

func (_ BoolType) IsTrue(v Val) bool

type Call

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

func NewCall

func NewCall(pos Pos, target *Fun, args []Val, retPc Pc) Call

type CallOp

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

func NewCallOp

func NewCallOp(pos Pos, target *Fun) *CallOp

func (CallOp) Dump

func (self CallOp) Dump(out io.Writer) error

type Compare

type Compare[T any] func(l, r T) int

type Deque

type Deque[T any] struct {
	// contains filtered or unexported fields
}

func NewDeque

func NewDeque[T any](items []T) *Deque[T]

func (*Deque[T]) Clear

func (self *Deque[T]) Clear()

func (*Deque[T]) Cut

func (self *Deque[T]) Cut(n int) []T

func (Deque[T]) Dump

func (self Deque[T]) Dump(out io.Writer) error

func (Deque[T]) DumpItems

func (self Deque[T]) DumpItems(out io.Writer) error

func (Deque[T]) Each

func (self Deque[T]) Each(f func(T) bool) bool

func (*Deque[T]) Init

func (self *Deque[T]) Init(items []T) *Deque[T]

func (Deque[T]) Items

func (self Deque[T]) Items() []T

func (Deque[T]) Len

func (self Deque[T]) Len() int

func (Deque[T]) PeekBack

func (self Deque[T]) PeekBack() *T

func (Deque[T]) PeekFront

func (self Deque[T]) PeekFront() T

func (*Deque[T]) PopBack

func (self *Deque[T]) PopBack() T

func (*Deque[T]) PopFront

func (self *Deque[T]) PopFront() T

func (*Deque[T]) PushBack

func (self *Deque[T]) PushBack(it T)

func (*Deque[T]) PushFront

func (self *Deque[T]) PushFront(it T)

func (Deque[T]) String

func (self Deque[T]) String() string

type DequeForm

type DequeForm struct {
	ItemsForm
}

func NewDequeForm

func NewDequeForm(pos Pos, items ...Form) *DequeForm

func (DequeForm) Dump

func (self DequeForm) Dump(out io.Writer) error

func (*DequeForm) Emit

func (self *DequeForm) Emit(args *Forms, vm *Vm, env Env) error

func (DequeForm) Eq

func (self DequeForm) Eq(other Form) bool

func (*DequeForm) Init

func (self *DequeForm) Init(pos Pos, items ...Form) *DequeForm

func (*DequeForm) Quote

func (self *DequeForm) Quote(vm *Vm) Val

type DequeOp

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

func NewDequeOp

func NewDequeOp(pos Pos, itemCount int) *DequeOp

func (DequeOp) Dump

func (self DequeOp) Dump(out io.Writer) error

type DequeType

type DequeType struct {
	BasicType
}

func (DequeType) IsTrue

func (_ DequeType) IsTrue(v Val) bool

type Env

type Env interface {
	Bind(id string, v Val)
	Find(id string) *Val
	Each(f EnvEachFunc)
	Import(source Env, names ...string) error
}

type EnvEachFunc

type EnvEachFunc func(id string, v Val)

type Error

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

func NewError

func NewError(pos Pos, spec string, args ...interface{}) Error

func (Error) Error

func (self Error) Error() string

func (Error) Message

func (self Error) Message() string

type ExprType

type ExprType struct {
	BasicType
}

func (ExprType) Dump

func (_ ExprType) Dump(v Val, out io.Writer) error

func (ExprType) Eq

func (_ ExprType) Eq(l, r Val) bool

type Form

type Form interface {
	Pos() Pos
	Emit(args *Forms, vm *Vm, env Env) error
	Quote(vm *Vm) Val
	Eq(other Form) bool
	Dump(out io.Writer) error
}

type Forms

type Forms struct {
	Deque[Form]
}

func (*Forms) Emit

func (self *Forms) Emit(vm *Vm, env Env) error

type Fun

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

func NewFun

func NewFun(name string, args FunArgs, ret Type, body FunBody) *Fun

func (Fun) Arity

func (self Fun) Arity() int

func (*Fun) Call

func (self *Fun) Call(vm *Vm, stack *Stack, pos Pos, pc Pc) (Pc, error)

func (*Fun) Init

func (self *Fun) Init(name string, args FunArgs, ret Type, body FunBody) *Fun

func (Fun) String

func (self Fun) String() string

type FunArg

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

type FunArgOp

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

func NewFunArgOp

func NewFunArgOp(pos Pos, index int) *FunArgOp

func (FunArgOp) Dump

func (self FunArgOp) Dump(out io.Writer) error

type FunArgType

type FunArgType struct {
	BasicType
}

func (FunArgType) Emit

func (_ FunArgType) Emit(v Val, args *Forms, vm *Vm, env Env, pos Pos) error

type FunArgs

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

func (*FunArgs) Add

func (self *FunArgs) Add(name string, t Type) *FunArgs

type FunBody

type FunBody = func(self *Fun, vm *Vm, stack *Stack, pos Pos, pc Pc) (Pc, error)

type FunType

type FunType struct {
	BasicType
}

func (FunType) Emit

func (_ FunType) Emit(v Val, args *Forms, vm *Vm, env Env, pos Pos) error

type GotoOp

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

func NewGotoOp

func NewGotoOp(pos Pos, pc Pc) *GotoOp

func (GotoOp) Dump

func (self GotoOp) Dump(out io.Writer) error

type IdForm

type IdForm struct {
	BasicForm
	// contains filtered or unexported fields
}

func NewIdForm

func NewIdForm(pos Pos, name string) *IdForm

func (IdForm) Dump

func (self IdForm) Dump(out io.Writer) error

func (*IdForm) Emit

func (self *IdForm) Emit(args *Forms, vm *Vm, env Env) error

func (IdForm) Eq

func (self IdForm) Eq(other Form) bool

func (*IdForm) Init

func (self *IdForm) Init(pos Pos, name string) *IdForm

func (IdForm) Quote

func (self IdForm) Quote(vm *Vm) Val

func (IdForm) String

func (self IdForm) String() string

type IfOp

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

func NewIfOp

func NewIfOp(pos Pos, elsePc Pc) *IfOp

func (IfOp) Dump

func (self IfOp) Dump(out io.Writer) error

type IntType

type IntType struct {
	BasicType
}

func (IntType) Compare

func (_ IntType) Compare(l, r Val) int

func (IntType) IsTrue

func (_ IntType) IsTrue(v Val) bool

type ItemsForm

type ItemsForm struct {
	BasicForm
	// contains filtered or unexported fields
}

func (ItemsForm) Dump

func (self ItemsForm) Dump(out io.Writer) error

func (ItemsForm) Emit

func (self ItemsForm) Emit(args *Forms, vm *Vm, env Env) error

func (ItemsForm) EqItems

func (self ItemsForm) EqItems(other []Form) bool

func (*ItemsForm) Init

func (self *ItemsForm) Init(pos Pos, items []Form) *ItemsForm

type Lib

type Lib interface {
	Env
	Name() string
}

type ListForm

type ListForm struct {
	ItemsForm
}

func NewListForm

func NewListForm(pos Pos, items ...Form) *ListForm

func (ListForm) Dump

func (self ListForm) Dump(out io.Writer) error

func (ListForm) Eq

func (self ListForm) Eq(other Form) bool

func (*ListForm) Init

func (self *ListForm) Init(pos Pos, items ...Form) *ListForm

func (*ListForm) Quote

func (self *ListForm) Quote(vm *Vm) Val

type LitForm

type LitForm struct {
	BasicForm
	// contains filtered or unexported fields
}

func NewLitForm

func NewLitForm(pos Pos, v Val) *LitForm

func (LitForm) Dump

func (self LitForm) Dump(out io.Writer) error

func (LitForm) Emit

func (self LitForm) Emit(args *Forms, vm *Vm, env Env) error

func (LitForm) Eq

func (self LitForm) Eq(other Form) bool

func (*LitForm) Init

func (self *LitForm) Init(pos Pos, v Val) *LitForm

func (*LitForm) Quote

func (self *LitForm) Quote(vm *Vm) Val

type Macro

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

func NewMacro

func NewMacro(name string, body MacroBody) *Macro

func (*Macro) Emit

func (self *Macro) Emit(args *Forms, vm *Vm, env Env, pos Pos) error

func (*Macro) Init

func (self *Macro) Init(name string, body MacroBody) *Macro

func (*Macro) String

func (self *Macro) String() string

type MacroBody

type MacroBody = func(self *Macro, args *Forms, vm *Vm, env Env, pos Pos) error

type MacroType

type MacroType struct {
	BasicType
}

func (MacroType) Emit

func (_ MacroType) Emit(v Val, args *Forms, vm *Vm, env Env, pos Pos) error

type Op

type Op interface {
	Dump(out io.Writer) error
}

type OrOp

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

func NewOrOp

func NewOrOp(pos Pos, truePc Pc) *OrOp

func (OrOp) Dump

func (self OrOp) Dump(out io.Writer) error

type Order

type Order = int

type Pair

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

func NewPair

func NewPair(left, right Val) Pair

type PairForm

type PairForm struct {
	BasicForm
	// contains filtered or unexported fields
}

func NewPairForm

func NewPairForm(pos Pos, left, right Form) *PairForm

func (PairForm) Dump

func (self PairForm) Dump(out io.Writer) error

func (PairForm) Emit

func (self PairForm) Emit(args *Forms, vm *Vm, env Env) error

func (PairForm) Eq

func (self PairForm) Eq(other Form) bool

func (*PairForm) Init

func (self *PairForm) Init(pos Pos, left, right Form) *PairForm

func (*PairForm) Quote

func (self *PairForm) Quote(vm *Vm) Val

type PairOpT

type PairOpT struct{}
var PairOp PairOpT

func (PairOpT) Dump

func (_ PairOpT) Dump(out io.Writer) error

type PairType

type PairType struct {
	BasicType
}

func (PairType) Compare

func (_ PairType) Compare(l, r Val) int

func (PairType) Dump

func (_ PairType) Dump(v Val, out io.Writer) error

func (PairType) IsTrue

func (_ PairType) IsTrue(v Val) bool

type Pc

type Pc = int

type Pos

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

func NewPos

func NewPos(source string, line, column int) Pos

func (*Pos) Init

func (self *Pos) Init(source string, line, column int)

func (*Pos) Source

func (self *Pos) Source() string

func (Pos) String

func (self Pos) String() string

type PushOp

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

func NewPushOp

func NewPushOp(pos Pos, val Val) *PushOp

func (PushOp) Dump

func (self PushOp) Dump(out io.Writer) error

type RetOpT

type RetOpT struct{}
var RetOp RetOpT

func (RetOpT) Dump

func (_ RetOpT) Dump(out io.Writer) error

type Set

type Set[T any] struct {
	Deque[T]
	// contains filtered or unexported fields
}

func NewSet

func NewSet[T any](compare Compare[T], items []T) *Set[T]

func (*Set[T]) Add

func (self *Set[T]) Add(v T) bool

func (Set[T]) Dump

func (self Set[T]) Dump(out io.Writer) error

func (Set[T]) Find

func (self Set[T]) Find(v T) *T

func (Set[T]) Index

func (self Set[T]) Index(v T) (int, *T)

func (*Set[T]) Init

func (self *Set[T]) Init(compare Compare[T], items []T) *Set[T]

func (*Set[T]) Remove

func (self *Set[T]) Remove(v T) *T

func (Set[T]) String

func (self Set[T]) String() string

type SetForm

type SetForm struct {
	ItemsForm
}

func NewSetForm

func NewSetForm(pos Pos, items ...Form) *SetForm

func (SetForm) Dump

func (self SetForm) Dump(out io.Writer) error

func (*SetForm) Emit

func (self *SetForm) Emit(args *Forms, vm *Vm, env Env) error

func (SetForm) Eq

func (self SetForm) Eq(other Form) bool

func (*SetForm) Init

func (self *SetForm) Init(pos Pos, items ...Form) *SetForm

func (*SetForm) Quote

func (self *SetForm) Quote(vm *Vm) Val

type SetOp

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

func NewSetOp

func NewSetOp(pos Pos, itemCount int) *SetOp

func (SetOp) Dump

func (self SetOp) Dump(out io.Writer) error

type SetType

type SetType struct {
	BasicType
}

func (SetType) IsTrue

func (_ SetType) IsTrue(v Val) bool

type Stack

type Stack = Deque[Val]

func NewStack

func NewStack(items []Val) *Stack

type StopOpT

type StopOpT struct{}
var StopOp StopOpT

func (StopOpT) Dump

func (_ StopOpT) Dump(out io.Writer) error

type StrType

type StrType struct {
	BasicType
}

func (StrType) Compare

func (_ StrType) Compare(l, r Val) int

func (StrType) Dump

func (_ StrType) Dump(v Val, out io.Writer) error

func (StrType) IsTrue

func (_ StrType) IsTrue(v Val) bool

type Sym

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

func NewSym

func NewSym(name string) *Sym

type SymType

type SymType struct {
	BasicType
}

func (SymType) Compare

func (_ SymType) Compare(l, r Val) int

func (SymType) Dump

func (_ SymType) Dump(v Val, out io.Writer) error

type Syms

type Syms = map[string]*Sym

type TimeType

type TimeType struct {
	BasicType
}

func (TimeType) Compare

func (_ TimeType) Compare(l, r Val) int

func (TimeType) IsTrue

func (_ TimeType) IsTrue(v Val) bool

type TraceOpT

type TraceOpT struct{}
var TraceOp TraceOpT

func (TraceOpT) Dump

func (_ TraceOpT) Dump(out io.Writer) error

type Type

type Type interface {
	Init(name string)
	Name() string
	String() string

	Compare(l, r Val) int
	Emit(v Val, args *Forms, vm *Vm, env Env, pos Pos) error
	Eq(l, r Val) bool
	IsTrue(v Val) bool
	Dump(v Val, out io.Writer) error
	Write(v Val, out io.Writer) error
}

type Val

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

func NewVal

func NewVal(t Type, d any) Val

func (Val) Compare

func (self Val) Compare(other Val) int

func (Val) Dump

func (self Val) Dump(out io.Writer) error

func (Val) Emit

func (self Val) Emit(args *Forms, vm *Vm, env Env, pos Pos) error

func (Val) Eq

func (self Val) Eq(other Val) bool

func (*Val) Init

func (self *Val) Init(t Type, d any)

func (Val) IsTrue

func (self Val) IsTrue() bool

func (Val) String

func (self Val) String() string

func (Val) Write

func (self Val) Write(out io.Writer) error

type ValDeque

type ValDeque = Deque[Val]

func NewValDeque

func NewValDeque(items []Val) *ValDeque

type ValSet

type ValSet = Set[Val]

func NewValSet

func NewValSet(items []Val) *ValSet

type Vm

type Vm struct {
	Trace bool
	// contains filtered or unexported fields
}

func (*Vm) Emit

func (self *Vm) Emit(op Op, trace bool) Pc

func (*Vm) EmitPc

func (self *Vm) EmitPc() Pc

func (*Vm) Eval

func (self *Vm) Eval(stack *Stack, pc Pc) (Pc, error)

func (*Vm) Init

func (self *Vm) Init() *Vm

func (*Vm) Sym

func (self *Vm) Sym(name string) *Sym

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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