Added 2 tests and asthelpers.go

This commit is contained in:
2025-05-29 16:27:36 -05:00
parent 44f3cfca5c
commit 50b38254ab
4 changed files with 572 additions and 116 deletions

View File

@@ -27,14 +27,21 @@ type Decl interface {
isDecl()
}
// FnFlags is a bitfield for function declaration options.
type FnFlags uint8
const (
FnExport FnFlags = 1 << iota
FnExtern
FnInline
FnNoInline
FnThreadLocal
)
// FnDecl represents a function declaration.
type FnDecl struct {
Export bool
Extern bool
Flags FnFlags
ExternName string // Optional string for extern
Inline bool
NoInline bool
ThreadLocal bool
Name string // May be empty (anonymous)
Params []*ParamDecl
ByteAlign *Expr
@@ -47,13 +54,22 @@ type FnDecl struct {
func (*FnDecl) isDecl() {}
// GlobalVarDecl represents a global variable declaration.
// GlobalVarFlags is a bitfield for global variable declaration options.
type GlobalVarFlags uint8
const (
GlobalVarConst GlobalVarFlags = 1 << iota
GlobalVarExport
GlobalVarExtern
GlobalVarThreadLocal
)
// GlobalVarDecl represents a top-level (global) variable or constant declaration.
// These are only allowed at the container/module scope and use a restricted syntax:
// no destructuring or multi-var declarations, just a single name and optional type/initializer.
type GlobalVarDecl struct {
Export bool
Extern bool
Flags GlobalVarFlags
ExternName string // Optional string for extern
ThreadLocal bool
Const bool
Name string
Type TypeExpr // Optional
ByteAlign *Expr
@@ -95,6 +111,25 @@ type ParamDecl struct {
Type TypeExpr // 'anytype' if empty
}
// ContainerDecl represents a struct, enum, union, or opaque declaration.
type ContainerDecl struct {
Extern bool
Packed bool
Kind string // "struct", "enum", "union", "opaque"
TagType TypeExpr // Optional (for enum/union)
Fields []*ContainerMember
DocComment DocComment
}
func (*ContainerDecl) isDecl() {}
// ErrorSetDecl represents an error set declaration.
type ErrorSetDecl struct {
Names []string
}
func (*ErrorSetDecl) isDecl() {}
// Block represents a block of statements.
type Block struct {
Label string // Optional
@@ -113,7 +148,8 @@ type ExprStmt struct {
func (*ExprStmt) isStmt() {}
// VarDeclStmt represents a variable or const declaration at statement level, supporting destructuring and multi-var declarations.
// VarDeclStmt represents a local variable or constant declaration statement inside a function or block.
// These support destructuring and multi-var declarations, and are only valid at statement/block scope.
type VarDeclStmt struct {
Const bool
Pattern VarPattern // Destructuring or multiple variable names
@@ -276,21 +312,6 @@ type AsmInputItem struct {
Expr Expr
}
// ContainerDecl represents a struct, enum, union, or opaque declaration.
type ContainerDecl struct {
Extern bool
Packed bool
Kind string // "struct", "enum", "union", "opaque"
TagType TypeExpr // Optional (for enum/union)
Fields []*ContainerMember
DocComment DocComment
}
// ErrorSetDecl represents an error set declaration.
type ErrorSetDecl struct {
Names []string
}
// InitListExpr represents an initializer list.
// Exactly one of Fields, Values, or Empty must be set (non-nil/non-empty or true).
type InitListExpr struct {
@@ -381,6 +402,12 @@ type Expr interface{}
// TypeExpr is any type expression.
type TypeExpr interface{}
// PrefixTypeExpr represents a type with a string prefix. Examples include optionals and pointers.
type PrefixTypeExpr struct {
Op string
Base TypeExpr
}
// DocComment represents a doc comment (/// or //! lines).
// Newlines in the string automatically add more comments in the output.
type DocComment string