lang

package
v0.0.0-...-51ce559 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2020 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Void     = ""
	Int      = "int"
	String   = "string"
	Float64  = "float64"
	Bool     = "bool"
	Anything = "interface{}"
	Writer   = "io.Writer"

	SQL = "std.SQL"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Assign

type Assign struct {
	Right *Expression

	FirstDefinition bool
	// contains filtered or unexported fields
}

func NewAssign

func NewAssign(left *Variable, right Expression) (*Assign, error)

TODO: Behaviour is not the same like it is in other New* functions, missing parent. This should probably be unified.

func (Assign) Left

func (a Assign) Left() *Variable

func (Assign) Parent

func (a Assign) Parent() Node

func (*Assign) SetParent

func (a *Assign) SetParent(n Node)

func (Assign) String

func (a Assign) String() string

func (Assign) Type

func (a Assign) Type() Typ

type BinaryOp

type BinaryOp struct {
	Operation string
	// contains filtered or unexported fields
}

func NewBinaryOp

func NewBinaryOp(op string, left, right Expression) (*BinaryOp, error)

func (BinaryOp) OperatorPrecedence

func (p BinaryOp) OperatorPrecedence() int

See https://golang.ir/ref/spec#Operator_precedence

func (BinaryOp) Parent

func (p BinaryOp) Parent() Node

func (*BinaryOp) SetParent

func (p *BinaryOp) SetParent(n Node)

func (BinaryOp) String

func (p BinaryOp) String() string

func (BinaryOp) Type

func (p BinaryOp) Type() Typ

type Block

type Block interface {
	Node

	AddStatement(Node)
	DefineVariable(*Variable)

	// HasVariable tries to find a variable definition
	// at any cost, even searching out of scope. It is
	// bounded to the DefinesVariable, this function
	// does the out of scope search, HasVariable changes
	// the definition so it can be used in Go.
	HasVariable(string, bool) *Variable
	// contains filtered or unexported methods
}

Block is the second most common type found in the AST. It can contain nodes, and has own symbol table, in my case it is just an array of defined variables.

type Break

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

func (Break) Parent

func (b Break) Parent() Node

func (*Break) SetParent

func (b *Break) SetParent(n Node)

func (Break) String

func (b Break) String() string

type Case

type Case struct {
	Block     *Code
	Condition Expression
	// contains filtered or unexported fields
}

func NewCase

func NewCase(parent *Switch) *Case

func (Case) AddStatement

func (Case) AddStatement(Node)

func (Case) DefineVariable

func (Case) DefineVariable(*Variable)

func (*Case) HasVariable

func (c *Case) HasVariable(name string, oos bool) *Variable

func (Case) Parent

func (c Case) Parent() Node

func (*Case) SetParent

func (c *Case) SetParent(n Node)

func (Case) String

func (c Case) String() string

type Code

type Code struct {
	Vars       []*Variable
	Statements []Node
	// contains filtered or unexported fields
}

func NewCode

func NewCode(parent Node) *Code

func (*Code) AddStatement

func (c *Code) AddStatement(n Node)

func (*Code) DefineVariable

func (c *Code) DefineVariable(v *Variable)

func (*Code) HasVariable

func (c *Code) HasVariable(name string, oos bool) *Variable

func (Code) Parent

func (c Code) Parent() Node

func (*Code) SetParent

func (c *Code) SetParent(n Node)

func (Code) String

func (c Code) String() string

type Const

type Const struct {
	Value string
	// contains filtered or unexported fields
}

func (Const) Parent

func (c Const) Parent() Node

func (*Const) SetParent

func (c *Const) SetParent(n Node)

func (Const) String

func (c Const) String() string

func (Const) Type

func (c Const) Type() Typ

TODO: Is this correct return type?

type Continue

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

func (Continue) Parent

func (c Continue) Parent() Node

func (*Continue) SetParent

func (c *Continue) SetParent(n Node)

