/// <summary> /// Analyzes a single syntaxTree node to decide what to do next. /// </summary> /// <param name="parentNode">Current syntaxTree node to precess.</param> protected void analyzeNode(DTreeNode <string> parentNode) { int type = 0; string s = LexemTypeHelper.parse(ref type, parentNode.Value); string identType; switch (type) { case 1: if (s == "=" || s == "+") { checkAssignment(parentNode); } break; case 3: if (identifiers.TryGetValue(s, out identType)) { checkDeclaring(parentNode); } else { identifiers.Add(s, LexemTypeHelper.getParsedValue(parentNode.Nodes[0].Value)); } break; } foreach (DTreeNode <string> node in parentNode.Nodes) { analyzeNode(node); } }
/// <summary> /// Gets type of parts of math expression or assignment from a /// single child node. /// </summary> /// <param name="parentNode">Curent syntaxTree node to process.</param> /// <returns>Type of child node.</returns> protected Type getChildType(DTreeNode <string> parentNode) { int type = 0; string s = LexemTypeHelper.parse(ref type, parentNode.Value); switch (type) { case 1: if (s == "=" || s == "+" || s == "*" || (s == "-" && parentNode.Nodes.Count != 1)) { return(checkAssignment(parentNode)); } else { return(getChildType(parentNode.Nodes[0])); } case 3: return(getIdentifierType(parentNode, s)); case 4: return(typeof(double)); case 5: return(typeof(int)); default: undefError(); return(typeof(int)); } }
/// <summary> /// Checks, if identifier is already declared. /// </summary> /// <param name="parentNode">Current syntaxTree node to process.</param> protected void checkDeclaring(DTreeNode <string> parentNode) { if (parentNode.Nodes.Count > 0) { int type = 0; string name = LexemTypeHelper.parse(ref type, parentNode.Nodes[0].Value); if (type == 2 && (name == "int" || name == "double" || name == "String")) { identifierError(parentNode); } } }
/// <summary> /// Handles assignment operator. /// </summary> /// <param name="node">SyntaxTree node of operator.</param> protected void handleAssignment(DTreeNode <string> node) { int type = 0; string name = LexemTypeHelper.parse(ref type, node.Nodes[0].Value); getValue(node.Nodes[1]); ilg.Emit(OpCodes.Stloc, handleIdentifier(node.Nodes[0])); if (node.Nodes.Count > 2) { next(node.Nodes[2]); } }
/// <summary> /// Processes the next independent command in the code. /// </summary> /// <param name="node">SyntaxTree node of the command.</param> protected void next(DTreeNode <string> node) { int type = 0; string value = LexemTypeHelper.parse(ref type, node.Value); switch (type) { case 1: handeOperator(node); break; case 2: handleKeyword(node); break; case 3: handleIdentifier(node); break; default: break; } }
/// <summary> /// Handles identifier. Get LocalBuilder or creates it. /// </summary> /// <param name="node">SyntaxTree node of identifier.</param> /// <returns>LocalBUilder instance that represents the identifier.</returns> protected LocalBuilder handleIdentifier(DTreeNode <string> node) { int type = 0; string s = LexemTypeHelper.parse(ref type, node.Value); LocalBuilder lBuilder; if (!locals.TryGetValue(s, out lBuilder)) { if (node.Nodes.Count == 0) { // ERROR! TODO } lBuilder = ilg.DeclareLocal(ILHelper.getILType(LexemTypeHelper.getParsedValue(node.Nodes[0].Value))); locals.Add(s, lBuilder); } return(lBuilder); }