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
variableReferenceThe variable reference to assign to.
valueIEzrObjectThe value object to be assigned.
nodeVariableAssignmentNodeThe VariableAssignmentNode being executed.
executionContextContextThe Context in which the variable will be assigned.
callingContextContextaccessibilityModifiersAccessModThe 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
nodeVariableAssignmentNodeThe VariableAssignmentNode being executed.
variablesReference[]The array of variable references to be assigned to.
isSingleVariableboolIs only one variable being assigned?
executionContextContextThe Context in which the variable will be assigned.
callingContextContextaccessibilityModifiersAccessModThe 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
astNodeThe AST, as a single Node object.
runtimeContextContextThe run-time Context in which the AST will be executed.
accessibilityModifiersAccessModThe 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
firstIEzrObjectThe first value in the operation.
secondIEzrObjectThe second value in the operation.
nodeNodeThe operation's AST node.
operationTokenTypeThe operation to perform.
executionContextContextThe 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
nodeBinaryOperationNodeThe operation's AST node.
executionContextContextThe context under which the operation will take place.
callingContextContextaccessibilityModifiersAccessModThe 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
nodeBinaryOperationNodeThe operation's AST node.
executionContextContextThe context under which the operation will take place.
callingContextContextaccessibilityModifiersAccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
ignoreUndefinedVariableboolShould 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
nodeNodeThe node to check.
executionContextContextThe Context in which the variable will be assigned.
callingContextContextaccessibilityModifiersAccessModThe 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
statusContext.SetStatusThe set status.
referenceNamestringThe name of the set reference.
referenceNodeNodeThe node of the reference object.
referenceContextContextThe 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.
referenceNamestringThe name of the set reference.
referenceNodeNodeThe node of the reference object.
referenceContextContextThe 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
iBigIntegerBigInteger?The current iteration of the loop as a System.Numerics.BigInteger.
iDoubledouble?The current iteration of the loop as a double.
returnsList<IEzrObject>The list of the returns of the loop.
nodeCountNodeThe loop node.
callingContextContextThe Context calling on the execution of the iteration.
executionContextContextThe Context under which the iteration will be executed.
accessibilityModifiersAccessModThe accessibility modifiers for objects that will be assigned from the executing iteration.
operationAccessibilityModifiersAccessModThe accessibility modifiers for the iteration variable.
iterationVariableReferenceReferenceThe iteration variable reference.
iterationVariableNamestringThe name of the iteration variable
iterationVariableRegisteredContextContextThe 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
nodeArrayLikeNodeThe ArrayLikeNode to execute.
executionContextContextThe Context under which the array will be created.
callingContextContextaccessibilityModifiersAccessModThe 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
nodeArrayLikeNodeThe ArrayLikeNode to execute.
executionContextContextThe Context under which the list will be created.
callingContextContextaccessibilityModifiersAccessModThe 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
nodeBinaryOperationNodeThe BinaryOperationNode to execute.
executionContextContextThe Context under which the operation will be executed.
callingContextContextaccessibilityModifiersAccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
ignoreUndefinedVariableboolShould 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
nodeCallNodeThe CallNode to execute.
executionContextContextThe Context under which the call takes place.
callingContextContextaccessibilityModifiersAccessModThe 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
nodeClassDefinitionNodeThe ClassDefinitionNode to execute.
executionContextContextThe Context in which the class will be created.
callingContextContextaccessibilityModifiersAccessModThe 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
nodeCountNodeThe CountNode to execute.
executionContextContextThe Context under which the loop will be executed.
callingContextContextaccessibilityModifiersAccessModThe 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
nodeDefineBlockNodeThe DefineBlockNode to execute.
executionContextContextThe Context in which the block will be executed.
callingContextContextaccessibilityModifiersAccessModThe 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
nodeDictionaryNodeThe DictionaryNode to execute.
executionContextContextThe Context under which the dictionary will be created.
callingContextContextaccessibilityModifiersAccessModThe 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
nodeForEachNodeThe ForEachNode to execute.
executionContextContextThe Context under which the loop will be executed.
callingContextContextaccessibilityModifiersAccessModThe 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
nodeFunctionDefinitionNodeThe FunctionDefinitionNode to execute.
executionContextContextThe Context in which the function will be created.
callingContextContextaccessibilityModifiersAccessModThe 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
nodeIfNodeThe IfNode to execute.
executionContextContextThe Context under which the expression will be executed.
callingContextContextaccessibilityModifiersAccessModThe 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
nodeNoValueNodeThe NoValueNode to execute.
executionContextContextThe 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
astNodeNodeThe Node to execute.
executionContextContextThe Context under which the execution will take place.
callingContextContextaccessibilityModifiersAccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
ignoreUndefinedVariableboolShould the interpreter ignore undefined variables?
Exceptions
- ArgumentException
Raised if
astNodeis 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
astNodehas 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
nodeReturnNodeThe ReturnNode to execute.
executionContextContextThe Context from which the arguments of the ReturnNode will be taken, if any.
callingContextContextaccessibilityModifiersAccessModThe 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
nodeStatementsNodeThe StatementsNode to execute.
executionContextContextThe Context under which the statements will be executed.
callingContextContextThe Context calling on the execution of the statements.
accessibilityModifiersAccessModThe 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
nodeTryNodeThe TryNode to execute.
executionContextContextThe Context under which the block will be executed.
callingContextContextaccessibilityModifiersAccessModThe 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
nodeUnaryOperationNodeThe UnaryOperationNode to execute.
executionContextContextThe Context under which the operation will be executed.
callingContextContextaccessibilityModifiersAccessModThe 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
nodeValueNodeThe ValueNode to execute.
executionContextContextThe 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
nodeVariableAccessNodeThe VariableAccessNode to execute.
executionContextContextThe Context from which the variable will be accessed.
callingContextContextaccessibilityModifiersAccessModThe accessibility modifiers for objects that will be assigned from the executing Node.
ignoreUndefinedVariableboolShould 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
nodeVariableAssignmentNodeThe VariableAssignmentNode to execute.
executionContextContextThe Context in which the variable will be assigned.
callingContextContextaccessibilityModifiersAccessModThe 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)