Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.49 KB | None | 0 0
  1. package Languages;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.HashMap;
  6.  
  7. import Token.Token;
  8. import struckture.DeclarationStructure;
  9. import struckture.IfWhileStruckture;
  10. import struckture.SimpleStruckture;
  11. import struckture.Struckture;
  12.  
  13. public class Pascal extends Language{
  14.    
  15.    
  16.  
  17.     public Pascal()
  18.     {
  19.     lexems =   new HashMap<String, String>();
  20.     lexems.put("declaration","var");
  21.     lexems.put("float type", "Real");
  22.     lexems.put("int type", "Integer");
  23.     lexems.put("str type", "String");
  24.     lexems.put("of type", ":");
  25.     lexems.put("acquire", ":=");
  26.     lexems.put("close", "end");
  27.     lexems.put("open", "Begin");
  28.     lexems.put("condition", "if");
  29.     lexems.put("cycle while", "while");
  30.     lexems.put("else chain", "else");
  31.     lexems.put("function left", "(");
  32.     lexems.put("function right", ")");
  33.     lexems.put("end of condition", "then");
  34.     lexems.put("end of condition while", "do");
  35.     lexems.put("lower", "<");
  36.     lexems.put("greater", ">");
  37.     lexems.put("sum", "+");
  38.     lexems.put("sub", "-");
  39.     lexems.put("div", "div");
  40.     lexems.put("mod", "mod");
  41.     lexems.put("mul", "*");  
  42.     lexems.put("semicolon", ";");
  43.     lexems.put("string mark ", "\\\"");
  44.     lexems.put("goa", ">=");
  45.     lexems.put("equals", "=");
  46.     lexems.put("loa", "<=");
  47.     commentSymbol = "\\\"";
  48.     }
  49.  
  50.     public ArrayList<Struckture> parseArray (Token[] tokens)
  51.     {
  52.         ArrayList <Struckture> strucktures = new ArrayList<>();
  53.         for (int i = 0; i< tokens.length; i++)
  54.         {
  55.            
  56.             if (Arrays.binarySearch(structuresIfWhile, tokens[i].getType()) >=0)
  57.             {
  58.                 Token keyWord = tokens[i];
  59.                 ArrayList <Token> condition = new ArrayList<>();
  60.                 ArrayList <Token> body = new ArrayList<>();
  61.                 i++;
  62.                 while (tokens[i].getType() != "end of condition" && tokens[i].getType() !=  "end of condition while")
  63.                 {
  64.                     if  (tokens[i].getType() != "ws")
  65.                     {
  66.                         condition.add(tokens[i]);
  67.                         i++;
  68.                     }
  69.                     else
  70.                         i++;               
  71.                 }
  72.                 i++;
  73.                
  74.                 while (tokens[i].getType() != "close")
  75.                 {
  76.                     if (tokens[i].getType() != "open" && tokens[i].getType() != "ws")
  77.                     {
  78.                         body.add(tokens[i]);
  79.                         i++;
  80.                     }
  81.                     else
  82.                         i++;               
  83.                 }
  84.                
  85.                 Struckture newS = new IfWhileStruckture(keyWord, condition.toArray(new Token[condition.size()]),body.toArray(new Token[body.size()]) );
  86.                 strucktures.add(newS);
  87.             }
  88.            
  89.             else if ( tokens[i].getType() == "declaration")
  90.             {
  91.                
  92.                 ArrayList <Token> variables = new ArrayList<>();
  93.                
  94.                
  95.                 i++;
  96.                 while (tokens[i].getType() != "open")
  97.                 {
  98.                     if (tokens[i].getType() == "identifier")
  99.                     {
  100.                         variables.add(tokens[i]);
  101.                         i++;
  102.                     }
  103.                     else if (Arrays.binarySearch(declarations, tokens[i].getType()) >=0)
  104.                     {
  105.                         strucktures.add(new DeclarationStructure (tokens[i],variables.toArray(new Token[variables.size()])));
  106.                         variables = new ArrayList<>();
  107.                         i++;
  108.                     }
  109.                     else
  110.                         i++;
  111.                 }
  112.             }
  113.            
  114.             else if (tokens[i].getType() == "identifier")
  115.             {
  116.                 ArrayList <Token> body = new ArrayList<>();
  117.                 while (tokens[i].getType() != "semicolon")
  118.                 {
  119.                     body.add(tokens[i]);
  120.                     i++;
  121.                 }
  122.                 strucktures.add(new SimpleStruckture (body.toArray(new Token[body.size()])));
  123.             }
  124.            
  125.            
  126.         }
  127.         return strucktures;
  128.     }
  129.  
  130. public String[] concat(ArrayList <Struckture> strucktures){
  131.    
  132.     String result = "";
  133.        
  134.     result.add("var");
  135.    
  136.     for (int i = 0; i < strucktures.length; i++){
  137.         if (structures.get(i) instanceof DeclarationStructure){
  138.             String variablesString = "";
  139.             for (int j = 0; i < structures.get(i).variables.length() - 1; i++){
  140.                 variablesString.concat(structures.get(i).variables[j] + ",");
  141.             }
  142.             variablesString.concat(structures.get(i).variables[variables.length()]);
  143.             result.add(variablesString + ":" + lexems.get(structures.get(i).getType().getType()) + ";");
  144.             strucktures.remove(structures.get(i));
  145.         }
  146.     }
  147.    
  148.     for (int i = 0; i < strucktures.length; i++){
  149.         if (structures.get(i) instanceof IfWhileStruckture){
  150.             if (structures.get(i).getType().getType() == "cycle while"){
  151.                 String cycleString = "While ";
  152.                 for (int j = 0; j < structures.get(i).getCondition().size(); j++){
  153.                     cycleString.concat(lexems.get(structures.get(i).getCondition()[j].getType()) + " ");
  154.                 }
  155.                 cycleString.concat(" Do");
  156.                 result.add(cycleString);
  157.                 result.add("Begin");
  158.                 String bodyString = "";
  159.                 for (int j = 0; j < structures.get(i).getBody().length()){
  160.                     bodyString.concat(lexems.get(structures.get(i).getBody()[j].getType() + " ");
  161.                 }
  162.                 bodyString.concat(";");
  163.                 result.add(bodyString);
  164.                 result.add("End;");
  165.             }
  166.            
  167.             if (structures.get(i).getType().getType() == "if"){
  168.                 String ifString = "if ";
  169.                 for (int j = 0; j < structures.get(i).getCondition().size(); j++){
  170.                     cycleString.concat(lexems.get(structures.get(i).getCondition()[j].getType()) + " ");
  171.                 }
  172.                 cycleString.concat(" then");
  173.                 result.add(cycleString);
  174.                 for (int j = 0; j < structures.get(i).getBody().length()){
  175.                     bodyString.concat(lexems.get(structures.get(i).getBody()[j].getType() + " ");
  176.                 }
  177.                 bodyString.concat(";");
  178.                 result.add(bodyString);
  179.                 result.add("End;");
  180.                 }
  181.             }
  182.        
  183.         if (structures.get(i) instanceof SimpleStruckture){
  184.             String line = "";
  185.             for (j = 0; j < structures.get(i).detBody().length()){
  186.                 if (structures.get(i).getBody().[j] != "semicolon"){
  187.                     line.concat((lexems.get(structures.get(i).getBody()[j].getType() + " ");
  188.                 }
  189.                 else {
  190.                     line.concat(";");
  191.                     result.add(line);
  192.                 }
  193.         }
  194.        
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement