gogen

package module
v0.0.0-...-f497ca0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2023 License: MIT Imports: 6 Imported by: 8

README

gogen

A simplification of Go's go/ast package that allows for some interesting code generation. Currently very rough.

Examples

Hello World

package main

import (
	"os"
	. "github.com/garslo/gogen"
)

func main() {
	pkg := Package{Name: "main"}
	pkg.Declare(Import{"fmt"})
	pkg.Declare(Function{
		Name: "main",
		Body: []Statement{
			CallFunction{
				Func:   Dotted{Var{"fmt"}, "Println"},
				Params: []Expression{Var{`"Hello World!"`}},
			},
		},
	})
	pkg.WriteTo(os.Stdout)
}

Output:

package main

import "fmt"

func main() {
	fmt.Println("Hello World!")
}

More

See the examples directory for more examples and a build/run script.

$ ./run-example.sh for_loop.go
CODE:
package main

import "os"
import "fmt"

func main() {
	var i int
	for i = 0; i <= 10; i++ {
		fmt.Println(i)
	}
	os.Exit(i)
}
RUN RESULT:
0
1
2
3
4
5
6
7
8
9
10
exit status 11

Documentation

Index

Constants

View Source
const (
	ArchiveTar        = "archive/tar"
	ArchiveZip        = "archive/zip"
	Bufio             = "bufio"
	Builtin           = "builtin"
	Bytes             = "bytes"
	CompressBzip2     = "compress/bzip2"
	CompressFlate     = "compress/flate"
	CompressGzip      = "compress/gzip"
	CompressLzw       = "compress/lzw"
	CompressZlib      = "compress/zlib"
	ContainerHeap     = "container/heap"
	ContainerList     = "container/list"
	ContainerRing     = "container/ring"
	Crypto            = "crypto"
	CryptoAes         = "crypto/aes"
	CryptoCipher      = "crypto/cipher"
	CryptoDes         = "crypto/des"
	CryptoDsa         = "crypto/dsa"
	CryptoEcdsa       = "crypto/ecdsa"
	CryptoElliptic    = "crypto/elliptic"
	CryptoHmac        = "crypto/hmac"
	CryptoMd5         = "crypto/md5"
	CryptoRand        = "crypto/rand"
	CryptoRc4         = "crypto/rc4"
	CryptoRsa         = "crypto/rsa"
	CryptoSha1        = "crypto/sha1"
	CryptoSha256      = "crypto/sha256"
	CryptoSha512      = "crypto/sha512"
	CryptoSubtle      = "crypto/subtle"
	CryptoTls         = "crypto/tls"
	CryptoX509        = "crypto/x509"
	CryptoX509Pkix    = "crypto/x509/pkix"
	DatabaseSql       = "database/sql"
	DatabaseSqlDriver = "database/sql/driver"
	DebugDwarf        = "debug/dwarf"
	DebugElf          = "debug/elf"
	DebugGosym        = "debug/gosym"
	DebugMacho        = "debug/macho"
	DebugPe           = "debug/pe"
	DebugPlan9obj     = "debug/plan9obj"
	Encoding          = "encoding"
	EncodingAscii85   = "encoding/ascii85"
	EncodingAsn1      = "encoding/asn1"
	EncodingBase32    = "encoding/base32"
	EncodingBase64    = "encoding/base64"
	EncodingBinary    = "encoding/binary"
	EncodingCsv       = "encoding/csv"
	EncodingGob       = "encoding/gob"
	EncodingHex       = "encoding/hex"
	EncodingJson      = "encoding/json"
	EncodingPem       = "encoding/pem"
	EncodingXml       = "encoding/xml"
	Errors            = "errors"
	Expvar            = "expvar"
	Flag              = "flag"
	Fmt               = "fmt"
	GoAst             = "go/ast"
	GoBuild           = "go/build"
	GoDoc             = "go/doc"
	GoFormat          = "go/format"
	GoParser          = "go/parser"
	GoPrinter         = "go/printer"
	GoScanner         = "go/scanner"
	GoToken           = "go/token"
	Hash              = "hash"
	HashAdler32       = "hash/adler32"
	HashCrc32         = "hash/crc32"
	HashCrc64         = "hash/crc64"
	HashFnv           = "hash/fnv"
	Html              = "html"
	HtmlTemplate      = "html/template"
	Image             = "image"
	ImageColor        = "image/color"
	ImageColorPalette = "image/color/palette"
	ImageDraw         = "image/draw"
	ImageGif          = "image/gif"
	ImageJpeg         = "image/jpeg"
	ImagePng          = "image/png"
	IndexSuffixarray  = "index/suffixarray"
	InternalSyscall   = "internal/syscall"
	Io                = "io"
	IoIoutil          = "io/ioutil"
	Log               = "Log"
	LogSyslog         = "log/syslog"
	Math              = "math"
	MathBig           = "math/big"
	MathCmplx         = "math/cmplx"
	MathRand          = "math/rand"
	Mime              = "mime"
	MimeMultipart     = "mime/multipart"
	Net               = "net"
	NetHttp           = "net/http"
	NetHttpCgi        = "net/http/cgi"
	NetHttpCookiejar  = "net/http/cookiejar"
	NetHttpFcgi       = "net/http/fcgi"
	NetHttpHttptest   = "net/http/httptest"
	NetHttpHttputil   = "net/http/httputil"
	NetHttpInternal   = "net/http/internal"
	NetHttpPprof      = "net/http/pprof"
	NetMail           = "net/mail"
	NetRpc            = "net/rpc"
	NetRpcJsonrpc     = "net/rpc/jsonrpc"
	NetSmtp           = "net/smtp"
	NetTextproto      = "net/textproto"
	NetUrl            = "net/url"
	Os                = "os"
	OsExec            = "os/exec"
	OsSignal          = "os/signal"
	OsUser            = "os/user"
	Path              = "path"
	PathFilepath      = "path/filepath"
	Reflect           = "reflect"
	Regexp            = "regexp"
	RegexpSyntax      = "regexp/syntax"
	Runtime           = "runtime"
	RuntimeCgo        = "runtime/cgo"
	RuntimeDebug      = "runtime/debug"
	RuntimePprof      = "runtime/pprof"
	RuntimeRace       = "runtime/race"
	Sort              = "sort"
	Strconv           = "strconv"
	Strings           = "strings"
	Sync              = "sync"
	SyncAtomic        = "sync/atomic"
	Syscall           = "syscall"
	Testing           = "testing"
	TestingIotest     = "testing/iotest"
	TestingQuick      = "testing/quick"
	TextScanner       = "text/scanner"
	TextTabwriter     = "text/tabwriter"
	TextTemplate      = "text/template"
	TextTemplateParse = "text/template/parse"
	Time              = "time"
	Unicode           = "unicode"
	UnicodeUtf16      = "unicode/utf16"
	UnicodeUtf8       = "unicode/utf8"
	Unsafe            = "unsafe"
)
View Source
const (
	StringT = "string"
	IntT    = "int"
)

