Documentation
¶
Index ¶
- func AddEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value
- func CmpEQEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value
- func CmpGEEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value
- func CmpGTEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value
- func CmpLEEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value
- func CmpLTEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value
- func CmpNEEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value
- func Compile(ast *Ast) *ir.Module
- func DivideEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value
- func MinusEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value
- func MultipleEval(scope *Scope, value1 value.Value, value2 value.Value) value.Value
- func OppositeEval(scope *Scope, value value.Value) value.Value
- type Addition
- type Ast
- type Comparison
- type Constant
- type Expression
- type FunctionCall
- type InstAssignment
- type InstCall
- type InstConditionBr
- type InstDeclareVariable
- type InstInput
- type InstOutput
- type InstPrintfD
- type InstPrintfF
- type InstRepeat
- type InstWhile
- type Instruction
- type Interpreter
- type Key
- type KeyToken
- type Multiplication
- type OpAddition
- type OpComparison
- type OpMultiplication
- type Primary
- type Scope
- func (scope *Scope) FindFunction(name string) *ir.Func
- func (scope *Scope) FindVariable(name string) value.Value
- func (scope *Scope) InitRuntime()
- func (scope *Scope) IsGlobal() bool
- func (scope *Scope) NewFuncScope(function *ir.Func) *Scope
- func (scope *Scope) NewScope(block *ir.Block) *Scope
- func (scope *Scope) RegisterFunction(name string, value *ir.Func)
- func (scope *Scope) RegisterVariable(name string, val value.Value)
- type ScopeFuncMap
- type ScopeVariableMap
- type Unary
- type Variable
- type VariableType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DivideEval ¶
DivideEval generates IR for divide
func MultipleEval ¶
MultipleEval generates IR for multiple
Types ¶
type Addition ¶
type Addition struct { //Pos lexer.Position Head Multiplication `@@` Items []*OpAddition `(@@)*` }
Addition adds two or more values
type Comparison ¶
type Comparison struct { //Pos lexer.Position Head Addition `@@` Items []*OpComparison `(@@)*` }
Comparison compares two or more values
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
type Expression ¶
type Expression struct { //Pos lexer.Position Comparison Comparison `@@` }
Expression is expression of an value
type FunctionCall ¶
type FunctionCall struct { Name string `@Ident` Params []*Expression `"(" (@@ ("," @@)*)? ")"` }
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!")
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 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
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
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 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
type OpAddition ¶
type OpAddition struct { Operator string `@("+"|"-")` Item Multiplication `@@` }
OpAddition adds another value
type OpComparison ¶
type OpComparison struct { //Pos lexer.Position Operator string `@("<" ">" | "=" | "<" "=" | ">" "=" | "<" | ">")` Item Addition `@@` }
OpComparison makes a comparison with another value
type OpMultiplication ¶
OpMultiplication multiples with another value
type Primary ¶
type Primary struct { Constant *Constant ` @@` Function *FunctionCall `| @@` Key *Key `| @@` Subexpression *Expression `| "(" @@ ")"` }
Primary is the smallest universal unit in an expression
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 ¶
FindFunction locates the function in the current scope.
func (*Scope) FindVariable ¶
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) NewFuncScope ¶
NewFuncScope creates a function scope under global scope.
func (*Scope) RegisterFunction ¶
RegisterFunction registers a function to the current scope for further usages. Functions should be registered to global scope only!
type ScopeFuncMap ¶
ScopeFuncMap is a map to store all the functions in a scope. ir.Func is a struct
type ScopeVariableMap ¶
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