go-zig-compiler/internal/zig/asthelpers.go

137 lines
3.5 KiB
Go

package zig
// Id creates an identifier expression.
func Id(name string) *Identifier {
return &Identifier{Name: name}
}
// Lit creates a literal expression.
func Lit(kind, value string) *Literal {
return &Literal{Kind: kind, Value: value}
}
// FieldAccess creates a field access expression.
func FieldAccess(recv Expr, field string) *FieldAccessExpr {
return &FieldAccessExpr{Receiver: recv, Field: field}
}
// Call creates a function call expression.
func Call(fun Expr, args ...Expr) *CallExpr {
return &CallExpr{Fun: fun, Args: args}
}
// InitList creates an initializer list expression.
func InitList(exprs ...Expr) *InitListExpr {
if len(exprs) == 0 {
return &InitListExpr{Empty: true}
}
return &InitListExpr{Values: exprs}
}
// DeclareFn creates a function declaration with flags.
func DeclareFn(name string, retType TypeExpr, body *Block, params []*ParamDecl, flags FnFlags) *FnDecl {
return &FnDecl{
Name: name,
ReturnType: retType,
Body: body,
Flags: flags,
Params: params,
}
}
// NewExprStmt creates an expression statement.
func NewExprStmt(expr Expr) *ExprStmt {
return &ExprStmt{Expr: expr}
}
// DeclareVarStmt creates a variable or const declaration statement.
func DeclareVarStmt(constant bool, names []string, typ TypeExpr, value Expr) *VarDeclStmt {
return &VarDeclStmt{
Const: constant,
Pattern: VarPattern{Names: names},
Type: typ,
Value: value,
}
}
// DeclareGlobalVar creates a global variable declaration with flags.
func DeclareGlobalVar(name string, value Expr, flags GlobalVarFlags) *GlobalVarDecl {
return &GlobalVarDecl{
Flags: flags,
Name: name,
Value: value,
}
}
// Return creates a return statement.
func Return(value Expr) *ReturnStmt {
return &ReturnStmt{Value: value}
}
// If creates an if statement.
func If(cond Expr, then, els Stmt) *IfStmt {
return &IfStmt{Cond: cond, Then: then, Else: els}
}
// NewBlock creates a block of statements.
func NewBlock(stmts ...Stmt) *Block {
return &Block{Stmts: stmts}
}
// NewBlock creates a block statement containing a block of statements.
func NewBlockStmt(stmts ...Stmt) *BlockStmt {
return &BlockStmt{
Block: &Block{Stmts: stmts},
}
}
// Try creates a try expression.
func Try(expr Expr) *TryExpr {
return &TryExpr{Expr: expr}
}
// Binary creates a binary expression.
func Binary(op string, left, right Expr) *BinaryExpr {
return &BinaryExpr{Op: op, Left: left, Right: right}
}
// Param creates a function parameter declaration.
func Param(name string, typ TypeExpr) *ParamDecl {
return &ParamDecl{Name: name, Type: typ}
}
// StructDecl creates a struct declaration with the given fields/members.
func StructDecl(fields ...*ContainerMember) *ContainerDecl {
return &ContainerDecl{
Kind: "struct",
Fields: fields,
}
}
// Field creates a struct field (optionally with initializer).
func Field(name string, typ TypeExpr, byteAlign *Expr, value Expr) *ContainerMember {
return &ContainerMember{
Field: &ContainerField{
Name: name,
Type: typ,
ByteAlign: byteAlign,
Value: value,
},
}
}
// Method creates a method (function declaration) as a struct member.
func Method(fn *FnDecl) *ContainerMember {
return &ContainerMember{Decl: fn}
}
// OptionalType creates an optional type (?T).
func OptionalType(base TypeExpr) *PrefixTypeExpr {
return &PrefixTypeExpr{Op: "?", Base: base}
}
// PointerType creates a pointer type (*T).
func PointerType(base TypeExpr) *PrefixTypeExpr {
return &PrefixTypeExpr{Op: "*", Base: base}
}