lox

package module
v0.0.0-...-b5f37c2 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

kilox

Kim's Lox implementation from Crafting Interpreters book.

Go implementation

go run cmd/lox/lox.go
Additions
  • Ignore params ending with underscore
  • Instance initializer
  • Class var
  • Class var initializer
  • new for class initialization
  • typing (experimental)

C implementation (ongoing)

pushd src
make build
popd
src/clox

Documentation

Overview

Generated file, do not modify Invocation: gen_ast -spec ./cmd/gen_ast/expr.spec -dest expr.go -extensions typename

Generated file, do not modify Invocation: gen_ast -spec ./cmd/gen_ast/stmt.spec -dest stmt.go

Generated file, do not modify Invocation: gen_ast -spec ./cmd/gen_ast/type.spec -dest type.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrintExpr

func PrintExpr(expr Expr) string

func PrintStmts

func PrintStmts(stmts ...Stmt) string

func PrintType

func PrintType(t Type) string

Types

type AssignmentExpr

type AssignmentExpr struct {
	Name  Token
	Value Expr
}

func (*AssignmentExpr) Accept

func (e *AssignmentExpr) Accept(v exprVisitor)

func (*AssignmentExpr) String

func (e *AssignmentExpr) String() string

func (*AssignmentExpr) TypeName

func (*AssignmentExpr) TypeName() string

type BinaryExpr

type BinaryExpr struct {
	Left     Expr
	Operator Token
	Right    Expr
}

func (*BinaryExpr) Accept

func (e *BinaryExpr) Accept(v exprVisitor)

func (*BinaryExpr) String

func (e *BinaryExpr) String() string

func (*BinaryExpr) TypeName

func (*BinaryExpr) TypeName() string

type BlockStmt

type BlockStmt struct {
	Statements []Stmt
}

func (BlockStmt) Accept

func (s BlockStmt) Accept(v stmtVisitor)

func (BlockStmt) String

func (s BlockStmt) String() string

type BoolType

type BoolType struct {
	Token Token
}

func (BoolType) Accept

func (t BoolType) Accept(v typeVisitor)

func (BoolType) String

func (t BoolType) String() string

type BreakStmt

type BreakStmt struct {
	Keyword Token
}

func (BreakStmt) Accept

func (s BreakStmt) Accept(v stmtVisitor)

func (BreakStmt) String

func (s BreakStmt) String() string

type CallExpr

type CallExpr struct {
	Callee Expr
	Paren  Token
	Args   []Expr
}

func (*CallExpr) Accept

func (e *CallExpr) Accept(v exprVisitor)

func (*CallExpr) String

func (e *CallExpr) String() string

func (*CallExpr) TypeName

func (*CallExpr) TypeName() string

type Callable

type Callable interface {
	Arity() int
	Call(i *Interpreter, args []any) any
}

type ClassStmt

type ClassStmt struct {
	Name          Token
	Methods       []FunctionStmt
	Vars          []VarStmt
	StaticMethods []FunctionStmt
	StaticVars    []VarStmt
}

func (ClassStmt) Accept

func (s ClassStmt) Accept(v stmtVisitor)

func (ClassStmt) String

func (s ClassStmt) String() string

type ContinueStmt

type ContinueStmt struct {
	Keyword Token
}

func (ContinueStmt) Accept

func (s ContinueStmt) Accept(v stmtVisitor)

func (ContinueStmt) String

func (s ContinueStmt) String() string

type Environment

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

func NewEnvironment

func NewEnvironment(t dynType) *Environment

func (*Environment) Child

func (env *Environment) Child(t dynType) *Environment

func (*Environment) Debug

func (env *Environment) Debug() string

func (*Environment) Define

func (env *Environment) Define(name string, value any)

func (*Environment) Get

func (env *Environment) Get(name Token) any

func (*Environment) GetStatic

func (env *Environment) GetStatic(distance int, index int) any

func (*Environment) Set

func (env *Environment) Set(name Token, value any)

func (*Environment) SetStatic

func (env *Environment) SetStatic(distance int, index int, value any)

type Expr

type Expr interface {
	Accept(v exprVisitor)
	TypeName() string
}

type ExpressionStmt

type ExpressionStmt struct {
	Expression Expr
}

func (ExpressionStmt) Accept

