Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Languages;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import Token.Token;
- import struckture.DeclarationStructure;
- import struckture.IfWhileStruckture;
- import struckture.SimpleStruckture;
- import struckture.Struckture;
- public class Pascal extends Language{
- public Pascal()
- {
- lexems = new HashMap<String, String>();
- lexems.put("declaration","var");
- lexems.put("float type", "Real");
- lexems.put("int type", "Integer");
- lexems.put("str type", "String");
- lexems.put("of type", ":");
- lexems.put("acquire", ":=");
- lexems.put("close", "end");
- lexems.put("open", "Begin");
- lexems.put("condition", "if");
- lexems.put("cycle while", "while");
- lexems.put("else chain", "else");
- lexems.put("function left", "(");
- lexems.put("function right", ")");
- lexems.put("end of condition", "then");
- lexems.put("end of condition while", "do");
- lexems.put("lower", "<");
- lexems.put("greater", ">");
- lexems.put("sum", "+");
- lexems.put("sub", "-");
- lexems.put("div", "div");
- lexems.put("mod", "mod");
- lexems.put("mul", "*");
- lexems.put("semicolon", ";");
- lexems.put("string mark ", "\\\"");
- lexems.put("goa", ">=");
- lexems.put("equals", "=");
- lexems.put("loa", "<=");
- commentSymbol = "\\\"";
- }
- public ArrayList<Struckture> parseArray (Token[] tokens)
- {
- ArrayList <Struckture> strucktures = new ArrayList<>();
- for (int i = 0; i< tokens.length; i++)
- {
- if (Arrays.binarySearch(structuresIfWhile, tokens[i].getType()) >=0)
- {
- Token keyWord = tokens[i];
- ArrayList <Token> condition = new ArrayList<>();
- ArrayList <Token> body = new ArrayList<>();
- i++;
- while (tokens[i].getType() != "end of condition" && tokens[i].getType() != "end of condition while")
- {
- if (tokens[i].getType() != "ws")
- {
- condition.add(tokens[i]);
- i++;
- }
- else
- i++;
- }
- i++;
- while (tokens[i].getType() != "close")
- {
- if (tokens[i].getType() != "open" && tokens[i].getType() != "ws")
- {
- body.add(tokens[i]);
- i++;
- }
- else
- i++;
- }
- Struckture newS = new IfWhileStruckture(keyWord, condition.toArray(new Token[condition.size()]),body.toArray(new Token[body.size()]) );
- strucktures.add(newS);
- }
- else if ( tokens[i].getType() == "declaration")
- {
- ArrayList <Token> variables = new ArrayList<>();
- i++;
- while (tokens[i].getType() != "open")
- {
- if (tokens[i].getType() == "identifier")
- {
- variables.add(tokens[i]);
- i++;
- }
- else if (Arrays.binarySearch(declarations, tokens[i].getType()) >=0)
- {
- strucktures.add(new DeclarationStructure (tokens[i],variables.toArray(new Token[variables.size()])));
- variables = new ArrayList<>();
- i++;
- }
- else
- i++;
- }
- }
- else if (tokens[i].getType() == "identifier")
- {
- ArrayList <Token> body = new ArrayList<>();
- while (tokens[i].getType() != "semicolon")
- {
- body.add(tokens[i]);
- i++;
- }
- strucktures.add(new SimpleStruckture (body.toArray(new Token[body.size()])));
- }
- }
- return strucktures;
- }
- public String[] concat(ArrayList <Struckture> strucktures){
- String result = "";
- result.add("var");
- for (int i = 0; i < strucktures.length; i++){
- if (structures.get(i) instanceof DeclarationStructure){
- String variablesString = "";
- for (int j = 0; i < structures.get(i).variables.length() - 1; i++){
- variablesString.concat(structures.get(i).variables[j] + ",");
- }
- variablesString.concat(structures.get(i).variables[variables.length()]);
- result.add(variablesString + ":" + lexems.get(structures.get(i).getType().getType()) + ";");
- strucktures.remove(structures.get(i));
- }
- }
- for (int i = 0; i < strucktures.length; i++){
- if (structures.get(i) instanceof IfWhileStruckture){
- if (structures.get(i).getType().getType() == "cycle while"){
- String cycleString = "While ";
- for (int j = 0; j < structures.get(i).getCondition().size(); j++){
- cycleString.concat(lexems.get(structures.get(i).getCondition()[j].getType()) + " ");
- }
- cycleString.concat(" Do");
- result.add(cycleString);
- result.add("Begin");
- String bodyString = "";
- for (int j = 0; j < structures.get(i).getBody().length()){
- bodyString.concat(lexems.get(structures.get(i).getBody()[j].getType() + " ");
- }
- bodyString.concat(";");
- result.add(bodyString);
- result.add("End;");
- }
- if (structures.get(i).getType().getType() == "if"){
- String ifString = "if ";
- for (int j = 0; j < structures.get(i).getCondition().size(); j++){
- cycleString.concat(lexems.get(structures.get(i).getCondition()[j].getType()) + " ");
- }
- cycleString.concat(" then");
- result.add(cycleString);
- for (int j = 0; j < structures.get(i).getBody().length()){
- bodyString.concat(lexems.get(structures.get(i).getBody()[j].getType() + " ");
- }
- bodyString.concat(";");
- result.add(bodyString);
- result.add("End;");
- }
- }
- if (structures.get(i) instanceof SimpleStruckture){
- String line = "";
- for (j = 0; j < structures.get(i).detBody().length()){
- if (structures.get(i).getBody().[j] != "semicolon"){
- line.concat((lexems.get(structures.get(i).getBody()[j].getType() + " ");
- }
- else {
- line.concat(";");
- result.add(line);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement