interpreter

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TokenPlus         = "+"
	TokenMinus        = "-"
	TokenMultiply     = "*"
	TokenFloatDiv     = "/"
	TokenLParen       = "("
	TokenRParen       = ")"
	TokenLSquare      = "["
	TokenRSquare      = "]"
	TokenLCurly       = "{"
	TokenRCurly       = "}"
	TokenSemi         = ";"
	TokenDot          = "."
	TokenColon        = ":"
	TokenComma        = ","
	TokenProgram      = "PROGRAM"
	TokenImport       = "IMPORT"
	TokenInteger      = "INTEGER"
	TokenFloat        = "FLOAT"
	TokenString       = "STRING"
	TokenBoolean      = "BOOLEAN"
	TokenList         = "LIST"
	TokenDict         = "DICT"
	TokenMessage      = "MESSAGE"
	TokenIntegerDiv   = "DIV"
	TokenVar          = "VAR"
	TokenFunction     = "FUNCTION"
	TokenBegin        = "BEGIN"
	TokenEnd          = "END"
	TokenReturn       = "RETURN"
	TokenPrint        = "PRINT"
	TokenID           = "ID"
	TokenIntegerConst = "INTEGER_CONST"
	TokenFloatConst   = "FLOAT_CONST"
	TokenStringConst  = "STRING_CONST"
	TokenMessageConst = "MESSAGE_CONST"
	TokenAssign       = ":="
	TokenEOF          = "EOF"
	TokenIf           = "IF"
	TokenThen         = "THEN"
	TokenElse         = "ELSE"
	TokenWhile        = "WHILE"
	TokenDo           = "DO"
	TokenOr           = "OR"
	TokenAnd          = "AND"
	TokenTrue         = "TRUE"
	TokenFalse        = "FALSE"
	TokenEqual        = "=="
	TokenNotEqual     = "!="
	TokenGreater      = ">"
	TokenGreaterEqual = ">="
	TokenLess         = "<"
	TokenLessEqual    = "<="
	TokenAt           = "@"
	TokenHash         = "#"
)
View Source
const BuiltinPackage = "builtin"

Variables

View Source
var ReservedKeywords = map[string]Token{
	"PROGRAM": {Type: TokenProgram, Value: TokenProgram},
	"IMPORT":  {Type: TokenImport, Value: TokenImport},
	"FUNC":    {Type: TokenFunction, Value: TokenFunction},
	"VAR":     {Type: TokenVar, Value: TokenVar},
	"DIV":     {Type: TokenIntegerDiv, Value: TokenIntegerDiv},
	"INT":     {Type: TokenInteger, Value: TokenInteger},
	"FLOAT":   {Type: TokenFloat, Value: TokenFloat},
	"STRING":  {Type: TokenString, Value: TokenString},
	"BOOL":    {Type: TokenBoolean, Value: TokenBoolean},
	"BEGIN":   {Type: TokenBegin, Value: TokenBegin},
	"END":     {Type: TokenEnd, Value: TokenEnd},
	"IF":      {Type: TokenIf, Value: TokenIf},
	"THEN":    {Type: TokenThen, Value: TokenThen},
	"ELSE":    {Type: TokenElse, Value: TokenElse},
	"WHILE":   {Type: TokenWhile, Value: TokenWhile},
	"DO":      {Type: TokenDo, Value: TokenDo},
	"OR":      {Type: TokenOr, Value: TokenOr},
	"AND":     {Type: TokenAnd, Value: TokenAnd},
	"TRUE":    {Type: TokenTrue, Value: true},
	"FALSE":   {Type: TokenFalse, Value: false},
	"PRINT":   {Type: TokenPrint, Value: TokenPrint},
	"RETURN":  {Type: TokenReturn, Value: TokenReturn},
	"LIST":    {Type: TokenList, Value: TokenList},
	"DICT":    {Type: TokenDict, Value: TokenDict},
	"MESSAGE": {Type: TokenMessage, Value: TokenMessage},
}

Functions

This section is empty.

Types

type ARType

type ARType string
const (
	ARTypeProgram  ARType = "PROGRAM"
	ARTypeFunction ARType = "FUNCTION"
)

type ActivationRecord

type ActivationRecord struct {
	Name         string
	Type         ARType
	NestingLevel int
	Members      map[string]interface{}
	ReturnValue  interface{}
}

func NewActivationRecord

func NewActivationRecord(name string, t ARType, nestingLevel int) *ActivationRecord

func (*ActivationRecord) Get

func (r *ActivationRecord) Get(key string) interface{}

func (*ActivationRecord) Set

