lsl

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2025 License: MIT Imports: 10 Imported by: 2

Documentation

Overview

Package lsl provides a simple interface to the Lacking Shader Language.

Index

Constants

View Source
const (
	// TypeNameBool is the name of the boolean type.
	TypeNameBool = "bool"

	// TypeNameInt is the name of the signed 32bit integer type.
	TypeNameInt = "int"

	// TypeNameUint is the name of the unsigned 32bit integer type.
	TypeNameUint = "uint"

	// TypeNameFloat is the name of the 32bit floating point type.
	TypeNameFloat = "float"

	// TypeNameVec2 is the name of the 32bit floating point 2D vector type.
	TypeNameVec2 = "vec2"

	// TypeNameVec3 is the name of the 32bit floating point 3D vector type.
	TypeNameVec3 = "vec3"

	// TypeNameVec4 is the name of the 32bit floating point 4D vector type.
	TypeNameVec4 = "vec4"

	// TypeNameBVec2 is the name of the boolean 2D vector type.
	TypeNameBVec2 = "bvec2"

	// TypeNameBVec3 is the name of the boolean 3D vector type.
	TypeNameBVec3 = "bvec3"

	// TypeNameBVec4 is the name of the boolean 4D vector type.
	TypeNameBVec4 = "bvec4"

	// TypeNameIVec2 is the name of the signed 32bit integer 2D vector type.
	TypeNameIVec2 = "ivec2"

	// TypeNameIVec3 is the name of the signed 32bit integer 3D vector type.
	TypeNameIVec3 = "ivec3"

	// TypeNameIVec4 is the name of the signed 32bit integer 4D vector type.
	TypeNameIVec4 = "ivec4"

	// TypeNameUVec2 is the name of the unsigned 32bit integer 2D vector type.
	TypeNameUVec2 = "uvec2"

	// TypeNameUVec3 is the name of the unsigned 32bit integer 3D vector type.
	TypeNameUVec3 = "uvec3"

	// TypeNameUVec4 is the name of the unsigned 32bit integer 4D vector type.
	TypeNameUVec4 = "uvec4"

	// TypeNameMat2 is the name of the 2x2 matrix type.
	TypeNameMat2 = "mat2"

	// TypeNameMat3 is the name of the 3x3 matrix type.
	TypeNameMat3 = "mat3"

	// TypeNameMat4 is the name of the 4x4 matrix type.
	TypeNameMat4 = "mat4"

	// TypeNameSampler2D is the name of the 2D sampler type.
	TypeNameSampler2D = "sampler2D"

	// TypeNameSamplerCube is the name of the cube sampler type.
	TypeNameSamplerCube = "samplerCube"
)
View Source
const (
	// AssignmentOperatorEq is the assignment operator "=". It assigns the
	// value to the variable.
	AssignmentOperatorEq = "="

	// AssignmentOperatorAuto is the assignment operator ":=", where the type
	// of the variable is inferred from the value.
	AssignmentOperatorAuto = ":="

	// AssignmentOperatorAdd is the assignment operator "+=". It adds the value
	// to the variable.
	AssignmentOperatorAdd = "+="

	// AssignmentOperatorSub is the assignment operator "-=". It subtracts the
	// value from the variable.
	AssignmentOperatorSub = "-="

	// AssignmentOperatorMul is the assignment operator "*=". It multiplies the
	// variable by the value.
	AssignmentOperatorMul = "*="

	// AssignmentOperatorDiv is the assignment operator "/=". It divides the
	// variable by the value.
	AssignmentOperatorDiv = "/="

	// AssignmentOperatorMod is the assignment operator "%=". It takes the
	// modulo of the variable and the value.
	AssignmentOperatorMod = "%="

	// AssignmentOperatorShl is the assignment operator "<<=". It shifts the
	// variable to the left by the value.
	AssignmentOperatorShl = "<<="

	// AssignmentOperatorShr is the assignment operator ">>=". It shifts the
	// variable to the right by the value.
	AssignmentOperatorShr = ">>="

	// AssignmentOperatorAnd is the assignment operator "&=". It performs a
	// bitwise AND operation on the variable and the value.
	AssignmentOperatorAnd = "&="

	// AssignmentOperatorOr is the assignment operator "|=". It performs a
	// bitwise OR operation on the variable and the value.
	AssignmentOperatorOr = "|="

	// AssignmentOperatorXor is the assignment operator "^=". It performs a
	// bitwise XOR operation on the variable and the value.
	AssignmentOperatorXor = "^="
)
View Source
const (
	// UnaryOperatorNot is the unary operator "!". It inverts the value.
	UnaryOperatorNot = "!"

	// UnaryOperatorNeg is the unary operator "-". It negates the value.
	UnaryOperatorNeg = "-"

	// UnaryOperatorPos is the unary operator "+". It is a no-op.
	UnaryOperatorPos = "+"

	// UnaryOperatorBitNot is the unary operator "^". It performs a bitwise
	// NOT operation on the value.
	UnaryOperatorBitNot = "^"
)
View Source
const (
	// BinaryOperatorAdd is the binary operator "+". It adds two values.
	BinaryOperatorAdd = "+"

	// BinaryOperatorSub is the binary operator "-". It subtracts two values.
	BinaryOperatorSub = "-"

	// BinaryOperatorMul is the binary operator "*". It multiplies two values.
	BinaryOperatorMul = "*"

	// BinaryOperatorDiv is the binary operator "/". It divides two values.
	BinaryOperatorDiv = "/"

	// BinaryOperatorMod is the binary operator "%". It takes the modulo of two
	// values.
	BinaryOperatorMod = "%"

	// BinaryOperatorShl is the binary operator "<<". It shifts the first value
	// to the left by the second value.
	BinaryOperatorShl = "<<"

	// BinaryOperatorShr is the binary operator ">>". It shifts the first value
	// to the right by the second value.
	BinaryOperatorShr = ">>"

	// BinaryOperatorEq is the binary operator "==". It checks if two values are
	// equal.
	BinaryOperatorEq = "=="

	// BinaryOperatorNotEq is the binary operator "!=". It checks if two values
	// are not equal.
	BinaryOperatorNotEq = "!="

	// BinaryOperatorLess is the binary operator "<". It checks if the first
	// value is less than the second value.
	BinaryOperatorLess = "<"

	// BinaryOperatorGreater is the binary operator ">". It checks if the first
	// value is greater than the second value.
	BinaryOperatorGreater = ">"

	// BinaryOperatorLessEq is the binary operator "<=". It checks if the first
	// value is less than or equal to the second value.
	BinaryOperatorLessEq = "<="

	// BinaryOperatorGreaterEq is the binary operator ">=". It checks if the
	// first value is greater than or equal to the second value.
	BinaryOperatorGreaterEq = ">="

	// BinaryOperatorBitAnd is the binary operator "&". It performs a bitwise
	// AND operation on two values.
	BinaryOperatorBitAnd = "&"

	// BinaryOperatorBitOr is the binary operator "|". It performs a bitwise OR
	// operation on two values.
	BinaryOperatorBitOr = "|"

	// BinaryOperatorBitXor is the binary operator "^". It performs a bitwise
	// XOR operation on two values.
	BinaryOperatorBitXor = "^"

	// BinaryOperatorAnd is the binary operator "&&". It performs a logical AND
	// operation on two values.
	BinaryOperatorAnd = "&&"

	// BinaryOperatorOr is the binary operator "||". It performs a logical OR
	// operation on two values.
	BinaryOperatorOr = "||"
)

