validator

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package validator provides semantic validation for CodeAI AST.

Package validator provides semantic validation for CodeAI AST.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorType

type ErrorType int

ErrorType categorizes validation errors for structured handling.

const (
	// ErrorScope indicates a scope-related error (undefined/duplicate variables).
	ErrorScope ErrorType = iota
	// ErrorType indicates a type-related error.
	ErrorTypeCheck
	// ErrorFunction indicates a function-related error (wrong args, undefined).
	ErrorFunction
	// ErrorSemantic indicates a general semantic error.
	ErrorSemantic
)

func (ErrorType) String

func (et ErrorType) String() string

String returns the string representation of ErrorType.

type Symbol

type Symbol struct {
	Name       string
	Kind       SymbolKind
	Type       Type // Inferred type
	ParamCount int  // For functions: number of parameters
}

Symbol represents a declared identifier in the program.

type SymbolKind

type SymbolKind int

SymbolKind distinguishes between different kinds of symbols.

const (
	// SymbolVariable represents a variable declaration.
	SymbolVariable SymbolKind = iota
	// SymbolFunction represents a function declaration.
	SymbolFunction
	// SymbolParameter represents a function parameter.
	SymbolParameter
)

func (SymbolKind) String

func (sk SymbolKind) String() string

String returns the string representation of SymbolKind.

type SymbolTable

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

SymbolTable manages symbol declarations across nested scopes.

func NewSymbolTable

func NewSymbolTable() *SymbolTable

NewSymbolTable creates a new symbol table with global scope initialized.

func (*SymbolTable) CurrentScopeDepth

func (st *SymbolTable) CurrentScopeDepth() int

CurrentScopeDepth returns the nesting depth of the current scope. Useful for debugging scope issues.

func (*SymbolTable) Declare

func (st *SymbolTable) Declare(name string, kind SymbolKind) error

Declare adds a new symbol to the current scope. Returns an error if the symbol is already declared in the current scope.

func (*SymbolTable) DeclareFunction

func (st *SymbolTable) DeclareFunction(name string, paramCount int) error

DeclareFunction adds a function symbol with parameter count.

func (*SymbolTable) DeclareWithType

func (st *SymbolTable) DeclareWithType(name string, kind SymbolKind, typ Type) error

DeclareWithType adds a new symbol with a known type.

func (*SymbolTable) EnterScope

func (st *SymbolTable) EnterScope()

EnterScope creates and enters a new nested scope.

func (*SymbolTable) ExitScope

func (st *SymbolTable) ExitScope()

ExitScope exits the current scope and returns to the parent.

func (*SymbolTable) Lookup

func (st *SymbolTable) Lookup(name string) (*Symbol, bool)

Lookup searches for a symbol starting from the current scope up to global. Returns the symbol and true if found, nil and false otherwise.

func (*SymbolTable) LookupLocal

func (st *SymbolTable) LookupLocal(name string) (*Symbol, bool)

LookupLocal searches for a symbol only in the current scope. Used for checking duplicate declarations in the same scope.

func (*SymbolTable) UpdateType

func (st *SymbolTable) UpdateType(name string, typ Type)

UpdateType updates the type of an existing symbol.

type Type

type Type int

Type represents the type of a value in the CodeAI language.

const (
	// TypeUnknown represents an undetermined type.
	TypeUnknown Type = iota
	// TypeString represents string values.
	TypeString
	// TypeNumber represents numeric values.
	TypeNumber
	// TypeBool represents boolean values.
	TypeBool
	// TypeArray represents array values.
	TypeArray
	// TypeFunction represents function values.
	TypeFunction
	// TypeVoid represents no value (for statements).
	TypeVoid
)

func (Type) String

func (t Type) String() string

String returns the string representation of Type.

type TypeChecker

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

TypeChecker performs type inference and checking on AST nodes.

func NewTypeChecker

func NewTypeChecker(symbols *SymbolTable) *TypeChecker

NewTypeChecker creates a new type checker with the given symbol table.

func (*TypeChecker) CheckCompatible

func (tc *TypeChecker) CheckCompatible(left, right Type) bool

CheckCompatible checks if two types are compatible for an operation.

func (*TypeChecker) InferType

func (tc *TypeChecker) InferType(expr ast.Expression) Type

InferType determines the type of an expression.

func (*TypeChecker) IsIterable

func (tc *TypeChecker) IsIterable(typ Type) bool

IsIterable checks if a type can be used in a for-in loop.

type ValidationError

type ValidationError struct {
	Position ast.Position
	Message  string
	Type     ErrorType
}

ValidationError represents a single semantic validation error.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error implements the error interface.

type ValidationErrors

type ValidationErrors struct {
	Errors []*ValidationError
}

ValidationErrors aggregates multiple validation errors.

func (*ValidationErrors) Add

func (ve *ValidationErrors) Add(err *ValidationError)

Add appends a validation error to the collection.

func (*ValidationErrors) Error

func (ve *ValidationErrors) Error() string

Error implements the error interface, formatting all errors.

func (*ValidationErrors) HasErrors

func (ve *ValidationErrors) HasErrors() bool

HasErrors returns true if there are any validation errors.

func (*ValidationErrors) Unwrap

func (ve *ValidationErrors) Unwrap() []error

Unwrap returns the underlying errors for errors.Is/As compatibility.

type Validator

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

Validator performs semantic analysis on CodeAI AST.

func New

func New() *Validator

New creates a new Validator instance.

func (*Validator) TypeOf

func (v *Validator) TypeOf(name string) (Type, bool)

TypeOf returns the inferred type of a variable by name. Used for testing type inference.

func (*Validator) Validate

func (v *Validator) Validate(program *ast.Program) error

Validate performs semantic validation on the given AST program. It returns an error if any validation issues are found.

Jump to

Keyboard shortcuts

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