transpiler

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Copyright 2025 The Hulo Authors. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright 2025 The Hulo Authors. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright 2025 The Hulo Authors. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright 2025 The Hulo Authors. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Token

func Token(tok htok.Token) btok.Token

func Transpile

func Transpile(opts *config.Huloc, vfs vfs.VFS, basePath string) (map[string]string, error)

Types

type BashTranspiler

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

func NewBashTranspiler

func NewBashTranspiler(opts *config.Huloc, vfs vfs.VFS) *BashTranspiler

func (*BashTranspiler) Convert

func (b *BashTranspiler) Convert(node hast.Node) bast.Node

func (*BashTranspiler) ConvertArrayLiteralExpr

func (b *BashTranspiler) ConvertArrayLiteralExpr(node *hast.ArrayLiteralExpr) bast.Node

ConvertArrayLiteralExpr 转换数组字面量表达式

func (*BashTranspiler) ConvertAssignStmt

func (b *BashTranspiler) ConvertAssignStmt(node *hast.AssignStmt) bast.Node

func (*BashTranspiler) ConvertBinaryExpr

func (b *BashTranspiler) ConvertBinaryExpr(node *hast.BinaryExpr) bast.Node

func (*BashTranspiler) ConvertBlockStmt

func (b *BashTranspiler) ConvertBlockStmt(node *hast.BlockStmt) bast.Node

func (*BashTranspiler) ConvertCallExpr

func (b *BashTranspiler) ConvertCallExpr(node *hast.CallExpr) bast.Node

func (*BashTranspiler) ConvertClassDecl

func (b *BashTranspiler) ConvertClassDecl(node *hast.ClassDecl) bast.Node

func (*BashTranspiler) ConvertCmdExpr

func (b *BashTranspiler) ConvertCmdExpr(node *hast.CmdExpr) bast.Node

func (*BashTranspiler) ConvertComment

func (b *BashTranspiler) ConvertComment(node *hast.Comment) bast.Node

func (*BashTranspiler) ConvertCommentGroup

func (b *BashTranspiler) ConvertCommentGroup(node *hast.CommentGroup) bast.Node

func (*BashTranspiler) ConvertDeclareDecl

func (b *BashTranspiler) ConvertDeclareDecl(node *hast.DeclareDecl) bast.Node

func (*BashTranspiler) ConvertDoWhileStmt

func (b *BashTranspiler) ConvertDoWhileStmt(node *hast.DoWhileStmt) bast.Node

ConvertDoWhileStmt 将 do-while 语句转换为 while 循环

func (*BashTranspiler) ConvertExprStmt

func (b *BashTranspiler) ConvertExprStmt(node *hast.ExprStmt) bast.Node

func (*BashTranspiler) ConvertFalseLiteral

func (b *BashTranspiler) ConvertFalseLiteral(node *hast.FalseLiteral) bast.Node

func (*BashTranspiler) ConvertFile

func (b *BashTranspiler) ConvertFile(node *hast.File) bast.Node

func (*BashTranspiler) ConvertForInStmt

func (b *BashTranspiler) ConvertForInStmt(node *hast.ForInStmt) bast.Node

ConvertForInStmt 转换 for-in 循环

func (*BashTranspiler) ConvertForOfStmt

func (b *BashTranspiler) ConvertForOfStmt(node *hast.ForeachStmt) bast.Node

func (*BashTranspiler) ConvertForStmt

func (b *BashTranspiler) ConvertForStmt(node *hast.ForStmt) bast.Node

func (*BashTranspiler) ConvertForeachStmt

func (b *BashTranspiler) ConvertForeachStmt(node *hast.ForeachStmt) bast.Node

ConvertForeachStmt 转换 foreach 循环

func (*BashTranspiler) ConvertFuncDecl

func (b *BashTranspiler) ConvertFuncDecl(node *hast.FuncDecl) bast.Node

func (*BashTranspiler) ConvertIdent

func (b *BashTranspiler) ConvertIdent(node *hast.Ident) bast.Node

func (*BashTranspiler) ConvertIfStmt

func (b *BashTranspiler) ConvertIfStmt(node *hast.IfStmt) bast.Node

func (*BashTranspiler) ConvertImport

func (b *BashTranspiler) ConvertImport(node *hast.Import) bast.Node

func (*BashTranspiler) ConvertIncDecExpr

func (b *BashTranspiler) ConvertIncDecExpr(node *hast.IncDecExpr) bast.Node

func (*BashTranspiler) ConvertMatchStmt

func (b *BashTranspiler) ConvertMatchStmt(node *hast.MatchStmt) bast.Node

ConvertMatchStmt 将 match 语句转换为 case 语句

func (*BashTranspiler) ConvertNumericLiteral

func (b *BashTranspiler) ConvertNumericLiteral(node *hast.NumericLiteral) bast.Node

func (*BashTranspiler) ConvertObjectLiteralExpr

func (b *BashTranspiler) ConvertObjectLiteralExpr(node *hast.ObjectLiteralExpr) bast.Node

ConvertObjectLiteralExpr 转换对象字面量表达式

func (*BashTranspiler) ConvertParameter

func (b *BashTranspiler) ConvertParameter(node *hast.Parameter) bast.Node

ConvertParameter 处理函数参数

func (*BashTranspiler) ConvertRefExpr

func (b *BashTranspiler) ConvertRefExpr(node *hast.RefExpr) bast.Node

ConvertRefExpr 处理引用表达式 $x

func (*BashTranspiler) ConvertReturnStmt

func (b *BashTranspiler) ConvertReturnStmt(node *hast.ReturnStmt) bast.Node

func (*BashTranspiler) ConvertSelectExpr

func (b *BashTranspiler) ConvertSelectExpr(node *hast.SelectExpr) bast.Node