Variables

This section is empty.

Functions

func Tokenize added in v0.22.0

func Tokenize(source string) iter.Seq[Token]

Tokenize is a helper function that creates a new iterator that yields tokens from the given source code. Internally it creates a new Tokenizer and uses it to generate tokens.

func Validate

func Validate(shader *Shader, schema Schema) error

Types

type Assignment

type Assignment struct {
	Operator   string
	Target     Expression
	Expression Expression
}

type BinaryExpression

type BinaryExpression struct {
	Operator string
	Left     Expression
	Right    Expression
}

type Conditional

type Conditional struct {
	Condition Expression
	Then      []Statement
	ElseIf    *Conditional
	Else      []Statement
}

type Declaration

type Declaration interface {
	// contains filtered or unexported methods
}

Declaration represents a top-level construct in a shader.

Example:

func hello() {}

type Discard

type Discard struct{}

type Expression

type Expression interface {
	// contains filtered or unexported methods
}

Expression represents a sequence of tokens that can be evaluated to a value.

Example:

1 + sin(10)

type ExpressionGroup

type ExpressionGroup struct {
	Expression Expression
}

ExpressionGroup represents a paren enclosed expression.

type Field

type Field struct {
	Name string
	Type string
}

type FieldIdentifier

type FieldIdentifier struct {
	ObjName   string // TODO: Identifier, or maybe even expression?
	FieldName string // TODO: Identifier
}

