xxscript

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT Imports: 56 Imported by: 0

Documentation

Overview

Package xxscript provides a simple scripting language for XxSql.

Package xxscript provides a simple scripting language for XxSql.

Package xxscript provides a simple scripting language for XxSql.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayExpr

type ArrayExpr struct {
	Elements []Expression
}

ArrayExpr represents an array literal.

func (*ArrayExpr) String

func (e *ArrayExpr) String() string

type AssignExpr

type AssignExpr struct {
	Left  Expression
	Value Expression
}

AssignExpr represents an assignment expression.

func (*AssignExpr) String

func (e *AssignExpr) String() string

type BinaryExpr

type BinaryExpr struct {
	Left  Expression
	Op    TokenType
	Right Expression
}

BinaryExpr represents a binary expression.

func (*BinaryExpr) String

func (e *BinaryExpr) String() string

type BlockStmt

type BlockStmt struct {
	Statements []Statement
}

BlockStmt represents a block of statements.

func (*BlockStmt) String

func (s *BlockStmt) String() string

type BoolExpr

type BoolExpr struct {
	Value bool
}

BoolExpr represents a boolean literal.

func (*BoolExpr) String

func (e *BoolExpr) String() string

type BreakStmt

type BreakStmt struct{}

BreakStmt represents a break statement.

func (*BreakStmt) String

func (s *BreakStmt) String() string

type CallExpr

type CallExpr struct {
	Func Expression
	Args []Expression
}

CallExpr represents a function call.

func (*CallExpr) String

func (e *CallExpr) String() string

type Callable

type Callable interface {
	Call(args []Value) (Value, error)
}

Callable interface for callable values

type Context

type Context struct {
	Variables   map[string]Value
	Functions   map[string]*UserFunc
	Executor    SQLExecutor
	Engine      *storage.Engine
	HTTPWriter  http.ResponseWriter
	HTTPRequest *http.Request
	MaxSteps    int
	BaseDir     string // Base directory for file operations
	Timezone    *time.Location
	// contains filtered or unexported fields
}

Context provides the execution context.

func NewContext

func NewContext() *Context

NewContext creates a new execution context.

func (*Context) SetupBuiltins

func (ctx *Context) SetupBuiltins()

SetupBuiltins sets up built-in objects.

type ContinueStmt

type ContinueStmt struct{}

ContinueStmt represents a continue statement.

func (*ContinueStmt) String

func (s *ContinueStmt) String() string

type DBExecFunc

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

DBExecFunc executes a statement.

func (*DBExecFunc) Call

func (f *DBExecFunc) Call(args []Value) (Value, error)

Call implements Callable.

type DBObject

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

DBObject provides database-related methods.

func NewDBObject

func NewDBObject(ctx *Context) *DBObject

NewDBObject creates a new DB object.

func (*DBObject) GetMember

func (d *DBObject) GetMember(name string) (Value, error)

GetMember returns a member value.

type DBQueryFunc

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

DBQueryFunc executes a query and returns results.

func (*DBQueryFunc) Call

func (f *DBQueryFunc) Call(args []Value) (Value, error)

Call implements Callable.

type DBQueryRowFunc

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

DBQueryRowFunc executes a query and returns a single row.

func (*DBQueryRowFunc) Call

func (f *DBQueryRowFunc) Call(args []Value) (Value, error)

Call implements Callable.

type ErrorValue added in v0.0.7

type ErrorValue struct {
	Message string
	Type    string
	Cause   error
}

ErrorValue is a special type for error values

type ExprStmt

type ExprStmt struct {
	Expr Expression
}

ExprStmt represents an expression statement.

func (*ExprStmt) String

func (s *ExprStmt) String() string

type Expression

type Expression interface {
	Node
	// contains filtered or unexported methods
}

Expression represents an expression.

type ForStmt

type ForStmt struct {
	Init      Statement
	Condition Expression
	Update    Statement
	Body      *BlockStmt
}

ForStmt represents a for loop.

func (*ForStmt) String

func (s *ForStmt) String() string

type FuncStmt

type FuncStmt struct {
	Name   string
	Params []string
	Body   *BlockStmt
}

FuncStmt represents a function declaration.

func (*FuncStmt) String

func (s *FuncStmt) String() string

type HTTPBodyFunc

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

HTTPBodyFunc reads the request body.

func (*HTTPBodyFunc) Call

func (f *HTTPBodyFunc) Call(args []Value) (Value, error)

Call implements Callable.