Variables

View Source
var BuiltinTypes = [...]string{
	"int",
	"int8",
	"int16",
	"int32",
	"int64",
	"uint",
	"uint8",
	"uint16",
	"uint32",
	"uint64",
	"uintptr",
	"float",
	"float32",
	"float64",
	"string",
	"bool",
	"byte",
	"complex128",
	"complex64",
	"error",
	"rune",
}

Functions

This section is empty.

Types

type AddressOf

type AddressOf struct {
	Value Expression
}

func (AddressOf) Expression

func (me AddressOf) Expression() ast.Expr

type Assign

type Assign struct {
	Lhs Expression
	Rhs Expression
}

func (Assign) Statement

func (me Assign) Statement() ast.Stmt

type CallFunction

type CallFunction struct {
	Func   Expression
	Params []Expression
}

func (CallFunction) Expression

func (me CallFunction) Expression() ast.Expr

func (CallFunction) Statement

func (me CallFunction) Statement() ast.Stmt

type Declaration

type Declaration interface {
	Declaration() ast.Decl
}

type Declare

type Declare struct {
	Name     string
	TypeName string
}

func (Declare) Statement

func (me Declare) Statement() ast.Stmt

type DeclareAndAssign

type DeclareAndAssign struct {
	Lhs Expression
	Rhs Expression
}

func (DeclareAndAssign) Statement

func (me DeclareAndAssign) Statement() ast.Stmt

type Decrement

type Decrement struct {
	Value Expression
}

func (Decrement) Statement

func (me Decrement) Statement() ast.Stmt

type Dotted

type Dotted struct {
	Receiver Expression
	Name     string
}

func (Dotted) Expression

func (me Dotted) Expression() ast.Expr

type Equals

type Equals struct {
	Lhs Expression
	Rhs Expression
}

func (Equals) Expression

func (me Equals) Expression() ast.Expr

type Expression

type Expression interface {
	Expression() ast.Expr
}

type Field

type Field struct {
	Name     string
	TypeName string
	Tag      string
}

func (Field) Ast

func (me Field) Ast() *ast.Field

type Fields

type Fields []Field

func (Fields) Ast

func (me Fields) Ast() *ast.FieldList

type For

type For struct {
	Init      Statement
	Condition Expression
	Post      Statement
	Body      []Statement
}

func (For) Statement