FieldIdentifier represents a reference to a field of a structure.

type FloatLiteral

type FloatLiteral struct {
	Value float64
}

type FunctionCall

type FunctionCall struct {
	Name      string
	Arguments []Expression
}

type FunctionDeclaration

type FunctionDeclaration struct {
	Name    string
	Inputs  []Field
	Outputs []Field
	Body    []Statement
}

type Identifier

type Identifier struct {
	Name string
}

Identifier represents a reference to a variable or a function.

type IntLiteral

type IntLiteral struct {
	Value int64
}

type ParseError added in v0.22.0

type ParseError struct {

	// Pos is the position in the source code where the error occurred.
	Pos Position

	// Message is the error message.
	Message string
}

ParseError is an error that occurs during parsing.

func (*ParseError) Error added in v0.22.0

func (e *ParseError) Error() string

Error returns the error message.

type Parser

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

Parser is responsible for parsing LSL source code into a shader AST object.

func NewParser

func NewParser(source string) *Parser

NewParser creates a new LSL parser for the given source code.

func (*Parser) ParseBlockEnd

func (p *Parser) ParseBlockEnd() error

ParseBlockEnd parses the closing bracket of a block. It assumes that the next token to follow is a closing bracket token. If the token is not a closing bracket, an error is returned. Whitespace characters up to the closing bracket token are ignored.

func (*Parser) ParseBlockStart

func (p *Parser) ParseBlockStart() error

ParseBlockStart parses the opening bracket of a block. It assumes that the next token to follow is an opening bracket token. If the token is not an opening bracket, an error is returned. Whitespace characters up to the opening bracket token are ignored.

func (*Parser) ParseComment

func (p *Parser) ParseComment() error

ParseComment assumes that the next token to follow is a comment token and consumes it. Whitespace characters up to the comment token are ignored. Anything other will result in an error. If the comment is followed by a new line token, it is also consumed.

func (*Parser) ParseExpression

func (p *Parser) ParseExpression() (Expression, error)

func (*Parser) ParseFunction

func (p *Parser) ParseFunction() (*FunctionDeclaration, error)

func (*Parser) ParseNamedParameterList

func (p *Parser) ParseNamedParameterList() ([]Field, error)

ParseNamedParameterList parses a list of field name and type pairs.

func (*Parser) ParseNewLine

func (p *Parser) ParseNewLine() error

ParseNewLine assumes that the next token to follow is a new line token and consumes it. Whitespace characters up to the new line token are ignored. Anything other will result in an error.

func (*Parser) ParseOptionalRemainder

func (p *Parser) ParseOptionalRemainder() error

ParseOptionalRemainder consumes the remainder of the line, including any new line or comment tokens. It assumes that anything to follow is non-vital (comments, new lines) and can be ignored.

func (*Parser) ParseShader

func (p *Parser) ParseShader() (*Shader, error)

ParseShader parses the LSL source code and returns a shader AST object. If the source code is invalid, an error is returned.

func (*Parser) ParseStatement

func (p *Parser) ParseStatement() (Statement, error)

func (*Parser) ParseStatementList

func (p *Parser) ParseStatementList() ([]Statement, error)

func (*Parser) ParseTextureBlock

func (p *Parser) ParseTextureBlock() (*TextureBlockDeclaration, error)

ParseTextureBlock parses a block containing texture fields.

func (*Parser) ParseUniformBlock

