runtime

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2018 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package runtime implements functions to runtime.

Package runtime implements functions to scope.

Package runtime implements functions to types.

Index

Constants

View Source
const (
	TTBoolean     = "Boolean"
	TTString      = "String"
	TTInteger     = "Int"
	TTFloat       = "Float"
	TTArray       = "Array"
	TTDictionary  = "Dictionary"
	TTSymbol      = "Symbol"
	TTPlaceHolder = "PlaceHolder"
	TTFunction    = "Function"
	TTModule      = "Module"
	TTBreak       = "Break"
	TTContinue    = "Continue"
	TTReturn      = "Return"
	TTNil         = "Nil"
)

Variables

View Source
var (
	Nil = &TNil{}
	Yes = &TBoolean{Value: true}
	No  = &TBoolean{Value: false}
)
View Source
var FnRuntime = map[string]TRuntimeFn{

	"echo": func(args ...Data) (Data, error) {
		for _, arg := range args {
			fmt.Println(arg.Check())
		}
		return &TString{Value: ""}, nil
	},

	"put": func(args ...Data) (Data, error) {
		for _, arg := range args {
			fmt.Print(arg.Check())
		}
		return &TString{Value: ""}, nil
	},

	"puts": func(args ...Data) (Data, error) {
		for _, arg := range args {
			fmt.Println(arg.Check())
		}
		return &TString{Value: ""}, nil
	},

	"write": func(args ...Data) (Data, error) {
		for _, arg := range args {
			fmt.Print(arg.Check())
		}
		return &TString{Value: ""}, nil
	},

	"writeln": func(args ...Data) (Data, error) {
		for _, arg := range args {
			fmt.Println(arg.Check())
		}
		return &TString{Value: ""}, nil
	},

	"print": func(args ...Data) (Data, error) {
		for _, arg := range args {
			fmt.Print(arg.Check())
		}
		return &TString{Value: ""}, nil
	},

	"println": func(args ...Data) (Data, error) {
		for _, arg := range args {
			fmt.Println(arg.Check())
		}
		return &TString{Value: ""}, nil
	},

	"prompt": func(args ...Data) (Data, error) {
		reader := bufio.NewReader(os.Stdin)
		if len(args) > 0 {
			fmt.Print(strings.Trim(args[0].Check(), "\""))
		}
		out, _ := reader.ReadString('\n')
		return &TString{Value: strings.Trim(out, "\r\n")}, nil
	},

	"quit": func(args ...Data) (Data, error) {
		os.Exit(0)
		return nil, nil
	},

	"panic": func(args ...Data) (Data, error) {
		var message string
		if len(args) > 0 {
			message = args[0].Check()
		}
		return nil, rerror.ErrorFmt(message)
	},

	"typeof": func(args ...Data) (Data, error) {
		if len(args) != 1 {
			return nil, rerror.ErrorFmt("typeof() expects exactly 1 argument")
		}
		return &TString{Value: args[0].Type()}, nil
	},

	"len": func(args ...Data) (Data, error) {
		if len(args) != 1 {
			return nil, rerror.ErrorFmt("len() expects exactly 1 argument")
		}
		switch object := args[0].(type) {
		case *TArray:
			return &TInteger{Value: int64(len(object.Elements))}, nil
		case *TString:
			return &TInteger{Value: int64(len(object.Value))}, nil
		default:
			return nil, rerror.ErrorFmt("argument to `len` not supported, got %s", object.Type())
		}
	},

	"first": func(args ...Data) (Data, error) {
		if len(args) != 1 {
			return nil, rerror.ErrorFmt("first() expects exactly 1 argument")
		}
		if args[0].Type() != TTArray {
			return nil, rerror.ErrorFmt("argument to `first` must be ARRAY, got %s", args[0].Type())
		}
		arr := args[0].(*TArray)
		if len(arr.Elements) > 0 {
			return arr.Elements[0], nil
		}
		return nil, nil
	},

	"last": func(args ...Data) (Data, error) {
		if len(args) != 1 {
			return nil, rerror.ErrorFmt("last() expects exactly 1 argument")
		}
		if args[0].Type() != TTArray {
			return nil, rerror.ErrorFmt("argument to `last` must be ARRAY, got %s", args[0].Type())
		}
		arr := args[0].(*TArray)
		length := len(arr.Elements)
		if length > 0 {
			return arr.Elements[length-1], nil
		}
		return nil, nil
	},

	"rest": func(args ...Data) (Data, error) {
		if len(args) != 1 {
			return nil, rerror.ErrorFmt("rest() expects exactly 1 argument")
		}
		if args[0].Type() != TTArray {
			return nil, rerror.ErrorFmt("argument to `rest` must be ARRAY, got %s", args[0].Type())
		}
		arr := args[0].(*TArray)
		length := len(arr.Elements)
		if length > 0 {
			newElements := make([]Data, length-1, length-1)
			copy(newElements, arr.Elements[1:length])
			return &TArray{Elements: newElements}, nil
		}
		return nil, nil
	},

	"push": func(args ...Data) (Data, error) {
		if len(args) != 2 {
			return nil, rerror.ErrorFmt("push() expects exactly 2 argument")
		}
		if args[0].Type() != TTArray {
			return nil, rerror.ErrorFmt("argument to `push` must be ARRAY, got %s", args[0].Type())
		}
		arr := args[0].(*TArray)
		length := len(arr.Elements)
		newElements := make([]Data, length+1, length+1)
		copy(newElements, arr.Elements)
		newElements[length] = args[1]
		return &TArray{Elements: newElements}, nil
	},

	"String": func(args ...Data) (Data, error) {
		if len(args) != 1 {
			return nil, rerror.ErrorFmt("String() expects exactly 1 argument")
		}
		switch object := args[0].(type) {
		case *TInteger:
			return &TString{Value: fmt.Sprintf("%d", object.Value)}, nil
		case *TFloat:
			return &TString{Value: fmt.Sprintf("%f", object.Value)}, nil
		case *TBoolean:
			return &TString{Value: fmt.Sprintf("%t", object.Value)}, nil
		case *TString:
			return object, nil
		default:
			return nil, rerror.ErrorFmt("String() can't convert '%s' to String", object.Type())
		}
	},

	"Int": func(args ...Data) (Data, error) {
		if len(args) != 1 {
			return nil, rerror.ErrorFmt("Int() expects exactly 1 argument")
		}
		switch object := args[0].(type) {
		case *TString:
			i, err := strconv.Atoi(object.Value)
			if err != nil {
				return nil, rerror.ErrorFmt("Int() can't convert '%s' to Integer", object.Value)
			}
			return &TInteger{Value: int64(i)}, nil
		case *TFloat:
			return &TInteger{Value: int64(object.Value)}, nil
		case *TBoolean:
			result := 0
			if object.Value {
				result = 1
			}
			return &TInteger{Value: int64(result)}, nil
		case *TInteger:
			return object, nil
		default:
			return nil, rerror.ErrorFmt("Int() can't convert '%s' to Integer", object.Type())
		}
	},

	"Float": func(args ...Data) (Data, error) {
		if len(args) != 1 {
			return nil, rerror.ErrorFmt("Float() expects exactly 1 argument")
		}
		switch object := args[0].(type) {
		case *TString:
			i, err := strconv.ParseFloat(object.Value, 64)
			if err != nil {
				return nil, rerror.ErrorFmt("Float() can't convert '%s' to Integer", object.Value)
			}
			return &TFloat{Value: i}, nil
		case *TInteger:
			return &TFloat{Value: float64(object.Value)}, nil
		case *TBoolean:
			result := 0
			if object.Value {
				result = 1
			}
			return &TFloat{Value: float64(result)}, nil
		case *TFloat:
			return &TFloat{Value: object.Value}, nil
		default:
			return nil, rerror.ErrorFmt("Float() can't convert '%s' to Integer", object.Type())
		}
	},

	"Array": func(args ...Data) (Data, error) {
		if len(args) != 1 {
			return nil, rerror.ErrorFmt("Array() expects exactly 1 argument")
		}
		switch object := args[0].(type) {
		case *TArray:
			return object, nil
		default:
			return &TArray{Elements: []Data{object}}, nil
		}
	},

	"runtime_rand": func(args ...Data) (Data, error) {
		if len(args) != 2 {
			return nil, rerror.ErrorFmt("runtime_rand() expects exactly 2 arguments")
		}
		if args[0].Type() != TTInteger || args[1].Type() != TTInteger {
			return nil, rerror.ErrorFmt("runtime_rand() expects min and max as Integers")
		}
		min := int(args[0].(*TInteger).Value)
		max := int(args[1].(*TInteger).Value)
		if max < min {
			return nil, rerror.ErrorFmt("runtime_rand() expects max higher than min")
		}
		rand.Seed(time.Now().UnixNano())
		random := rand.Intn(max-min) + min
		return &TInteger{Value: int64(random)}, nil
	},

	"runtime_tolower": func(args ...Data) (Data, error) {
		if len(args) != 1 {
			return nil, rerror.ErrorFmt("runtime_tolower() expects exactly 1 argument")
		}
		if args[0].Type() != TTString {
			return nil, rerror.ErrorFmt("runtime_tolower() expects a String")
		}
		str := args[0].(*TString).Value
		return &TString{Value: strings.ToLower(str)}, nil
	},

	"runtime_toupper": func(args ...Data) (Data, error) {
		if len(args) != 1 {
			return nil, rerror.ErrorFmt("runtime_toupper() expects exactly 1 argument")
		}
		if args[0].Type() != TTString {
			return nil, rerror.ErrorFmt("runtime_toupper() expects a String")
		}
		str := args[0].(*TString).Value
		return &TString{Value: strings.ToUpper(str)}, nil
	},

	"runtime_regex_match": func(args ...Data) (Data, error) {
		if len(args) != 2 {
			return nil, rerror.ErrorFmt("runtime_regex_match() expects exactly 2 arguments")
		}
		if args[0].Type() != TTString {
			return nil, rerror.ErrorFmt("runtime_regex_match() expects a String")
		}
		if args[1].Type() != TTString {
			return nil, rerror.ErrorFmt("runtime_regex_match() expects a String regex")
		}
		object := args[0].(*TString).Value
		match := args[1].(*TString).Value
		regx, err := regexp.Compile(match)
		if err != nil {
			return nil, rerror.ErrorFmt("runtime_regex_match() couldn't compile the regular expression")
		}
		return &TBoolean{Value: regx.Find([]byte(object)) != nil}, nil
	},

	"Environment": func(args ...Data) (Data, error) {
		return &TString{Value: util.Environment()}, nil
	},

	"NameVersionEnvironment": func(args ...Data) (Data, error) {
		return &TString{Value: util.NameVersionEnvironment()}, nil
	},

	"Name": func(args ...Data) (Data, error) {
		return &TString{Value: util.Name()}, nil
	},

	"Version": func(args ...Data) (Data, error) {
		return &TString{Value: util.Version()}, nil
	},

	"NameVersion": func(args ...Data) (Data, error) {
		return &TString{Value: util.NameVersion()}, nil
	},

	"AuthorName": func(args ...Data) (Data, error) {
		return &TString{Value: util.AuthorName()}, nil
	},

	"AuthorEmail": func(args ...Data) (Data, error) {
		return &TString{Value: util.AuthorEmail()}, nil
	},

	"Copyright": func(args ...Data) (Data, error) {
		return &TString{Value: util.CopyrightDescription()}, nil
	},
}

Functions

This section is empty.

Types

type Data

type Data interface {
	Type() string
	Check() string
}

type Scope

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

func NewScope

func NewScope() *Scope

func NewScopeFrom

func NewScopeFrom(parent *Scope) *Scope

func (*Scope) Merge

func (s *Scope) Merge(scope *Scope)

func (*Scope) Read

func (s *Scope) Read(name string) (Data, bool)

func (*Scope) Update

func (s *Scope) Update(name string, value Data)

func (*Scope) Write

func (s *Scope) Write(name string, value Data)

type TArray

type TArray struct {
	Elements []Data
}

func (*TArray) Check

func (t *TArray) Check() string

func (*TArray) Type

func (t *TArray) Type() string

type TBoolean

type TBoolean struct {
	Value bool
}

func (*TBoolean) Check

func (t *TBoolean) Check() string

func (*TBoolean) Type

func (t *TBoolean) Type() string

type TBreak

type TBreak struct{}

func (*TBreak) Check

func (t *TBreak) Check() string

func (*TBreak) Type

func (t *TBreak) Type() string

type TContinue

type TContinue struct{}

func (*TContinue) Check

func (t *TContinue) Check() string

func (*TContinue) Type

func (t *TContinue) Type() string

type TDictionary

type TDictionary struct {
	Pairs map[Data]Data
}

func (*TDictionary) Check

func (t *TDictionary) Check() string

func (*TDictionary) Type

func (t *TDictionary) Type() string

type TFloat

type TFloat struct {
	Value float64
}

func (*TFloat) Check

func (t *TFloat) Check() string

func (*TFloat) Type

func (t *TFloat) Type() string

type TFunction

type TFunction struct {
	Parameters []*ast.FunctionParameter
	Body       *ast.BlockStatement
	ReturnType *ast.Identifier
	Variadic   bool
	Scope      *Scope
}

func (*TFunction) Check

func (t *TFunction) Check() string

func (*TFunction) Type

func (t *TFunction) Type() string

type TInteger

type TInteger struct {
	Value int64
}

func (*TInteger) Check

func (t *TInteger) Check() string

func (*TInteger) Type

func (t *TInteger) Type() string

type TModule

type TModule struct {
	Name *ast.Identifier
	Body *ast.BlockStatement
}

func (*TModule) Check

func (t *TModule) Check() string

func (*TModule) Type

func (t *TModule) Type() string

type TNil

type TNil struct{}

func (*TNil) Check

func (t *TNil) Check() string

func (*TNil) Type

func (t *TNil) Type() string

type TPlaceHolder

type TPlaceHolder struct{}

func (*TPlaceHolder) Check

func (t *TPlaceHolder) Check() string

func (*TPlaceHolder) Type

func (t *TPlaceHolder) Type() string

type TReturn

type TReturn struct {
	Value Data
}

func (*TReturn) Check

func (t *TReturn) Check() string

func (*TReturn) Type

func (t *TReturn) Type() string

type TRuntimeFn

type TRuntimeFn func(args ...Data) (Data, error)

type TString

type TString struct {
	Value string
}

func (*TString) Check

func (t *TString) Check() string

func (*TString) Type

func (t *TString) Type() string

type TSymbol

type TSymbol struct {
	Value string
}

func (*TSymbol) Check

func (t *TSymbol) Check() string

func (*TSymbol) Type

func (t *TSymbol) Type() string

Directories

Path Synopsis
Package cmd implements functions to commands.
Package cmd implements functions to commands.
Package stdlib implements functions to standard library.
Package stdlib implements functions to standard library.

Jump to

Keyboard shortcuts

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