func (s ExpressionStmt) Accept(v stmtVisitor)

func (ExpressionStmt) String

func (s ExpressionStmt) String() string

type FunctionExpr

type FunctionExpr struct {
	Keyword Token
	Params  []Token
	Body    []Stmt
}

func (*FunctionExpr) Accept

func (e *FunctionExpr) Accept(v exprVisitor)

func (*FunctionExpr) String

func (e *FunctionExpr) String() string

func (*FunctionExpr) TypeName

func (*FunctionExpr) TypeName() string

type FunctionStmt

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

func (FunctionStmt) Accept

func (s FunctionStmt) Accept(v stmtVisitor)

func (FunctionStmt) String

func (s FunctionStmt) String() string

type FunctionType

type FunctionType struct {
	Params []Type
	Return Type
}

func (FunctionType) Accept

func (t FunctionType) Accept(v typeVisitor)

func (FunctionType) String

func (t FunctionType) String() string

type GetExpr

type GetExpr struct {
	Object Expr
	Name   Token
}

func (*GetExpr) Accept

func (e *GetExpr) Accept(v exprVisitor)

func (*GetExpr) String

func (e *GetExpr) String() string

func (*GetExpr) TypeName

func (*GetExpr) TypeName() string

type GroupingExpr

type GroupingExpr struct {
	Expression Expr
}

func (*GroupingExpr) Accept

func (e *GroupingExpr) Accept(v exprVisitor)

func (*GroupingExpr) String

func (e *GroupingExpr) String() string

func (*GroupingExpr) TypeName

func (*GroupingExpr) TypeName() string

type IfStmt

type IfStmt struct {
	Condition Expr
	Then      Stmt
	Else      Stmt
}

func (IfStmt) Accept

func (s IfStmt) Accept(v stmtVisitor)

func (IfStmt) String

func (s IfStmt) String() string

type Interpreter

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

func NewInterpreter

func NewInterpreter() *Interpreter

func (*Interpreter) Debug

func (i *Interpreter) Debug() string

func (*Interpreter) Interpret

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

func (*Interpreter) SetStdout

func (i *Interpreter) SetStdout(w io.Writer)

func (*Interpreter) VisitAssignmentExpr

func (i *Interpreter) VisitAssignmentExpr(expr *AssignmentExpr)

func (*Interpreter) VisitBinaryExpr

func (i *Interpreter) VisitBinaryExpr(expr *BinaryExpr)

func (*Interpreter) VisitBlockStmt

func (i *Interpreter) VisitBlockStmt(stmt BlockStmt)

func (*Interpreter) VisitBreakStmt

func (i *Interpreter) VisitBreakStmt(stmt BreakStmt)

func (*Interpreter) VisitCallExpr

func (i *Interpreter) VisitCallExpr(expr *CallExpr)

func (*Interpreter) VisitClassStmt

func (i *Interpreter) VisitClassStmt(stmt ClassStmt)

func (*Interpreter) VisitContinueStmt

func (i *Interpreter) VisitContinueStmt(stmt ContinueStmt)

func (*Interpreter) VisitExpressionStmt

func (i *Interpreter) VisitExpressionStmt(stmt ExpressionStmt)

func (*Interpreter) VisitFunctionExpr

func (i *Interpreter) VisitFunctionExpr(expr *FunctionExpr)

func (*Interpreter) VisitFunctionStmt

func (i *Interpreter) VisitFunctionStmt(stmt FunctionStmt)

func (*Interpreter) VisitGetExpr

func (i *Interpreter) VisitGetExpr(expr *GetExpr)

func (*Interpreter) VisitGroupingExpr

func (i *Interpreter) VisitGroupingExpr(expr *GroupingExpr)

func (*Interpreter) VisitIfStmt

func (i *Interpreter) VisitIfStmt(stmt IfStmt)

func (*Interpreter) VisitLiteralExpr

func (i *Interpreter) VisitLiteralExpr(expr *LiteralExpr)

func (*Interpreter) VisitLogicExpr

func (i *Interpreter) VisitLogicExpr(expr *LogicExpr)

func (*Interpreter) VisitLoopStmt

func (i *Interpreter) VisitLoopStmt(stmt LoopStmt)

func (*Interpreter) VisitPrintStmt

func (i *Interpreter) VisitPrintStmt(stmt PrintStmt)

