lox

package
v0.0.0-...-174701c Latest Latest
Warning

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

Go to latest
Published: May 7, 2023 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Assign

type Assign struct {
	Name  Token
	Value Expr
}

func NewAssign

func NewAssign(name Token, value Expr) *Assign

func (*Assign) Accept

func (a *Assign) Accept(visitor ExpressionVisitor) interface{}

type Binary

type Binary struct {
	Left     Expr
	Operator Token
	Right    Expr
}

func NewBinary

func NewBinary(left Expr, operator Token, right Expr) *Binary

func (*Binary) Accept

func (b *Binary) Accept(visitor ExpressionVisitor) interface{}

type Block

type Block struct {
	Statements []Stmt
}

func NewBlock

func NewBlock(statements []Stmt) *Block

func (*Block) Accept

func (b *Block) Accept(visitor StatementVisitor)

type Call

type Call struct {
	Callee    Expr
	Paren     Token
	Arguments []Expr
}

func NewCall

func NewCall(callee Expr, paren Token, arguments []Expr) *Call

func (*Call) Accept

func (c *Call) Accept(visitor ExpressionVisitor) interface{}

type CallFunc

type CallFunc func(interpreter *Interpreter, arguments []interface{}) interface{}

type Callable

type Callable interface {
	Call(interpreter *Interpreter, arguments []interface{}) interface{}
	Arity() int
}

type Class

type Class struct {
	Name       Token
	Superclass *Variable
	Methods    []Function
}

func NewClass

func NewClass(name Token, superclass *Variable, methods []Function) *Class

func (*Class) Accept

func (c *Class) Accept(visitor StatementVisitor)

type ClassType

type ClassType int
const (
	CLASS_NONE ClassType = iota
	CLASS_CLASS
	CLASS_SUBCLASS
)

type Environment

type Environment struct {
	Values map[string]interface{}
	// contains filtered or unexported fields
}

func NewEnvironment

func NewEnvironment() *Environment

func NewEnvironmentWithEnclosing

func NewEnvironmentWithEnclosing(enclosing *Environment) *Environment

func (*Environment) Assign

func (e *Environment) Assign(name Token, value interface{})

func (*Environment) AssignAt

func (e *Environment) AssignAt(distance int, name Token, value interface{})

func (*Environment) Define

func (e *Environment) Define(name string, value interface{})

func (*Environment) Get

func (e *Environment) Get(name Token) interface{}

func (*Environment) GetAt

func (e *Environment) GetAt(distance int, name string) interface{}

type Expr

type Expr interface {
	Accept(visitor ExpressionVisitor) interface{}
}

type Expression

type Expression struct {
	Expression Expr
}

func NewExpression

func NewExpression(expr Expr) *Expression

func (*Expression) Accept

func (s *Expression) Accept(visitor StatementVisitor)

type ExpressionVisitor

type ExpressionVisitor interface {
	VisitBinaryExpr(expr Binary) interface{}
	VisitCallExpr(expr Call) interface{}
	VisitGroupingExpr(expr Grouping) interface{}
	VisitLiteralExpr(expr Literal) interface{}
	VisitLogicalExpr(expr Logical) interface{}
	VisitUnaryExpr(expr Unary) interface{}
	VisitVariableExpr(expr Variable) interface{}
	VisitAssignExpr(expr Assign) interface{}
	VisitGetExpr(expr Get) interface{}
	VisitSetExpr(expr Set) interface{}
	VisitSuperExpr(expr *Super) interface{}
	VisitThisExpr(expr This) interface{}
}

type Function

type Function struct {
	Name   Token
	Params []Token
	Body   []Stmt
}

func NewFunction

func NewFunction(name Token, params []Token, body []Stmt) *Function

func (*Function) Accept

func (f *Function) Accept(visitor StatementVisitor)

type FunctionType

type FunctionType int
const (
	NONE FunctionType = iota
	FUNCTION
	INITIALIZER
	METHOD
)

type Get

type Get struct {
	Name   Token
	Object Expr
}

func NewGet

func NewGet(name Token, object Expr) *Get

func (*Get) Accept

func (g *Get) Accept(visitor ExpressionVisitor) interface{}

type Grouping

type Grouping struct {
	Expression Expr
}

func NewGrouping

func NewGrouping(expr Expr) *Grouping

func (*Grouping) Accept

func (g *Grouping) Accept(visitor ExpressionVisitor) interface{}

type If

