twik

package
v0.0.0-...-8f4bfd8 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2016 License: LGPL-3.0, BSD-3-Clause Imports: 4 Imported by: 0

README

Installation and usage

See gopkg.in/twik.v1 for documentation and usage details.

Changes

These are the changes to the original code:

  • added convenience functions ''unless'' and ''when'' that have the same semantic as in Common Lisp. ''if'' now always needs three expressions to function.
  • added comparision functions ''>'', ''>='', ''<'' and ''<='' that can only be applied to numbers (int and float) and always contain 2 epxressions. will return true or false.
  • Scope is now an interface and the previous was renamed to DefaultScope
  • Scope's can now be "stacked" via the Enclosure method
  • added ''split'' functions for strings
  • added ''nth'' and ''length'' functions for easy usage of list from ''split''

ToDo

  • Update test cases with new functions

Documentation

Overview

Package twik implements a tiny embeddable language for Go.

For details, see the blog post:

http://blog.labix.org/2013/07/16/twik-a-tiny-language-for-go

Index

Constants

This section is empty.

Variables

View Source
var Globals = []struct {
	Name  string
	value interface{}
}{
	{"true", true},
	{"false", false},
	{"nil", nil},
	{"error", errorFn},
	{"==", eqFn},
	{"!=", neFn},
	{"+", plusFn},
	{"-", minusFn},
	{"*", mulFn},
	{"/", divFn},
	{">", gtFn},
	{">=", gteFn},
	{"<", ltFn},
	{"<=", lteFn},
	{"or", orFn},
	{"and", andFn},
	{"if", ifFn},
	{"when", whenFn},
	{"unless", unlessFn},
	{"var", varFn},
	{"set", setFn},
	{"do", doFn},
	{"func", funcFn},
	{"for", forFn},
	{"range", rangeFn},
	{"split", splitFn},
	{"nth", nthFn},
	{"length", lengthFn},
}

Functions

func NewFileSet

func NewFileSet() *ast.FileSet

NewFileSet returns a new FileSet to hold positioning information for a set of parsed twik sources.

func Parse

func Parse(fset *ast.FileSet, name string, code []byte) (ast.Node, error)

Parse parses a byte slice containing twik code and returns the resulting parsed tree.

Positioning information for the parsed code will be stored in fset under the given name.

func ParseString

func ParseString(fset *ast.FileSet, name string, code string) (ast.Node, error)

ParseString parses a string containing twik code and returns the resulting parsed tree.

Positioning information for the parsed code will be stored in fset under the given name.

Types

type DefaultScope

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

DefaultScope contains standard functions (symbols) like 'if', 'and' etc. Implements Scope interface.

func (*DefaultScope) Branch

func (s *DefaultScope) Branch() Scope

Branch returns a new scope that has s as a parent.

func (*DefaultScope) Create

func (s *DefaultScope) Create(symbol string, value interface{}) error

Create defines a new symbol with the given value in the s scope. It is an error to redefine an existent symbol.

func (*DefaultScope) Enclose

func (s *DefaultScope) Enclose(parent Scope) error

func (*DefaultScope) Eval

func (s *DefaultScope) Eval(node ast.Node) (value interface{}, err error)

Eval evaluates node in the s scope and returns the resulting value.

func (*DefaultScope) Get

func (s *DefaultScope) Get(symbol string) (value interface{}, err error)

Get returns the value of symbol in the shallowest scope it is defined in. It is an error to get the value of an undefined symbol.

func (*DefaultScope) Set

func (s *DefaultScope) Set(symbol string, value interface{}) error

Set sets symbol to the given value in the shallowest scope it is defined in. It is an error to set an undefined symbol.

type Error

type Error struct {
	Err     error
	PosInfo *ast.PosInfo
}

Error holds an error and the source position where the error was found.

func (*Error) Error

func (e *Error) Error() string

type Scope

type Scope interface {
	Create(string, interface{}) error
	Set(string, interface{}) error
	Get(string) (interface{}, error)
	Branch() Scope
	Eval(ast.Node) (interface{}, error)
	Enclose(Scope) error
}

Scope is an environment where twik logic may be evaluated in.

func NewDefaultScope

func NewDefaultScope(fset *ast.FileSet) Scope

NewScope returns a new scope for evaluating logic that was parsed into fset.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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