antlr4yak

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: AGPL-3.0 Imports: 24 Imported by: 0

README

Antlr4 Yak

Generate Code, use re-generate-antlr-schema.sh

statement

  • go
  • include
  • defer
  • assert
  • return
  • importmod
  • range
  • func
  • recover 变量
  • new/make/chan
  • const
  • type
  • struct
  • interface 操作符
  • in
  • .获取成员
  • []获取元素 流程控制
  • if/elif/else
  • for
  • switch 其它
  • 语法警告提示
  • 语法错误提示
  • 支持char类型(Rune / Byte 本质都为 Int)

Documentation

Index

Constants

View Source
const YAKC_CACHE_MAX_LENGTH = 300

Variables

View Source
var (
	MAGIC_NUMBER        = []byte{0xbc, 0xed}
	CRYPTO_MAGIC_NUMBER = []byte{0xcc, 0xed}
)

Functions

func HaveYakcCache

func HaveYakcCache(code string) ([]byte, bool)

func HaveYakcCacheWithKey

func HaveYakcCacheWithKey(code string, key []byte) ([]byte, bool)

func Import added in v1.2.3

func Import(name string, f interface{})

func InjectContextBuiltinFunction added in v1.2.9

func InjectContextBuiltinFunction(engine *Engine)

func IsCryptoYakc

func IsCryptoYakc(b []byte) bool

func IsNormalYakc

func IsNormalYakc(b []byte) bool

func IsYakc

func IsYakc(b []byte) bool

func SaveYakcCache

func SaveYakcCache(code string, yakc []byte)

func SaveYakcCacheWithKey

func SaveYakcCacheWithKey(code string, yakc []byte, key []byte)

Types

type Engine

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

func New

func New() *Engine

func (*Engine) CallYakFunction

func (n *Engine) CallYakFunction(ctx context.Context, funcName string, params []interface{}) (interface{}, error)

函数调用时如果不加锁,并发会有问题

func (*Engine) CallYakFunctionNative

func (n *Engine) CallYakFunctionNative(ctx context.Context, function *yakvm.Function, params ...interface{}) (interface{}, error)

func (*Engine) Compile

func (n *Engine) Compile(code string) ([]*yakvm.Code, error)

func (*Engine) CopyVars

func (n *Engine) CopyVars() map[string]interface{}

func (*Engine) EnableDebug

func (n *Engine) EnableDebug()

func (*Engine) EnableStrictMode

func (n *Engine) EnableStrictMode()

func (*Engine) Eval

func (n *Engine) Eval(ctx context.Context, code string) error

func (*Engine) EvalInline

func (n *Engine) EvalInline(ctx context.Context, code string) error

func (*Engine) EvalWithInline

func (n *Engine) EvalWithInline(ctx context.Context, code string, inline bool) error

func (*Engine) ExecYakc

func (n *Engine) ExecYakc(ctx context.Context, b []byte, key []byte, code string) error

func (*Engine) ExecYakcWithCode

func (n *Engine) ExecYakcWithCode(ctx context.Context, b []byte, key []byte, code string) error

func (*Engine) ExecuteAsBooleanExpression

func (n *Engine) ExecuteAsBooleanExpression(expr string, dependencies map[string]interface{}) (bool, error)

func (*Engine) ExecuteAsExpression

func (n *Engine) ExecuteAsExpression(expr string, dependencies map[string]interface{}) (interface{}, error)

func (*Engine) FormattedAndSyntaxChecking

func (n *Engine) FormattedAndSyntaxChecking(code string) (string, error)

func (*Engine) GetCurrentScope

func (n *Engine) GetCurrentScope() (*yakvm.Scope, error)

func (*Engine) GetFntable

func (n *Engine) GetFntable() map[string]interface{}

func (*Engine) GetLastStackValue

func (n *Engine) GetLastStackValue() (*yakvm.Value, error)

func (*Engine) GetScopeInspects

func (n *Engine) GetScopeInspects() ([]*ScopeValue, error)

func (*Engine) GetSymNames

func (n *Engine) GetSymNames() []string

func (*Engine) GetVM

func (n *Engine) GetVM() *yakvm.VirtualMachine

func (*Engine) GetVar

func (n *Engine) GetVar(name string) (interface{}, bool)

func (*Engine) HaveEvaluatedCode

func (n *Engine) HaveEvaluatedCode() bool

func (*Engine) ImportLibs

func (n *Engine) ImportLibs(libs map[string]interface{})

func (*Engine) ImportSubLibs

func (n *Engine) ImportSubLibs(parent string, libs map[string]interface{})

func (*Engine) LoadCode

func (n *Engine) LoadCode(ctx context.Context, code string, table map[string]interface{}) error

LoadCode 暂时使用Eval的方式加载代码,后面可以进行优化,只编译声明语句和assign语句