func (me For) Statement() ast.Stmt

type Function

type Function struct {
	Receiver    Receiver
	Name        string
	ReturnTypes Types
	Parameters  Types
	Body        []Statement
}

func (Function) Call

func (me Function) Call(params ...Expression) CallFunction

func (Function) Declaration

func (me Function) Declaration() ast.Decl

func (Function) Declare

func (me Function) Declare(pkg *Package) Function

type Functions

type Functions []Function

func (*Functions) Add

func (me *Functions) Add(fn Function)

type Functor

type Functor struct {
	Func Expression
}

TODO: Bad name, change it

func (Functor) Call

func (me Functor) Call(params ...Expression) CallFunction

type GreaterThan

type GreaterThan struct {
	Lhs Expression
	Rhs Expression
}

func (GreaterThan) Expression

func (me GreaterThan) Expression() ast.Expr

type GreaterThanOrEqual

type GreaterThanOrEqual struct {
	Lhs Expression
	Rhs Expression
}

func (GreaterThanOrEqual) Expression

func (me GreaterThanOrEqual) Expression() ast.Expr

type If

type If struct {
	Init      Statement
	Condition Expression
	Body      []Statement
}

func (If) Statement

func (me If) Statement() ast.Stmt

type Import

type Import struct {
	Name string
}

func (Import) Declaration

func (me Import) Declaration() ast.Decl

type Imports

type Imports []Import

func (*Imports) Add

func (me *Imports) Add(imp Import)

type Increment

type Increment struct {
	Value Expression
}

func (Increment) Statement

func (me Increment) Statement() ast.Stmt

type Index

type Index struct {
	Value, Index Expression
}

func (Index) Expression

func (me Index) Expression() ast.Expr

type LessThan

type LessThan struct {
	Lhs Expression
	Rhs Expression
}

func (LessThan) Expression

func (me LessThan) Expression() ast.Expr

type LessThanOrEqual

type LessThanOrEqual struct {
	Lhs Expression
	Rhs Expression
}

func (LessThanOrEqual) Expression

func (me LessThanOrEqual) Expression() ast.Expr

type Not

type Not struct {
	Value Expression
}

func (Not) Expression

func (me Not) Expression() ast.Expr

type NotEqual

type NotEqual struct {
	Lhs Expression
	Rhs Expression
}

func (NotEqual) Expression

func (me NotEqual) Expression() ast.Expr

type Package

type Package struct {
	Name         string
	Declarations []Declaration
}

func (*Package) Ast

func (me *Package) Ast() ast.Node

func (*Package) Declare

func (me *Package) Declare(decl Declaration) *Package

func (*Package) WriteTo

func (me *Package) WriteTo(w io.Writer) error

type Range

type Range struct {
	Key          Expression
	Value        Expression
	RangeValue   Expression
	Body         []Statement
	DoNotDeclare bool
}

func (Range) Statement

func (me Range) Statement() ast.Stmt

type Receiver

type Receiver struct {
	Name string
	Type Expression
}

func (Receiver) Ast

func (me Receiver) Ast() *ast.FieldList

type Return

type Return struct {
	Values []Expression
}

func (Return) Statement

func (me Return) Statement() ast.Stmt

type Star

type Star struct {
	Value Expression
}

func (Star) Expression

func (me Star) Expression() ast.Expr

type Statement

type Statement interface {
	Statement() ast.Stmt
}

type String

type String struct {
	Value string
}

func (String) Expression

func (me String) Expression() ast.Expr

type Struct

type Struct struct {
	Name        string
	Fields      Fields
	Methods     Functions
	FieldValues map[string]Expression
}

func (Struct) Declaration

func (me Struct) Declaration() ast.Decl

func (Struct) Expression

func (me Struct) Expression() ast.Expr

func (Struct) WithValues

func (me Struct) WithValues(vals map[string]Expression) Struct

type Structs

type Structs []Struct

func (*Structs) Add

func (me *Structs) Add(st Struct)

type Thunk

type Thunk struct {
	Expr ast.Expr
	Stmt ast.Stmt
	Decl ast.Decl
}

func (Thunk) Declaration

func (me Thunk) Declaration() ast.Decl

func (Thunk) Expression

func (me Thunk) Expression() ast.Expr

func (Thunk) Statement

func (me Thunk) Statement() ast.Stmt

type Type

type Type struct {
	Name        string // Optional, named type
	TypeName    string
	PackageName string // Optional
}

type Types

type Types []Type

type Var

type Var struct {
	Name string
}

func Int

func Int(value int) Var

func Name

func Name(value string) Var

func Pkg

func Pkg(value string) Var

func (Var) Expression

func (me Var) Expression() ast.Expr

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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