func (Continue) String

func (c Continue) String() string

type Dec

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

func NewDec

func NewDec(parent Node, v *VarRef, strV func(Expression) (Expression, error)) *Dec

func (Dec) Parent

func (d Dec) Parent() Node

func (*Dec) SetParent

func (d *Dec) SetParent(n Node)

func (Dec) String

func (d Dec) String() string

func (Dec) UsedVar

func (d Dec) UsedVar() *Variable

type Default

type Default struct {
	Block *Code
	// contains filtered or unexported fields
}

func NewDefault

func NewDefault(parent *Switch) *Default

func (Default) AddStatement

func (Default) AddStatement(Node)

func (Default) DefineVariable

func (Default) DefineVariable(*Variable)

func (*Default) HasVariable

func (d *Default) HasVariable(name string, oos bool) *Variable

func (Default) Parent

func (d Default) Parent() Node

func (*Default) SetParent

func (d *Default) SetParent(n Node)

func (Default) String

func (d Default) String() string

type Expression

type Expression interface {
	Node

	// Type returns type of the expression.
	Type() Typ
}

Expression the the most common type here. It represents everything, which can return value. In the hielarchy it is on the same level as Block, just another Node representation. But this one returns value, thanks to that it can be used in the arithmetic operations and so on.

type Fallthrough

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

func (Fallthrough) Parent

func (c Fallthrough) Parent() Node

func (*Fallthrough) SetParent

func (c *Fallthrough) SetParent(n Node)

func (Fallthrough) String

func (c Fallthrough) String() string

type File

type File struct {
	Name string

	Funcs map[string]*Function

	Main *Function
	// contains filtered or unexported fields
}

func NewFile

func NewFile(gc *GlobalContext, name string, server, withMain bool) *File

func (*File) Add

func (f *File) Add(fc *Function)

func (*File) AddImport

func (f *File) AddImport(name string)

func (*File) DefineVariable

func (f *File) DefineVariable(v *Variable)

func (File) HasVariable

func (f File) HasVariable(name string, oos bool) *Variable

func (File) Parent

func (f File) Parent() Node

func (File) SetParent

func (f File) SetParent(n Node)

func (*File) String

func (f *File) String() string

type Float

type Float struct {
	Value string
	// contains filtered or unexported fields
}

Float and Number can be merged, only Type is different. There is no way how can I write down 0.0 as some nice string for := operator.

func (Float) Parent

func (f Float) Parent() Node

func (*Float) SetParent

func (f *Float) SetParent(n Node)

func (Float) String

func (f Float) String() string

func (Float) Type

func (f Float) Type() Typ

type For

type For struct {
	Vars []*Variable

	Init Node

	Loop Node

	Block *Code

	Labels []Const
	// contains filtered or unexported fields
}

func NewFor

func NewFor(parent Block) *For

func (*For) AddStatement

func (f *For) AddStatement(n Node)

func (*For) DefineVariable

func (f *For) DefineVariable(v *Variable)

func (For) HasVariable

func (f For) HasVariable(name string, oos bool) *Variable

func (For) Parent

func (f For) Parent() Node

func (*For) SetCond

func (f *For) SetCond(e Expression) error

func (*For) SetParent

func (f *For) SetParent(n Node)

func (For) String

func (f For) String() string

type Foreach

type Foreach struct {
	Iterated Expression

	Key   *Variable
	Value Variable

	Block *Code

	Labels []Const
	// contains filtered or unexported fields
}

TODO: This does not work as it should, that variable assignment is done just by a simple string, I do not care if these values are used later on. This makes sense if I want to use this structure for other languages, too. Range is fairly specific.

func NewForeach

func NewForeach(parent Block) *Foreach

func (*Foreach) AddStatement

func (f *Foreach) AddStatement(n Node)

func (*Foreach) DefineVariable

func (f *Foreach) DefineVariable(v *Variable)

func (Foreach) HasVariable

