gisp

package
v0.0.0-...-6e7bffa Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2015 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Axiom = Environment{
	Meta: map[string]interface{}{
		"name":     "axiom",
		"category": "environment",
	},
	Content: map[string]function{
		"quote": func(env Env) element {
			return func(args ...interface{}) (interface{}, error) {
				return Quote{args[0]}, nil
			}
		},
		"var": func(env Env) element {
			return func(args ...interface{}) (interface{}, error) {
				value, err := eval(env, args[1])
				if err != nil {
					return nil, err
				}
				err = env.Define((args[0].(Atom)).Name, value)
				return nil, err
			}
		},
		"set": func(env Env) element {
			return func(args ...interface{}) (interface{}, error) {
				value, err := eval(env, args[1])
				if err != nil {
					return nil, err
				}
				err = env.SetVar((args[0].(Atom)).Name, value)
				if err == nil {
					return nil, err
				} else {
					return value, nil
				}
			}
		},
		"equal": func(env Env) element {
			return func(args ...interface{}) (interface{}, error) {
				x, err := eval(env, args[0])
				if err != nil {
					return nil, err
				}
				y, err := eval(env, args[1])
				if err != nil {
					return nil, err
				}
				return reflect.DeepEqual(x, y), nil
			}
		},
		"cond": func(env Env) element {
			return func(args ...interface{}) (interface{}, error) {
				cases := args[0].([]interface{})
				l := len(args)
				var els interface{}
				if l > 1 {
					els = args[1]
				} else {
					els = nil
				}

				for _, b := range cases {
					branch := b.([]interface{})
					cond := branch[0].(List)
					result, err := eval(env, cond)
					if err != nil {
						return nil, err
					}
					if ok := result.(bool); ok {
						return eval(env, branch[1])
					}
				}

				if els != nil {
					return eval(env, els)
				} else {
					return nil, nil
				}
			}
		},
		"car": func(env Env) element {
			return func(args ...interface{}) (interface{}, error) {

				lisp, err := eval(env, args[0])
				if err != nil {
					return nil, err
				}
				return (lisp.(List))[0], nil
			}
		},
		"cdr": func(env Env) element {
			return func(args ...interface{}) (interface{}, error) {

				lisp, err := eval(env, args[0])
				if err != nil {
					return nil, err
				}
				return (lisp.(List))[1:], nil
			}
		},

		"atom": func(env Env) element {
			return func(args ...interface{}) (interface{}, error) {
				arg := args[0]
				if l, ok := arg.(List); ok {
					return len(l) == 0, nil
				} else {
					return true, nil
				}
			}
		},

		"concat": func(env Env) element {
			return func(args ...interface{}) (interface{}, error) {
				return List(args), nil
			}
		},
	},
}
View Source
var BoolParser = Bind(Choice(String("true"), String("false")), func(input interface{}) Parser {
	return func(st ParseState) (interface{}, error) {
		switch input.(string) {
		case "true":
			return true, nil
		case "false":
			return false, nil
		default:
			return nil, fmt.Errorf("Unexpect bool token %v", input)
		}
	}
})
View Source
var EscapeChar = Bind_(Rune('\\'), func(st ParseState) (interface{}, error) {
	r, err := OneOf("nrt\"\\")(st)
	if err == nil {
		ru := r.(rune)
		switch ru {
		case 'r':
			return '\r', nil
		case 'n':
			return '\n', nil

		case '\'':
			return '\'', nil
		case '"':
			return '"', nil
		case '\\':
			return '\\', nil
		case 't':
			return '\t', nil
		default:
			return nil, st.Trap("Unknown escape sequence \\%c", r)
		}
	} else {
		return nil, err
	}
})
View Source
var NilParser = Bind_(String("nil"), Return(nil))
View Source
var RuneParser = Bind(
	Between(Rune('\''), Rune('\''),
		Either(Try(EscapeChar), NoneOf("'"))),
	ReturnString)
View Source
var StringParser = Bind(
	Between(Rune('"'), Rune('"'),
		Many(Either(Try(EscapeChar), NoneOf("\"")))),
	ReturnString)

Functions

func AtomParser

func AtomParser(st ParseState) (interface{}, error)

func LambdaExpr

func LambdaExpr(env Env) element

func LetExpr

func LetExpr(env Env) element

