Class Interpreter
- Namespace
- EzrSquared.Runtime
- Assembly
- ezrSquared-lib.dll
The ezrĀ² Interpreter. The job of the Interpreter is to execute the Abstract Syntax Tree (AST) received from the Parser (as a single Node object)!
public class Interpreter
- Inheritance
-
objectInterpreter
Fields
RuntimeResult
The result of the current execution.
public readonly RuntimeResult RuntimeResult
Field Value
Methods
AssignValueToVariable(Reference, IEzrObject, VariableAssignmentNode, Context, Context, AccessMod)
Assigns one of the values to one of the variables defined in the VariableAssignmentNode.
private void AssignValueToVariable(Reference variable, IEzrObject value, VariableAssignmentNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
variable
ReferenceThe variable reference to assign to.
value
IEzrObjectThe value object to be assigned.
node
VariableAssignmentNodeThe VariableAssignmentNode being executed.
executionContext
ContextThe Context in which the variable will be assigned.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
AssignValuesToVariables(VariableAssignmentNode, Reference[], bool, Context, Context, AccessMod)
Assigns the value(s) to the variable(s) defined in the VariableAssignmentNode.
private void AssignValuesToVariables(VariableAssignmentNode node, Reference[] variables, bool isSingleVariable, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
VariableAssignmentNodeThe VariableAssignmentNode being executed.
variables
Reference[]The array of variable references to be assigned to.
isSingleVariable
boolIs only one variable being assigned?
executionContext
ContextThe Context in which the variable will be assigned.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
Execute(Node, Context, AccessMod)
Executes an AST under the given Context and returns the result.
public RuntimeResult Execute(Node ast, Context runtimeContext, AccessMod accessibilityModifiers = AccessMod.None)
Parameters
ast
NodeThe AST, as a single Node object.
runtimeContext
ContextThe run-time Context in which the AST will be executed.
accessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
Returns
- RuntimeResult
The RuntimeResult object.
ExecuteBinaryOperation(IEzrObject, IEzrObject, Node, TokenType, Context)
Executes a binary operation.
private void ExecuteBinaryOperation(IEzrObject first, IEzrObject second, Node node, TokenType operation, Context executionContext)
Parameters
first
IEzrObjectThe first value in the operation.
second
IEzrObjectThe second value in the operation.
node
NodeThe operation's AST node.
operation
TokenTypeThe operation to perform.
executionContext
ContextThe context under which the operation will take place.
Exceptions
- NotImplementedException
Thrown if a case for the TokenType of the operand was not defined.
ExecuteConjunctiveOperators(BinaryOperationNode, Context, Context, AccessMod)
Executes a boolean conjunctive operation.
private void ExecuteConjunctiveOperators(BinaryOperationNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
BinaryOperationNodeThe operation's AST node.
executionContext
ContextThe context under which the operation will take place.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
ExecuteObjectAttributeAccess(BinaryOperationNode, Context, Context, AccessMod, bool)
Executes an object attribute access operation.
private void ExecuteObjectAttributeAccess(BinaryOperationNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers, bool ignoreUndefinedVariable)
Parameters
node
BinaryOperationNodeThe operation's AST node.
executionContext
ContextThe context under which the operation will take place.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
ignoreUndefinedVariable
boolShould the interpreter ignore undefined variables?
GetRegisteredReference(Node, Context, Context, AccessMod)
Gets and checks if a node represents a variable reference.
private Reference? GetRegisteredReference(Node node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
NodeThe node to check.
executionContext
ContextThe Context in which the variable will be assigned.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
Returns
HandleSetStatus(SetStatus, string, Node, Context)
Throws errors for Set(Context?, string, Reference) returns.
private void HandleSetStatus(Context.SetStatus status, string referenceName, Node referenceNode, Context referenceContext)
Parameters
status
Context.SetStatusThe set status.
referenceName
stringThe name of the set reference.
referenceNode
NodeThe node of the reference object.
referenceContext
ContextThe context of the reference.
Exceptions
- NotImplementedException
Thrown if an unknown set status is encountered.
HandleSetStatus((SetStatus Status, Reference Reference), string, Node, Context)
Throws errors for Set(Context?, (IEzrObject Object, string Name), Reference, AccessMod) returns.
private Reference HandleSetStatus((Context.SetStatus Status, Reference Reference) setResult, string referenceName, Node referenceNode, Context referenceContext)
Parameters
setResult
(Context.SetStatus, Reference)The set result.
referenceName
stringThe name of the set reference.
referenceNode
NodeThe node of the reference object.
referenceContext
ContextThe context of the reference.
Returns
- Reference
The set reference.
IterateLoop(BigInteger?, double?, List<IEzrObject>, CountNode, Context, Context, AccessMod, AccessMod, Reference?, string, Context)
Code to run in an iteration of a count expression.
private int IterateLoop(BigInteger? iBigInteger, double? iDouble, List<IEzrObject> returns, CountNode node, Context callingContext, Context executionContext, AccessMod accessibilityModifiers, AccessMod operationAccessibilityModifiers, Reference? iterationVariableReference, string iterationVariableName, Context iterationVariableRegisteredContext)
Parameters
iBigInteger
BigInteger?The current iteration of the loop as a System.Numerics.BigInteger.
iDouble
double?The current iteration of the loop as a double.
returns
List<IEzrObject>The list of the returns of the loop.
node
CountNodeThe loop node.
callingContext
ContextThe Context calling on the execution of the iteration.
executionContext
ContextThe Context under which the iteration will be executed.
accessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing iteration.
operationAccessibilityModifiers
AccessModThe accessibility modifiers for the iteration variable.
iterationVariableReference
ReferenceThe iteration variable reference.
iterationVariableName
stringThe name of the iteration variable
iterationVariableRegisteredContext
ContextThe context under which the iteration variable is registered.
Returns
- int
-1 for errors, 1 to skip the iteration, 2 for stopping the loop or 0 for continuation of the loop.
VisitArrayLikeNodeArray(ArrayLikeNode, Context, Context, AccessMod)
Creates an array of objects.
private void VisitArrayLikeNodeArray(ArrayLikeNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
ArrayLikeNodeThe ArrayLikeNode to execute.
executionContext
ContextThe Context under which the array will be created.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
VisitArrayLikeNodeList(ArrayLikeNode, Context, Context, AccessMod)
Creates a list of references to its elements.
private void VisitArrayLikeNodeList(ArrayLikeNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
ArrayLikeNodeThe ArrayLikeNode to execute.
executionContext
ContextThe Context under which the list will be created.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
VisitBinaryOperationNode(BinaryOperationNode, Context, Context, AccessMod, bool)
Executes a BinaryOperationNode and performs a binary operation.
private void VisitBinaryOperationNode(BinaryOperationNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers, bool ignoreUndefinedVariable)
Parameters
node
BinaryOperationNodeThe BinaryOperationNode to execute.
executionContext
ContextThe Context under which the operation will be executed.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
ignoreUndefinedVariable
boolShould the interpreter ignore undefined variables?
VisitCallNode(CallNode, Context, Context, AccessMod)
Executes a CallNode and calls the Execute(Reference[], Interpreter, RuntimeResult) function in Receiver.
private void VisitCallNode(CallNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
CallNodeThe CallNode to execute.
executionContext
ContextThe Context under which the call takes place.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
VisitClassDefinitionNode(ClassDefinitionNode, Context, Context, AccessMod)
Executes a ClassDefinitionNode and creates a class.
private void VisitClassDefinitionNode(ClassDefinitionNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
ClassDefinitionNodeThe ClassDefinitionNode to execute.
executionContext
ContextThe Context in which the class will be created.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
VisitCountNode(CountNode, Context, Context, AccessMod)
Executes a CountNode and creates a counting loop.
private void VisitCountNode(CountNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
CountNodeThe CountNode to execute.
executionContext
ContextThe Context under which the loop will be executed.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
VisitDefineBlockNode(DefineBlockNode, Context, Context, AccessMod)
Executes a DefineBlockNode under its accessibility modifiers in the given Context.
private void VisitDefineBlockNode(DefineBlockNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
DefineBlockNodeThe DefineBlockNode to execute.
executionContext
ContextThe Context in which the block will be executed.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
VisitDictionaryNode(DictionaryNode, Context, Context, AccessMod)
Creates a EzrDictionary object from a DictionaryNode.
private void VisitDictionaryNode(DictionaryNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
DictionaryNodeThe DictionaryNode to execute.
executionContext
ContextThe Context under which the dictionary will be created.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
VisitForEachNode(ForEachNode, Context, Context, AccessMod)
Executes a ForEachNode and creates a for-each loop.
private void VisitForEachNode(ForEachNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
ForEachNodeThe ForEachNode to execute.
executionContext
ContextThe Context under which the loop will be executed.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
VisitFunctionDefinitionNode(FunctionDefinitionNode, Context, Context, AccessMod)
Executes a FunctionDefinitionNode and creates a function.
private void VisitFunctionDefinitionNode(FunctionDefinitionNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
FunctionDefinitionNodeThe FunctionDefinitionNode to execute.
executionContext
ContextThe Context in which the function will be created.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
VisitIfNode(IfNode, Context, Context, AccessMod)
Executes an IfNode and performs a boolean expression.
private void VisitIfNode(IfNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
IfNodeThe IfNode to execute.
executionContext
ContextThe Context under which the expression will be executed.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
VisitNoValueNode(NoValueNode, Context)
Executes a NoValueNode.
private void VisitNoValueNode(NoValueNode node, Context executionContext)
Parameters
node
NoValueNodeThe NoValueNode to execute.
executionContext
ContextThe Context under which the node is executed.
Remarks
This is (currently) only used for executing loop skip (calling SetSkipFlag(Reference)) and loop stop (calling SetStopFlag(Reference)) expressions.
Exceptions
- NotImplementedException
Thrown when an unimplemented or unexpected ValueType is encountered.
VisitNode(Node, Context, Context?, AccessMod, bool)
Vists a Node and sends it to the appropriate execution function.
internal void VisitNode(Node astNode, Context executionContext, Context? callingContext, AccessMod accessibilityModifiers, bool ignoreUndefinedVariable = false)
Parameters
astNode
NodeThe Node to execute.
executionContext
ContextThe Context under which the execution will take place.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
ignoreUndefinedVariable
boolShould the interpreter ignore undefined variables?
Exceptions
- ArgumentException
Raised if
astNode
is of type InvalidNode. This should never happen as this means something has gone very wrong in the Parser.- NotImplementedException
Raised if the method for executing
astNode
has not been implemented.
VisitReturnNode(ReturnNode, Context, Context, AccessMod)
Executes a ReturnNode and sets the return flag in RuntimeResult.
private void VisitReturnNode(ReturnNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
ReturnNodeThe ReturnNode to execute.
executionContext
ContextThe Context from which the arguments of the ReturnNode will be taken, if any.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
VisitStatementsNode(StatementsNode, Context, Context, AccessMod)
Interprets multiple statements.
private void VisitStatementsNode(StatementsNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
StatementsNodeThe StatementsNode to execute.
executionContext
ContextThe Context under which the statements will be executed.
callingContext
ContextThe Context calling on the execution of the statements.
accessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from executing the statements.
VisitTryNode(TryNode, Context, Context, AccessMod)
Executes a TryNode and creates a try-catch block.
private void VisitTryNode(TryNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
TryNodeThe TryNode to execute.
executionContext
ContextThe Context under which the block will be executed.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
VisitUnaryOperationNode(UnaryOperationNode, Context, Context, AccessMod)
Executes a UnaryOperationNode and performs a unary operation.
private void VisitUnaryOperationNode(UnaryOperationNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
UnaryOperationNodeThe UnaryOperationNode to execute.
executionContext
ContextThe Context under which the operation will be executed.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
Exceptions
- NotImplementedException
Thrown if a case for the TokenType of the operand was not defined.
VisitValueNode(ValueNode, Context)
Creates a "value" type object - i.e. EzrInteger, EzrFloat, EzrString, EzrCharacter and EzrCharacterList, from a ValueNode.
private void VisitValueNode(ValueNode node, Context executionContext)
Parameters
node
ValueNodeThe ValueNode to execute.
executionContext
ContextThe Context under which the value will be created.
Exceptions
- NotImplementedException
Raised if an unimplemented or unexpected TokenType is encountered in
node
.
VisitVariableAccessNode(VariableAccessNode, Context, Context, AccessMod, bool)
Executes a VariableAccessNode and access a variable from the given Context.
private void VisitVariableAccessNode(VariableAccessNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers, bool ignoreUndefinedVariable)
Parameters
node
VariableAccessNodeThe VariableAccessNode to execute.
executionContext
ContextThe Context from which the variable will be accessed.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
ignoreUndefinedVariable
boolShould the interpreter ignore undefined variables?
Exceptions
- NotImplementedException
Thrown if a case for the Context.GetStatus of the operation was not defined.
VisitVariableAssignmentNode(VariableAssignmentNode, Context, Context, AccessMod)
Executes a VariableAssignmentNode and sets a variable/constant in the given Context.
private void VisitVariableAssignmentNode(VariableAssignmentNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)
Parameters
node
VariableAssignmentNodeThe VariableAssignmentNode to execute.
executionContext
ContextThe Context in which the variable will be assigned.
callingContext
ContextaccessibilityModifiers
AccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
Remarks
A binary operation may also be executed depending on AssignmentOperator.
In the case a ArrayLikeNode is provided as Variable, multiple variables/constants will be assigned.
VisitWhileNode(WhileNode, Context, Context, AccessMod)
Executes a WhileNode and creates a while (condition = true) loop.
private void VisitWhileNode(WhileNode node, Context executionContext, Context callingContext, AccessMod accessibilityModifiers)