func (r *ActivationRecord) Set(key string, value interface{})

func (*ActivationRecord) String

func (r *ActivationRecord) String() string

type Assign

type Assign struct {
	Left  Ast
	Op    *Token
	Right Ast
}

func NewAssign

func NewAssign(left Ast, op *Token, right Ast) *Assign

type Ast

type Ast interface{}

type BinOp

type BinOp struct {
	Left  Ast
	Token *Token
	Op    *Token
	Right Ast
}

func NewBinOp

func NewBinOp(left Ast, op *Token, right Ast) *BinOp

type Block

type Block struct {
	Declarations      [][]Ast
	CompoundStatement Ast
}

func NewBlock

func NewBlock(declarations [][]Ast, compoundStatement Ast) *Block

type Boolean

type Boolean struct {
	Token *Token
	Value bool
}

func NewBoolean

func NewBoolean(token *Token) *Boolean

type BuiltinTypeSymbol

type BuiltinTypeSymbol struct {
	Name       string
	Type       Symbol
	ScopeLevel int
}

func NewBuiltinTypeSymbol

func NewBuiltinTypeSymbol(name string) *BuiltinTypeSymbol

func (*BuiltinTypeSymbol) String

func (s *BuiltinTypeSymbol) String() string

type CallFunc

type CallFunc func(i *Interpreter, args []interface{}) interface{}

type CallStack

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

func NewCallStack

func NewCallStack() *CallStack

func (*CallStack) Peek

func (s *CallStack) Peek() *ActivationRecord

func (*CallStack) Pop

func (s *CallStack) Pop() *ActivationRecord

func (*CallStack) Push

func (s *CallStack) Push(ar *ActivationRecord)

func (*CallStack) String

func (s *CallStack) String() string

type Compound

type Compound struct {
	Children []Ast
}

func NewCompound

func NewCompound() *Compound

type Dict

type Dict struct {
	Token *Token
	Value map[string]Ast
}

func NewDict

func NewDict(token *Token) *Dict

type Error

type Error struct {
	ErrorCode ErrorCode
	Token     *Token
	Message   string
	Type      ErrorType
}

func (Error) Error

func (e Error) Error() string

type ErrorCode

type ErrorCode string
const (
	UnexpectedToken   ErrorCode = "Unexpected token"
	IdNotFound        ErrorCode = "Identifier not found"
	DuplicateId       ErrorCode = "Duplicate id found"
	WrongParamsNum    ErrorCode = "Wrong number of arguments"
	UndefinedFunction ErrorCode = "Undefined function"
)

type ErrorType

type ErrorType string
const (
	LexerErrorType    ErrorType = "LexerError"
	ParserErrorType   ErrorType = "ParserError"
	SemanticErrorType ErrorType = "SemanticError"
)

type FunctionCall

type FunctionCall struct {
	PackageName  string
	FuncName     string
	ActualParams []Ast
	Token        *Token
	FuncSymbol   Symbol
}

func NewFunctionCall

func NewFunctionCall(packageName string, funcName string, actualParams []Ast, token *Token) *FunctionCall

type FunctionDecl

type FunctionDecl struct {
	PackageName  string
	FuncName     string
	FormalParams []Ast
	BlockNode    Ast
	ReturnType   Ast
}

func NewFunctionDecl

func NewFunctionDecl(funcName string, formalParams []Ast, blockNode Ast, returnType Ast) *FunctionDecl

type FunctionRef

type FunctionRef struct {
	PackageName string
	FuncName    string
	Token       *Token
}

func NewFunctionRef

func NewFunctionRef(packageName string, funcName string, token *Token) *FunctionRef

type FunctionSymbol

type FunctionSymbol struct {
	Package      string
	Name         string
	FormalParams []Ast
	ReturnType   Ast
	BlockAst     Ast
	ScopeLevel   int
	Call         CallFunc
}

func NewFunctionSymbol

func NewFunctionSymbol(name string) *FunctionSymbol

func (*FunctionSymbol) String

func (s *FunctionSymbol) String() string

type If

type If struct {
	Condition  Ast
	ThenBranch []Ast
	ElseBranch []Ast
}

func NewIf

func NewIf(condition Ast, thenBranch []Ast, elseBranch []Ast) *If

type Interpreter

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

func NewInterpreter

func NewInterpreter(tree Ast) *Interpreter

func (*Interpreter) Interpret

func (i *Interpreter) Interpret() (float64, error)

func (*Interpreter) Stdout

func (i *Interpreter) Stdout() string

func (*Interpreter) Visit

func (i *Interpreter) Visit(node Ast) interface{}