func (p *Parser) ParseUniformBlock() (*UniformBlockDeclaration, error)

ParseUniformBlock parses a block containing uniform fields.

func (*Parser) ParseUnnamedParameterList

func (p *Parser) ParseUnnamedParameterList() ([]Field, error)

ParseUnnamedParameterList parses a list of field types.

func (*Parser) ParseVaryingBlock

func (p *Parser) ParseVaryingBlock() (*VaryingBlockDeclaration, error)

ParseVaryingBlock parses a block containing varying fields.

type Position added in v0.22.0

type Position struct {

	// Line is the line number, starting from 1.
	Line uint32

	// Column is the column number, starting from 1.
	Column uint32
}

Position represents a position of interest in the source code.

func At added in v0.22.0

func At(line, column uint32) Position

At creates a new Position with the given line and column numbers.

func (Position) String added in v0.22.0

func (p Position) String() string

String returns a string representation of the position.

type Schema

type Schema interface {
	// IsAllowedTextureType returns whether the provided type name is
	// allowed to be used in a texture block.
	IsAllowedTextureType(typeName string) bool

	// IsAllowedUniformType returns whether the provided type name is
	// allowed to be used in a uniform block.
	IsAllowedUniformType(typeName string) bool

	// IsAllowedVaryingType returns whether the provided type name is
	// allowed to be used in a varying block.
	IsAllowedVaryingType(typeName string) bool

	// IsAllowedVariableType returns whether the provided type name is
	// allowed to be used in a variable declaration.
	IsAllowedVariableType(typeName string) bool
}

func DefaultSchema

func DefaultSchema() Schema

DefaultSchema returns the default schema implementation.

func ForwardSchema

func ForwardSchema() Schema

func GeometrySchema

func GeometrySchema() Schema

func PostprocessSchema

func PostprocessSchema() Schema

func ShadowSchema

func ShadowSchema() Schema

func SkySchema

func SkySchema() Schema

type Shader

type Shader struct {
	Declarations []Declaration
}

func MustParse

func MustParse(source string) *Shader

MuseParse parses the given LSL source code and returns a shader AST object. If the source code is invalid, it will panic.

func Parse

func Parse(source string) (*Shader, error)

Parse parses the given LSL source code and returns a shader AST object.

func (*Shader) FindFunction

func (s *Shader) FindFunction(name string) (*FunctionDeclaration, bool)

func (*Shader) FindTextureBlock

func (s *Shader) FindTextureBlock() (*TextureBlockDeclaration, bool)

func (*Shader) FindUniformBlock

func (s *Shader) FindUniformBlock() (*UniformBlockDeclaration, bool)

func (*Shader) FindVaryingBlock

func (s *Shader) FindVaryingBlock() (*VaryingBlockDeclaration, bool)

func (*Shader) Functions

func (s *Shader) Functions() []*FunctionDeclaration

func (*Shader) TextureBlocks

func (s *Shader) TextureBlocks() []*TextureBlockDeclaration

func (*Shader) UniformBlocks

func (s *Shader) UniformBlocks() []*UniformBlockDeclaration

func (*Shader) VaryingBlocks

func (s *Shader) VaryingBlocks() []*VaryingBlockDeclaration

type Statement

type Statement interface {
	// contains filtered or unexported methods
}

Statement represents a code line in a function.

type TextureBlockDeclaration

type TextureBlockDeclaration struct {
	Fields []Field
}

type Token added in v0.22.0

type Token struct {

	// Type is the type of token.
	Type TokenType

	// Value is the value of the token.
	Value string

	// Pos is the position of the token in the source code.
	Pos Position
}

Token represents a single item of interest in the LSL source code. A Tokenizer will convert a string of LSL source code into a sequence of tokens.

func (Token) IsAssignmentOperator added in v0.22.0

func (t Token) IsAssignmentOperator() bool

IsAssignmentOperator returns true if the token is an assignment operator token.

func (Token) IsBinaryOperator added in v0.22.0

func (t Token) IsBinaryOperator() bool

IsBinaryOperator returns true if the token is a binary operator token.

func (Token) IsComment added in v0.22.0

