/// <summary> /// Analyzes ariphmetic expression. /// </summary> /// <param name="startPos">Start position of an expression.</param> /// <param name="finishPos">End position of an expression.</param> /// <param name="parentNode">Tree parent node of an expression.</param> private void processor_ariphmetic(int startPos, int finishPos, DTreeNode <string> parentNode) { // Check for minus int type = 0, value = 0; token.token(startPos, ref type, ref value); if (value == 1) { DTreeNode <string> node = parentNode.Nodes.Add(LexemTypeHelper.getTypedValue(1, valueOf(startPos))); if (finishPos > startPos) { processor_ariphmetic(startPos + 1, finishPos, node); } return; } // Search for (...)+(...) not only +, but + - * int pos = findAriphmeticOutsideBraces(startPos, finishPos); if (pos != -1) { DTreeNode <string> node = parentNode.Nodes.Add(LexemTypeHelper.getTypedValue(1, valueOf(pos))); processor_ariphmetic(startPos, pos - 1, node); processor_ariphmetic(pos + 1, finishPos, node); return; } // Search for (...) int openPos, closePos = 0; openPos = findMatchingBraces(startPos, 2, ref closePos); if (openPos == startPos && closePos == finishPos) { processor_ariphmetic(startPos + 1, finishPos - 1, parentNode); return; } if (startPos + 2 == finishPos) { token.token(startPos + 1, ref type, ref value); DTreeNode <string> t = parentNode.Nodes.Add(lAnalyzer.tokenStringBuilder(type, value)); token.token(startPos, ref type, ref value); t.Nodes.Add(lAnalyzer.tokenStringBuilder(type, value)); token.token(startPos + 2, ref type, ref value); t.Nodes.Add(lAnalyzer.tokenStringBuilder(type, value)); return; } // Search for identifiers token.token(startPos, ref type, ref value); if (startPos == finishPos && type > 2) { parentNode.Nodes.Add(lAnalyzer.tokenStringBuilder(type, value)); return; } else { error(startPos); } }
/// <summary> /// Process the code segment belongs to class. /// </summary> /// <param name="start">Number of the first token, that belongs to class.</param> /// <param name="pos"></param> private void processor_Class(int pos) { int curlyStart = 0, curlyFinish = 0; int finded = 0; int type = 0, value = 0; int currentPos = pos; // finding open curly brace of a class finded = findFirstOccurence(1, 11, currentPos); if (finded == -1) { error(currentPos); } else { curlyStart = finded; currentPos++; } // finding closing curly brace findMatchingBraces(curlyStart, 1, ref curlyFinish); // adding class occurence in the tree DTreeNode <string> node = tree.getRootNode(); token.token(pos + 1, ref type, ref value); node = node.Nodes.Add(LexemTypeHelper.getTypedValue(3, ident.identifier(value))); // go to class content handler processor_findMethodsInClass(curlyStart, curlyFinish, node); }
/// <summary> /// Analyzes expression. /// </summary> /// <param name="startPos">Position, where expression starts.</param> /// <param name="assigmentPos">Position, where assigment sign is.</param> /// <param name="finishPos">Position, were expression ends.</param> private DTreeNode <string> processor_expression(int startPos, int assigmentPos, int finishPos, DTreeNode <string> parentNode) { DTreeNode <string> node = parentNode.Nodes.Add(LexemTypeHelper.getTypedValue(1, valueOf(assigmentPos))); processor_variable(startPos, assigmentPos - 1, node); processor_ariphmetic(assigmentPos + 1, finishPos - 1, node); return(node); }
/// <summary> /// Analyzes basic statement (1 compare sign). /// </summary> /// <param name="startPos">Start position of a statement.</param> /// <param name="finishPos">End position of a statement.</param> /// <param name="parentNode">Tree parent node of a statement.</param> /// <returns></returns> private DTreeNode <string> processor_statement(int startPos, int finishPos, DTreeNode <string> parentNode) { DTreeNode <string> node = parentNode.Nodes.Add(LexemTypeHelper.getTypedValue(1, valueOf(startPos + 1))); int type = 0, value = 0; token.token(startPos, ref type, ref value); node.Nodes.Add(lAnalyzer.tokenStringBuilder(type, value)); token.token(finishPos, ref type, ref value); node.Nodes.Add(lAnalyzer.tokenStringBuilder(type, value)); return(node); }
/// <summary> /// Finds all methods within a class. /// </summary> /// <param name="start">Open curly brace of a class.</param> /// <param name="finish">Close curly brace of a class.</param> /// <param name="parentNode">Tree node of the wrapping class.</param> private void processor_findMethodsInClass(int start, int finish, DTreeNode <string> parentNode) { int curlyFinish = 0, curPos = start; DTreeNode <string> curNode = tree.addNode(parentNode, LexemTypeHelper.getTypedValue(6, "methods")); while ((curPos = findFirstIdentOccurence(curPos)) != -1) { findMatchingBraces(curPos + 1, 1, ref curlyFinish); processor_method(start + 1, curPos, curlyFinish, curNode); curPos = curlyFinish; } }
/// <summary> /// Analyzes System namespace command. /// </summary> /// <param name="startPos">Start position of a command.</param> /// <param name="finishPos">End position of a command.</param> /// <param name="parentNode">Tree parent node of a command.</param> /// <returns></returns> private DTreeNode <string> processor_system(int startPos, int finishPos, DTreeNode <string> parentNode) { parentNode = parentNode.Nodes.Add(LexemTypeHelper.getTypedValue(2, valueOf(startPos))); int i = startPos + 2; DTreeNode <string> node2 = parentNode; while (i < finishPos) { node2 = node2.Nodes.Add(LexemTypeHelper.getTypedValue(2, valueOf(i))); i += 2; } return(parentNode); }
/// <summary> /// Analyzes if statement. /// </summary> /// <param name="startPos">Start position of a statement.</param> /// <param name="finishPos">End position of a statement.</param> /// <param name="parentNode">Tree parent node of a statement.</param> /// <returns></returns> private DTreeNode <string> processor_if(int startPos, int finishPos, DTreeNode <string> parentNode) { int braceStart, braceFinish = 0; braceStart = findMatchingBraces(startPos, 2, ref braceFinish); DTreeNode <string> node = parentNode.Nodes.Add(LexemTypeHelper.getTypedValue(2, valueOf(startPos))); processor_statement(braceStart + 1, braceFinish - 1, node); braceStart = findMatchingBraces(startPos, 1, ref braceFinish); processor_code(braceStart + 1, braceFinish, node); return(node); }
/// <summary> /// Processes argument list of a method. /// </summary> /// <param name="parentNode">Args parent node to add arguments.</param> /// <param name="curlyStart">Position of open round brace.</param> /// <param name="curlyFinish">Position of close round brace.</param> private void processor_args(DTreeNode <string> parentNode, int braceStart, int braceFinish) { int pos, prevPos = braceStart + 1; DTreeNode <string> node; while ((pos = findFirstIdentOccurence(prevPos)) != -1 && pos < braceFinish) { node = parentNode.Nodes.Add(LexemTypeHelper.getTypedValue(3, valueOf(pos))); for (int i = prevPos; i < pos; i++) { node.Nodes.Add(LexemTypeHelper.getTypedValue(2, valueOf(i))); } prevPos = pos + 2; } }
/// <summary> /// Analyzes variable declaration or using as left part of assignment operator. /// </summary> /// <param name="startPos">Start position of sequence.</param> /// <param name="finishPos">Position of a name of variable.</param> /// <param name="parentNode">Tree node that is a parent of current expression.</param> private DTreeNode <string> processor_variable(int startPos, int finishPos, DTreeNode <string> parentNode) { DTreeNode <string> node = null; int type = 0, value = 0; token.token(finishPos, ref type, ref value); if (type != 3) { error(startPos); } else { node = parentNode.Nodes.Add(LexemTypeHelper.getTypedValue(3, valueOf(finishPos))); for (int i = startPos; i < finishPos; i++) { node.Nodes.Add(LexemTypeHelper.getTypedValue(2, valueOf(i))); } } return(node); }
/// <summary> /// Processes a single method of a class. /// </summary> /// <param name="start">Token, that starts a method (it's name or modifiers).</param> /// <param name="namePos">Method name' position.</param> /// <param name="finish">Position of the close curly brace.</param> /// <param name="parentNode">Tree node "methods" of a wrapping class.</param> private void processor_method(int start, int namePos, int finish, DTreeNode <string> parentNode) { DTreeNode <string> curNode = tree.addNode(parentNode, LexemTypeHelper.getTypedValue(3, valueOf(namePos))); if (valueOf(namePos) == "main") { tree.setEntryPoint(curNode); } for (int i = start; i < namePos; i++) { tree.addNode(curNode, LexemTypeHelper.getTypedValue(2, valueOf(i))); } DTreeNode <string> args = tree.addNode(curNode, LexemTypeHelper.getTypedValue(6, "args")); int closeBrace = 0; findMatchingBraces(namePos, 2, ref closeBrace); processor_args(args, namePos + 1, closeBrace); processor_code(closeBrace + 2, finish, curNode); }