func (*Interpreter) VisitAssign

func (i *Interpreter) VisitAssign(node *Assign) float64

func (*Interpreter) VisitBinOp

func (i *Interpreter) VisitBinOp(node *BinOp) float64

func (*Interpreter) VisitBlock

func (i *Interpreter) VisitBlock(node *Block) float64

func (*Interpreter) VisitBoolean

func (i *Interpreter) VisitBoolean(node *Boolean) bool

func (*Interpreter) VisitCompound

func (i *Interpreter) VisitCompound(node *Compound) float64

func (*Interpreter) VisitDict

func (i *Interpreter) VisitDict(node *Dict) map[string]interface{}

func (*Interpreter) VisitFunctionCall

func (i *Interpreter) VisitFunctionCall(node *FunctionCall) interface{}

func (*Interpreter) VisitFunctionDecl

func (i *Interpreter) VisitFunctionDecl(node *FunctionDecl) float64

func (*Interpreter) VisitFunctionRef

func (i *Interpreter) VisitFunctionRef(node *FunctionRef) *FunctionRef

func (*Interpreter) VisitIf

func (i *Interpreter) VisitIf(node *If) interface{}

func (*Interpreter) VisitList

func (i *Interpreter) VisitList(node *List) []interface{}

func (*Interpreter) VisitLogical

func (i *Interpreter) VisitLogical(node *Logical) bool

func (*Interpreter) VisitMessage

func (i *Interpreter) VisitMessage(node *Message) interface{}

func (*Interpreter) VisitNoOp

func (i *Interpreter) VisitNoOp(node *NoOp) float64

func (*Interpreter) VisitNumber

func (i *Interpreter) VisitNumber(node *Number) float64

func (*Interpreter) VisitPackage

func (i *Interpreter) VisitPackage(node *Package) float64

func (*Interpreter) VisitPrint

func (i *Interpreter) VisitPrint(node *Print) interface{}

func (*Interpreter) VisitProgram

func (i *Interpreter) VisitProgram(node *Program) float64

func (*Interpreter) VisitReturn

func (i *Interpreter) VisitReturn(node *Return) interface{}

func (*Interpreter) VisitString

func (i *Interpreter) VisitString(node *String) string

func (*Interpreter) VisitType

func (i *Interpreter) VisitType(node *Type) float64

func (*Interpreter) VisitUnaryOp

func (i *Interpreter) VisitUnaryOp(node *UnaryOp) float64

func (*Interpreter) VisitVar

func (i *Interpreter) VisitVar(node *Var) interface{}

func (*Interpreter) VisitVarDecl

func (i *Interpreter) VisitVarDecl(node *VarDecl) float64

func (*Interpreter) VisitWhile

func (i *Interpreter) VisitWhile(node *While) float64

type Lexer

type Lexer struct {
	Text        []rune
	Pos         int
	CurrentChar rune
	LineNo      int
	Column      int
}

func NewLexer

func NewLexer(text []rune) *Lexer

func (*Lexer) Advance

func (l *Lexer) Advance()

func (*Lexer) GetNextToken

func (l *Lexer) GetNextToken() (*Token, error)

func (*Lexer) Id

func (l *Lexer) Id() (*Token, error)

func (*Lexer) Message

func (l *Lexer) Message() (*Token, error)

func (*Lexer) Number

func (l *Lexer) Number() (*Token, error)

func (*Lexer) Peek

func (l *Lexer) Peek() rune

func (*Lexer) SkipComment

func (l *Lexer) SkipComment()

func (*Lexer) SkipWhitespace

func (l *Lexer) SkipWhitespace()

func (*Lexer) String

func (l *Lexer) String() (*Token, error)

type List

type List struct {
	Token *Token
	Value []Ast
}

func NewList

func NewList(token *Token) *List

type Logical

type Logical struct {
	Left  Ast
	Op    *Token
	Right Ast
}

func NewLogical

func NewLogical(left Ast, op *Token, right Ast) *Logical

type Message

type Message struct {
	Token *Token
	Value interface{}
}

func NewMessage

func NewMessage(token *Token) *Message

type NoOp

type NoOp struct{}

func NewNoOp

func NewNoOp() *NoOp

type Number

type Number struct {
	Token *Token
	Value float64
}

func NewNumber

func NewNumber(token *Token) *Number

type Package

type Package struct {
	Name string
}

func NewPackage

func NewPackage(name string) *Package

type Param

type Param struct {
	VarNode  Ast
	TypeNode Ast
}

func NewParam

func NewParam(varNode Ast, typeNode Ast) *Param