type HTTPBodyJSONFunc

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

HTTPBodyJSONFunc parses the request body as JSON.

func (*HTTPBodyJSONFunc) Call

func (f *HTTPBodyJSONFunc) Call(args []Value) (Value, error)

Call implements Callable.

type HTTPCookieFunc

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

HTTPCookieFunc gets a cookie value.

func (*HTTPCookieFunc) Call

func (f *HTTPCookieFunc) Call(args []Value) (Value, error)

Call implements Callable.

type HTTPHeaderFunc

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

HTTPHeaderFunc gets a header value.

func (*HTTPHeaderFunc) Call

func (f *HTTPHeaderFunc) Call(args []Value) (Value, error)

Call implements Callable.

type HTTPJSONFunc

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

HTTPJSONFunc writes JSON response.

func (*HTTPJSONFunc) Call

func (f *HTTPJSONFunc) Call(args []Value) (Value, error)

Call implements Callable.

type HTTPObject

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

HTTPObject provides HTTP-related methods.

func NewHTTPObject

func NewHTTPObject(ctx *Context) *HTTPObject

NewHTTPObject creates a new HTTP object.

func (*HTTPObject) GetMember

func (h *HTTPObject) GetMember(name string) (Value, error)

GetMember returns a member value.

type HTTPParamFunc

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

HTTPParamFunc gets a query parameter.

func (*HTTPParamFunc) Call

func (f *HTTPParamFunc) Call(args []Value) (Value, error)

Call implements Callable.

type HTTPRedirectFunc

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

HTTPRedirectFunc redirects the client.

func (*HTTPRedirectFunc) Call

func (f *HTTPRedirectFunc) Call(args []Value) (Value, error)

Call implements Callable.

type HTTPSetCookieFunc

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

HTTPSetCookieFunc sets a cookie.

func (*HTTPSetCookieFunc) Call

func (f *HTTPSetCookieFunc) Call(args []Value) (Value, error)

Call implements Callable.

type HTTPSetHeaderFunc

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

HTTPSetHeaderFunc sets a response header.

func (*HTTPSetHeaderFunc) Call

func (f *HTTPSetHeaderFunc) Call(args []Value) (Value, error)

Call implements Callable.

type HTTPStatusFunc

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

HTTPStatusFunc sets the HTTP status code.

func (*HTTPStatusFunc) Call

func (f *HTTPStatusFunc) Call(args []Value) (Value, error)

Call implements Callable.

type HTTPWriteFunc

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

HTTPWriteFunc writes to the response.

func (*HTTPWriteFunc) Call

func (f *HTTPWriteFunc) Call(args []Value) (Value, error)

Call implements Callable.

type IdentExpr

type IdentExpr struct {
	Name string
}

IdentExpr represents an identifier.

func (*IdentExpr) String

func (e *IdentExpr) String() string

type IfStmt

type IfStmt struct {
	Condition Expression
	Then      *BlockStmt
	Else      Statement // can be *BlockStmt or *IfStmt
}

IfStmt represents an if statement.

func (*IfStmt) String

func (s *IfStmt) String() string

type IndexExpr

type IndexExpr struct {
	Object Expression
	Index  Expression
}

IndexExpr represents an index access (arr[index]).

func (*IndexExpr) String

func (e *IndexExpr) String() string

type Interpreter

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

Interpreter interprets XxScript AST.

func NewInterpreter

func NewInterpreter(ctx *Context) *Interpreter

NewInterpreter creates a new interpreter.

func (*Interpreter) Run

func (i *Interpreter) Run(prog *Program) (Value, error)

Run executes a program.

type Lexer

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

Lexer tokenizes XxScript source code.

func NewLexer

func NewLexer(input string) *Lexer

NewLexer creates a new lexer.

func (*Lexer) NextToken

func (l *Lexer) NextToken() Token

NextToken returns the next token.

type MapExpr

type MapExpr struct {
	Pairs map[string]Expression
}

MapExpr represents a map/object literal.

func (*MapExpr) String

func (e *MapExpr) String() string

type MemberExpr

type MemberExpr struct {
	Object Expression
	Member Expression
}

MemberExpr represents a member access (obj.field or obj["field"]).

func (*MemberExpr) String

func (e *MemberExpr) String() string

type Node

type Node interface {
	String() string
	// contains filtered or unexported methods
}

Node represents an AST node.

type NullExpr

type NullExpr struct{}

NullExpr represents null.

func (*NullExpr) String

func (e *NullExpr) String() string

type NumberExpr

