interpreter

package
v0.0.0-...-1e0949a Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAlreadyDefined = errors.New("The name is already defined in this scope.")
	ErrUndefined      = errors.New("Undefined name.")
)

Functions

func Check

func Check(program []Stmt, lines [][]rune) error

func Interpret

func Interpret(program []Stmt, lines [][]rune) error

func PrintAST

func PrintAST(program Stmt) string

Types

type ASTPrinter

type ASTPrinter struct{}

func (ASTPrinter) VisitAnonymousFunction

func (a ASTPrinter) VisitAnonymousFunction(expr *ExprAnonymousFunction) (any, error)

func (ASTPrinter) VisitAssign

func (a ASTPrinter) VisitAssign(assign *ExprAssign) (any, error)

func (ASTPrinter) VisitBinary

func (a ASTPrinter) VisitBinary(binary *ExprBinary) (any, error)

func (ASTPrinter) VisitBlock

func (a ASTPrinter) VisitBlock(stmt *StmtBlock) error

func (ASTPrinter) VisitCall

func (a ASTPrinter) VisitCall(call *ExprCall) (any, error)

func (ASTPrinter) VisitExpression

func (a ASTPrinter) VisitExpression(stmt *StmtExpression) error

func (ASTPrinter) VisitFor

func (a ASTPrinter) VisitFor(stmt *StmtFor) error

func (ASTPrinter) VisitFuncDecl

func (a ASTPrinter) VisitFuncDecl(stmt *StmtFuncDecl) error

func (ASTPrinter) VisitGrouping

func (a ASTPrinter) VisitGrouping(grouping *ExprGrouping) (any, error)

func (ASTPrinter) VisitIf

func (a ASTPrinter) VisitIf(stmt *StmtIf) error

func (ASTPrinter) VisitList

func (a ASTPrinter) VisitList(list *ExprList) (any, error)

func (ASTPrinter) VisitLiteral

func (a ASTPrinter) VisitLiteral(literal *ExprLiteral) (any, error)

func (ASTPrinter) VisitLogical

func (a ASTPrinter) VisitLogical(logical *ExprLogical) (any, error)

func (ASTPrinter) VisitLoopControl

func (a ASTPrinter) VisitLoopControl(stmt *StmtLoopControl) error

func (ASTPrinter) VisitReturn

func (a ASTPrinter) VisitReturn(stmt *StmtReturn) error

func (ASTPrinter) VisitSubscript

func (a ASTPrinter) VisitSubscript(expr *ExprSubscript) (any, error)

func (ASTPrinter) VisitTernary

func (a ASTPrinter) VisitTernary(ternary *ExprTernary) (any, error)

func (ASTPrinter) VisitThrow

func (a ASTPrinter) VisitThrow(stmt *StmtThrow) error

func (ASTPrinter) VisitTry

func (a ASTPrinter) VisitTry(stmt *StmtTry) error

func (ASTPrinter) VisitUnary

func (a ASTPrinter) VisitUnary(unary *ExprUnary) (any, error)

func (ASTPrinter) VisitVarDecl

func (a ASTPrinter) VisitVarDecl(stmt *StmtVarDecl) error

func (ASTPrinter) VisitVariable

func (a ASTPrinter) VisitVariable(variable *ExprVariable) (any, error)

func (ASTPrinter) VisitWhile

func (a ASTPrinter) VisitWhile(stmt *StmtWhile) error

type CallError

type CallError struct {
	Message string
}

func (CallError) Error

func (t CallError) Error() string

type Callable

type Callable interface {
	ArgumentCount() int
	ReturnValueCount() int
	Throws() bool
	Call(i *interpreter, args []any) (any, error)
}

type Environment

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

func NewEnvironment

func NewEnvironment(parent *Environment) *Environment

func (*Environment) Assign

func (e *Environment) Assign(name string, value any, nestingLevel int)

func (*Environment) Define