type If struct {
	Condition  Expr
	ThenBranch Stmt
	ElseBranch Stmt
}

func NewIf

func NewIf(condition Expr, thenBranch Stmt, elseBranch Stmt) *If

func (*If) Accept

func (i *If) Accept(visitor StatementVisitor)

type Interpreter

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

func NewInterpreter

func NewInterpreter() *Interpreter

func (*Interpreter) Interpret

func (i *Interpreter) Interpret(statements []Stmt) (err error)

func (*Interpreter) Resolve

func (i *Interpreter) Resolve(expr Expr, depth int)

func (*Interpreter) VisitAssignExpr

func (i *Interpreter) VisitAssignExpr(expr Assign) interface{}

func (*Interpreter) VisitBinaryExpr

func (i *Interpreter) VisitBinaryExpr(expr Binary) interface{}

func (*Interpreter) VisitBlockStmt

func (i *Interpreter) VisitBlockStmt(stmt Block)

func (*Interpreter) VisitCallExpr

func (i *Interpreter) VisitCallExpr(expr Call) interface{}

func (*Interpreter) VisitClassStmt

func (i *Interpreter) VisitClassStmt(stmt Class)

func (*Interpreter) VisitExpressionStmt

func (i *Interpreter) VisitExpressionStmt(stmt Expression)

func (*Interpreter) VisitFunctionStmt

func (i *Interpreter) VisitFunctionStmt(stmt Function)

func (*Interpreter) VisitGetExpr

func (i *Interpreter) VisitGetExpr(expr Get) interface{}

func (*Interpreter) VisitGroupingExpr

func (i *Interpreter) VisitGroupingExpr(expr Grouping) interface{}

func (*Interpreter) VisitIfStmt

func (i *Interpreter) VisitIfStmt(stmt If)

func (*Interpreter) VisitLiteralExpr

func (i *Interpreter) VisitLiteralExpr(expr Literal) interface{}

func (*Interpreter) VisitLogicalExpr

func (i *Interpreter) VisitLogicalExpr(expr Logical) interface{}

func (*Interpreter) VisitPrintStmt

func (i *Interpreter) VisitPrintStmt(stmt Print)

func (*Interpreter) VisitReturnStmt

func (i *Interpreter) VisitReturnStmt(stmt Return)

func (*Interpreter) VisitSetExpr

func (i *Interpreter) VisitSetExpr(expr Set) interface{}

func (*Interpreter) VisitSuperExpr

func (i *Interpreter) VisitSuperExpr(expr *Super) interface{}

func (*Interpreter) VisitThisExpr

func (i *Interpreter) VisitThisExpr(expr This) interface{}

func (*Interpreter) VisitUnaryExpr

func (i *Interpreter) VisitUnaryExpr(expr Unary) interface{}

func (*Interpreter) VisitVarStmt

func (i *Interpreter) VisitVarStmt(stmt Var)

func (*Interpreter) VisitVariableExpr

func (i *Interpreter) VisitVariableExpr(expr Variable) interface{}

func (*Interpreter) VisitWhileStmt

func (i *Interpreter) VisitWhileStmt(stmt While)

type Literal

type Literal struct {
	Value interface{}
}

func NewLiteral

func NewLiteral(value interface{}) *Literal

func (*Literal) Accept

func (l *Literal) Accept(visitor ExpressionVisitor) interface{}

type Logical

type Logical struct {
	Left     Expr
	Operator Token
	Right    Expr
}

func NewLogical

func NewLogical(left Expr, operator Token, right Expr) *Logical

func (*Logical) Accept

func (l *Logical) Accept(visitor ExpressionVisitor) interface{}

type LoxClass

type LoxClass struct {
	Name       string
	Superclass *LoxClass
	Methods    map[string]LoxFunction
}

func NewLoxClass

func NewLoxClass(name string, superclass *LoxClass, methods map[string]LoxFunction) *LoxClass

func (*LoxClass) Arity

func (c *LoxClass) Arity() int

func (*LoxClass) Call

func (c *LoxClass) Call(interpreter *Interpreter, arguments []interface{}) interface{}

func (*LoxClass) FindMethod

func (c *LoxClass) FindMethod(name string) *LoxFunction

func (LoxClass) String

func (c LoxClass) String() string

type LoxError

type LoxError struct {
	Token   Token
	Message string
}

func NewLoxError

func NewLoxError(token Token, message string) *LoxError

func (*LoxError) Error

