ast

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayLiteral

type ArrayLiteral struct {
	Token    token.Token // the "[" token
	Elements []Expression
}

ArrayLiteral is used to construct an ast.Node for array literals ([1,2,3]). Parsing the tokens of an array literal should return an ArrayLiteral struct. ArrayLiteral is a valid expression node within the abstract-syntax tree.

func (*ArrayLiteral) String

func (al *ArrayLiteral) String() string

String builds the entire ArrayLiteral as a string, first by stringifying all its elments, then building the string with the ArrayLiteral expected components

func (*ArrayLiteral) TokenLiteral

func (al *ArrayLiteral) TokenLiteral() string

TokenLiteral returns the literal value (Token.Literal) for the opening bracket of the array

type BlockStatement

type BlockStatement struct {
	Token      token.Token // the "{" token
	Statements []Statement
}

BlockStatement holds the necessary information to construct a statement(s) that exist within an IfExpression or Function Literal

func (*BlockStatement) String

func (bs *BlockStatement) String() string

String will construct the entire BlockStatement as a string by iterating through and stringifying all its statements. The statements can be any combination of LET, RETURN or ExpressionStatements

func (*BlockStatement) TokenLiteral

func (bs *BlockStatement) TokenLiteral() string

TokenLiteral returns the literal value (Token.Literal) for the "{" token

type Boolean

type Boolean struct {
	Token token.Token
	Value bool
}

Boolean holds a Token field (Token{TokenType, Literal}) for the boolean and a Value field for the actual bool value

func (*Boolean) String

func (b *Boolean) String() string

String returns the literal value (Token.Literal) for the the Boolean

func (*Boolean) TokenLiteral

func (b *Boolean) TokenLiteral() string

TokenLiteral returns the literal value (Token.Literal) for the the Boolean

type CallExpression

type CallExpression struct {
	Token     token.Token  // The "(" token
	Function  Expression   // Identifier of Function Literal
	Arguments []Expression // The list of expressions that are arguments to the function call
}

CallExpression consist of an expression that results in a function when evaluated and a list of expressions that are the arguments of this function call

func (*CallExpression) String

func (ce *CallExpression) String() string

String builds the entire CallExpression as a string, first by stringifying all its arguments, then building the string with the CallExpressions expected components

func (*CallExpression) TokenLiteral

func (ce *CallExpression) TokenLiteral() string

TokenLiteral returns the literal value (Token.Literal) for the "(" token

type Expression

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

Expression is the interface that embeds the Node interface and the expressionNode method

expressionNode() is a dummy method that does not do anything practical at the moment, it simply distinguishes the Expression interface

type ExpressionStatement

type ExpressionStatement struct {
	Token      token.Token // the first token of the expression
	Expression Expression
}

ExpressionStatement holds a Token field and an Expression field for the expression. It implements the Node and Statement interfaces.

func (*ExpressionStatement) String

func (es *ExpressionStatement) String() string

String constructs the entire ExpressionStatement node as a string

func (*ExpressionStatement) TokenLiteral

func (es *ExpressionStatement) TokenLiteral() string

TokenLiteral returns the literal value (Token.Literal) for the first token in the expression

type FunctionLiteral

type FunctionLiteral struct {
	Token      token.Token     // The 'fn' token
	Parameters []*Identifier   // The parameters of the function
	Body       *BlockStatement // The collection of statements in the body of the function
	Name       string          // The name the function is bound to
}

FunctionLiteral holds the necessary information to construct a function-literal expression

func (*FunctionLiteral) String

func (fl *FunctionLiteral) String() string

String builds the entire FunctionLiteral as a string, first by stringifying all its params, then building the string with the FunctionLiterals expected components

func (*FunctionLiteral) TokenLiteral

func (fl *FunctionLiteral) TokenLiteral() string

TokenLiteral returns the literal value (Token.Literal) for the "fn" token

type HashLiteral

type HashLiteral struct {
	Token token.Token               // the '{' token
	Pairs map[Expression]Expression // the key value pairs of the hash
}

HashLiteral is used to construct an ast.Node for hash literals ({ "a": 1 }) Parsing the tokens of a hash literal should return an HashLiteral struct. HashLiteral is a valid expression node within the abstract-syntax tree.

func (*HashLiteral) String

func (hl *HashLiteral) String() string

String builds the entire HashLiteral as a string. It stringifies the key value pairs, then builds the string with the String expected components

func (*HashLiteral) TokenLiteral

func (hl *HashLiteral) TokenLiteral() string

TokenLiteral returns the literal value (Token.Literal) for the opening brace of the hash literal

type Identifier

type Identifier struct {
	Token token.Token // the token.IDENT token
	// Value is used to represent the name in a variable binding x in `let x = 5`,
	Value string
}

Identifier holds the identifier of a binding eg: x in `let x = 5` and implements the Expression interface

func (*Identifier) String

func (i *Identifier) String() string

String() returns the identifier's name value (x in let x = 5)

func (*Identifier) TokenLiteral

func (i *Identifier) TokenLiteral() string

TokenLiteral returns the literal value (Token.Literal) for a token of type Token.IDENT

type IfExpression

type IfExpression struct {
	Token       token.Token     // The 'if' token
	Condition   Expression      // The condition to be evalated
	Consequence *BlockStatement // The collection of statements which is a direct result of the passing Condition
	Alternative *BlockStatement // The alternative statements should the condition not pass
}

IfExpression holds the necessary information to construct an if-expression

func (*IfExpression) String

func (ie *IfExpression) String() string

