Documentation
¶
Index ¶
- Constants
- Variables
- func Evaluator(node Node, env *Environment) value.Value
- func Keywords(kind token.Kind, ident string) value.Value
- type ArrayLiteral
- type AssignmentExpression
- type BlockStatement
- type Bytecode
- type CallExpression
- type Compiler
- type DeferStatement
- type DestructMode
- type Environment
- func (e *Environment) All() map[string]value.Value
- func (e *Environment) Get(name string) (value.Value, bool)
- func (e *Environment) Outer() *Environment
- func (e *Environment) Reset()
- func (e *Environment) Set(name string, val value.Value) value.Value
- func (e *Environment) SetOuter(outer *Environment)
- func (e *Environment) Store() map[string]value.Value
- type Expression
- type ExpressionStatement
- type ForStatement
- type FunctionLiteral
- type Identifier
- type IfExpression
- type IndexExpression
- type InfixExpression
- type Lexer
- type Literal
- type MemberExpression
- type MethodCallExpression
- type Node
- type ObjectEntry
- type ObjectLiteral
- type ParameterList
- type Parser
- type PrefixExpression
- type Program
- type ReturnStatement
- type SpawnStatement
- type SpreadExpression
- type Statement
- type TemplateLiteral
- type VarStatement
Constants ¶
const ( LOWEST int ASSIGN // = OR // || AND // && EQUALS // == LESSGREATER // > hoặc < SUM // + PRODUCT // * PREFIX // -X hoặc !X CALL // f(x) INDEX // a[0] MEMBER // a.b ARROW // => )
Bảng độ ưu tiên
Variables ¶
var (
NULL_VAL = value.NULL
)
Hằng số nội bộ để tối ưu hiệu năng
Functions ¶
Types ¶
type ArrayLiteral ¶
type ArrayLiteral struct {
Token token.Token
Elements []Expression
}
ArrayLiteral: [1, 2, 3]
func (*ArrayLiteral) String ¶
func (al *ArrayLiteral) String() string
type AssignmentExpression ¶
type AssignmentExpression struct {
Token token.Token
Name Expression // Thường là Identifier hoặc IndexExpression
Value Expression
}
AssignmentExpression: a = 10
func (*AssignmentExpression) String ¶
func (ae *AssignmentExpression) String() string
type BlockStatement ¶
BlockStatement: Code nằm trong dấu { }
func (*BlockStatement) String ¶
func (bs *BlockStatement) String() string
type CallExpression ¶
type CallExpression struct {
Token token.Token // Dấu '('
Function Expression // Tên hàm hoặc object.method
Arguments []Expression
}
CallExpression: f(a, b)
func (*CallExpression) String ¶
func (ce *CallExpression) String() string
type Compiler ¶
type Compiler struct {
// contains filtered or unexported fields
}
Compiler chịu trách nhiệm chuyển đổi AST thành Bytecode
func NewCompiler ¶
func NewCompiler() *Compiler
func (*Compiler) ByteCodeResult ¶
type DeferStatement ¶
type DeferStatement struct {
Token token.Token // Dấu 'defer'
Fn Expression
}
DeferStatement: defer () => { }
func (*DeferStatement) String ¶
func (ds *DeferStatement) String() string
type DestructMode ¶
type DestructMode int
const ( DestructNone DestructMode = 0 DestructObject DestructMode = 1 DestructArray DestructMode = 2 )
type Environment ¶
type Environment struct {
// contains filtered or unexported fields
}
func NewEnclosedEnvironment ¶
func NewEnclosedEnvironment(outer *Environment) *Environment
Tạo Scope con (dùng cho hàm hoặc block {})
func NewEnvironment ¶
func NewEnvironment() *Environment
func (*Environment) All ¶
func (e *Environment) All() map[string]value.Value
All trả về tất cả các biến (Warning: slow, copies data)
func (*Environment) Reset ¶
func (e *Environment) Reset()
func (*Environment) SetOuter ¶
func (e *Environment) SetOuter(outer *Environment)
type Expression ¶
type Expression interface {
Node
// contains filtered or unexported methods
}
Expression đại diện cho các biểu thức (trả về giá trị)
type ExpressionStatement ¶
type ExpressionStatement struct {
Token token.Token
Expression Expression
}
ExpressionStatement: Dùng cho các lệnh đứng độc lập (vd: call();)
func (*ExpressionStatement) String ¶
func (es *ExpressionStatement) String() string
type ForStatement ¶
type ForStatement struct {
Token token.Token // Dấu 'for'
Item *Identifier
Iterable Expression
Body *BlockStatement
}
ForStatement: for (item in list) { }
func (*ForStatement) String ¶
func (fs *ForStatement) String() string
type FunctionLiteral ¶
type FunctionLiteral struct {
Token token.Token
Parameters []*Identifier
Body *BlockStatement
Address int // Compiled bytecode address
}
FunctionLiteral: (x, y) => { }
func (*FunctionLiteral) String ¶
func (fl *FunctionLiteral) String() string
type Identifier ¶
Identifier: Tên biến (db, user, task)
func (*Identifier) String ¶
func (i *Identifier) String() string
type IfExpression ¶
type IfExpression struct {
Token token.Token
Condition Expression
Consequence *BlockStatement
Alternative *BlockStatement
}
IfExpression: if (cond) { con } else { alt }
func (*IfExpression) String ¶
func (ie *IfExpression) String() string
type IndexExpression ¶
type IndexExpression struct {
Token token.Token // Dấu '['
Left Expression
Index Expression
}
IndexExpression: array[index]
func (*IndexExpression) String ¶
func (ie *IndexExpression) String() string
type InfixExpression ¶
type InfixExpression struct {
Token token.Token
Left Expression
Operator string
Right Expression
}
InfixExpression: a + b, x > y, a == b
func (*InfixExpression) String ¶
func (ie *InfixExpression) String() string
type MemberExpression ¶
type MemberExpression struct {
Token token.Token // Dấu '.'
Object Expression
Property *Identifier
}
MemberExpression: object.property
func (*MemberExpression) String ¶
func (me *MemberExpression) String() string
type MethodCallExpression ¶
type MethodCallExpression struct {
Token token.Token // Dấu '.'
Object Expression // Đối tượng (ví dụ: "hello")
Method *Identifier // Tên phương thức (ví dụ: upper)
Arguments []Expression // Các tham số truyền vào
}
MethodCallExpression: object.method(args...)
func (*MethodCallExpression) String ¶
func (mce *MethodCallExpression) String() string
type Node ¶
type Node interface {
String() string
}
Node là interface gốc cho mọi thành phần trong cây
type ObjectEntry ¶ added in v0.1.1
type ObjectEntry struct {
Key Expression
Value Expression
IsSpread bool
}
ObjectEntry đại diện cho một phần tử trong Object (cặp key-value hoặc spread)
type ObjectLiteral ¶
type ObjectLiteral struct {
Token token.Token
Entries []ObjectEntry
}
ObjectLiteral: { "key": "value", ...obj }
func (*ObjectLiteral) String ¶
func (ol *ObjectLiteral) String() string
type ParameterList ¶
type ParameterList struct {
Token token.Token
Parameters []*Identifier
}
ParameterList: Dùng tạm để chứa danh sách tham số trước khi định nghĩa Lambda
func (*ParameterList) String ¶
func (pl *ParameterList) String() string
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
func (*Parser) ParseProgram ¶
type PrefixExpression ¶
type PrefixExpression struct {
Token token.Token
Operator string
Right Expression
}
PrefixExpression: !true, -5
func (*PrefixExpression) String ¶
func (pe *PrefixExpression) String() string
type Program ¶
type Program struct {
Statements []Statement
}
Program là nút gốc chứa toàn bộ script
type ReturnStatement ¶
type ReturnStatement struct {
Token token.Token
ReturnValue Expression
}
ReturnStatement: return 10;
func (*ReturnStatement) String ¶
func (rs *ReturnStatement) String() string
type SpawnStatement ¶
type SpawnStatement struct {
Token token.Token // Dấu 'go'
Fn Expression
}
SpawnStatement: go () => { }
func (*SpawnStatement) String ¶
func (ss *SpawnStatement) String() string
type SpreadExpression ¶ added in v0.1.1
type SpreadExpression struct {
Token token.Token // Dấu '...'
Value Expression
}
SpreadExpression: ...obj
func (*SpreadExpression) String ¶ added in v0.1.1
func (se *SpreadExpression) String() string
type Statement ¶
type Statement interface {
Node
// contains filtered or unexported methods
}
Statement đại diện cho các câu lệnh (không trả về giá trị trực tiếp)
type TemplateLiteral ¶ added in v0.1.1
type TemplateLiteral struct {
Token token.Token
Parts []Expression // Alternating Literal (strings) and Expressions
}
TemplateLiteral: `Hello ${user.name}`
func (*TemplateLiteral) String ¶ added in v0.1.1
func (tl *TemplateLiteral) String() string
type VarStatement ¶
type VarStatement struct {
Token token.Token // const, let
Names []*Identifier
Value Expression
DestructMode DestructMode
}
VarStatement: const a = 10; let b = 20;
func (*VarStatement) String ¶
func (vs *VarStatement) String() string