func (*Engine) Marshal

func (n *Engine) Marshal(code string, key []byte) ([]byte, error)

func (*Engine) MustCompile

func (n *Engine) MustCompile(code string) []*yakvm.Code

func (*Engine) RunFile

func (n *Engine) RunFile(ctx context.Context, path string) error

RunFile 手动执行Yak脚本一般都是从文件开始执行,这种情况建议使用RunFile执行代码,便于报错时提供文件路径信息

func (*Engine) SafeEval

func (n *Engine) SafeEval(ctx context.Context, code string) (err error)

func (*Engine) SafeEvalInline

func (n *Engine) SafeEvalInline(ctx context.Context, code string) (err error)

func (*Engine) SafeExecYakc

func (n *Engine) SafeExecYakc(ctx context.Context, b []byte, key []byte, code string) (fErr error)

func (*Engine) SafeExecYakcWithCode

func (n *Engine) SafeExecYakcWithCode(ctx context.Context, b []byte, key []byte, code string) (fErr error)

func (*Engine) SetDebugCallback

func (n *Engine) SetDebugCallback(callback func(*yakvm.Debugger))

func (*Engine) SetDebugInit

func (n *Engine) SetDebugInit(callback func(*yakvm.Debugger))

func (*Engine) SetDebugMode

func (n *Engine) SetDebugMode(debug bool)

func (*Engine) SetExternalVarGetter added in v1.2.8

func (n *Engine) SetExternalVarGetter(f func(name string) (any, bool))

func (*Engine) SetSandboxMode added in v1.3.1

func (e *Engine) SetSandboxMode(mode bool)

func (*Engine) SetSourceFilePath

func (n *Engine) SetSourceFilePath(path string)

func (*Engine) SetStrictMode added in v1.2.3

func (e *Engine) SetStrictMode(b bool)

func (*Engine) SetVar

func (n *Engine) SetVar(k string, v interface{})

func (*Engine) UnMarshal

func (n *Engine) UnMarshal(b []byte, key []byte, code string) (*yakvm.SymbolTable, []*yakvm.Code, error)

func (*Engine) Var

func (n *Engine) Var(name string) interface{}

func (*Engine) YakBuiltinEval added in v1.2.9

func (e *Engine) YakBuiltinEval(code string)

eval 执行任意 Yak 代码 这个函数是存在副作用的,即能够获取和改变当前引擎中的上下文 Example: ``` a = 1 eval("a++") assert a == 2 ```

func (*Engine) YakBuiltinGetFromScope added in v1.2.9

func (e *Engine) YakBuiltinGetFromScope(v string, vals ...any) any

getFromScope 获取当前作用域中的变量,返回变量值 Example: ``` a, b = 1, "yak" { assert getFromScope("a") == 1 } { assert getFromScope("b") == "yak" } ```

func (*Engine) YakBuiltinGetScopeInspects added in v1.2.9

func (e *Engine) YakBuiltinGetScopeInspects() ([]*ScopeValue, error)

getScopeInspects 获取当前作用域中的所有变量,返回 ScopeValue 结构体引用切片 Example: ``` a, b = 1, "yak" values, err = getScopeInspects() for v in values { println(v.Value) } ```

func (*Engine) YakBuiltinfmt added in v1.2.9

func (e *Engine) YakBuiltinfmt(code string) string

yakfmt 格式化任意 Yak 代码,返回格式化后的代码 Example: ``` yakfmt("for { println(`hello yak`) }") ```

func (*Engine) YakBuiltinfmtWithError added in v1.2.9

func (e *Engine) YakBuiltinfmtWithError(code string) (string, error)

yakfmtWithError 格式化任意 Yak 代码,返回格式化后的代码和错误 Example: ``` yakfmtWithError("for { println(`hello yak`) }") ```

type Executor

type Executor struct {
	Compiler *FakeCompiler
	VM       *FakeVM
}

func NewExecutor

func NewExecutor(i string) *Executor

type FakeCompiler

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

func (*FakeCompiler) GetErrors

func (f *FakeCompiler) GetErrors() yakast.YakMergeError

func (*FakeCompiler) GetFormattedCode

func (f *FakeCompiler) GetFormattedCode() string

type FakeVM

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

func (*FakeVM) DebugExec

func (f *FakeVM) DebugExec()

func (*FakeVM) Exec

func (f *FakeVM) Exec()

func (*FakeVM) GetCodes

func (f *FakeVM) GetCodes() []*yakvm.Code

func (*FakeVM) NormalExec

func (f *FakeVM) NormalExec()

func (*FakeVM) SafeExec

func (f *FakeVM) SafeExec() error

type ScopeValue

type ScopeValue struct {
	Name         string
	Id           int
	Value        interface{}
	ValueVerbose string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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