func (t Token) IsComment() bool

IsComment returns true if the token is a comment token.

func (Token) IsEOF added in v0.22.0

func (t Token) IsEOF() bool

IsEOF returns true if the token is an EOF token.

func (Token) IsError added in v0.22.0

func (t Token) IsError() bool

IsError returns true if the token is an error token.

func (Token) IsIdentifier added in v0.22.0

func (t Token) IsIdentifier() bool

IsIdentifier returns true if the token is an identifier token.

func (Token) IsNewLine added in v0.22.0

func (t Token) IsNewLine() bool

IsNewLine returns true if the token is a new line token.

func (Token) IsNumber added in v0.22.0

func (t Token) IsNumber() bool

IsNumber returns true if the token is a numeric token.

func (Token) IsOperator added in v0.22.0

func (t Token) IsOperator() bool

IsOperator returns true if the token is an operator token.

func (Token) IsSpecificIdentifier added in v0.22.0

func (t Token) IsSpecificIdentifier(value string) bool

IsSpecificIdentifier returns true if the token is an identifier token with the specified value.

func (Token) IsSpecificOperator added in v0.22.0

func (t Token) IsSpecificOperator(value string) bool

IsSpecificOperator returns true if the token is an operator token with the specified value.

func (Token) IsTerminal added in v0.22.0

func (t Token) IsTerminal() bool

IsTerminal returns true if the token is a final token and no subsequent tokens will be returned.

func (Token) IsUnaryOperator added in v0.22.0

func (t Token) IsUnaryOperator() bool

IsUnaryOperator returns true if the token is a unary operator token.

func (Token) String added in v0.22.0

func (t Token) String() string

String returns a string representation of the token.

type TokenType added in v0.22.0

type TokenType uint8

TokenType represents the type of a token.

const (
	// TokenTypeEOF represents the end of the input.
	TokenTypeEOF TokenType = iota

	// TokenTypeError represents an error during tokenization.
	TokenTypeError

	// TokenTypeNewLine represents a new line.
	TokenTypeNewLine

	// TokenTypeComment represents a comment.
	TokenTypeComment

	// TokenTypeIdentifier represents an identifier (e.g. variable name,
	// type name, field, function name, etc).
	TokenTypeIdentifier

	// TokenTypeOperator represents an operator (e.g. assignment, braces, etc).
	TokenTypeOperator

	// TokenTypeNumber represents a numeric value.
	TokenTypeNumber
)

func (TokenType) String added in v0.22.0

func (t TokenType) String() string

String returns a string representation of the token type.

type Tokenizer added in v0.22.0

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

Tokenizer is a mechanism to split an LSL source code into key pieces of information, called tokens. Each token represents a single element of the source code, such as an identifier, a number, an operator, etc.

One would normally use the Parser to process LSL source code, since the Tokenizer provides low-level information about the source code.

func NewTokenizer added in v0.22.0

func NewTokenizer(source string) *Tokenizer

NewTokenizer creates a new Tokenizer for the given source code. The source code is expected to be a valid UTF-8 string. If the source code is not a valid UTF-8 string, the tokenizer will return an error token.

func (*Tokenizer) Next added in v0.22.0

func (t *Tokenizer) Next() Token

Next returns the next token in the source code. If there are no more tokens to return, it returns a token with the type TokenTypeEOF.

type UnaryExpression

type UnaryExpression struct {
	Operator string
	Operand  Expression
}

type UniformBlockDeclaration

type UniformBlockDeclaration struct {
	Fields []Field
}

type UsageScope

type UsageScope uint8
const (
	UsageScopeNone         UsageScope = 0
	UsageScopeVertexShader UsageScope = 1 << iota
	UsageScopeFragmentShader
)

type Validator

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

func NewValidator

func NewValidator(shader *Shader, schema Schema) *Validator

func (*Validator) Validate

func (v *Validator) Validate() error

type VariableDeclaration

type VariableDeclaration struct {
	Name       string
	Type       string
	Assignment Expression
}

type VaryingBlockDeclaration

type VaryingBlockDeclaration struct {
	Fields []Field
}

Jump to

Keyboard shortcuts

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