goscheme

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

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

Go to latest
Published: Mar 2, 2019 License: GPL-3.0 Imports: 14 Imported by: 0

README

GoScheme


Just another shceme interpreter written in Go.

godoc

Installation

go get github.com/xrlin/goscheme/cmd/goscheme

Or you can download the corresponding pre-compiled executable file in release page.

Usage

# Just run goscheme to enter interactive shell
goscheme

# Run a scheme file
goscheme test.scm

Examples

  • Calculate nth fibonacci number

    ; calculate nth fibonacci number
    (define (fib n)
       (if (<= n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))
    
    (fib 10)
    
    ;#=> 55
    
    ; calculate nth fibnacci number in tail recursion
    (define (fib2 n)
       (begin (define (fib-iter a b n)
             (if (= n 0) b (fib-iter b (+ a b) (- n 1))))
       (fib-iter 0 1 (- n 1))))
    (fib2 30)
    ;#=>832040
    
  • Mutually recursion

    (letrec (
        (zero? (lambda (x) (= x 0)))
        (even?
        (lambda (n)
        (if (zero? n)
            #t
            (odd? (- n 1)))))
        (odd?
            (lambda (n)
            (if (zero? n)
                #f
                (even? (- n 1))))))
    (even? 88))
    ;#=>#t
    

Explore example.scm for more examples.

Features

  • Interactive REPL shell

  • Tail recursion optimization

  • Lazy evaluation

  • Short circut logic

  • Type: String, Number, Quote, LambdaProcess, Pair, Bool ...

  • syntax, builtin functions and procedures

    load define let let* letrec begin lambda and or not if cond delay map reduce force + - * / = cons list append list-length list-ref quote null? ' eval apply set! set-cdr! set-car! ... etc

Though it is a toy project just for fun and practice, Feel free to open an issue or make a merge request is you find bugs or have some suggestions.

Happy coding...

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NilObj = NilType{}

NilObj is the common object of NilType

View Source
var SyntaxMap = make(map[string]*Syntax)

SyntaxMap contains all defined scheme syntax.

View Source
var UndefObj = Undef{}

UndefObj is the common Undef object.

Functions

func IsBoolean

func IsBoolean(exp Expression) bool

IsBoolean return true if the expression represents bool.

func IsLambdaType

func IsLambdaType(expression Expression) bool

IsLambdaType checks whether this expression low level value is *LambdaProcess

func IsNilObj

func IsNilObj(obj Expression) bool

IsNilObj returns true when the expression is NilTyp.

func IsNullExp

func IsNullExp(exp Expression) bool

IsNullExp checks whether the expression represents Null(nil, NilType, blank list, blank expression).

func IsNumber

func IsNumber(exp Expression) bool

IsNumber check whether the expression represents Number.

func IsPair

func IsPair(obj Expression) bool

IsPair checks whether the expression value is a *Pair.

func IsPrimitiveExpression

func IsPrimitiveExpression(exp Expression) bool

IsPrimitiveExpression checks whether the expressions value is the primitive types.

func IsQuote

func IsQuote(exp Expression) bool

IsQuote check whether the value is Quote.

func IsString

func IsString(exp Expression) bool

IsString check whether the expression represents String in scheme.

func IsSymbol

func IsSymbol(expression Expression) bool

IsSymbol checks whether the expression is Symbol.

func IsSyntaxExpression

func IsSyntaxExpression(exp Expression) bool

IsSyntaxExpression check whether the expression is a scheme syntax expression.

func IsThunk

func IsThunk(exp Expression) bool

IsThunk checks whether an expression is a thunk and return the result

func IsTrue

func IsTrue(exp Expression) bool

IsTrue check whether the condition is true. Return false when Exp is #f or false, otherwise return true

func IsUndefObj

func IsUndefObj(obj Expression) bool

IsUndefObj returns true when the expression is Undef.

func Tokenize

func Tokenize(inputScript string) []string

Tokenize return the scheme tokens of input string

Types

type Env

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

Env represents the context of code.

func (*Env) Find

func (e *Env) Find(symbol Symbol) (Expression, error)

Find search all the relative environments to find the variable matching symbol.

func (*Env) Set

func (e *Env) Set(symbol Symbol, value Expression)

Set a symbol and its value in current environment

func (*Env) Symbols

func (e *Env) Symbols() []Symbol

Symbols returns the bound symbols including the outer frame

type Expression

type Expression interface{}

Expression represent the parsed tokens of scheme syntax tree or the low level builtin types.

func ActualValue

func ActualValue(exp Expression) (Expression, error)

ActualValue returns the actual value of an expression. If the expression is a Thunk, eval and return the result, otherwise return the expression itself.

func Eval

func Eval(exp Expression, env *Env) (ret Expression, err error)

Eval is the main function to evaluate the expression in an environment.

func EvalAll

func EvalAll(exps []Expression, env *Env) (ret Expression, err error)

EvalAll iterate the sequence of expressions and evaluate each one. Returns the last evaluated value as the result

func Parse

func Parse(tokens *[]string) (ret []Expression, err error)

Parse read and parse the tokens to construct a syntax tree represents in nested slices.

type Function

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

Function represents the basic scheme function in pure go.

func NewFunction

func NewFunction(funcName string, f commonFunction, minArgs int, maxArgs int) Function

NewFunction return a Function struct init with arguments. minArgs, maxArgs define the arguments count limitation of Function. Set to -1 means no limitation.

func (Function) Call

func (f Function) Call(args ...Expression) (Expression, error)

Call eval the function with args and returns the result.

func (Function) String

func (f Function) String() string

String returns the message to display

type Interpreter

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

Interpreter read from source and evaluate them.

func NewFileInterpreter

func NewFileInterpreter(reader io.Reader) *Interpreter

NewFileInterpreter construct a *Interpreter from file.

func NewFileInterpreterWithEnv

func NewFileInterpreterWithEnv(reader io.Reader, env *Env) *Interpreter

NewFileInterpreterWithEnv construct a *Interpreter from io.reader init with env.

func NewREPLInterpreter

func NewREPLInterpreter() *Interpreter

NewREPLInterpreter construct a REPL *Interpreter.

func (*Interpreter) Run

func (i *Interpreter) Run() (err error)

Run start the interpreter and evaluate the input.

type InterpreterMode

type InterpreterMode uint8

InterpreterMode represents mode the interpreter will run

const (
	// Interactive set the interpreter running as interactive shell
	Interactive InterpreterMode = iota
	// NoneInteractive set the interpreter running in normal mode without shell.
	NoneInteractive
)

type LambdaProcess

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

LambdaProcess wraps the body and env of a lambda expression

func (*LambdaProcess) Body

func (lambda *LambdaProcess) Body() Expression

Body returns the expressions of body.

func (*LambdaProcess) String

func (lambda *LambdaProcess) String() string

String implements the stringer interface

type NilType

type NilType struct{}

NilType represents Nil in scheme

func (NilType) String

func (n NilType) String() string

Strings returns the string representing NilType.

type Number

type Number float64

Number in scheme.

type Pair

type Pair struct {
	Car, Cdr Expression
}

Pair combines the two values. Should only use with pointer

func (*Pair) IsList

func (p *Pair) IsList() bool

IsList check whether the *Pair is a well formed list.

func (*Pair) IsNull

func (p *Pair) IsNull() bool

IsNull checks whether the *Pair is null.

func (*Pair) String

func (p *Pair) String() string

String returns the string representing the *Pair.

type Quote

type Quote string

Quote type in scheme

type String

type String string

String represents string in scheme.

func (String) String

func (s String) String() string

String return the string to display wrapping the low level string with quotes.

type Symbol

type Symbol string

Symbol represents the variable name in scheme.

type Syntax

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

Syntax wrap a syntax and give method to eval it.

func NewSyntax

func NewSyntax(name string, fn SyntaxFunc) *Syntax

NewSyntax construct a Syntax with custom name and SyntaxFunc

func (*Syntax) Eval

func (s *Syntax) Eval(args []Expression, env *Env) (Expression, error)

Eval runs the syntax and return the result.

func (*Syntax) String

func (s *Syntax) String() string

String return the string to display representing the syntax.

type SyntaxFunc

type SyntaxFunc func(args []Expression, env *Env) (Expression, error)

SyntaxFunc specified the common func format for Syntax

type Thunk

type Thunk struct {
	// expression to execute
	Exp Expression

	// context to execute Exp
	Env *Env
	// contains filtered or unexported fields
}

Thunk wraps expression for lazy execution Thunk should use as pointer

func NewThunk

func NewThunk(exp Expression, env *Env) *Thunk

NewThunk creates a thunk and returns the pointer

func (Thunk) String

func (t Thunk) String() string

String returns the string represents the Thunk struct.

func (*Thunk) Value

func (t *Thunk) Value() (Expression, error)

Value returns the actual value of the thunk

type Tokenizer

type Tokenizer struct {
	Source *bufio.Reader
	EOF    bool
	// contains filtered or unexported fields
}

Tokenizer wraps the input to generate tokens.

func NewTokenizerFromReader

func NewTokenizerFromReader(input io.Reader) *Tokenizer

NewTokenizerFromReader construct *Tokenizer from io.Reader

func NewTokenizerFromString

func NewTokenizerFromString(input string) *Tokenizer

NewTokenizerFromString construct *Tokenizer from string

func (*Tokenizer) NextToken

func (t *Tokenizer) NextToken() (string, bool)

NextToken read ahead and returns the next valid token.

func (*Tokenizer) Tokens

func (t *Tokenizer) Tokens() []string

Tokens returns all the tokens

type Undef

type Undef struct{}

Undef represents undefined expression value.

func (Undef) String

func (u Undef) String() string

String just implements the Stringer interface.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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