func (e *Environment) Define(name string, value any) error

func (*Environment) Exists

func (e *Environment) Exists(name string) bool

func (*Environment) Get

func (e *Environment) Get(name string, nestingLevel int) any

type Exception

type Exception struct {
	StackTrace []int // line numbers most detailed to most general
	Value      any
	// contains filtered or unexported fields
}

func (Exception) Error

func (e Exception) Error() string

type Expr

type Expr interface {
	Accept(visitor ExprVisitor) (any, error)
}

type ExprAnonymousFunction

type ExprAnonymousFunction struct {
	Keyword          Token
	Body             Stmt
	Parameters       []string
	ReturnValueCount int
	Throws           bool
}

func (*ExprAnonymousFunction) Accept

func (e *ExprAnonymousFunction) Accept(visitor ExprVisitor) (any, error)

type ExprAssign

type ExprAssign struct {
	Operator  Token
	Assignees []Expr
	Expr      Expr
}

func (*ExprAssign) Accept

func (e *ExprAssign) Accept(visitor ExprVisitor) (any, error)

type ExprBinary

type ExprBinary struct {
	Operator Token
	Left     Expr
	Right    Expr
}

func (*ExprBinary) Accept

func (e *ExprBinary) Accept(visitor ExprVisitor) (any, error)

type ExprCall

type ExprCall struct {
	OpenParen Token
	Callee    Expr
	Args      []Expr
}

func (*ExprCall) Accept

func (e *ExprCall) Accept(visitor ExprVisitor) (any, error)

type ExprGrouping

type ExprGrouping struct {
	Expr Expr
}

func (*ExprGrouping) Accept

func (e *ExprGrouping) Accept(visitor ExprVisitor) (any, error)

type ExprList

type ExprList struct {
	OpenBracket Token
	Values      []Expr
}

func (*ExprList) Accept

func (e *ExprList) Accept(visitor ExprVisitor) (any, error)

type ExprLiteral

type ExprLiteral struct {
	Value any
}

func (*ExprLiteral) Accept

func (e *ExprLiteral) Accept(visitor ExprVisitor) (any, error)

type ExprLogical

type ExprLogical struct {
	Operator Token
	Left     Expr
	Right    Expr
}

func (*ExprLogical) Accept

func (e *ExprLogical) Accept(visitor ExprVisitor) (any, error)

type ExprSubscript

type ExprSubscript struct {
	OpenBracket Token
	Object      Expr
	Subscript   Expr
}

func (*ExprSubscript) Accept

func (e *ExprSubscript) Accept(visitor ExprVisitor) (any, error)

type ExprTernary

type ExprTernary struct {
	Left      Expr
	Operator1 Token
	Center    Expr
	Operator2 Token
	Right     Expr
}

func (*ExprTernary) Accept

func (e *ExprTernary) Accept(visitor ExprVisitor) (any, error)

type ExprUnary

type ExprUnary struct {
	Operator Token
	Right    Expr
}

func (*ExprUnary) Accept

func (e *ExprUnary) Accept(visitor ExprVisitor) (any, error)

type ExprVariable

type ExprVariable struct {
	Name         Token
	NestingLevel int
}

func (*ExprVariable) Accept

func (e *ExprVariable) Accept(visitor ExprVisitor) (any, error)

type ExprVisitor

type ExprVisitor interface {
	VisitLiteral(expr *ExprLiteral) (any, error)
	VisitVariable(expr *ExprVariable) (any, error)
	VisitCall(expr *ExprCall) (any, error)
	VisitSubscript(expr *ExprSubscript) (any, error)
	VisitGrouping(expr *ExprGrouping) (any, error)
	VisitList(expr *ExprList) (any, error)
	VisitUnary(expr *ExprUnary) (any, error)
	VisitBinary(expr *ExprBinary) (any, error)
	VisitLogical(expr *ExprLogical) (any, error)
	VisitTernary(expr *ExprTernary) (any, error)
	VisitAssign(expr *ExprAssign) (any, error)
	VisitAnonymousFunction(expr *ExprAnonymousFunction) (any, error)
}