func (*Interpreter) VisitReturnStmt

func (i *Interpreter) VisitReturnStmt(stmt ReturnStmt)

func (*Interpreter) VisitSetExpr

func (i *Interpreter) VisitSetExpr(expr *SetExpr)

func (*Interpreter) VisitThisExpr

func (i *Interpreter) VisitThisExpr(expr *ThisExpr)

func (*Interpreter) VisitUnaryExpr

func (i *Interpreter) VisitUnaryExpr(expr *UnaryExpr)

func (*Interpreter) VisitVarStmt

func (i *Interpreter) VisitVarStmt(stmt VarStmt)

func (*Interpreter) VisitVariableExpr

func (i *Interpreter) VisitVariableExpr(expr *VariableExpr)

type LiteralExpr

type LiteralExpr struct {
	Token Token
	Value any
}

func (*LiteralExpr) Accept

func (e *LiteralExpr) Accept(v exprVisitor)

func (*LiteralExpr) String

func (e *LiteralExpr) String() string

func (*LiteralExpr) TypeName

func (*LiteralExpr) TypeName() string

type LogicExpr

type LogicExpr struct {
	Left     Expr
	Operator Token
	Right    Expr
}

func (*LogicExpr) Accept

func (e *LogicExpr) Accept(v exprVisitor)

func (*LogicExpr) TypeName

func (*LogicExpr) TypeName() string

type LoopStmt

type LoopStmt struct {
	Condition Expr
	Body      Stmt
	OnLoop    Expr
}

func (LoopStmt) Accept

func (s LoopStmt) Accept(v stmtVisitor)

func (LoopStmt) String

func (s LoopStmt) String() string

type NilType

type NilType struct {
	Token Token
}

func (NilType) Accept

func (t NilType) Accept(v typeVisitor)

func (NilType) String

func (t NilType) String() string

type NumberType

type NumberType struct {
	Token Token
}

func (NumberType) Accept

func (t NumberType) Accept(v typeVisitor)

func (NumberType) String

func (t NumberType) 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)

func (*Parser) ParseExpression

func (p *Parser) ParseExpression() (expr Expr, err error)

type PrintStmt

type PrintStmt struct {
	Expression Expr
}

func (PrintStmt) Accept

func (s PrintStmt) Accept(v stmtVisitor)

func (PrintStmt) String

func (s PrintStmt) String() string

type RefType

type RefType struct {
	Value Type
	ID    int
}

func (*RefType) Accept

func (t *RefType) Accept(v typeVisitor)

func (*RefType) String

func (t *RefType) String() string

type Resolver

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

func NewResolver

func NewResolver(interpreter *Interpreter) *Resolver

func (*Resolver) Resolve

func (r *Resolver) Resolve(stmts []Stmt) error

func (*Resolver) VisitAssignmentExpr

func (r *Resolver) VisitAssignmentExpr(expr *AssignmentExpr)

func (*Resolver) VisitBinaryExpr

func (r *Resolver) VisitBinaryExpr(expr *BinaryExpr)

func (*Resolver) VisitBlockStmt

func (r *Resolver) VisitBlockStmt(stmt BlockStmt)

func (*Resolver) VisitBreakStmt

func (r *Resolver) VisitBreakStmt(stmt BreakStmt)

func (*Resolver) VisitCallExpr

func (r *Resolver) VisitCallExpr(expr *CallExpr)

func (*Resolver) VisitClassStmt

func (r *Resolver) VisitClassStmt(stmt ClassStmt)

func (*Resolver) VisitContinueStmt

func (r *Resolver) VisitContinueStmt(stmt ContinueStmt)

func (*Resolver) VisitExpressionStmt

func (r *Resolver) VisitExpressionStmt(stmt ExpressionStmt)

func (*Resolver) VisitFunctionExpr

func (r *Resolver) VisitFunctionExpr(expr *FunctionExpr)

func (*Resolver) VisitFunctionStmt

func (r *Resolver) VisitFunctionStmt(stmt FunctionStmt)

func (*Resolver) VisitGetExpr

func (r *Resolver) VisitGetExpr(expr *GetExpr)

func (*Resolver) VisitGroupingExpr

func (r *Resolver) VisitGroupingExpr(expr *GroupingExpr)

func (*Resolver) VisitIfStmt

