asm

package
v0.0.0-...-3bf39c3 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chunk

type Chunk struct {
	Sig    string
	Ver    uint32
	Consts []*Const
	Fn     FnShape
}

func Compile

func Compile(code string, externals []string) *Chunk

func NewChunk

func NewChunk() *Chunk

func (*Chunk) AddConstNum

func (c *Chunk) AddConstNum(num float64) int

func (*Chunk) AddConstStr

func (c *Chunk) AddConstStr(str string) int

func (*Chunk) ConstStr

func (c *Chunk) ConstStr(i int) string

func (*Chunk) Dump

func (c *Chunk) Dump() string

func (*Chunk) GetConst

func (c *Chunk) GetConst(i int) *Const

func (*Chunk) HasConstStr

func (c *Chunk) HasConstStr(i int) bool

func (*Chunk) IdxOfConstNum

func (c *Chunk) IdxOfConstNum(num float64) int

func (*Chunk) IdxOfConstStr

func (c *Chunk) IdxOfConstStr(str string) int

type CodegenVisitor

type CodegenVisitor struct {
	parser.BaseRippletParserVisitor
	// contains filtered or unexported fields
}

func NewCodegenVisitor

func NewCodegenVisitor(symtab *SymTab) *CodegenVisitor

func (*CodegenVisitor) Finalize

func (v *CodegenVisitor) Finalize() *Chunk

func (*CodegenVisitor) Visit

func (v *CodegenVisitor) Visit(tree antlr.ParseTree) interface{}

func (*CodegenVisitor) VisitAddExpr

func (v *CodegenVisitor) VisitAddExpr(ctx *parser.AddExprContext) interface{}

func (*CodegenVisitor) VisitArgument

func (v *CodegenVisitor) VisitArgument(ctx *parser.ArgumentContext) interface{}

func (*CodegenVisitor) VisitArguments

func (v *CodegenVisitor) VisitArguments(ctx *parser.ArgumentsContext) int

func (*CodegenVisitor) VisitArrayExpr

func (v *CodegenVisitor) VisitArrayExpr(ctx *parser.ArrayExprContext) interface{}

func (*CodegenVisitor) VisitArrayLiteral

func (v *CodegenVisitor) VisitArrayLiteral(ctx *parser.ArrayLiteralContext) interface{}

func (*CodegenVisitor) VisitAssignStmt

func (v *CodegenVisitor) VisitAssignStmt(ctx *parser.AssignStmtContext) interface{}

func (*CodegenVisitor) VisitBlockStmt

func (v *CodegenVisitor) VisitBlockStmt(ctx *parser.BlockStmtContext) interface{}

func (*CodegenVisitor) VisitBoolLiteral

func (v *CodegenVisitor) VisitBoolLiteral(ctx *parser.BoolLiteralContext) interface{}

func (*CodegenVisitor) VisitBreakStmt

func (v *CodegenVisitor) VisitBreakStmt(ctx *parser.BreakStmtContext) interface{}

01: OPCODE_X 02: OPCODE_X 03: JMP ─────────────────┐ 04: OPCODE_X │ 05: TEST │ 06: JMP_F │ ------------- │ 07: OPCODE_X ◄──────────┘

func (*CodegenVisitor) VisitCallExpr

func (v *CodegenVisitor) VisitCallExpr(ctx *parser.CallExprContext) interface{}

func (*CodegenVisitor) VisitChildren

func (v *CodegenVisitor) VisitChildren(node antlr.RuleNode) interface{}

func (*CodegenVisitor) VisitEqualityExpr

func (v *CodegenVisitor) VisitEqualityExpr(ctx *parser.EqualityExprContext) interface{}

func (*CodegenVisitor) VisitExprStmt

func (v *CodegenVisitor) VisitExprStmt(ctx *parser.ExprStmtContext) interface{}

func (*CodegenVisitor) VisitFnBody

func (v *CodegenVisitor) VisitFnBody(ctx *parser.FnBodyContext) interface{}

func (*CodegenVisitor) VisitFnExpr

func (v *CodegenVisitor) VisitFnExpr(ctx *parser.FnExprContext) interface{}

func (*CodegenVisitor) VisitFormalParamList

func (v *CodegenVisitor) VisitFormalParamList(ctx *parser.FormalParamListContext) interface{}

func (*CodegenVisitor) VisitFormalParams

func (v *CodegenVisitor) VisitFormalParams(ctx *parser.FormalParamsContext) interface{}

func (*CodegenVisitor) VisitHexLiteral

func (v *CodegenVisitor) VisitHexLiteral(ctx *parser.HexLiteralContext) interface{}

func (*CodegenVisitor) VisitIdentifer

func (v *CodegenVisitor) VisitIdentifer(ctx *parser.IdentiferContext, store bool, load bool, arg bool) interface{}

func (*CodegenVisitor) VisitIdentifierExpr