func (f Foreach) HasVariable(name string, oos bool) *Variable

func (Foreach) Parent

func (f Foreach) Parent() Node

func (*Foreach) SetParent

func (f *Foreach) SetParent(n Node)

func (Foreach) String

func (f Foreach) String() string

type Function

type Function struct {
	Args          []*Variable
	VariadicCount bool

	Body Code

	NeedsGlobal bool

	Name   string
	Return Typ
	// contains filtered or unexported fields
}

func NewFunc

func NewFunc(name string) *Function

func (*Function) AddStatement

func (f *Function) AddStatement(n Node)

func (*Function) DefineVariable

func (f *Function) DefineVariable(v *Variable)

func (*Function) HasVariable

func (f *Function) HasVariable(name string, oos bool) *Variable

func (Function) Parent

func (f Function) Parent() Node

func (*Function) SetParent

func (f *Function) SetParent(n Node)

func (Function) String

func (f Function) String() string

func (Function) Type

func (f Function) Type() Typ

type FunctionCall

type FunctionCall struct {
	Func   *Function
	Name   string
	Args   []Expression
	Return Typ
	// contains filtered or unexported fields
}

func (*FunctionCall) AddArg

func (f *FunctionCall) AddArg(e Expression)

func (FunctionCall) Parent

func (f FunctionCall) Parent() Node

func (*FunctionCall) SetParent

func (f *FunctionCall) SetParent(n Node)

func (*FunctionCall) String

func (f *FunctionCall) String() string

func (FunctionCall) Type

func (f FunctionCall) Type() Typ

type GlobalContext

type GlobalContext struct {
	Path string

	Files []*File

	Vars []*Variable
}

func NewGlobalContext

func NewGlobalContext() *GlobalContext

func (*GlobalContext) Add

func (gc *GlobalContext) Add(f *File)

func (*GlobalContext) DefineVariable

func (gc *GlobalContext) DefineVariable(v *Variable)

func (*GlobalContext) HasVariable

func (gc *GlobalContext) HasVariable(name string, oos bool) *Variable

func (GlobalContext) String

func (gc GlobalContext) String() string

type Goto

type Goto struct {
	Value Const
	// contains filtered or unexported fields
}

func (Goto) Parent

func (c Goto) Parent() Node

func (*Goto) SetParent

func (c *Goto) SetParent(n Node)

func (Goto) String

func (c Goto) String() string

func (Goto) Type

func (c Goto) Type() Typ

type If

type If struct {
	Vars []*Variable

	Init Node

	True  *Code
	False Block
	// contains filtered or unexported fields
}

func NewIf

func NewIf(parent Block) *If

func (*If) AddStatement

func (i *If) AddStatement(n Node)

func (*If) DefineVariable

func (i *If) DefineVariable(v *Variable)

func (If) HasVariable

func (i If) HasVariable(name string, oos bool) *Variable

func (If) Parent

func (i If) Parent() Node

func (*If) SetCond

func (i *If) SetCond(e Expression) error

func (*If) SetParent

func (i *If) SetParent(n Node)

func (If) String

func (i If) String() string

type Inc

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

func NewInc

func NewInc(parent Node, v *VarRef, strV func(Expression) (Expression, error)) *Inc

func (Inc) Parent

func (d Inc) Parent() Node

func (*Inc) SetParent

func (d *Inc) SetParent(n Node)

func (Inc) String

func (i Inc) String() string

func (Inc) UsedVar

func (d Inc) UsedVar() *Variable

type Negation

type Negation struct {
	Right Expression
	// contains filtered or unexported fields
}

func (Negation) Parent

func (neg Negation) Parent() Node

func (*Negation) SetParent

func (neg *Negation) SetParent(n Node)

func (Negation) String

func (neg Negation) String() string

func (Negation) Type

func (neg Negation) Type() Typ

type Node

