Documentation ¶
Overview ¶
GoBlockly Interpreter
Interpreter for the Blockly programming language (https://developers.google.com/blockly).
Blockly is a library for building visual programming editors. This interpreter is capable of digesting the output of Blockly.Xml.domToText (in the Blockly library) by interpreting the resulting XML as a program and running that program. It supports the basic block types that Blockly ships with.
To use:
Create a blockly.Interpreter.
Populate the interpreter with a Console (io.Writer) as an output destination and a FailHandler to be run if the Blockly script cannot be interpreted.
Parse the XML into a BlockXml using the tools in encoding/xml.
Interpret the XML with
interpreter.Run(blockXml.Blocks)
Index ¶
- func PrepareEvaluators()
- func ShadowVariables(i *Interpreter, newVariables map[string]Value) map[string]Value
- func UnshadowVariables(i *Interpreter, newVariables map[string]Value, preShadow map[string]Value)
- type Block
- func (b *Block) BlockStatementWithName(i *Interpreter, name string) *Block
- func (b *Block) BlockValueWithName(name string) *BlockValue
- func (b *Block) FieldWithName(name string) *BlockField
- func (b *Block) SingleBlockStatementWithName(i *Interpreter, name string) *Block
- func (b *Block) SingleBlockValueWithName(i *Interpreter, name string) *Block
- func (b *Block) SingleFieldWithName(i *Interpreter, name string) string
- type BlockField
- type BlockMutation
- type BlockMutationArg
- type BlockStatement
- type BlockValue
- type BlockXml
- type BoolValue
- func (v BoolValue) AsBoolean(i *Interpreter) bool
- func (v BoolValue) AsColour(i *Interpreter) Colour
- func (v BoolValue) AsDateTime(i *Interpreter) time.Time
- func (v BoolValue) AsList(i *Interpreter) List
- func (v BoolValue) AsNumber(i *Interpreter) float64
- func (v BoolValue) AsString(i *Interpreter) string
- func (v BoolValue) Equals(i *Interpreter, v2 Value) bool
- func (v BoolValue) IsLessThan(i *Interpreter, v2 Value) bool
- type BreakEvent
- type BreakType
- type Colour
- func (v Colour) AsBoolean(i *Interpreter) bool
- func (v Colour) AsColour(i *Interpreter) Colour
- func (v Colour) AsDateTime(i *Interpreter) time.Time
- func (v Colour) AsList(i *Interpreter) List
- func (v Colour) AsNumber(i *Interpreter) float64
- func (v Colour) AsString(*Interpreter) string
- func (v Colour) Equals(i *Interpreter, value Value) bool
- func (v *Colour) FromHex(i *Interpreter, code string)
- func (v Colour) IsLessThan(i *Interpreter, value Value) bool
- type DateTimeValue
- func (v DateTimeValue) AsBoolean(i *Interpreter) bool
- func (v DateTimeValue) AsColour(i *Interpreter) Colour
- func (v DateTimeValue) AsDateTime(i *Interpreter) time.Time
- func (v DateTimeValue) AsList(i *Interpreter) List
- func (v DateTimeValue) AsNumber(i *Interpreter) float64
- func (v DateTimeValue) AsString(i *Interpreter) string
- func (v DateTimeValue) Equals(i *Interpreter, v2 Value) bool
- func (v DateTimeValue) IsLessThan(i *Interpreter, v2 Value) bool
- func (v DateTimeValue) MarshalJSON() ([]byte, error)
- type Evaluator
- type Function
- type Interpreter
- func (i *Interpreter) CheckBreak(continuing *bool)
- func (i *Interpreter) CheckReturn(retval *Value)
- func (i *Interpreter) DefineFunction(b *Block)
- func (i *Interpreter) Evaluate(b *Block) Value
- func (i *Interpreter) Fail(reason string)
- func (i *Interpreter) Run(b []Block)
- func (i *Interpreter) WriteToConsole(s string)
- type List
- func (v List) AsBoolean(i *Interpreter) bool
- func (v List) AsColour(i *Interpreter) Colour
- func (v List) AsDateTime(i *Interpreter) time.Time
- func (v List) AsList(i *Interpreter) List
- func (v List) AsNumber(i *Interpreter) float64
- func (v List) AsString(i *Interpreter) string
- func (v List) Equals(i *Interpreter, v2 Value) bool
- func (v *List) InsertElementAtIndex(i *Interpreter, idx int, element Value)
- func (v List) IsLessThan(i *Interpreter, v2 Value) bool
- func (v *List) RemoveElementAtIndex(i *Interpreter, idx int)
- type NilValue
- func (v NilValue) AsBoolean(i *Interpreter) bool
- func (v NilValue) AsColour(i *Interpreter) Colour
- func (v NilValue) AsDateTime(i *Interpreter) time.Time
- func (v NilValue) AsList(i *Interpreter) List
- func (v NilValue) AsNumber(i *Interpreter) float64
- func (v NilValue) AsString(i *Interpreter) string
- func (v NilValue) Equals(i *Interpreter, v2 Value) bool
- func (v NilValue) IsLessThan(i *Interpreter, v2 Value) bool
- type NumberValue
- func (v NumberValue) AsBoolean(i *Interpreter) bool
- func (v NumberValue) AsColour(i *Interpreter) Colour
- func (v NumberValue) AsDateTime(i *Interpreter) time.Time
- func (v NumberValue) AsList(i *Interpreter) List
- func (v NumberValue) AsNumber(i *Interpreter) float64
- func (v NumberValue) AsString(i *Interpreter) string
- func (v NumberValue) Equals(i *Interpreter, v2 Value) bool
- func (v NumberValue) IsLessThan(i *Interpreter, v2 Value) bool
- type StringValue
- func (v StringValue) AsBoolean(i *Interpreter) bool
- func (v StringValue) AsColour(i *Interpreter) Colour
- func (v StringValue) AsDateTime(i *Interpreter) time.Time
- func (v StringValue) AsList(i *Interpreter) List
- func (v StringValue) AsNumber(i *Interpreter) float64
- func (v StringValue) AsString(i *Interpreter) string
- func (v StringValue) Equals(i *Interpreter, v2 Value) bool
- func (v StringValue) IsLessThan(i *Interpreter, v2 Value) bool
- type Value
- func ColourBlendEvaluator(i *Interpreter, b *Block) Value
- func ColourPickerEvaluator(i *Interpreter, b *Block) Value
- func ColourRandomEvaluator(i *Interpreter, b *Block) Value
- func ColourRgbEvaluator(i *Interpreter, b *Block) Value
- func ControlFlowStatements(i *Interpreter, b *Block) Value
- func ControlForEachEvaluator(i *Interpreter, b *Block) Value
- func ControlForEvaluator(i *Interpreter, b *Block) Value
- func ControlIfEvaluator(i *Interpreter, b *Block) Value
- func ControlRepeatExtEvaluator(i *Interpreter, b *Block) Value
- func ControlWhileUntil(i *Interpreter, b *Block) Value
- func ListCreateEmptyEvaluator(i *Interpreter, b *Block) Value
- func ListCreateWithEvaluator(i *Interpreter, b *Block) Value
- func ListGetIndexEvaluator(i *Interpreter, b *Block) Value
- func ListGetSublistEvaluator(i *Interpreter, b *Block) Value
- func ListIndexOfEvaluator(i *Interpreter, b *Block) Value
- func ListIsEmptyEvaluator(i *Interpreter, b *Block) Value
- func ListLengthEvaluator(i *Interpreter, b *Block) Value
- func ListRepeatEvaluator(i *Interpreter, b *Block) Value
- func ListSetIndexEvaluator(i *Interpreter, b *Block) Value
- func ListSplitEvaluator(i *Interpreter, b *Block) Value
- func LogicBooleanEvaluator(i *Interpreter, b *Block) Value
- func LogicCompareEvaluator(i *Interpreter, b *Block) Value
- func LogicNegateEvaluator(i *Interpreter, b *Block) Value
- func LogicOperationEvaluator(i *Interpreter, b *Block) Value
- func LogicTernaryEvaluator(i *Interpreter, b *Block) Value
- func NumberArithmeticEvaluator(i *Interpreter, b *Block) Value
- func NumberChangeEvaluator(i *Interpreter, b *Block) Value
- func NumberConstantEvaluator(i *Interpreter, b *Block) Value
- func NumberConstrainEvaluator(i *Interpreter, b *Block) Value
- func NumberEvaluator(i *Interpreter, b *Block) Value
- func NumberModuloEvaluator(i *Interpreter, b *Block) Value
- func NumberOnListEvaluator(i *Interpreter, b *Block) Value
- func NumberPropertyEvaluator(i *Interpreter, b *Block) Value
- func NumberRandomFloatEvaluator(i *Interpreter, b *Block) Value
- func NumberRandomIntEvaluator(i *Interpreter, b *Block) Value
- func NumberSingleEvaluator(i *Interpreter, b *Block) Value
- func PrintEvaluator(i *Interpreter, b *Block) Value
- func ProceduresFunctionCallEvaluator(i *Interpreter, b *Block) Value
- func ProceduresIfReturnEvaluator(i *Interpreter, b *Block) Value
- func TextAppendEvaluator(i *Interpreter, b *Block) Value
- func TextChangeCaseEvaluator(i *Interpreter, b *Block) Value
- func TextCharAtEvaluator(i *Interpreter, b *Block) Value
- func TextEvaluator(i *Interpreter, b *Block) Value
- func TextGetSubstringEvaluator(i *Interpreter, b *Block) Value
- func TextIndexOfEvaluator(i *Interpreter, b *Block) Value
- func TextIsEmptyEvaluator(i *Interpreter, b *Block) Value
- func TextJoinEvaluator(i *Interpreter, b *Block) Value
- func TextLengthEvaluator(i *Interpreter, b *Block) Value
- func TextTrimEvaluator(i *Interpreter, b *Block) Value
- func VariableGetEvaluator(i *Interpreter, b *Block) Value
- func VariableSetEvaluator(i *Interpreter, b *Block) Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PrepareEvaluators ¶
func PrepareEvaluators()
PrepareEvaluators populates the default evaluator table. It must be called once before any calls to Interpreter.Run.
func ShadowVariables ¶
func ShadowVariables(i *Interpreter, newVariables map[string]Value) map[string]Value
ShadowVariables builds a backup copy of variables in the Interpreter context, then sets those variables to the shadow values. It returns a map of the shadowed variables.
func UnshadowVariables ¶
func UnshadowVariables(i *Interpreter, newVariables map[string]Value, preShadow map[string]Value)
UnshadowVariables restores the variables in the Interpreter to their pre-shadow values.
Types ¶
type Block ¶
type Block struct { XMLName xml.Name `xml:"block"` Type string `xml:"type,attr"` X string `xml:"x,attr"` Y string `xml:"y,attr"` Values []BlockValue `xml:"value"` Fields []BlockField `xml:"field"` Statements []BlockStatement `xml:"statement"` Next *Block `xml:"next>block"` Mutation *BlockMutation `xml:"mutation"` }
A single Blockly block
func (*Block) BlockStatementWithName ¶
func (b *Block) BlockStatementWithName(i *Interpreter, name string) *Block
BlockStatementWithName retrieves the statement with the specified name, or returns nil if the statement doesn't exist.
func (*Block) BlockValueWithName ¶
func (b *Block) BlockValueWithName(name string) *BlockValue
BlockValueWithName retrieves the block value with the specified name, or returns nil if the block value doesn't exist.
func (*Block) FieldWithName ¶
func (b *Block) FieldWithName(name string) *BlockField
FieldWithName fetches the field with a given name, or returns nil if the field doesn't exist.
func (*Block) SingleBlockStatementWithName ¶
func (b *Block) SingleBlockStatementWithName(i *Interpreter, name string) *Block
SingleBlockStatementWIthName retrieves the single block in the block statement with the specified name, or nil. Fails interpretation if there is not a single block in the specified statement.
func (*Block) SingleBlockValueWithName ¶
func (b *Block) SingleBlockValueWithName(i *Interpreter, name string) *Block
SingleBlockValueWithName retrieves the single block in the block value with the specified name, or nil. Fails interpretation if there is not exactly one single block for the specified value.
func (*Block) SingleFieldWithName ¶
func (b *Block) SingleFieldWithName(i *Interpreter, name string) string
SingleFieldWithName fetches the value of the field with the given name, or "". Fails interpretation if the field doesn't exist.
type BlockField ¶
A field attached to a Blockly block
type BlockMutation ¶
type BlockMutation struct { At bool `xml:"at,attr"` At1 bool `xml:"at1,attr"` At2 bool `xml:"at2,attr"` ElseIf int `xml:"elseif,attr"` Else int `xml:"else,attr"` Items int `xml:"items,attr"` Mode string `xml:"mode,attr"` Statement bool `xml:"statement,attr"` Statements bool `xml:"statements,attr"` Name string `xml:"name,attr"` Args []BlockMutationArg `xml:"arg"` }
Modifiers on Blockly blocks. Indicates if a block has special encoding (such as the "elseif" / "elses" mutations on if blocks).
type BlockMutationArg ¶
type BlockMutationArg struct {
Name string `xml:"name,attr"`
}
An argument mutation. Used with function blocks to indicate arguments to the function.
type BlockStatement ¶
type BlockStatement struct { XMLName xml.Name `xml:"statement"` Name string `xml:"name,attr"` Blocks []Block `xml:"block"` }
A statement in a Blockly block. Statements are (usually stacks of) Blockly blocks with an ignorable return value.
type BlockValue ¶
A value in a Blockly block. Values are blocks that will evaluate to a value.
type BoolValue ¶
type BoolValue bool
A boolean value.
func (BoolValue) AsBoolean ¶
func (v BoolValue) AsBoolean(i *Interpreter) bool
func (BoolValue) AsColour ¶
func (v BoolValue) AsColour(i *Interpreter) Colour
func (BoolValue) AsDateTime ¶
func (v BoolValue) AsDateTime(i *Interpreter) time.Time
func (BoolValue) AsList ¶
func (v BoolValue) AsList(i *Interpreter) List
func (BoolValue) AsNumber ¶
func (v BoolValue) AsNumber(i *Interpreter) float64
AsNumber causes the interpreter to Fail, as booleans cannot be cast to numbers.
func (BoolValue) AsString ¶
func (v BoolValue) AsString(i *Interpreter) string
func (BoolValue) Equals ¶
func (v BoolValue) Equals(i *Interpreter, v2 Value) bool
Equals returns true if and only if v2 and v are Go equal (meaning they must also be the same type, so "true" != true).
func (BoolValue) IsLessThan ¶
func (v BoolValue) IsLessThan(i *Interpreter, v2 Value) bool
IsLessThan returns true if and only if this value is false and v2 (coerced to a boolean) is true.
type BreakEvent ¶
BreakEvent is a special type sent via panic to indicate a break or continue occurred. Though Blockly's client-side editor will warn a user if they attempt to use a break block outside of a loop, Blockly will still allow the user to send a block in that form to the server, so we panic on breaks to allow the interpreter to handle this state.
BreakEvents can also be used by procedures_ifreturn to bail early from a function; in that use case, the ReturnValue is non-nil and specifies what return value came out of the function.
func (BreakEvent) Error ¶
func (evt BreakEvent) Error() string
Error method for BreakEvents (which are panicked as errors).
type Colour ¶
type Colour struct {
// The RGB colour channels.
Red, Green, Blue uint8
}
The colour type
func (Colour) AsBoolean ¶
func (v Colour) AsBoolean(i *Interpreter) bool
func (Colour) AsColour ¶
func (v Colour) AsColour(i *Interpreter) Colour
func (Colour) AsDateTime ¶
func (v Colour) AsDateTime(i *Interpreter) time.Time
func (Colour) AsList ¶
func (v Colour) AsList(i *Interpreter) List
func (Colour) AsNumber ¶
func (v Colour) AsNumber(i *Interpreter) float64
func (Colour) AsString ¶
func (v Colour) AsString(*Interpreter) string
func (*Colour) FromHex ¶
func (v *Colour) FromHex(i *Interpreter, code string)
FromHex Initializes a colour from a hex code string ('aabbcc').
func (Colour) IsLessThan ¶
func (v Colour) IsLessThan(i *Interpreter, value Value) bool
type DateTimeValue ¶
func (DateTimeValue) AsBoolean ¶
func (v DateTimeValue) AsBoolean(i *Interpreter) bool
func (DateTimeValue) AsColour ¶
func (v DateTimeValue) AsColour(i *Interpreter) Colour
func (DateTimeValue) AsDateTime ¶
func (v DateTimeValue) AsDateTime(i *Interpreter) time.Time
func (DateTimeValue) AsList ¶
func (v DateTimeValue) AsList(i *Interpreter) List
func (DateTimeValue) AsNumber ¶
func (v DateTimeValue) AsNumber(i *Interpreter) float64
AsNumber causes the interpreter to Fail, as booleans cannot be cast to numbers.
func (DateTimeValue) AsString ¶
func (v DateTimeValue) AsString(i *Interpreter) string
func (DateTimeValue) Equals ¶
func (v DateTimeValue) Equals(i *Interpreter, v2 Value) bool
Equals returns true if and only if v2 and v are Go equal (meaning they must also be the same type, so "true" != true).
func (DateTimeValue) IsLessThan ¶
func (v DateTimeValue) IsLessThan(i *Interpreter, v2 Value) bool
IsLessThan returns true if and only if this value is false and v2 (coerced to a boolean) is true.
func (DateTimeValue) MarshalJSON ¶
func (v DateTimeValue) MarshalJSON() ([]byte, error)
type Evaluator ¶
type Evaluator func(*Interpreter, *Block) Value
An Evaluator can evaluate a block, in the current context of the interpreter, into a value.
type Function ¶
type Function struct { // Names of inputs, which become variables defined within the function // while it is evaluated. Inputs []string // (optional) top-level block of the function body Body *Block // (optional) return statement Return *Block }
A Function maps arguments to a body that is executed, and (optionally) a value that is returned.
In Blockly, functions are not values.
type Interpreter ¶
type Interpreter struct { // The output console. All 'print' operations write to here. Console io.Writer // Function called if evaluation fails FailHandler func(string) // Variables in an execution cycle of the interpreter Context map[string]Value // Function mappings in an execution cycle of the interpreter Functions map[string]Function // Custom handlers for specific block prefixes. Inserting an evaluator // here will cause all blocks with type "prefix_<something>" to be // handled by the evaluator in key "prefix_". Blocks with a type not in // prefix get handled by the default evaluators or cause the interpreter // to Fail if there is no evaluator for the block type. PrefixHandlers map[string]Evaluator }
The Interpreter maintains interpretation state for Blockly evaluation (such as where print operations should go, what to do if evaluation fails, and variable values).
func (*Interpreter) CheckBreak ¶
func (i *Interpreter) CheckBreak(continuing *bool)
CheckBreak is a helper function that checks if a panic was due to breaking from a loop. If it was not, it re-panicks. If it was, it sets continuing to true if the enclosing loop should contiue with the next iteration, or false if the enclosing loop should break out.
func (*Interpreter) CheckReturn ¶
func (i *Interpreter) CheckReturn(retval *Value)
CheckReturn is a helper function that checks if a panic was due to a function returning early. If it was not, it re-panicks. If it was, it sets retval to the value returned.
func (*Interpreter) DefineFunction ¶
func (i *Interpreter) DefineFunction(b *Block)
DefineFunction interprets a function block into a definition of a function.
func (*Interpreter) Evaluate ¶
func (i *Interpreter) Evaluate(b *Block) Value
Evaluate evaluates a specific block by determining what evaluator can consume it. Generally, this is called by Run and by other evaluators; it should not need to be called directly.
PrefixHandlers may call Evaluate directly if they are evaluating a type of block that itself has values or statements.
func (*Interpreter) Fail ¶
func (i *Interpreter) Fail(reason string)
Fail causes interpretation to panic. If Fail is called in the context of a Run, the Run will recover and call the interpreter's FailHandler function.
func (*Interpreter) Run ¶
func (i *Interpreter) Run(b []Block)
Run evaluates a list of top-level blocks and handles Fail panics by recovering them into the interpreter's FailHandler. It also initializes the Interpreter's Context (i.e. clears all variables).
Top-level blocks that are functions definitions (procedures_defnoreturn and procedures_defreturn) are stored as function mappings for calling; all other top-level blocks are evaluated in arbitrary order.
func (*Interpreter) WriteToConsole ¶
func (i *Interpreter) WriteToConsole(s string)
WriteToConsole outputs a string to the interpreter's Console.
type List ¶
type List struct { // Values in the list. This is a pointer to a slice because lists in // Blockly are mutable; changes to this list need to be reflected in // other references to the same list. Values *[]Value }
func (List) AsBoolean ¶
func (v List) AsBoolean(i *Interpreter) bool
func (List) AsColour ¶
func (v List) AsColour(i *Interpreter) Colour
func (List) AsDateTime ¶
func (v List) AsDateTime(i *Interpreter) time.Time
func (List) AsList ¶
func (v List) AsList(i *Interpreter) List
func (List) AsNumber ¶
func (v List) AsNumber(i *Interpreter) float64
func (List) AsString ¶
func (v List) AsString(i *Interpreter) string
func (List) Equals ¶
func (v List) Equals(i *Interpreter, v2 Value) bool
Equals checks two lists for equality. The lists are Equal if they have the same length and for each index, every element in them satisfies the condition elemFromList1.Equals(elemFromList2) == true
func (*List) InsertElementAtIndex ¶
func (v *List) InsertElementAtIndex(i *Interpreter, idx int, element Value)
InsertElementAtIndex adds an element to the list at the specified (0-based) index.
func (List) IsLessThan ¶
func (v List) IsLessThan(i *Interpreter, v2 Value) bool
IsLessThan returns true if this list is strictly less than the other list.
func (*List) RemoveElementAtIndex ¶
func (v *List) RemoveElementAtIndex(i *Interpreter, idx int)
RemoveElementAtIndex removes the element at the specified (0-based) index from the list.
type NilValue ¶
type NilValue struct { }
The NilValue is a valueless value, generally returned from statement blocks and as the necessary return value for interpreter failure.
func (NilValue) AsBoolean ¶
func (v NilValue) AsBoolean(i *Interpreter) bool
func (NilValue) AsColour ¶
func (v NilValue) AsColour(i *Interpreter) Colour
func (NilValue) AsDateTime ¶
func (v NilValue) AsDateTime(i *Interpreter) time.Time
func (NilValue) AsList ¶
func (v NilValue) AsList(i *Interpreter) List
func (NilValue) AsNumber ¶
func (v NilValue) AsNumber(i *Interpreter) float64
func (NilValue) AsString ¶
func (v NilValue) AsString(i *Interpreter) string
func (NilValue) Equals ¶
func (v NilValue) Equals(i *Interpreter, v2 Value) bool
Nil values are only equal to themselves.
func (NilValue) IsLessThan ¶
func (v NilValue) IsLessThan(i *Interpreter, v2 Value) bool
Nil values always sort less than everything except other nil values.
type NumberValue ¶
type NumberValue float64
All Blockly numbers are floating-point. We use float64 for Go. Equality comparison is done as a Go direct-value compare (so string "2" is not equal to number 2).
func (NumberValue) AsBoolean ¶
func (v NumberValue) AsBoolean(i *Interpreter) bool
func (NumberValue) AsColour ¶
func (v NumberValue) AsColour(i *Interpreter) Colour
func (NumberValue) AsDateTime ¶
func (v NumberValue) AsDateTime(i *Interpreter) time.Time
func (NumberValue) AsList ¶
func (v NumberValue) AsList(i *Interpreter) List
func (NumberValue) AsNumber ¶
func (v NumberValue) AsNumber(i *Interpreter) float64
func (NumberValue) AsString ¶
func (v NumberValue) AsString(i *Interpreter) string
func (NumberValue) Equals ¶
func (v NumberValue) Equals(i *Interpreter, v2 Value) bool
func (NumberValue) IsLessThan ¶
func (v NumberValue) IsLessThan(i *Interpreter, v2 Value) bool
IsLessThan compares this value to another value (coerced to a number value). It returns true if this value is strictly less than the other value.
type StringValue ¶
type StringValue string
A value representing a string.
func (StringValue) AsBoolean ¶
func (v StringValue) AsBoolean(i *Interpreter) bool
func (StringValue) AsColour ¶
func (v StringValue) AsColour(i *Interpreter) Colour
func (StringValue) AsDateTime ¶
func (v StringValue) AsDateTime(i *Interpreter) time.Time
func (StringValue) AsList ¶
func (v StringValue) AsList(i *Interpreter) List
func (StringValue) AsNumber ¶
func (v StringValue) AsNumber(i *Interpreter) float64
func (StringValue) AsString ¶
func (v StringValue) AsString(i *Interpreter) string
func (StringValue) Equals ¶
func (v StringValue) Equals(i *Interpreter, v2 Value) bool
Equals returns true if and only if v2 and this value are Go equal (meaning they must also be the same type, so number 2 != "2").
Exception: As per the unit test in colour.xml, strings and colour are directly comparable.
func (StringValue) IsLessThan ¶
func (v StringValue) IsLessThan(i *Interpreter, v2 Value) bool
IsLessThan returns true if the second value (coerced to a string) is lexically strictly less than this value.
type Value ¶
type Value interface { // Coerce the type to a string, or Interpreter.Fail if the coercion cannot be done. AsString(*Interpreter) string // Coerce the type to a float, or Interpreter.Fail if the coercion cannot be done. AsNumber(*Interpreter) float64 // Coerce the type to a boolean, or Interpreter.Fail if the coercion cannot be done. AsBoolean(*Interpreter) bool // Coerce the type to a DateTiem, or Interpreter.Fail if the coercion cannot be done. AsDateTime(*Interpreter) time.Time // Coerce the type to a colour, or Interpreter.Fail if the coercion cannot be done. AsColour(*Interpreter) Colour // Coerce the type to a list, or Interpreter.Fail if the coercion cannot be done. AsList(*Interpreter) List // Return true if the second value equals this value. Equals(*Interpreter, Value) bool // Return true if this value is strictly less than the second value. IsLessThan(*Interpreter, Value) bool }
Value is the generic value type.
Values support type coercion (with the ability to fail if a type can't coerce).
func ColourBlendEvaluator ¶
func ColourBlendEvaluator(i *Interpreter, b *Block) Value
ColourBlendEvaluator creates a colour by blending two input colours with a ratio between 0 (all the first colour) and 1 (all the second colour).
func ColourPickerEvaluator ¶
func ColourPickerEvaluator(i *Interpreter, b *Block) Value
ColourPickerEvaluator evaluates the colour picker by transforming its hex-coded colour string into a Colour value.
func ColourRandomEvaluator ¶
func ColourRandomEvaluator(i *Interpreter, b *Block) Value
ColourRandomEvaluator chooses a random colour. Note: The shared source is assumed to have been initialized by the program.
func ColourRgbEvaluator ¶
func ColourRgbEvaluator(i *Interpreter, b *Block) Value
ColourRgbEvaluator creates a colour from three values for red, green, and blue, scaled between 0 and 100.
func ControlFlowStatements ¶
func ControlFlowStatements(i *Interpreter, b *Block) Value
func ControlForEachEvaluator ¶
func ControlForEachEvaluator(i *Interpreter, b *Block) Value
ControlForEachEvaluator runs the body of the control with the index variable set to each element of a list.
func ControlForEvaluator ¶
func ControlForEvaluator(i *Interpreter, b *Block) Value
ControlForEvaluator evaluates a Blockly for (from-value, to-value, by-value) block.
Note that Blockly includes a couple idiosynchrasies in its for manipulation (see https://github.com/google/blockly/blob/master/generators/javascript/loops.js for details):
FROM, TO, and BY are auto-populated with 0, 0, 1 (respectively) if not specified ¶
BY is always considered to be positive (absolute value taken). If FROM <= TO, BY is used to count up; if FROM > TO, BY is used to count down.
func ControlIfEvaluator ¶
func ControlIfEvaluator(i *Interpreter, b *Block) Value
ControlIfEvaluator evaluates if / elseif / else blocks.
func ControlRepeatExtEvaluator ¶
func ControlRepeatExtEvaluator(i *Interpreter, b *Block) Value
func ControlWhileUntil ¶
func ControlWhileUntil(i *Interpreter, b *Block) Value
func ListCreateEmptyEvaluator ¶
func ListCreateEmptyEvaluator(i *Interpreter, b *Block) Value
ListCreateEmptyEvaluator creates an empty list.
func ListCreateWithEvaluator ¶
func ListCreateWithEvaluator(i *Interpreter, b *Block) Value
ListCreateWithEvaluator creates a list with a set of values.
func ListGetIndexEvaluator ¶
func ListGetIndexEvaluator(i *Interpreter, b *Block) Value
ListGetIndexEvaluator fetches an element from a list at a specific index, and optionally removes that element from the list (mutating the list).
func ListGetSublistEvaluator ¶
func ListGetSublistEvaluator(i *Interpreter, b *Block) Value
ListGetSublistEvaluator grabs a sub-list of a list.
func ListIndexOfEvaluator ¶
func ListIndexOfEvaluator(i *Interpreter, b *Block) Value
ListIndexOfEvaluator returns the first or last occurence of a specified element in a list, or 0 if the item cannot be found. Note that Blockly uses 1-offset indexing (if element is first element, it is index 1, etc.).
func ListIsEmptyEvaluator ¶
func ListIsEmptyEvaluator(i *Interpreter, b *Block) Value
ListIsEmptyEvaluator returns true if the list's length is 0.
func ListLengthEvaluator ¶
func ListLengthEvaluator(i *Interpreter, b *Block) Value
ListLengthEvaluator returns the number of elements in the list.
func ListRepeatEvaluator ¶
func ListRepeatEvaluator(i *Interpreter, b *Block) Value
ListRepeatEvaluator creates a list of length n by repeating one element n times. Note that the repeated item is referenced, so if the item to repeat is a list, modifying that list will modify all copies of that list.
func ListSetIndexEvaluator ¶
func ListSetIndexEvaluator(i *Interpreter, b *Block) Value
ListSetIndexEvaluator changes an element in a list at a specific index or inserts an element into the list (mutating the list).
func ListSplitEvaluator ¶
func ListSplitEvaluator(i *Interpreter, b *Block) Value
ListSplitEvaluator splits a text string on a delimiter into a list, or joins a list of strings into a single string using the delimiter between concatenated strings. Return value for this interpreter is of either List or StringValue type.
func LogicBooleanEvaluator ¶
func LogicBooleanEvaluator(i *Interpreter, b *Block) Value
func LogicCompareEvaluator ¶
func LogicCompareEvaluator(i *Interpreter, b *Block) Value
func LogicNegateEvaluator ¶
func LogicNegateEvaluator(i *Interpreter, b *Block) Value
func LogicOperationEvaluator ¶
func LogicOperationEvaluator(i *Interpreter, b *Block) Value
func LogicTernaryEvaluator ¶
func LogicTernaryEvaluator(i *Interpreter, b *Block) Value
func NumberArithmeticEvaluator ¶
func NumberArithmeticEvaluator(i *Interpreter, b *Block) Value
func NumberChangeEvaluator ¶
func NumberChangeEvaluator(i *Interpreter, b *Block) Value
NumberChangeEvaluator adds a number to a value in an array (i.e. operator++).
func NumberConstantEvaluator ¶
func NumberConstantEvaluator(i *Interpreter, b *Block) Value
NumberConstantEvaluator evaluates to a fixed mathematical constant.
func NumberConstrainEvaluator ¶
func NumberConstrainEvaluator(i *Interpreter, b *Block) Value
NumberConstrainEvaluator clamps a number between two thresholds (low and high)
func NumberEvaluator ¶
func NumberEvaluator(i *Interpreter, b *Block) Value
func NumberModuloEvaluator ¶
func NumberModuloEvaluator(i *Interpreter, b *Block) Value
NumberModuloEvaluator interprets the input as modulo some other number.
func NumberOnListEvaluator ¶
func NumberOnListEvaluator(i *Interpreter, b *Block) Value
NumberOnListEvaluator evaluates functions that can be applied to a list of numbers or strings.
func NumberPropertyEvaluator ¶
func NumberPropertyEvaluator(i *Interpreter, b *Block) Value
NumberPropertyEvaluator returns boolean indicating whether a number satisfies a particular property (even, odd, prime, whole, positive, negative, divisible by another number)
func NumberRandomFloatEvaluator ¶
func NumberRandomFloatEvaluator(i *Interpreter, b *Block) Value
NumberRandomFloatEvaluator picks a random floating-point number in range [0.0, 1.0)
func NumberRandomIntEvaluator ¶
func NumberRandomIntEvaluator(i *Interpreter, b *Block) Value
NumberRandomIntEvaluator picks a random integer between two constraints.
func NumberSingleEvaluator ¶
func NumberSingleEvaluator(i *Interpreter, b *Block) Value
NumberSingleEvaluator runs one of several unary functions on an input number.
func PrintEvaluator ¶
func PrintEvaluator(i *Interpreter, b *Block) Value
func ProceduresFunctionCallEvaluator ¶
func ProceduresFunctionCallEvaluator(i *Interpreter, b *Block) Value
ProceduresFunctionCallEvaluator calls a function, passing arguments and returning any return value from the function. Arguments shadow the global variable values in the context of the evaluation of the function.evaluates a function.
func ProceduresIfReturnEvaluator ¶
func ProceduresIfReturnEvaluator(i *Interpreter, b *Block) Value
ProceduresIfReturnEvaluator evaluates the procedures_ifreturn block, which returns (either early or with a different value) if a conditional evaluates to true.
func TextAppendEvaluator ¶
func TextAppendEvaluator(i *Interpreter, b *Block) Value
TextAppendEvaluator appends the specified text to the value in the specified variable.
func TextChangeCaseEvaluator ¶
func TextChangeCaseEvaluator(i *Interpreter, b *Block) Value
TextChangeCaseEvaluator changes the case of the text.
func TextCharAtEvaluator ¶
func TextCharAtEvaluator(i *Interpreter, b *Block) Value
TextCharAtEvaluator returns the character at a specific location in a text block.
func TextEvaluator ¶
func TextEvaluator(i *Interpreter, b *Block) Value
func TextGetSubstringEvaluator ¶
func TextGetSubstringEvaluator(i *Interpreter, b *Block) Value
TextGetSubstringEvaluator returns the specified substring.
func TextIndexOfEvaluator ¶
func TextIndexOfEvaluator(i *Interpreter, b *Block) Value
TextIndexOfEvaluator returns the first (one-indexed) location at which a specified substring occurs in the searched string. Returns 0 if text is not found.
func TextIsEmptyEvaluator ¶
func TextIsEmptyEvaluator(i *Interpreter, b *Block) Value
TextIsEmptyEvaluator returns true if the length of the text is 0
func TextJoinEvaluator ¶
func TextJoinEvaluator(i *Interpreter, b *Block) Value
func TextLengthEvaluator ¶
func TextLengthEvaluator(i *Interpreter, b *Block) Value
TextLengthEvaluator returns the length of a text block
func TextTrimEvaluator ¶
func TextTrimEvaluator(i *Interpreter, b *Block) Value
TextTrimEvaluator removes spaces from either or both ends of an input.
func VariableGetEvaluator ¶
func VariableGetEvaluator(i *Interpreter, b *Block) Value
func VariableSetEvaluator ¶
func VariableSetEvaluator(i *Interpreter, b *Block) Value