interpreter

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const PANIC int = 128

Variables

View Source
var GlobalConstants = map[string]*Variable{
	"true":  {Constant: true, Value: TrueValue},
	"false": {Constant: true, Value: FalseValue},
	"null":  {Constant: true, Value: NullValue},
}
View Source
var GlobalDocs = []types.BuiltinDoc{
	{
		Name:        "len",
		Label:       "len(x)",
		Description: "Get the length of an array or string.",
	},
	{
		Name:        "print",
		Label:       "print(...)",
		Description: "Print values to the standard output.",
	},
	{
		Name:        "println",
		Label:       "println(...)",
		Description: "Print values to the standard output with a newline.",
	},
	{
		Name:        "printf",
		Label:       "printf(format, ...)",
		Description: "Print values to the standard output with a format string.",
	},
	{
		Name:        "exit",
		Label:       "exit(code)",
		Description: "Exit the program with an exit code.",
	},
	{
		Name:        "typeof",
		Label:       "typeof(value)",
		Description: "Get the type of a value.",
	},
}
View Source
var Globals = map[string]FuncType{
	"len": func(ctx *FuncContext) *Return {
		if len(ctx.Args) == 0 {
			ctx.Interp.Throw("len(x): expected 1 argument")
		}

		switch v := ctx.Args[0].(type) {
		case *Array:
			return NewReturn(NewInteger(big.NewInt(int64(len(v.Elements)))))
		case *String:
			return NewReturn(NewInteger(big.NewInt(int64(len(v.Value)))))
		default:
			ctx.Interp.Throw("len(x): expected an array or string, got %s", ctx.Args[0].Type())
			return nil
		}
	},

	"print": func(ctx *FuncContext) *Return {
		for i, arg := range ctx.Args {
			fmt.Fprint(ctx.Interp.Stdout(), ToString(ctx.Interp, ctx.Scope, arg))
			if i < len(ctx.Args)-1 {
				fmt.Fprint(ctx.Interp.Stdout(), " ")
			}
		}
		return &Return{}
	},
	"println": func(ctx *FuncContext) *Return {
		for i, arg := range ctx.Args {
			fmt.Fprint(ctx.Interp.Stdout(), ToString(ctx.Interp, ctx.Scope, arg))
			if i < len(ctx.Args)-1 {
				fmt.Fprint(ctx.Interp.Stdout(), " ")
			}
		}
		fmt.Fprintln(ctx.Interp.Stdout())
		return &Return{}
	},
	"printf": func(ctx *FuncContext) *Return {
		if len(ctx.Args) < 1 {
			ctx.Interp.Throw("printf(format, ...): expected at least 1 argument")
		}

		if _, ok := ctx.Args[0].(*String); !ok {
			ctx.Interp.Throw("printf(format, ...): expected string as first argument")
		}

		format := ctx.Args[0].(*String).Value
		ctx.Args = ctx.Args[1:]

		var values []any

		for _, arg := range ctx.Args {
			values = append(values, arg.Unwrap())
		}

		fmt.Fprintf(ctx.Interp.Stdout(), format, values...)
		return &Return{}
	},
	"exit": func(ctx *FuncContext) *Return {
		exitCode := 0
		if len(ctx.Args) != 0 {
			if _, ok := ctx.Args[0].(Numeric); !ok {
				ctx.Interp.Throw("exit(code): expected integer as first argument")
			}
			exitCode = ctx.Args[0].(Numeric).Int()
		}

		panic(gooseExit{exitCode})
	},
	"typeof": func(ctx *FuncContext) *Return {
		if len(ctx.Args) < 1 {
			ctx.Interp.Throw("typeof(value): expected 1 argument")
		}

		return NewReturn(ctx.Args[0].Type())
	},
}
View Source
var Natives = map[string]map[string]Value{
	"std:language/builtin.goose": std_language.Builtin,

	"std:fs/index.goose":       std_fs.Index,
	"std:json/index.goose":     std_json.Index,
	"std:math/index.goose":     std_math.Index,
	"std:platform/index.goose": std_platform.Index,
	"std:random/index.goose":   std_random.Index,
	"std:readline/index.goose": std_readline.Index,
}

Functions

func CreateGooseRoot

func CreateGooseRoot(gooseRoot string) error

func EvalExpr

func EvalExpr(expr ast.Expr, scope *Scope) (ret Value, err error)

func New

func New(file *ast.Module, fset *token.FileSet, trace bool, stdin io.Reader, stdout io.Writer, stderr io.Writer) (i *interp, err error)

Types

type CallFrame

type CallFrame struct {
	Module *Module
	Node   ast.Node
}

type Exception

type Exception struct {
	Message string
	Cause   *Exception
	Stack   []*CallFrame
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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