func (v *CodegenVisitor) VisitIdentifierExpr(ctx *parser.IdentifierExprContext, store bool, load bool, arg bool) interface{}

func (*CodegenVisitor) VisitIfStmt

func (v *CodegenVisitor) VisitIfStmt(ctx *parser.IfStmtContext) interface{}

01: TEST 02: JMP_F ─────────────────┐ 03: OFFSET │ 04: OPCODE_X │ 05: OPCODE_X │ 06: JMP ──────────────────┼────────┐ 07: OFFSET │ │ 08: OPCODE_X ◄─────────────┘ │ 09: OPCODE_X │ ------------- │ 10: OPCODE_X ◄──────────────────────┘

func (*CodegenVisitor) VisitIntLiteral

func (v *CodegenVisitor) VisitIntLiteral(ctx *parser.IntLiteralContext) interface{}

func (*CodegenVisitor) VisitLiteral

func (v *CodegenVisitor) VisitLiteral(ctx *parser.LiteralContext) interface{}

func (*CodegenVisitor) VisitLiteralExpr

func (v *CodegenVisitor) VisitLiteralExpr(ctx *parser.LiteralExprContext) interface{}

func (*CodegenVisitor) VisitLogicExpr

func (v *CodegenVisitor) VisitLogicExpr(ctx *parser.LogicExprContext) interface{}

func (*CodegenVisitor) VisitMulExpr

func (v *CodegenVisitor) VisitMulExpr(ctx *parser.MulExprContext) interface{}

func (*CodegenVisitor) VisitNegativeExpr

func (v *CodegenVisitor) VisitNegativeExpr(ctx *parser.NegativeExprContext) interface{}

func (*CodegenVisitor) VisitNilLiteral

func (v *CodegenVisitor) VisitNilLiteral(ctx *parser.NilLiteralContext) interface{}

func (*CodegenVisitor) VisitNumberLiteral

func (v *CodegenVisitor) VisitNumberLiteral(ctx *parser.NumberLiteralContext) interface{}

func (*CodegenVisitor) VisitProgram

func (v *CodegenVisitor) VisitProgram(ctx *parser.ProgramContext) interface{}

func (*CodegenVisitor) VisitRealLiteral

func (v *CodegenVisitor) VisitRealLiteral(ctx *parser.RealLiteralContext) interface{}

func (*CodegenVisitor) VisitRelationExpr

func (v *CodegenVisitor) VisitRelationExpr(ctx *parser.RelationExprContext) interface{}

func (*CodegenVisitor) VisitRepeatStmt

func (v *CodegenVisitor) VisitRepeatStmt(ctx *parser.RepeatStmtContext) interface{}

01: OPCODE_X 02: OPCODE_X 03: OPCODE_X 04: TEST 05: JMP_F

func (*CodegenVisitor) VisitReturnStmt

func (v *CodegenVisitor) VisitReturnStmt(ctx *parser.ReturnStmtContext) interface{}

func (*CodegenVisitor) VisitStatement

func (v *CodegenVisitor) VisitStatement(ctx *parser.StatementContext) interface{}

func (*CodegenVisitor) VisitStringInterpExpr

func (v *CodegenVisitor) VisitStringInterpExpr(ctx *parser.StringInterpExprContext) interface{}

func (*CodegenVisitor) VisitStringLiteral

func (v *CodegenVisitor) VisitStringLiteral(ctx *parser.StringLiteralContext) interface{}

func (*CodegenVisitor) VisitSubscriptExpr

func (v *CodegenVisitor) VisitSubscriptExpr(ctx *parser.SubscriptExprContext, store bool) interface{}

func (*CodegenVisitor) VisitVarDeclareLhs

func (v *CodegenVisitor) VisitVarDeclareLhs(ctx *parser.VarDeclareLhsContext) interface{}

func (*CodegenVisitor) VisitVarDeclareStmt

func (v *CodegenVisitor) VisitVarDeclareStmt(ctx *parser.VarDeclareStmtContext) interface{}

type Const

type Const struct {
	Typ ConstType
	Val interface{}
}

type ConstType

type ConstType = uint8
const (
	ConstStr ConstType = iota
	ConstNum
)

type FnPlot

type FnPlot struct {
	// scope id, used to retrieve its associate scope
	Scope uint

	Shape  *FnShape
	Parent *FnPlot

	// where this object is located at its `Parent.Subs`
	IdxInParent int
	Subs        []*FnPlot
}

FnPlot consist of the infomation collected and required for the function instructions generation

func NewFnPlot

func NewFnPlot(scope uint) *FnPlot

func (*FnPlot) ToShape

func (f *FnPlot) ToShape() *FnShape

type FnShape

type FnShape struct {
	Pos      Position
	LocalCnt int
	Instrs   []int
	Upvals   map[string]UpLoc
	Subs     []*FnShape
}

func NewFnShape

func NewFnShape() *FnShape

func (*FnShape) Dump

func (f *FnShape) Dump(chunk *Chunk) string

type Opcode

type Opcode int

TODO: reduce op size to byte to get a better memory footprint

const (
	CONST Opcode = iota
	LOAD
	LOAD_UP
	STORE
	STORE_UP
	LOAD_EXT
	CLOSURE
	LOAD_ARG
	ADD
	SUB
	MUL
	DIV
	MOD
	ARR
	LOAD_ARR
	STORE_ARR
	CONCAT
	CALL
	RET
	NOT
	TEST
	JMP_F
	JMP
	IS
	IS_NOT
	GT
	LT
	GE
	LE
	OR
	AND
	NEG
	BOOL_T
	BOOL_F
	NIL
)

func (Opcode) String

func (op Opcode) String() string

type Position

type Position struct {
	Line   int
	Column int
}

type RepeatInfo

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

type Scope

type Scope struct {
	// an auto-increment number which is generated according
	// the depth-first walk over the entire AST
	Id uint

	Parent *Scope
	Subs   []*Scope

	// formal parameters will be added in both `Params` and `Bindings`
	Params    map[string]int
	ParamsLen int

	Bindings    map[string]int
	BindingsLen int
}

func NewScope

func NewScope() *Scope

func (*Scope) AddBinding

func (s *Scope) AddBinding(name string)

func (*Scope) AddParam

func (s *Scope) AddParam(name string)

func (*Scope) HasBinding

func (s *Scope) HasBinding(name string) bool

func (*Scope) HasLocal

func (s *Scope) HasLocal(name string) bool

func (*Scope) HasParam

func (s *Scope) HasParam(name string) bool

func (*Scope) LocalCnt

func (s *Scope) LocalCnt() int

func (*Scope) LocalIdx

func (s *Scope) LocalIdx(name string) int

func (*Scope) ParentLocalIdx

func (s *Scope) ParentLocalIdx(name string) int

type SymTab

type SymTab struct {
	Externals []string
	Scopes    map[uint]*Scope
	Root      *Scope
	Cur       *Scope
}

func NewSymTab

func NewSymTab(externals []string) *SymTab

func (*SymTab) EnterScope

func (s *SymTab) EnterScope()

func (*SymTab) HasExternal

func (s *SymTab) HasExternal(name string) bool

func (*SymTab) LeaveScope

func (s *SymTab) LeaveScope()

type SymTabListener

type SymTabListener struct {
	*parser.BaseRippletParserListener
	// contains filtered or unexported fields
}

func NewSymTabListener

func NewSymTabListener(externals []string) *SymTabListener

func (*SymTabListener) EnterBlockStmt

func (s *SymTabListener) EnterBlockStmt(ctx *parser.BlockStmtContext)

func (*SymTabListener) EnterFnName

func (s *SymTabListener) EnterFnName(ctx *parser.FnNameContext)

func (*SymTabListener) EnterFormalParams

func (s *SymTabListener) EnterFormalParams(ctx *parser.FormalParamsContext)

func (*SymTabListener) EnterIdentifer

func (s *SymTabListener) EnterIdentifer(ctx *parser.IdentiferContext)

func (*SymTabListener) EnterMathClauses

func (s *SymTabListener) EnterMathClauses(ctx *parser.MathClausesContext)

func (*SymTabListener) EnterProgram

func (s *SymTabListener) EnterProgram(ctx *parser.ProgramContext)

func (*SymTabListener) EnterVarDeclareLhs

func (s *SymTabListener) EnterVarDeclareLhs(ctx *parser.VarDeclareLhsContext)

func (*SymTabListener) ExitBlockStmt

func (s *SymTabListener) ExitBlockStmt(ctx *parser.BlockStmtContext)

func (*SymTabListener) ExitFnBody

func (s *SymTabListener) ExitFnBody(ctx *parser.FnBodyContext)

func (*SymTabListener) ExitFnName

func (s *SymTabListener) ExitFnName(ctx *parser.FnNameContext)

func (*SymTabListener) ExitFormalParams

func (s *SymTabListener) ExitFormalParams(ctx *parser.FormalParamsContext)

func (*SymTabListener) ExitMathClauses

func (s *SymTabListener) ExitMathClauses(ctx *parser.MathClausesContext)

func (*SymTabListener) ExitProgram

func (s *SymTabListener) ExitProgram(ctx *parser.ProgramContext)

func (*SymTabListener) ExitVarDeclareLhs

func (s *SymTabListener) ExitVarDeclareLhs(ctx *parser.VarDeclareLhsContext)

func (*SymTabListener) Resolve

func (s *SymTabListener) Resolve(tree *parser.IProgramContext) (symtab *SymTab, err error)

type UpLoc

type UpLoc struct {
	Typ UpLocType
	At  interface{}
}

type UpLocType

type UpLocType int
const (
	UP_LOC_LOCAL UpLocType = iota
	UP_LOC_UP
)

Jump to

Keyboard shortcuts

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