type Parser

type Parser struct {
	Lexer        *Lexer
	CurrentToken *Token
}

func NewParser

func NewParser(lexer *Lexer) (*Parser, error)

func (*Parser) AssignmentStatement

func (p *Parser) AssignmentStatement() (Ast, error)

func (*Parser) Block

func (p *Parser) Block() (Ast, error)

func (*Parser) Comparison

func (p *Parser) Comparison() (Ast, error)

func (*Parser) CompoundStatement

func (p *Parser) CompoundStatement() (Ast, error)

func (*Parser) Declarations

func (p *Parser) Declarations() ([][]Ast, error)

func (*Parser) Dict

func (p *Parser) Dict() (Ast, error)

func (*Parser) Eat

func (p *Parser) Eat(tokenType TokenType) (err error)

func (*Parser) Empty

func (p *Parser) Empty() (Ast, error)

func (*Parser) Equality

func (p *Parser) Equality() (Ast, error)

func (*Parser) Expr

func (p *Parser) Expr() (Ast, error)

func (*Parser) Expression

func (p *Parser) Expression() (Ast, error)

func (*Parser) Factor

func (p *Parser) Factor() (Ast, error)

func (*Parser) FormalParameterList

func (p *Parser) FormalParameterList() ([]Ast, error)

func (*Parser) FormalParameters

func (p *Parser) FormalParameters() ([]Ast, error)

func (*Parser) FunctionCallStatement

func (p *Parser) FunctionCallStatement() (Ast, error)

func (*Parser) FunctionDeclaration

func (p *Parser) FunctionDeclaration() (Ast, error)

func (*Parser) FunctionReference

func (p *Parser) FunctionReference() (Ast, error)

func (*Parser) IfStatement

func (p *Parser) IfStatement() (Ast, error)

func (*Parser) List

func (p *Parser) List() (Ast, error)

func (*Parser) LogicAnd

func (p *Parser) LogicAnd() (Ast, error)

func (*Parser) LogicOr

func (p *Parser) LogicOr() (Ast, error)

func (*Parser) Package

func (p *Parser) Package() ([]Ast, error)

func (*Parser) Parse

func (p *Parser) Parse() (Ast, error)

func (*Parser) PrintStatement

func (p *Parser) PrintStatement() (Ast, error)

func (*Parser) Program

func (p *Parser) Program() (Ast, error)

func (*Parser) ReturnStatement

func (p *Parser) ReturnStatement() (Ast, error)

func (*Parser) Statement

func (p *Parser) Statement() (Ast, error)

func (*Parser) StatementList

func (p *Parser) StatementList() ([]Ast, error)

func (*Parser) Term

func (p *Parser) Term() (Ast, error)

func (*Parser) TypeSpec

func (p *Parser) TypeSpec() (Ast, error)

func (*Parser) Variable

func (p *Parser) Variable() (Ast, error)

func (*Parser) VariableDeclaration

func (p *Parser) VariableDeclaration() ([]Ast, error)

func (*Parser) WhileStatement

func (p *Parser) WhileStatement() (Ast, error)

type Print

type Print struct {
	Statement Ast
}

func NewPrint

func NewPrint(statement Ast) *Print

type Program

type Program struct {
	Name     string
	Block    Ast
	Packages []Ast
}

func NewProgram

func NewProgram(name string, packages []Ast, block Ast) *Program

type Return

type Return struct {
	Statement Ast
}

func NewReturn

func NewReturn(statement Ast) *Return

type ScopedSymbolTable

type ScopedSymbolTable struct {
	ScopeName      string
	ScopeLevel     int
	EnclosingScope *ScopedSymbolTable
	// contains filtered or unexported fields
}

func NewScopedSymbolTable

func NewScopedSymbolTable(scopeName string, scopeLevel int, enclosingScope *ScopedSymbolTable) *ScopedSymbolTable

func (*ScopedSymbolTable) Insert

func (t *ScopedSymbolTable) Insert(symbol Symbol)

func (*ScopedSymbolTable) Lookup

func (t *ScopedSymbolTable) Lookup(name string, currentScopeOnly bool) Symbol

func (*ScopedSymbolTable) String

func (t *ScopedSymbolTable) String() string

type SemanticAnalyzer

type SemanticAnalyzer struct {
	CurrentScope *ScopedSymbolTable
}

func NewSemanticAnalyzer

func NewSemanticAnalyzer() *SemanticAnalyzer

func (*SemanticAnalyzer) Visit

func (b *SemanticAnalyzer) Visit(node Ast)

func (*SemanticAnalyzer) VisitAssign