type LoopControl

type LoopControl struct {
	Type TokenType
}

func (LoopControl) Error

func (l LoopControl) Error() string

type ParseError

type ParseError struct {
	Token   Token
	Message string
	Line    []rune
}

func (ParseError) Error

func (p ParseError) Error() string

type PrinterResult

type PrinterResult string

func (PrinterResult) Error

func (p PrinterResult) Error() string

type Return

type Return struct {
	Values []any
}

func (Return) Error

func (r Return) Error() string

type RuntimeError

type RuntimeError struct {
	Token   Token
	Message string
	Line    []rune
}

func (RuntimeError) Error

func (r RuntimeError) Error() string

type ScanError

type ScanError struct {
	Line     int
	LineText []rune
	Column   int
	Message  string
}

func (ScanError) Error

func (s ScanError) Error() string

type Stmt

type Stmt interface {
	Accept(visitor StmtVisitor) error
}

func Parse

func Parse(tokens []Token, lines [][]rune) ([]Stmt, []error)

type StmtBlock

type StmtBlock struct {
	Statements []Stmt
}

func (*StmtBlock) Accept

func (s *StmtBlock) Accept(visitor StmtVisitor) error

type StmtExpression

type StmtExpression struct {
	Expr Expr
}

func (*StmtExpression) Accept

func (s *StmtExpression) Accept(visitor StmtVisitor) error

type StmtFor

type StmtFor struct {
	Keyword     Token
	Initializer Stmt
	Condition   Expr
	Increment   Expr
	Body        Stmt
}

func (*StmtFor) Accept

func (s *StmtFor) Accept(visitor StmtVisitor) error

type StmtFuncDecl

type StmtFuncDecl struct {
	Name             Token
	Body             Stmt
	Parameters       []string
	ReturnValueCount int
	Throws           bool
}

func (*StmtFuncDecl) Accept

func (s *StmtFuncDecl) Accept(visitor StmtVisitor) error

type StmtIf

type StmtIf struct {
	Keyword   Token
	Condition Expr
	Body      Stmt
	ElseBody  Stmt
}

func (*StmtIf) Accept

func (s *StmtIf) Accept(visitor StmtVisitor) error

type StmtLoopControl

type StmtLoopControl struct {
	Keyword Token
}

func (*StmtLoopControl) Accept

func (s *StmtLoopControl) Accept(visitor StmtVisitor) error

type StmtReturn

type StmtReturn struct {
	Keyword Token
	Values  []Expr
}

func (*StmtReturn) Accept

func (s *StmtReturn) Accept(visitor StmtVisitor) error

type StmtThrow

type StmtThrow struct {
	Keyword Token
	Value   Expr
}

func (*StmtThrow) Accept

func (s *StmtThrow) Accept(visitor StmtVisitor) error

type StmtTry

type StmtTry struct {
	Keyword       Token
	Body          Stmt
	CatchBody     Stmt
	ExceptionName Token
}

func (*StmtTry) Accept

func (s *StmtTry) Accept(visitor StmtVisitor) error

type StmtVarDecl

type StmtVarDecl struct {
	Operator Token
	Names    []Token
	Expr     Expr
}

func (*StmtVarDecl) Accept

func (s *StmtVarDecl) Accept(visitor StmtVisitor) error

type StmtVisitor

type StmtVisitor interface {
	VisitExpression(stmt *StmtExpression) error
	VisitBlock(stmt *StmtBlock) error
	VisitVarDecl(stmt *StmtVarDecl) error
	VisitFuncDecl(stmt *StmtFuncDecl) error
	VisitIf(stmt *StmtIf) error
	VisitWhile(stmt *StmtWhile) error
	VisitFor(stmt *StmtFor) error
	VisitLoopControl(stmt *StmtLoopControl) error
	VisitReturn(stmt *StmtReturn) error
	VisitThrow(stmt *StmtThrow) error
	VisitTry(stmt *StmtTry) error
}