ConvertSelectExpr 处理选择表达式,区分模块访问和对象方法调用

func (*BashTranspiler) ConvertStringLiteral

func (b *BashTranspiler) ConvertStringLiteral(node *hast.StringLiteral) bast.Node

func (*BashTranspiler) ConvertTrueLiteral

func (b *BashTranspiler) ConvertTrueLiteral(node *hast.TrueLiteral) bast.Node

func (*BashTranspiler) ConvertWhileStmt

func (b *BashTranspiler) ConvertWhileStmt(node *hast.WhileStmt) bast.Node

func (*BashTranspiler) Emit

func (b *BashTranspiler) Emit(stmt bast.Stmt)

func (*BashTranspiler) Flush

func (v *BashTranspiler) Flush() []bast.Stmt

func (*BashTranspiler) Transpile

func (b *BashTranspiler) Transpile(mainFile string) (map[string]string, error)

type CompileRuleFunc

type CompileRuleFunc func(node hast.Node) (bast.Node, error)

type DependencyResolver

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

type ExportInfo

type ExportInfo struct {
	Name   string
	Value  hast.Node
	Kind   ExportKind
	Public bool
}

type ExportKind

type ExportKind int

Function, Class, Constant, etc.

const (
	ExportFunction ExportKind = iota
	ExportClass
	ExportConstant
	ExportVariable
)

type HCRDispatcher

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

func (*HCRDispatcher) Get

func (d *HCRDispatcher) Get(ruleName string) (transpiler.Strategy[bast.Node], error)

func (*HCRDispatcher) Put

func (d *HCRDispatcher) Put(fullRule string, cb transpiler.Strategy[bast.Node]) error

type ImportInfo

type ImportInfo struct {
	ModulePath string
	SymbolName []string
	Alias      string
	Kind       ImportKind
}

type ImportKind

type ImportKind int

Named, All, Default

const (
	ImportSingle ImportKind = iota
	ImportMulti
	ImportAll
)

type Module

type Module struct {
	Name         string
	Path         string
	AST          *hast.File
	Exports      map[string]*ExportInfo
	Imports      []*ImportInfo
	Dependencies []string
	Transpiled   *bast.File
	Symbols      *SymbolTable
}

type ModuleManager

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

func (*ModuleManager) GetModule

func (mm *ModuleManager) GetModule(path string) *Module

func (*ModuleManager) ResolveAllDependencies

func (mm *ModuleManager) ResolveAllDependencies(mainFile string) ([]*Module, error)

type Scope

type Scope struct {
	ID        string             // 作用域唯一标识
	Type      ScopeType          // 作用域类型
	Variables map[string]*Symbol // 变量名 -> 符号
	Parent    *Scope             // 父作用域
	Module    *Module            // 所属模块
}

type ScopeStack

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

func (*ScopeStack) Current

func (ss *ScopeStack) Current() *Scope

func (*ScopeStack) Pop

func (ss *ScopeStack) Pop()

func (*ScopeStack) Push

func (ss *ScopeStack) Push(scope *Scope)

type ScopeType

type ScopeType int
const (
	GlobalScope ScopeType = iota
	FunctionScope
	ClassScope
	BlockScope
)

type Symbol

type Symbol struct {
	Name        string     // 原始名称
	MangledName string     // 混淆后的名称
	Type        SymbolType // 符号类型
	Value       hast.Node  // 原始AST节点
	Scope       *Scope     // 所属作用域
	Module      *Module    // 所属模块
	IsExported  bool       // 是否导出
}

type SymbolTable

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

func NewSymbolTable

func NewSymbolTable(moduleName string, enableMangle bool) *SymbolTable

NewSymbolTable 创建新的符号表

func (*SymbolTable) AddClass

func (st *SymbolTable) AddClass(name string, node hast.Node)

AddClass 添加类

func (*SymbolTable) AddConstant

func (st *SymbolTable) AddConstant(name string, node hast.Node)

AddConstant 添加常量

func (*SymbolTable) AddFunction

func (st *SymbolTable) AddFunction(name string, node hast.Node)

AddFunction 添加函数

func (*SymbolTable) AddSymbol

func (st *SymbolTable) AddSymbol(symbol *Symbol)

添加符号到当前作用域

func (*SymbolTable) AddVariable

func (st *SymbolTable) AddVariable(name string, node hast.Node)

AddVariable 添加变量

func (*SymbolTable) AllocVariableName

func (st *SymbolTable) AllocVariableName(originalName string, scope *Scope) string

变量分配策略

func (*SymbolTable) CurrentScope

func (st *SymbolTable) CurrentScope() *Scope

func (*SymbolTable) HasClass

func (st *SymbolTable) HasClass(name string) bool

HasClass 检查是否包含类

func (*SymbolTable) HasConstant

func (st *SymbolTable) HasConstant(name string) bool

HasConstant 检查是否包含常量

func (*SymbolTable) HasFunction

func (st *SymbolTable) HasFunction(name string) bool

HasFunction 检查是否包含函数

func (*SymbolTable) HasVariable

func (st *SymbolTable) HasVariable(name string) bool

HasVariable 检查是否包含变量

func (*SymbolTable) LookupSymbol

func (st *SymbolTable) LookupSymbol(name string) *Symbol

符号查找

func (*SymbolTable) PopScope

func (st *SymbolTable) PopScope()

func (*SymbolTable) PushScope

func (st *SymbolTable) PushScope(scopeType ScopeType, module *Module) *Scope

作用域管理方法

type SymbolType

type SymbolType int
const (
	SymbolVariable SymbolType = iota
	SymbolFunction
	SymbolClass
	SymbolConstant
	SymbolArray
	SymbolObject
)

Directories

Path Synopsis
codegen

Jump to

Keyboard shortcuts

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