String will contruct the entire IfExpression as a string

func (*IfExpression) TokenLiteral

func (ie *IfExpression) TokenLiteral() string

TokenLiteral returns the literal value (Token.Literal) for the the if token

type IndexExpression

type IndexExpression struct {
	Token token.Token // The [ Token
	Left  Expression
	Index Expression
}

IndexExpression is used to construct an ast.Node for index operator expressions ([1, 2, 3][1]) Parsing the tokens of an index operator expression should return an IndexExpression struct. IndexExpression is a valid expression node within the abstract-syntax tree.

func (*IndexExpression) String

func (ie *IndexExpression) String() string

String builds the entire IndexExpression as a string. It stringifies the array and index, then builds the string with the IndexExpression expected components

func (*IndexExpression) TokenLiteral

func (ie *IndexExpression) TokenLiteral() string

TokenLiteral returns the literal value (Token.Literal) for the opening bracket of the index operation

type InfixExpression

type InfixExpression struct {
	Token    token.Token
	Left     Expression
	Operator string
	Right    Expression
}

PrefixExpression holds a Token field for the input, Left contains the expression to the right of the operator. Operator is a string that contains either "-" or "!" and Right contains the expression to the right of the operator.

func (*InfixExpression) String

func (ie *InfixExpression) String() string

String constructs a string for the InfixExpression, explicitly adding paranthesis around the constructed string to distinguish it from other expressions

func (*InfixExpression) TokenLiteral

func (ie *InfixExpression) TokenLiteral() string

TokenLiteral returns the literal value (Token.Literal) for the the InfixExpression input

type IntegerLiteral

type IntegerLiteral struct {
	Token token.Token
	Value int64
}

IntegerLiteral holds a Token field (Token{TokenType, Literal}) for the integer and a Value field for the actual integer value

func (*IntegerLiteral) String

func (il *IntegerLiteral) String() string

String constructs the integer value as a string

func (*IntegerLiteral) TokenLiteral

func (il *IntegerLiteral) TokenLiteral() string

TokenLiteral returns the literal value (Token.Literal) for the the integer

type LetStatement

type LetStatement struct {
	Token token.Token // the token.LET token
	Name  *Identifier // Name holds the identifer of the binding
	// Value is the expression that produces the value eg: 5 in `let x = 5`.
	// Coincidentally, it can also be an identifier for an expression in a different statement
	// eg: valueProducingIdentifier in `let x = valueProducingIdentifier` is an identifier
	// for a different statement eg: `let valueProducingIdentifier = 5`
	// Therefore in this statement, `let x = valueProducingIdentifier`, valueProducingIdentifer
	// is an identifier that serves as an expression - it produces a value
	Value Expression
}

LetStatement holds the name and value for a let statement

func (*LetStatement) String

func (ls *LetStatement) String() string

String constructs the entire LetStatement node as a string

func (*LetStatement) TokenLiteral

func (ls *LetStatement) TokenLiteral() string

TokenLiteral returns the literal value (Token.Literal) for a token of type Token.LET

type Node

type Node interface {
	TokenLiteral() string
	String() string
}

Node is the interface that wraps TokenLiteral method

TokenLiteral() should return a token's literal value (Token.Literal) in a Node String() should return the AST node as a string

type PrefixExpression

type PrefixExpression struct {
	Token    token.Token
	Operator string
	Right    Expression
}

PrefixExpression holds a Token field for the input, Operator is a string that contains either "-" or "!" and Right contains the expression to the right of the operator.

func (*PrefixExpression) String

func (pe *PrefixExpression) String() string

String constructs a string for the PrefixExpression, explicitly adding paranthesis around the constructed string to distinguish it from other expressions

func (*PrefixExpression) TokenLiteral

func (pe *PrefixExpression) TokenLiteral() string

TokenLiteral returns the literal value (Token.Literal) for the the PrefixExpression input

type Program

type Program struct {
	Statements []Statement // Statements are just a slice of AST nodes
}

Program serves as the root node of every AST a parser produces.

func (*Program) String

func (p *Program) String() string

String creates a buffer and writes the return value of each statement's String() method to it. Finally it returns the buffer as a string.

func (*Program) TokenLiteral

func (p *Program) TokenLiteral() string

TokenLiteral returns the token literal for the first statement in the program

type ReturnStatement

type ReturnStatement struct {
	Token       token.Token // the token.RETURN token
	ReturnValue Expression
}

ReturnStatement holds a Token field for the return token and a ReturnValue field for the expression that's to be returned

func (*ReturnStatement) String

func (rs *ReturnStatement) String() string

String constructs the entire ReturnStatement node as a string

func (*ReturnStatement) TokenLiteral

func (rs *ReturnStatement) TokenLiteral() string

TokenLiteral returns the literal value (Token.Literal) for a token of type token.RETURN

type Statement

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

Statement is the interface that embeds the Node interface and the statementNode method

statementNode() is a dummy method that does not do anything practical at the moment, it simply distinguishes the Statement interface

type StringLiteral

type StringLiteral struct {
	Token token.Token
	Value string
}

StringLiteral holds a Token field (Token{TokenType, Literal}) for the lexed string and a Value field for the actual string value

func (*StringLiteral) String

func (sl *StringLiteral) String() string

String returns the literal value (Token.Literal) for the the StringLiteral

func (*StringLiteral) TokenLiteral

func (sl *StringLiteral) TokenLiteral() string

TokenLiteral returns the literal value (Token.Literal) for the string

Jump to

Keyboard shortcuts

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