func (err *LoxError) Error() string

type LoxFunction

type LoxFunction struct {
	Declaration Function
	Closure     *Environment
	// contains filtered or unexported fields
}

func NewLoxFunction

func NewLoxFunction(declaration Function, closure *Environment, isInitializer bool) *LoxFunction

func (*LoxFunction) Arity

func (f *LoxFunction) Arity() int

func (*LoxFunction) Bind

func (f *LoxFunction) Bind(instance *LoxInstance) *LoxFunction

func (*LoxFunction) Call

func (f *LoxFunction) Call(interpreter *Interpreter, arguments []interface{}) (retVal interface{})

func (LoxFunction) String

func (f LoxFunction) String() string

type LoxInstance

type LoxInstance struct {
	Class  LoxClass
	Fields map[string]interface{}
}

func NewLoxInstance

func NewLoxInstance(class LoxClass) *LoxInstance

func (*LoxInstance) Get

func (i *LoxInstance) Get(name Token) interface{}

func (*LoxInstance) Set

func (i *LoxInstance) Set(name Token, value interface{})

func (LoxInstance) String

func (i LoxInstance) String() string

type Parser

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

func NewParser

func NewParser(tokens []Token) *Parser

func (*Parser) Parse

func (p *Parser) Parse() ([]Stmt, error)

type Print

type Print struct {
	Expression Expr
}

func NewPrint

func NewPrint(expr Expr) *Print

func (*Print) Accept

func (p *Print) Accept(visitor StatementVisitor)

type ProtoCallable

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

func NewProtoCallable

func NewProtoCallable(arity int, call CallFunc) *ProtoCallable

func (*ProtoCallable) Arity

func (p *ProtoCallable) Arity() int

func (*ProtoCallable) Call

func (p *ProtoCallable) Call(interpreter *Interpreter, arguments []interface{}) interface{}

type Resolver

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

func NewResolver

func NewResolver(interpreter *Interpreter) *Resolver

func (*Resolver) ResolveStatements

func (r *Resolver) ResolveStatements(statements []Stmt) (err error)

func (*Resolver) VisitAssignExpr

func (r *Resolver) VisitAssignExpr(expr Assign) interface{}

func (*Resolver) VisitBinaryExpr

func (r *Resolver) VisitBinaryExpr(expr Binary) interface{}

func (*Resolver) VisitBlockStmt

func (r *Resolver) VisitBlockStmt(stmt Block)

func (*Resolver) VisitCallExpr

func (r *Resolver) VisitCallExpr(expr Call) interface{}

func (*Resolver) VisitClassStmt

func (r *Resolver) VisitClassStmt(stmt Class)

func (*Resolver) VisitExpressionStmt

func (r *Resolver) VisitExpressionStmt(stmt Expression)

func (*Resolver) VisitFunctionStmt

func (r *Resolver) VisitFunctionStmt(stmt Function)

func (*Resolver) VisitGetExpr

func (r *Resolver) VisitGetExpr(expr Get) interface{}

func (*Resolver) VisitGroupingExpr

func (r *Resolver) VisitGroupingExpr(expr Grouping) interface{}

func (*Resolver) VisitIfStmt

func (r *Resolver) VisitIfStmt(stmt If)

func (*Resolver) VisitLiteralExpr

func (r *Resolver) VisitLiteralExpr(expr Literal) interface{}

func (*Resolver) VisitLogicalExpr

func (r *Resolver) VisitLogicalExpr(expr Logical) interface{}

func (*Resolver) VisitPrintStmt

func (r *Resolver) VisitPrintStmt(stmt Print)

func (*Resolver) VisitReturnStmt

func (r *Resolver) VisitReturnStmt(stmt Return)

func (*Resolver) VisitSetExpr

func (r *Resolver) VisitSetExpr(expr Set) interface{}

func (*Resolver) VisitSuperExpr

func (r *Resolver) VisitSuperExpr(expr *Super) interface{}

func (*Resolver) VisitThisExpr

func (r *Resolver) VisitThisExpr(expr This) interface{}

func (*Resolver) VisitUnaryExpr

func (r *Resolver) VisitUnaryExpr(expr Unary) interface{}

func (*Resolver) VisitVarStmt

func (r *Resolver) VisitVarStmt(stmt Var)

func (*Resolver) VisitVariableExpr

func (r *Resolver) VisitVariableExpr(expr Variable) interface{}

func (*Resolver) VisitWhileStmt