func (r *Resolver) VisitIfStmt(stmt IfStmt)

func (*Resolver) VisitLiteralExpr

func (r *Resolver) VisitLiteralExpr(expr *LiteralExpr)

func (*Resolver) VisitLogicExpr

func (r *Resolver) VisitLogicExpr(expr *LogicExpr)

func (*Resolver) VisitLoopStmt

func (r *Resolver) VisitLoopStmt(stmt LoopStmt)

func (*Resolver) VisitPrintStmt

func (r *Resolver) VisitPrintStmt(stmt PrintStmt)

func (*Resolver) VisitReturnStmt

func (r *Resolver) VisitReturnStmt(stmt ReturnStmt)

func (*Resolver) VisitSetExpr

func (r *Resolver) VisitSetExpr(expr *SetExpr)

func (*Resolver) VisitThisExpr

func (r *Resolver) VisitThisExpr(expr *ThisExpr)

func (*Resolver) VisitUnaryExpr

func (r *Resolver) VisitUnaryExpr(expr *UnaryExpr)

func (*Resolver) VisitVarStmt

func (r *Resolver) VisitVarStmt(stmt VarStmt)

func (*Resolver) VisitVariableExpr

func (r *Resolver) VisitVariableExpr(expr *VariableExpr)

type ReturnStmt

type ReturnStmt struct {
	Keyword Token
	Result  Expr
}

func (ReturnStmt) Accept

func (s ReturnStmt) Accept(v stmtVisitor)

func (ReturnStmt) String

func (s ReturnStmt) String() 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 SetExpr

type SetExpr struct {
	Object Expr
	Name   Token
	Value  Expr
}

func (*SetExpr) Accept

func (e *SetExpr) Accept(v exprVisitor)

func (*SetExpr) String

func (e *SetExpr) String() string

func (*SetExpr) TypeName

func (*SetExpr) TypeName() string

type Stmt

type Stmt interface {
	Accept(v stmtVisitor)
}

type StringType

type StringType struct {
	Token Token
}

func (StringType) Accept

func (t StringType) Accept(v typeVisitor)

func (StringType) String

func (t StringType) String() string

type ThisExpr

type ThisExpr struct {
	Keyword Token
}

func (*ThisExpr) Accept

func (e *ThisExpr) Accept(v exprVisitor)

func (*ThisExpr) String

func (e *ThisExpr) String() string

func (*ThisExpr) TypeName

func (*ThisExpr) TypeName() string

type Token

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

func (Token) String

func (t Token) String() string

type TokenType

type TokenType int
const (
	// Single-character tokens.
	LeftParen TokenType = iota
	RightParen
	LeftBrace
	RightBrace
	Comma
	Dot
	Minus
	Plus
	Semicolon
	Slash
	Star

	// One or two character tokens.
	Bang
	BangEqual
	Equal
	EqualEqual
	Greater
	GreaterEqual
	Less
	LessEqual

	// Literals.
	Identifier
	String
	Number

	// Keywords.
	And
	Break
	Class
	Continue
	Else
	False
	Fun
	For
	If
	Nil
	Or
	Print
	Return
	Super
	This
	True
	Var
	While

	// Sentinel for end-of-file.
	EOF
)

func (TokenType) String

func (i TokenType) String() string

type Type

type Type interface {
	Accept(v typeVisitor)
}

type UnaryExpr

type UnaryExpr struct {
	Operator Token
	Right    Expr
}

func (*UnaryExpr) Accept

func (e *UnaryExpr) Accept(v exprVisitor)

func (*UnaryExpr) String

func (e *UnaryExpr) String() string

func (*UnaryExpr) TypeName

func (*UnaryExpr) TypeName() string

type VarStmt

type VarStmt struct {
	Name Token
	Init Expr
}

func (VarStmt) Accept

func (s VarStmt) Accept(v stmtVisitor)

func (VarStmt) String

func (s VarStmt) String() string

type VariableExpr

type VariableExpr struct {
	Name Token
}

func (*VariableExpr) Accept

func (e *VariableExpr) Accept(v exprVisitor)

func (*VariableExpr) String

func (e *VariableExpr) String() string

func (*VariableExpr) TypeName

func (*VariableExpr) TypeName() string

Directories

Path Synopsis
cmd
lox

Jump to

Keyboard shortcuts

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