let => (let ((a, value), (b, value)...) ...)

func ListParser

func ListParser(st ParseState) (interface{}, error)

func NumberParser

func NumberParser(st ParseState) (interface{}, error)

func QuoteParser

func QuoteParser(st ParseState) (interface{}, error)

func ValueParser

func ValueParser(st ParseState) (interface{}, error)

Types

type Atom

type Atom struct {
	Name string
}

func (Atom) Eval

func (this Atom) Eval(env Env) (interface{}, error)

func (Atom) String

func (this Atom) String() string

type Env

type Env interface {
	Define(name string, value interface{}) error
	SetVar(name string, value interface{}) error
	Lookup(name string) (interface{}, bool)
	Local(name string) (interface{}, bool)
	Global(name string) (interface{}, bool)
}

type Environment

type Environment struct {
	Meta    map[string]interface{}
	Content map[string]function
}
var Propositions Environment = Environment{
	Meta: map[string]interface{}{
		"name":     "propositions",
		"category": "environment",
	},
	Content: map[string]function{
		"lambda": LambdaExpr,
		"let":    LetExpr,
		"+":      addExpr,
		"add":    addExpr,
		"-":      subExpr,
		"sub":    subExpr,
		"*":      mulExpr,
		"mul":    mulExpr,
		"/":      divExpr,
		"div":    divExpr,
	},
}

func (Environment) Global

func (this Environment) Global(name string) (interface{}, bool)

func (Environment) Local

func (this Environment) Local(name string) (interface{}, bool)

func (Environment) Lookup

func (this Environment) Lookup(name string) (interface{}, bool)

type Function

type Function struct {
	Meta    map[string]interface{}
	Content []interface{}
}

func (Function) Define

func (this Function) Define(name string, value interface{}) error

func (Function) Eval

func (this Function) Eval(env Env) (interface{}, error)

func (Function) Global

func (this Function) Global(name string) (interface{}, bool)

func (Function) Local

func (this Function) Local(name string) (interface{}, bool)

func (Function) Lookup

func (this Function) Lookup(name string) (interface{}, bool)

func (Function) Parameter

func (this Function) Parameter(name string) (interface{}, bool)

func (Function) SetVar

func (this Function) SetVar(name string, value interface{}) error

type GispParser

type GispParser struct {
	Meta    map[string]interface{}
	Content map[string]interface{}
}

func NewGisp

func NewGisp(buildins map[string]Environment) (*GispParser, error)

给定若干可以组合的基准环境

func (GispParser) Define

func (this GispParser) Define(name string, value interface{}) error

func (GispParser) Global

func (this GispParser) Global(name string) (interface{}, bool)

look up in buildins

func (GispParser) Local

func (this GispParser) Local(name string) (interface{}, bool)

func (GispParser) Lookup

func (this GispParser) Lookup(name string) (interface{}, bool)

func (GispParser) Parse

func (this GispParser) Parse(code string) (interface{}, error)

func (GispParser) SetVar

func (this GispParser) SetVar(name string, value interface{}) error

type Lambda

type Lambda struct {
	Meta    map[string]interface{}
	Content List
}

func DeclareLambda

func DeclareLambda(env Env, args List, lisps ...interface{}) (*Lambda, error)

(lambda (args...) body)

func (Lambda) Call

func (this Lambda) Call(args ...interface{}) Function

create a lambda s-expr can be eval

type Let

type Let struct {
	Meta    map[string]interface{}
	Content List
}

func (Let) Define

func (this Let) Define(name string, value interface{}) error

func (Let) Eval

func (this Let) Eval(env Env) (interface{}, error)

func (Let) Global

func (this Let) Global(name string) (interface{}, bool)

func (Let) Local

func (this Let) Local(name string) (interface{}, bool)

func (Let) Lookup

func (this Let) Lookup(name string) (interface{}, bool)

func (Let) SetVar

func (this Let) SetVar(name string, value interface{}) error

type Lisp

type Lisp interface {
	Eval(env Env) (interface{}, error)
}

type List

type List []interface{}

func (List) Eval

func (this List) Eval(env Env) (interface{}, error)

func (List) String

func (this List) String() string

type Quote

type Quote struct {
	Lisp interface{}
}

func (Quote) Eval

func (this Quote) Eval(env Env) (interface{}, error)

Jump to

Keyboard shortcuts

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