func (r *Resolver) VisitWhileStmt(stmt While)

type Return

type Return struct {
	Keyword Token
	Value   Expr
}

func NewReturn

func NewReturn(keyword Token, value Expr) *Return

func (*Return) Accept

func (r *Return) Accept(visitor StatementVisitor)

type ReturnWrapper

type ReturnWrapper struct {
	Value interface{}
}

func NewReturnWrapper

func NewReturnWrapper(value interface{}) *ReturnWrapper

type RuntimeError

type RuntimeError struct {
	Token   Token
	Message string
}

func NewRuntimeError

func NewRuntimeError(token Token, message string) *RuntimeError

func (*RuntimeError) Error

func (err *RuntimeError) Error() string

type Scanner

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

func NewScanner

func NewScanner(source string) *Scanner

func (*Scanner) ScanTokens

func (s *Scanner) ScanTokens() ([]Token, error)

type ScannerError

type ScannerError struct {
	Line  int
	Where string
	Error string
}

func NewScannerError

func NewScannerError(line int, where string, error string) *ScannerError

type Set

type Set struct {
	Name   Token
	Object Expr
	Value  Expr
}

func NewSet

func NewSet(object Expr, name Token, value Expr) *Set

func (*Set) Accept

func (s *Set) Accept(visitor ExpressionVisitor) interface{}

type StatementVisitor

type StatementVisitor interface {
	VisitPrintStmt(stmt Print)
	VisitExpressionStmt(stmt Expression)
	VisitVarStmt(stmt Var)
	VisitBlockStmt(stmt Block)
	VisitIfStmt(stmt If)
	VisitWhileStmt(stmt While)
	VisitFunctionStmt(stmt Function)
	VisitReturnStmt(stmt Return)
	VisitClassStmt(stmt Class)
}

type Stmt

type Stmt interface {
	Accept(visitor StatementVisitor)
}

type Super

type Super struct {
	Keyword Token
	Method  Token
}

func NewSuper

func NewSuper(keyword Token, method Token) *Super

func (*Super) Accept

func (s *Super) Accept(visitor ExpressionVisitor) interface{}

type This

type This struct {
	Keyword Token
}

func NewThis

func NewThis(keyword Token) *This

func (*This) Accept

func (t *This) Accept(visitor ExpressionVisitor) interface{}

type Token

type Token struct {
	TokenType TokenType
	Lexeme    string
	Literal   interface{}
	Line      int
}

func NewToken

func NewToken(tokenType TokenType, lexeme string, literal interface{}, line int) *Token

func (Token) String

func (t Token) String() string

type TokenType

type TokenType int
const (
	// Single-character tokens.
	LEFT_PAREN TokenType = iota
	RIGHT_PAREN
	LEFT_BRACE
	RIGHT_BRACE
	COMMA
	DOT
	MINUS
	PLUS
	SEMICOLON
	SLASH
	STAR
	// One or two character tokens.
	BANG
	BANG_EQUAL
	EQUAL
	EQUAL_EQUAL
	GREATER
	GREATER_EQUAL
	LESS
	LESS_EQUAL

	// Literals.
	IDENTIFIER
	STRING
	NUMBER

	// Keywords.
	AND
	CLASS
	ELSE
	FALSE
	FUN
	FOR
	IF
	NIL
	OR
	PRINT
	RETURN
	SUPER
	THIS
	TRUE
	VAR
	WHILE

	EOF
)

func (TokenType) String

func (i TokenType) String() string

type Unary

type Unary struct {
	Operator Token
	Right    Expr
}

func NewUnary

func NewUnary(operator Token, right Expr) *Unary

func (*Unary) Accept

func (u *Unary) Accept(visitor ExpressionVisitor) interface{}

type Var

type Var struct {
	Name        Token
	Initializer Expr
}

func NewVar

func NewVar(name Token, iniitializer Expr) *Var

func (*Var) Accept

func (v *Var) Accept(visitor StatementVisitor)

type Variable

type Variable struct {
	Name Token
}

func NewVariable

func NewVariable(name Token) *Variable

func (*Variable) Accept

func (v *Variable) Accept(visitor ExpressionVisitor) interface{}

type While

type While struct {
	Condition Expr
	Body      Stmt
}

func NewWhile

func NewWhile(condition Expr, body Stmt) *While

func (*While) Accept

func (w *While) Accept(visitor StatementVisitor)

Jump to

Keyboard shortcuts

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