type Node interface {
	fmt.Stringer

	// SetParent is needed to have an
	// option to move up in the tree.
	SetParent(Node)

	// Parent returns node's predecessor.
	Parent() Node
}

Node represents the most simple thing which can appear in the AST.

type Number

type Number struct {
	Value string
	// contains filtered or unexported fields
}

func (Number) Parent

func (nb Number) Parent() Node

func (*Number) SetParent

func (nb *Number) SetParent(n Node)

func (Number) String

func (nb Number) String() string

func (Number) Type

func (nb Number) Type() Typ

type Return

type Return struct {
	Expression Expression
	// contains filtered or unexported fields
}

func (Return) Parent

func (r Return) Parent() Node

func (*Return) SetParent

func (r *Return) SetParent(n Node)

func (Return) String

func (r Return) String() string

func (Return) Type

func (r Return) Type() Typ

type Str

type Str struct {
	Value string
	// contains filtered or unexported fields
}

func (Str) Parent

func (s Str) Parent() Node

func (*Str) SetParent

func (s *Str) SetParent(n Node)

func (Str) String

func (s Str) String() string

func (Str) Type

func (s Str) Type() Typ

type Switch

type Switch struct {
	Condition Expression
	// Default will end up here too,
	// to keep the order from PHP.
	Cases []Node

	Labels []Const
	// contains filtered or unexported fields
}

func (*Switch) AddStatement

func (sw *Switch) AddStatement(n Node)

func (*Switch) DefineVariable

func (sw *Switch) DefineVariable(v *Variable)

func (Switch) HasVariable

func (sw Switch) HasVariable(name string, oos bool) *Variable

func (Switch) Parent

func (sw Switch) Parent() Node

func (*Switch) SetParent

func (sw *Switch) SetParent(n Node)

func (Switch) String

func (sw Switch) String() string

type Typ

type Typ struct {
	IsPointer bool

	Addressable bool
	Tiles       map[string]Typ
	// contains filtered or unexported fields
}

func NewTyp

func NewTyp(typ string, IsPointer bool) Typ

func (Typ) Eq

func (t Typ) Eq(r Typ) bool

func (Typ) Equal

func (t Typ) Equal(s string) bool

func (Typ) Format

func (t Typ) Format() string

func (Typ) String

func (t Typ) String() string

type UnaryMinus

type UnaryMinus struct {
	Expr Expression
	// contains filtered or unexported fields
}

func (UnaryMinus) Parent

func (m UnaryMinus) Parent() Node

func (*UnaryMinus) SetParent

func (m *UnaryMinus) SetParent(n Node)

func (UnaryMinus) String

func (m UnaryMinus) String() string

func (UnaryMinus) Type

func (m UnaryMinus) Type() Typ

type VarDef

type VarDef struct {
	V     *Variable
	Right Expression
	// contains filtered or unexported fields
}

func (VarDef) Parent

func (v VarDef) Parent() Node

func (*VarDef) SetParent

func (v *VarDef) SetParent(n Node)

func (VarDef) String

func (v VarDef) String() string

type VarRef

type VarRef struct {
	V *Variable
	// contains filtered or unexported fields
}

func NewVarRef

func NewVarRef(v *Variable, t Typ) *VarRef

func (*VarRef) ByReference

func (v *VarRef) ByReference() error

func (VarRef) Parent

func (v VarRef) Parent() Node

func (*VarRef) SetParent

func (v *VarRef) SetParent(n Node)

func (VarRef) String

func (v VarRef) String() string

func (VarRef) Type

func (v VarRef) Type() Typ

type Variable

type Variable struct {
	Name  string
	Const bool

	FirstDefinition Node

	CurrentType Typ
	// contains filtered or unexported fields
}

func NewVariable

func NewVariable(name string, typ Typ, isConst bool) *Variable

func (Variable) String

func (v Variable) String() string

func (Variable) Type

func (v Variable) Type() Typ

Jump to

Keyboard shortcuts

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