type NumberExpr struct {
	Value float64
}

NumberExpr represents a number literal.

func (*NumberExpr) String

func (e *NumberExpr) String() string

type Parser

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

Parser parses XxScript source code.

func NewParser

func NewParser(tokens []Token) *Parser

NewParser creates a new parser.

type Program

type Program struct {
	Statements []Statement
}

Program represents a complete script.

func Parse

func Parse(source string) (*Program, error)

Parse parses the tokens and returns an AST.

func (*Program) String

func (p *Program) String() string

type ReturnStmt

type ReturnStmt struct {
	Value Expression
}

ReturnStmt represents a return statement.

func (*ReturnStmt) String

func (s *ReturnStmt) String() string

type SQLExecutor added in v0.0.6

type SQLExecutor interface {
	ExecuteForScript(query string) (interface{}, error)
}

SQLExecutor is an interface for executing SQL queries. This interface breaks the circular dependency between xxscript and executor packages.

type Statement

type Statement interface {
	Node
	// contains filtered or unexported methods
}

Statement represents a statement.

type StringExpr

type StringExpr struct {
	Value string
}

StringExpr represents a string literal.

func (*StringExpr) String

func (e *StringExpr) String() string

type ThrowError

type ThrowError struct {
	Value Value
}

ThrowError represents an error thrown by the throw statement.

func (*ThrowError) Error

func (e *ThrowError) Error() string

type ThrowStmt

type ThrowStmt struct {
	Error Expression
}

ThrowStmt represents a throw statement.

func (*ThrowStmt) String

func (s *ThrowStmt) String() string

type Token

type Token struct {
	Type  TokenType
	Value string
	Line  int
	Col   int
}

Token represents a token.

func Tokenize

func Tokenize(input string) []Token

Tokenize returns all tokens from the input.

type TokenType

type TokenType int

TokenType represents a token type.

const (
	TokEOF TokenType = iota
	TokError
	TokComment

	// Literals
	TokIdent  // identifier
	TokString // "string" or 'string'
	TokNumber // 123 or 123.45
	TokBool   // true or false
	TokNull   // null

	// Keywords
	TokVar      // var
	TokIf       // if
	TokElse     // else
	TokFor      // for
	TokWhile    // while
	TokFunc     // func
	TokReturn   // return
	TokBreak    // break
	TokContinue // continue
	TokTry      // try
	TokCatch    // catch
	TokThrow    // throw

	// Operators
	TokPlus    // +
	TokMinus   // -
	TokStar    // *
	TokSlash   // /
	TokPercent // %
	TokEq      // ==
	TokNe      // !=
	TokLt      // <
	TokLe      // <=
	TokGt      // >
	TokGe      // >=
	TokAnd     // &&
	TokOr      // ||
	TokNot     // !
	TokAssign  // =

	// Delimiters
	TokLParen    // (
	TokRParen    // )
	TokLBrace    // {
	TokRBrace    // }
	TokLBracket  // [
	TokRBracket  // ]
	TokComma     // ,
	TokSemicolon // ;
	TokDot       // .
	TokColon     // :
)

func (TokenType) String

func (t TokenType) String() string

String returns the string representation of a token type.

type TryStmt

type TryStmt struct {
	TryBlock   *BlockStmt
	CatchVar   string // variable name for caught error
	CatchBlock *BlockStmt
}

TryStmt represents a try-catch statement.

func (*TryStmt) String

func (s *TryStmt) String() string

type UnaryExpr

type UnaryExpr struct {
	Op   TokenType
	Expr Expression
}

UnaryExpr represents a unary expression.

func (*UnaryExpr) String

func (e *UnaryExpr) String() string

type UserFunc

type UserFunc struct {
	Params []string
	Body   *BlockStmt
}

UserFunc represents a user-defined function.

type Value

type Value interface{}

Value represents a runtime value.

func Run

func Run(script string, ctx *Context) (Value, error)

Run executes a script.

type ValueObject

type ValueObject interface {
	GetMember(name string) (Value, error)
}

ValueObject interface for objects with members

type VarStmt

type VarStmt struct {
	Name  string
	Value Expression
}

VarStmt represents a variable declaration.

func (*VarStmt) String

func (s *VarStmt) String() string

type WhileStmt

type WhileStmt struct {
	Condition Expression
	Body      *BlockStmt
}

WhileStmt represents a while loop.

func (*WhileStmt) String

func (s *WhileStmt) String() string

Jump to

Keyboard shortcuts

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