ast

package
v0.0.0-...-cca31bf Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ProgramNode = iota
	IdentifierExpressionNode
	NumericLiteralExpressionNode
	UnaryExpressionNode
	BinaryExpressionNode
	AssignmentExpressionNode
	VariableDeclarationStatementNode
)

Variables

View Source
var CurrentIndex int = 0
View Source
var CurrentTokens []lexer.Token = []lexer.Token{}
View Source
var NodeTypeNames = map[int]string{
	ProgramNode:                      "ProgramNode",
	IdentifierExpressionNode:         "IdentifierExpressionNode",
	UnaryExpressionNode:              "UnaryExpressionNode",
	NumericLiteralExpressionNode:     "NumericLiteralExpressionNode",
	BinaryExpressionNode:             "BinaryExpressionNode",
	AssignmentExpressionNode:         "AssignmentExpressionNode",
	VariableDeclarationStatementNode: "VariableDeclarationStatementNode",
}

Functions

func GetCurrentToken

func GetCurrentToken() lexer.Token

func GetNextToken

func GetNextToken() lexer.Token

func IsNextToken

func IsNextToken(kind lexer.TokenKind) bool

func IsNotEOF

func IsNotEOF() bool

func MoveNextWith

func MoveNextWith(kind lexer.TokenKind, message string) lexer.Token

func MoveToNextToken

func MoveToNextToken()

func PanicWithDetails

func PanicWithDetails(token lexer.Token, message string)

func ParseAdditiveExpression

func ParseAdditiveExpression() interface{}

ParseAdditiveExpression parses an additive expression e.g.: 1 + 2 e.g.: (1 + 2) - 3

func ParseAssignmentExpression

func ParseAssignmentExpression() interface{}

ParseAssignmentExpression parses an assignment expression e.g.: a = 1

func ParseExpression

func ParseExpression() interface{}

Precedence: 1. PrimaryExpression 2. UnaryExpression 3. MultiplicativeExpression 4. AdditiveExpression 5. AssignmentExpression

func ParseMultiplicativeExpression

func ParseMultiplicativeExpression() interface{}

ParseMultiplicativeExpression parses a multiplicative expression e.g. 1 * 2 e.g. (1 * 2) * 3

func ParsePrimaryExpression

func ParsePrimaryExpression() interface{}

func ParseStatement

func ParseStatement() interface{}

func ParseVariableDeclarationStatement

func ParseVariableDeclarationStatement() interface{}

Types

type AssignmentExpression

type AssignmentExpression struct {
	Expression
	Symbol string      `json:"symbol"`
	Value  interface{} `json:"value"`
}

AssignmentExpression is an assignment expression node that can be used to assign a value to a variable e.g. foo = 42

type BinaryExpression

type BinaryExpression struct {
	Expression
	Operator string      `json:"operator"`
	Left     interface{} `json:"left"`
	Right    interface{} `json:"right"`
}

BinaryExpression is a binary expression node that can be used to represent arithmetic operations and comparisons e.g. 1 + 2

type Expression

type Expression struct {
	NodeType int    `json:"nodeType"`
	NodeName string `json:"nodeName"`
}

Expression is a generic expression node that can be used as a base for more specific expression types

type IdentifierExpression

type IdentifierExpression struct {
	Expression
	Symbol string `json:"symbol"`
}

Identifier is an identifier node that represents a variable or function name e.g. foo

func ParseIdentifier

func ParseIdentifier() IdentifierExpression

ParseIdentifier parses an identifier expression e.g. foo

type Node

type Node struct {
	Token lexer.Token `json:"token"`
	Value interface{} `json:"value"`
}

type NumericLiteralExpression

type NumericLiteralExpression struct {
	Expression
	Value int `json:"value"`
}

NumericLiteral is a numeric literal node that represents a numeric value e.g. 42

func ParseNumericLiteral

func ParseNumericLiteral() NumericLiteralExpression

ParseNumericLiteral parses a numeric literal expression e.g. 42

type Program

type Program struct {
	NodeType   int           `json:"nodeType"`
	NodeName   string        `json:"nodeName"`
	Statements []interface{} `json:"statements"`
}

func GenerateAst

func GenerateAst(input string) Program

GenerateAst generates an abstract syntax tree (AST) from the input string and returns a Program struct containing the AST Precedence is based on the order of precedence in the language specification 1. PrimaryExpression 2. UnaryExpression 3. MultiplicativeExpression 4. AdditiveExpression

type Statement

type Statement struct {
	NodeType int    `json:"nodeType"`
	NodeName string `json:"nodeName"`
}

Statement is a generic statement node

type UnaryExpression

type UnaryExpression struct {
	Expression
	Operator string      `json:"operator"`
	Right    interface{} `json:"right"`
}

UnaryExpression is a unary expression node that can be used to represent unary operations e.g. -42 - Unary Minus (-): This operation negates the value of a number, changing its sign e.g. !foo - Boolean Not (!): This operation inverts the boolean value of its operand e.g. &foo - Address-of (&): This operation returns the memory address of its operand e.g. *foo - Dereference (*): This operation accesses the value stored at a pointer's address

type VariableDeclarationStatement

type VariableDeclarationStatement struct {
	Statement
	Symbol     string      `json:"symbol"`
	Value      interface{} `json:"value"`
	IsConstant bool        `json:"isConstant"`
}

Jump to

Keyboard shortcuts

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