ast

package
v0.0.0-...-9466f02 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2021 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

ast is a package that has implementation to generate syntax tree during parsing. we have three interfaces called `Node`, `Statement`, and `Expression`. every node in our AST has to implement the `Node` interface, meaning it has to provide a `TokenLiteral()` method that returns the literal value of the token it's associated with.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Expression

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

type Identifier

type Identifier struct {
	Token token.Token // the token.IDENT token
	Value string
}

Identifier is a struct type to hold the identifier of the binding. for example, in the case of `let x = 5;` for `x`, we have `Identifier` struct type which implements the `Expression` interface. But the `Identifier` in the `LetStatement` does not produce a value, reason why we handle this as expression is to keep things simple. `Identifier`s in other parts of the Gopher program DO produce values.

func (*Identifier) TokenLiteral

func (i *Identifier) TokenLiteral() string

type LetStatement

type LetStatement struct {
	Token token.Token // the token.LET token
	Name  *Identifier
	Value Expression
}

LetStatement has the fields we need: `Name` to hold the identifier of the binding `Value` for the expression that produces the value.

func (*LetStatement) TokenLiteral

func (ls *LetStatement) TokenLiteral() string

type Node

type Node interface {
	// TokenLiteral() will be used only for debugging and testing
	TokenLiteral() string
}

type Program

type Program struct {
	Statements []Statement
}

Program node is going to be the root node of every AST our parser produces. Every valid Gopher program is a series of statements. These statements are contained in the `Program.Statements`, which is just a slice of AST nodes that implement the `Statement` interface. with `Program`, `LetStatement` and `Identifier` defined this piece of Gopher source code:

let x = 5;

could be represented by an AST:

        --------------
       | *ast.Program |
       |--------------|
       |  Statements  |
        --------------
              |
              |
              v
        -------------------
       | *ast.LetStatement |
       |-------------------|
    ---|      Name         |
   |   |-------------------|
   |   |      Value        |---
   |    -------------------    |
   |                           |
   v                           v
 -----------------     -----------------
| *ast.Identifier |   | *ast.Expression |
 -----------------     -----------------

func (*Program) TokenLiteral

func (p *Program) TokenLiteral() string

type ReturnStatement

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

ReturnStatement a structural representation for `return` statement in the language. By the language definition, we need a keyword `return` and a following expression, this structure will represent the statement into our AST.

func (*ReturnStatement) TokenLiteral

func (rs *ReturnStatement) TokenLiteral() string

type Statement

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

Jump to

Keyboard shortcuts

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