compiler

package
v0.0.0-...-fb5d26c Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2019 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddEval

func AddEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value

AddEval generates IR for add

func CmpEQEval

func CmpEQEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value

CmpEQEval generates IR for =

func CmpGEEval

func CmpGEEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value

CmpGEEval generates IR for >=

func CmpGTEval

func CmpGTEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value

CmpGTEval generates IR for >

func CmpLEEval

func CmpLEEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value

CmpLEEval generates IR for <=

func CmpLTEval

func CmpLTEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value

CmpLTEval generates IR for <

func CmpNEEval

func CmpNEEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value

CmpNEEval generates IR for <>

func Compile

func Compile(ast *Ast) *ir.Module

Compile compiles the ast.

func DivideEval

func DivideEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value

DivideEval generates IR for divide

func MinusEval

func MinusEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value

MinusEval generates IR for minus

func MultipleEval

func MultipleEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value

MultipleEval generates IR for multiple

func OppositeEval

func OppositeEval(scope *Scope, value value.Value) value.Value

OppositeEval generates IR for opposite

Types

type Addition

type Addition struct {
	//Pos   lexer.Position
	Head  Multiplication `@@`
	Items []*OpAddition  `(@@)*`
}

Addition adds two or more values

func (*Addition) Evaluate

func (a *Addition) Evaluate(scope *Scope) value.Value

Evaluate evalutes addition

type Ast

type Ast struct {
	Instructions []*Instruction `(@@)+`
}

Ast is the instructions assembly

func Parse

func Parse(f io.Reader) *Ast

Parse parses the input into ast.

func (*Ast) Compile

func (ast *Ast) Compile(scope *Scope)

Compile compiles the ast It splits them into different instructions.

type Comparison

type Comparison struct {
	//Pos   lexer.Position
	Head  Addition        `@@`
	Items []*OpComparison `(@@)*`
}

Comparison compares two or more values

func (*Comparison) Evaluate

func (c *Comparison) Evaluate(scope *Scope) value.Value

Evaluate evalutes comparison

type Constant

type Constant struct {
	//Pos     lexer.Position
	VBool   *string  `  @("TRUE"|"FALSE")`
	VString *string  `| @String`
	VReal   *float64 `| @Float`
	VInt    *int64   `| @Int`
}

Constant shows direct value

func (*Constant) Evaluate

func (c *Constant) Evaluate(scope *Scope) value.Value

Evaluate gets the value of the constant. If it is a string, it would be stored as a global variable.

type Expression

type Expression struct {
	//Pos        lexer.Position
	Comparison Comparison `@@`
}

Expression is expression of an value

func (*Expression) Evaluate

func (e *Expression) Evaluate(scope *Scope) value.Value

Evaluate evalues the expression and generates IR.

type FunctionCall

type FunctionCall struct {
	Name   string        `@Ident`
	Params []*Expression `"(" (@@ ("," @@)*)? ")"`
}

func (*FunctionCall) Compile

func (f *FunctionCall) Compile(scope *Scope) value.Value

type InstAssignment

type InstAssignment struct {
	Pos   lexer.Position
	Left  Key        `@@ "<"`
	Right Expression `"-" @@ EOL`
}

InstAssignment assigns a variable the value of an expression

Example:

a <- 1

func (*InstAssignment) Compile

func (ins *InstAssignment) Compile(scope *Scope)

Compile compiles InstAssignment

type InstCall

type InstCall struct {
	Pos      lexer.Position
	Function *FunctionCall `"CALL" @@ EOL`
}

InstCall creates call block

Example:

CALL puts("Hello World!")

func (*InstCall) Compile

func (ins *InstCall) Compile(scope *Scope)

type InstConditionBr

type InstConditionBr struct {
	Pos       lexer.Position
	Condition Expression `"IF" @@ EOL`
	TrueBr    Ast        `"THEN" EOL @@`
	FalseBr   *Ast       `("ELSE" EOL @@)?`
	END       string     `"ENDIF" EOL`
}

InstConditionBr creates if..then..else...

Example:

IF 1==1
  THEN
    OUTPUT "TURE"
ENDIF

func (*InstConditionBr) Compile

func (ins *InstConditionBr) Compile(scope *Scope)

Compile compiles InstConditionBr

type InstDeclareVariable

type InstDeclareVariable struct {
	Pos  lexer.Position
	Name string       `"DECLARE" @Ident`
	Type VariableType `":" @@ EOL`
}

