compiler

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: May 29, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
var (
	NULL_VAL = value.NULL
)

Hằng số nội bộ để tối ưu hiệu năng

Functions

func Evaluator

func Evaluator(node Node, env *Environment) value.Value

Evaluator là hàm thực thi chính, duyệt qua các nút của cây AST

func Keywords

func Keywords(kind token.Kind, ident string) value.Value

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

type BlockStatement struct {
	Token      token.Token // Dấu '{'
	Statements []Statement
}

BlockStatement: Code nằm trong dấu { }

func (*BlockStatement) String

func (bs *BlockStatement) String() string

type Bytecode

type Bytecode struct {
	Instructions []byte
	Constants    []value.Value
	SourceMap    []int32
}

Bytecode đại diện cho kết quả sau khi biên dịch

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(source ...string) *Compiler

func (*Compiler) ByteCodeResult

func (c *Compiler) ByteCodeResult() *Bytecode

func (*Compiler) Compile

func (c *Compiler) Compile(node Node) error

Compile bắt đầu quá trình duyệt cây và phát sinh mã

func (*Compiler) Reset

func (c *Compiler) Reset()

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) Get

func (e *Environment) Get(name string) (value.Value, bool)

func (*Environment) Outer

func (e *Environment) Outer() *Environment

Outer trả về environment cha

func (*Environment) Reset

func (e *Environment) Reset()

func (*Environment) Set

func (e *Environment) Set(name string, val value.Value) value.Value

func (*Environment) SetOuter

func (e *Environment) SetOuter(outer *Environment)

func (*Environment) Store

func (e *Environment) Store() map[string]value.Value

Store trả về map lưu trữ trực tiếp của environment này (zero-copy)

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

type Identifier struct {
	Token token.Token
	Value string
}

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 Lexer

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

func NewLexer

func NewLexer(input string) *Lexer

func (*Lexer) NextToken

func (l *Lexer) NextToken() token.Token

type Literal

type Literal struct {
	Token token.Token
	Value value.Value // Thùng 24-byte
}

Literal: Giá trị thô (10, "hello", true, null)

func (*Literal) String

func (l *Literal) 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 NewParser

func NewParser(l *Lexer) *Parser

func (*Parser) Errors

func (p *Parser) Errors() []string

func (*Parser) ParseProgram

func (p *Parser) ParseProgram() *Program

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

func (*Program) String

func (p *Program) String() string

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

Jump to

Keyboard shortcuts

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