codegen

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FuncDefaults

type FuncDefaults struct {
	ParamNames    []string         // Parameter names in order
	DefaultValues []ast.Expression // Default values (nil if no default)
	HasVariadic   bool             // Whether the last parameter is variadic
}

Generator generates Go code from an AST.

ARCHITECTURE NOTE: Kukicha uses placeholders like "any" and "any2" in stdlib function signatures to represent generic type parameters. When generating Go code, we detect these placeholders and emit proper Go generics (e.g., [T any, K comparable]). This allows Kukicha users to write simple code while getting type-safe Go generics.

The isStdlibIter and placeholderMap fields work together:

  • isStdlibIter is true when generating stdlib/iterator or stdlib/slice code
  • placeholderMap maps Kukicha placeholders ("any", "any2") to Go type params ("T", "K")
  • During type annotation generation, we substitute placeholders with type params

This design keeps Kukicha's "beginner-friendly" goal: users don't write generic syntax, but the generated Go code is fully type-safe with proper generic constraints. FuncDefaults stores information about a function's default parameter values

type Generator

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

func New

func New(program *ast.Program) *Generator

New creates a new code generator

func (*Generator) Generate

func (g *Generator) Generate() (string, error)

Generate generates Go code from the AST

func (*Generator) SetExprReturnCounts added in v0.0.2

func (g *Generator) SetExprReturnCounts(counts map[ast.Expression]int)

SetExprReturnCounts passes semantic analysis return counts to the generator.

func (*Generator) SetMCPTarget added in v0.0.5

func (g *Generator) SetMCPTarget(v bool)

SetMCPTarget enables special codegen for MCP servers (e.g., print to stderr)

func (*Generator) SetSourceFile

func (g *Generator) SetSourceFile(path string)

SetSourceFile sets the source file path and detects if special transpilation is needed

func (*Generator) SetStdlibModule added in v0.0.6

func (g *Generator) SetStdlibModule(base string)

SetStdlibModule overrides the base module path used when rewriting "stdlib/X" imports to full Go module paths. The default is "github.com/duber000/kukicha". Set this when building a fork or vendoring the kukicha stdlib under a different module name.

type TypeParameter

type TypeParameter struct {
	Name        string // Generated name: T, U, V, etc.
	Placeholder string // Original placeholder: "any", "any2", etc.
	Constraint  string // "any", "comparable", "cmp.Ordered"
}

TypeParameter represents a type parameter for stdlib special transpilation This is internal to codegen and separate from the removed ast.TypeParameter

Jump to

Keyboard shortcuts

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