func (b *SemanticAnalyzer) VisitAssign(node *Assign)

func (*SemanticAnalyzer) VisitBinOp

func (b *SemanticAnalyzer) VisitBinOp(node *BinOp)

func (*SemanticAnalyzer) VisitBlock

func (b *SemanticAnalyzer) VisitBlock(node *Block)

func (*SemanticAnalyzer) VisitBoolean

func (b *SemanticAnalyzer) VisitBoolean(node *Boolean)

func (*SemanticAnalyzer) VisitCompound

func (b *SemanticAnalyzer) VisitCompound(node *Compound)

func (*SemanticAnalyzer) VisitDict

func (b *SemanticAnalyzer) VisitDict(node *Dict)

func (*SemanticAnalyzer) VisitFunctionCall

func (b *SemanticAnalyzer) VisitFunctionCall(node *FunctionCall)

func (*SemanticAnalyzer) VisitFunctionDecl

func (b *SemanticAnalyzer) VisitFunctionDecl(node *FunctionDecl)

func (*SemanticAnalyzer) VisitFunctionRef

func (b *SemanticAnalyzer) VisitFunctionRef(node *FunctionRef)

func (*SemanticAnalyzer) VisitIf

func (b *SemanticAnalyzer) VisitIf(node *If)

func (*SemanticAnalyzer) VisitList

func (b *SemanticAnalyzer) VisitList(node *List)

func (*SemanticAnalyzer) VisitLogical

func (b *SemanticAnalyzer) VisitLogical(node *Logical)

func (*SemanticAnalyzer) VisitMessage

func (b *SemanticAnalyzer) VisitMessage(node *Message)

func (*SemanticAnalyzer) VisitNoOp

func (b *SemanticAnalyzer) VisitNoOp(node *NoOp)

func (*SemanticAnalyzer) VisitNumber

func (b *SemanticAnalyzer) VisitNumber(node *Number)

func (*SemanticAnalyzer) VisitPackage

func (b *SemanticAnalyzer) VisitPackage(node *Package)

func (*SemanticAnalyzer) VisitPrint

func (b *SemanticAnalyzer) VisitPrint(node *Print)

func (*SemanticAnalyzer) VisitProgram

func (b *SemanticAnalyzer) VisitProgram(node *Program)

func (*SemanticAnalyzer) VisitReturn

func (b *SemanticAnalyzer) VisitReturn(node *Return)

func (*SemanticAnalyzer) VisitString

func (b *SemanticAnalyzer) VisitString(node *String)

func (*SemanticAnalyzer) VisitType

func (b *SemanticAnalyzer) VisitType(node *Type)

func (*SemanticAnalyzer) VisitUnaryOp

func (b *SemanticAnalyzer) VisitUnaryOp(node *UnaryOp)

func (*SemanticAnalyzer) VisitVar

func (b *SemanticAnalyzer) VisitVar(node *Var)

func (*SemanticAnalyzer) VisitVarDecl

func (b *SemanticAnalyzer) VisitVarDecl(node *VarDecl)

func (*SemanticAnalyzer) VisitWhile

func (b *SemanticAnalyzer) VisitWhile(node *While)

type String

type String struct {
	Token *Token
	Value string
}

func NewString

func NewString(token *Token) *String

type Symbol

type Symbol interface{}

type Token

type Token struct {
	Type   TokenType
	Value  interface{}
	LineNo int
	Column int
}

func (*Token) String

func (t *Token) String() string

type TokenType

type TokenType string

type Type

type Type struct {
	Token *Token
	Value interface{}
}

func NewType

func NewType(token *Token) *Type

type UnaryOp

type UnaryOp struct {
	Op   *Token
	Expr Ast
}

func NewUnaryOp

func NewUnaryOp(op *Token, expr Ast) *UnaryOp

type Var

type Var struct {
	Token *Token
	Value interface{}
}

func NewVar

func NewVar(token *Token) *Var

type VarDecl

type VarDecl struct {
	VarNode  Ast
	TypeNode Ast
}

func NewVarDecl

func NewVarDecl(varNode Ast, typeNode Ast) *VarDecl

type VarSymbol

type VarSymbol struct {
	Name       string
	Type       Symbol
	ScopeLevel int
}

func NewVarSymbol

func NewVarSymbol(name string, t Symbol) *VarSymbol

func (*VarSymbol) String

func (s *VarSymbol) String() string

type While

type While struct {
	Condition Ast
	DoBranch  []Ast
}

func NewWhile

func NewWhile(condition Ast, doBranch []Ast) *While

Jump to

Keyboard shortcuts

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