InstDeclareVariable declares a variable.

Example:

DECLARE a : INT

func (*InstDeclareVariable) Compile

func (ins *InstDeclareVariable) Compile(scope *Scope)

Compile compiles InstDeclareVariable

type InstInput

type InstInput struct {
	Pos     lexer.Position
	Content Key `"INPUT" @@ EOL`
}

func (*InstInput) Compile

func (ins *InstInput) Compile(scope *Scope)

Compile compiles InstInput

type InstOutput

type InstOutput struct {
	Pos     lexer.Position
	Content Expression `"OUTPUT" @@ EOL`
}

InstOutput outputs string Example:

OUTPUT "Hello World!\n"

func (*InstOutput) Compile

func (ins *InstOutput) Compile(scope *Scope)

Compile compiles InstOutput

type InstPrintfD

type InstPrintfD struct {
	Pos     lexer.Position
	Content Expression `"PrintfD" @@ EOL`
}

InstPrintfD outputs a expression of INT for debug usage

Example:

PrintfS 1

func (*InstPrintfD) Compile

func (ins *InstPrintfD) Compile(scope *Scope)

Compile compiles InstPrintfD

type InstPrintfF

type InstPrintfF struct {
	Pos     lexer.Position
	Content Expression `"PrintfF" @@ EOL`
}

InstPrintfF outputs a expression of REAL for debug usage

Example:

PrintF 1.0

func (*InstPrintfF) Compile

func (ins *InstPrintfF) Compile(scope *Scope)

Compile compiles InstPrintfF

type InstRepeat

type InstRepeat struct {
	Pos       lexer.Position
	Head      string     `"REPEAT" EOL`
	Body      Ast        `@@`
	Condition Expression `"UNTIL" @@ EOL`
}

InstWhile creates repeat block

Example:

REPEAT
	OUTPUT "HI"
UNTIL 1=1

func (*InstRepeat) Compile

func (ins *InstRepeat) Compile(scope *Scope)

Compile compiles repeat

type InstWhile

type InstWhile struct {
	Pos       lexer.Position
	Condition Expression `"WHILE" @@ "DO" EOL`
	Body      Ast        `@@`
	END       string     `"ENDWHILE" EOL`
}

InstWhile creates while block

Example:

WHILE 1=1 DO
	OUTPUT "Hi"
ENDWHILE

func (*InstWhile) Compile

func (ins *InstWhile) Compile(scope *Scope)

Compile compiles while

type Instruction

type Instruction struct {
	Pos             lexer.Position
	Output          *InstOutput          ` @@`
	Input           *InstInput           `|@@`
	Call            *InstCall            `|@@`
	PrintfD         *InstPrintfD         `|@@`
	PrintfF         *InstPrintfF         `|@@`
	DeclareVariable *InstDeclareVariable `|@@`
	ConditionBr     *InstConditionBr     `|@@`
	While           *InstWhile           `|@@`
	Repeat          *InstRepeat          `|@@`
	Assignment      *InstAssignment      `|@@`
	NullLine        *string              `|@EOL`
}

Instruction matches all kinds of instructions

type Interpreter

type Interpreter interface {
	Compile(*Scope)
}

Interpreter is the interface for all the interpreters for instructions.

type Key

type Key struct {
	Pos       lexer.Position
	Variables []*Variable `@@ ("." @@)*`
}

Key is an assignable terminal

func (*Key) Locate

func (key *Key) Locate(scope *Scope) value.Value

Locate tries to locate the key to a value via the scope given.

type KeyToken

type KeyToken struct {
	Pos lexer.Position

	Symbol *string `@Ident`

	Dot          *string `| @"."`
	LeftBracket  *string `| @"["`
	RightBracket *string `| @"]"`
}

KeyToken is the lexers of Key TODO: change into expression-like handling

type Multiplication

type Multiplication struct {
	//Pos   lexer.Position
	Head  Unary               `@@`
	Items []*OpMultiplication `(@@)*`
}

Multiplication multiples two values

func (*Multiplication) Evaluate

func (m *Multiplication) Evaluate(scope *Scope) value.Value

Evaluate evaluates multiplication

type OpAddition

type OpAddition struct {
	Operator string         `@("+"|"-")`
	Item     Multiplication `@@`
}

OpAddition adds another value

func (*OpAddition) Evaluate

func (o *OpAddition) Evaluate(scope *Scope, lhsValue value.Value) value.Value

Evaluate evalutes op based on lhs

type OpComparison

type OpComparison struct {
	//Pos lexer.Position
	Operator string   `@("<" ">" | "=" | "<" "=" | ">" "=" | "<" | ">")`
	Item     Addition `@@`
}

OpComparison makes a comparison with another value

func (*OpComparison) Evaluate

func (o *OpComparison) Evaluate(scope *Scope, lhsValue value.Value) value.Value

Evaluate evalutes op based on lhs

type OpMultiplication

type OpMultiplication struct {
	Operator string `@("*"|"/")`
	Item     Unary  `@@`
}

OpMultiplication multiples with another value

func (*OpMultiplication) Evaluate

func (o *OpMultiplication) Evaluate(scope *Scope, lhsValue value.Value) value.Value

Evaluate evalutes op based on lhs

type Primary

type Primary struct {
	Constant      *Constant     `  @@`
	Function      *FunctionCall `| @@`
	Key           *Key          `| @@`
	Subexpression *Expression   `| "(" @@ ")"`
}

Primary is the smallest universal unit in an expression

func (*Primary) Evaluate

func (p *Primary) Evaluate(scope *Scope) value.Value

Evaluate evaluates primary

type Scope

type Scope struct {
	Module *ir.Module
	Func   *ir.Func
	Block  *ir.Block

	Variables ScopeVariableMap
	Functions ScopeFuncMap

	// For Pseudocode, Anything in the root level belongs to function main.
	// So variables defined in main block are global variables,
	// variables defined in other blocks are private variables.
	Main bool

	// When the current scope is the same as GlobalScope,
	// it is the root scope.
	// Keep this field for function access and constant definition.
	GlobalScope *Scope
	Parent      *Scope
}

Scope keep track of all the informations in a block/sub-block.

func NewGlobalScope

func NewGlobalScope() *Scope

NewGlobalScope creates a global scope. There should be only one global scope.

func (*Scope) FindFunction

func (scope *Scope) FindFunction(name string) *ir.Func

FindFunction locates the function in the current scope.

func (*Scope) FindVariable

func (scope *Scope) FindVariable(name string) value.Value

FindVariable locates the variable registered. If the variable is not found, nil would be returned.

func (*Scope) InitRuntime

func (scope *Scope) InitRuntime()

InitRuntime inits the runtime for pseudocode. This includes: - C Standard Functions - Format for PrintfD and PrintfF

func (*Scope) IsGlobal

func (scope *Scope) IsGlobal() bool

IsGlobal checks if the current scope is the global scope.

func (*Scope) NewFuncScope

func (scope *Scope) NewFuncScope(function *ir.Func) *Scope

NewFuncScope creates a function scope under global scope.

func (*Scope) NewScope

func (scope *Scope) NewScope(block *ir.Block) *Scope

NewScope creates a new scope under the given scope.

func (*Scope) RegisterFunction

func (scope *Scope) RegisterFunction(name string, value *ir.Func)

RegisterFunction registers a function to the current scope for further usages. Functions should be registered to global scope only!

func (*Scope) RegisterVariable

func (scope *Scope) RegisterVariable(name string, val value.Value)

RegisterVariable register a variable to the current scope for further usages.

type ScopeFuncMap

type ScopeFuncMap map[string]*ir.Func

ScopeFuncMap is a map to store all the functions in a scope. ir.Func is a struct

type ScopeVariableMap

type ScopeVariableMap map[string]value.Value

ScopeVariableMap is a map to store all the variables in a scope. value.Value is an interface, so it should not be a ptr here.

type Unary

type Unary struct {
	//Pos     lexer.Position
	Not      *Unary   `  "!" @@`
	Opposite *Unary   `| "-" @@`
	Primary  *Primary `| @@`
}

Unary gives not or opposite

func (*Unary) Evaluate

func (u *Unary) Evaluate(scope *Scope) value.Value

Evaluate evaluates unary

type Variable

type Variable struct {
	Pos        lexer.Position
	Name       string `@Ident`
	ArrayIndex *int   `("[" @Int "]")?`
}

func (*Variable) Evaluate

func (v *Variable) Evaluate(scope *Scope) value.Value

type VariableType

type VariableType struct {
	Pos    lexer.Position
	BOOL   *string `  @"BOOL"`
	Int    *string `| @"INT"`
	REAL   *string `| @"REAL"`
	CUSTOM *string `| @Ident`
}

VariableType matches the variable type of declaration

Jump to

Keyboard shortcuts

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