type StmtWhile

type StmtWhile struct {
	Keyword   Token
	Condition Expr
	Body      Stmt
}

func (*StmtWhile) Accept

func (s *StmtWhile) Accept(visitor StmtVisitor) error

type Token

type Token struct {
	Line    int
	Column  int
	Type    TokenType
	Lexeme  string
	Literal any
}

func Scan

func Scan(source io.Reader) ([]Token, [][]rune, error)

func (Token) String

func (t Token) String() string

type TokenType

type TokenType string
const (
	PLUS                    TokenType = "PLUS"
	PLUS_EQUAL              TokenType = "PLUS_EQUAL"
	PLUS_PLUS               TokenType = "PLUS_PLUS"
	MINUS                   TokenType = "MINUS"
	MINUS_EQUAL             TokenType = "MINUS_EQUAL"
	MINUS_MINUS             TokenType = "MINUS_MINUS"
	ASTERISK                TokenType = "ASTERISK"
	ASTERISK_EQUAL          TokenType = "ASTERISK_EQUAL"
	ASTERISK_ASTERISK       TokenType = "ASTERISK_ASTERISK"
	ASTERISK_ASTERISK_EQUAL TokenType = "ASTERISK_ASTERISK_EQUAL"
	SLASH                   TokenType = "SLASH"
	SLASH_EQUAL             TokenType = "SLASH_EQUAL"
	PERCENT                 TokenType = "PERCENT"
	PERCENT_EQUAL           TokenType = "PERCENT_EQUAL"

	OPEN_PAREN    TokenType = "OPEN_PAREN"
	CLOSE_PAREN   TokenType = "CLOSE_PAREN"
	OPEN_BRACE    TokenType = "OPEN_BRACE"
	CLOSE_BRACE   TokenType = "CLOSE_BRACE"
	OPEN_BRACKET  TokenType = "OPEN_BRACKET"
	CLOSE_BRACKET TokenType = "CLOSE_BRACKET"

	EQUAL         TokenType = "EQUAL"
	EQUAL_EQUAL   TokenType = "EQUAL_EQUAL"
	BANG          TokenType = "BANG"
	BANG_EQUAL    TokenType = "BANG_EQUAL"
	LESS          TokenType = "LESS"
	LESS_EQUAL    TokenType = "LESS_EQUAL"
	GREATER       TokenType = "GREATER"
	GREATER_EQUAL TokenType = "GREATER_EQUAL"

	AND TokenType = "AND"
	OR  TokenType = "OR"
	XOR TokenType = "XOR"

	NUMBER     TokenType = "NUMBER"
	STRING     TokenType = "STRING"
	IDENTIFIER TokenType = "IDENTIFIER"

	SEMICOLON     TokenType = "SEMICOLON"
	COMMA         TokenType = "COMMA"
	QUESTION_MARK TokenType = "QUESTION_MARK"
	COLON         TokenType = "COLON"

	TRUE     TokenType = "TRUE"
	FALSE    TokenType = "FALSE"
	VAR      TokenType = "VAR"
	FUNC     TokenType = "FUNC"
	IF       TokenType = "IF"
	ELSE     TokenType = "ELSE"
	WHILE    TokenType = "WHILE"
	FOR      TokenType = "FOR"
	BREAK    TokenType = "BREAK"
	CONTINUE TokenType = "CONTINUE"
	RETURN   TokenType = "RETURN"
	TRY      TokenType = "TRY"
	CATCH    TokenType = "CATCH"
	THROW    TokenType = "THROW"
	THROWS   TokenType = "THROWS"

	EOF TokenType = "EOF"
)

Jump to

Keyboard shortcuts

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