public Token saveToken(string hash, string type) { Token t = new Token(); t.category = type; t.text = hash; t.error = false; return t; }
public int BuscaVar(Token t) { foreach (Token item in variablesInt) { if (t.text.Equals(item.text)) { return 1; } } foreach (Token item in variablesBool) { if(t.text.Equals(item.text)) { return 2; } } return -1; }
public BindingList<Token> ClassifyNumbers(BindingList<Token> list) { BindingList<Token> auxList = new BindingList<Token>(); Token token = null, nextToken = null, nextnextToken = null; int j = 0; string auxText = null; //for each token, test if it's a number it's type for (int i = 0; i < list.Count; i++) { token = list[i]; //É número if (token.category == "number") { nextToken = list[i++]; //Próximo é ponto if (nextToken.category == "dot") { j = i + 2; nextnextToken = list[j]; //Número real if (nextnextToken.category == "number") { auxText = token.text + nextToken.text + nextnextToken.text; //add into a new list Token auxToken = new Token(); auxToken.text = auxText; auxToken.error = false; auxToken.lineNumber = token.lineNumber; auxToken.category = "num_real"; auxText = null; auxList.Add(auxToken); i = i + 2; } else { //número integer auxText = token.text; //add into a new list Token auxToken = new Token(); auxToken.text = auxText; auxToken.error = false; auxToken.lineNumber = token.lineNumber; auxToken.category = "num_integer"; auxText = null; auxList.Add(auxToken); } } //Número inteiro else { auxText = token.text; //add into a new list Token auxToken = new Token(); auxToken.text = auxText; auxToken.error = false; auxToken.lineNumber = token.lineNumber; auxToken.category = "num_integer"; auxText = null; auxList.Add(auxToken); } } else { Token auxToken = new Token(); auxToken = token; auxList.Add(auxToken); } } return auxList; }
//como fazer a busca: //foreach (string item in operators) //{ // console.writeline(item); //} public BindingList<Token> transformToken(BindingList<Token> list) { BindingList<Token> auxList = new BindingList<Token>(); bool isText = false, isComment = false; int line = -1, lineComment = -1; string auxText = null, comment = null; //for each token, test if it's a commentary or a word/string foreach (Token token in list) { if (isText) { if (token.lineNumber == line) { if (token.text.Equals("'")) { isText = false; //add into a new list Token auxToken = new Token(); auxToken.text = auxText; auxToken.error = false; auxToken.lineNumber = line; auxToken.category = "word"; auxText = null; auxList.Add(auxToken); Token auxToken2 = new Token(); auxToken2 = token; auxList.Add(auxToken2); } else auxText += token.text + " "; } else { isText = false; //add into a new list Token auxToken = new Token(); auxToken.text = auxText; auxToken.error = false; auxToken.lineNumber = line; auxToken.category = "word"; auxText = null; auxList.Add(auxToken); } } else { if (token.text.Equals("'")) { isText = true; line = token.lineNumber; Token auxToken = new Token(); auxToken = token; auxList.Add(auxToken); } else { //add into a new list Token auxToken = new Token(); auxToken = token; auxList.Add(auxToken); } } if (isComment) { if (token.lineNumber == lineComment) { if (token.text.Equals("//")) { isComment = false; //add into a new list Token auxToken = new Token(); auxToken.text = comment; auxToken.error = false; auxToken.category = "comment"; auxToken.lineNumber = lineComment; auxText = null; auxList.Add(auxToken); } else { //concatenate words from a commentary area auxText += token.text + " "; } } else { isText = false; //add into a new list Token auxToken = new Token(); auxToken.text = comment; auxToken.category = null; auxToken.category = "comment"; auxToken.error = false; auxToken.lineNumber = lineComment; auxText = null; auxList.Add(auxToken); } } else { if (token.text.Equals("//")) { isText = true; lineComment = token.lineNumber; Token auxToken = new Token(); auxToken = token; auxList.Add(auxToken); } } } return auxList; }
/// <summary> /// Recebe uma lista de linhas do programa e cria uma lista de tokens /// </summary> /// <param name="value"></param> public void getProgramLines( List<ProgramLine> value ) { Console.WriteLine(value); string hash = null; int j = 0; char thischar = '0'; char nextchar = '0'; bool islast = false; bool isbreak = false; bool isdoublebreak = false; bool error = false; //-1 - no; 0 - space/enter; 1 - simple break; 2 - double break // Lê cada linha do programa foreach (var item in value) { //Lê cada char da linha for (int i = 0; i < item.text.Length; i++) { //início do for islast = false; isbreak = false; isdoublebreak = false; error = false; //salva char atual na variavel thischar = item.text[i]; //pega prox char j = i + 1; if (j < item.text.Length) { nextchar = item.text[j]; } else { islast = true; } #region Verifica tipo do char //testa se é letra if ((thischar >= 'a') && (thischar <= 'z')) { hash += thischar; } else { //testa se é número if ((thischar >= '0') && (thischar <= '9')) { hash += thischar; } //testa se é operador else { # region switch //verifica que tipo de operador é switch (thischar) { case ' ': case '\r': case '\n': case '=': case '[': case ']': case '.': case ',': case '(': case ')': case '^': case '@': case '{': case '}': case '$': case '#': case '&': case '%': case '\'': case '\"': case ';': isbreak = true; break; case '<': if (!islast) { if ((nextchar == '<') || (nextchar == '>') || (nextchar == '=')) { isdoublebreak = true; } } isbreak = true; break; case '>': if (!islast) { if ((nextchar == '<') || (nextchar == '>') || (nextchar == '=')) { isdoublebreak = true; } } isbreak = true; break; case '*': if (!islast) { if ((nextchar == '*') || (nextchar == '=')) { isdoublebreak = true; } } isbreak = true; break; case '/': if (!islast) { if ((nextchar == '/') || (nextchar == '=')) { isdoublebreak = true; } } isbreak = true; break; case ':': if (!islast) { if ((nextchar == '=')) { isdoublebreak = true; } } isbreak = true; break; case '+': if (!islast) { if ((nextchar == '=') || (nextchar == '+')) { isdoublebreak = true; } } isbreak = true; break; case '-': if (!islast) { if ((nextchar == '=') || (nextchar == '-')) { isdoublebreak = true; } } isbreak = true; break; default: isbreak = true; error = true; break; } #endregion #region isbreak == true //eh um char de parada if (isbreak == true) { if (hash != null) { //cria uma instancia Token Token _token = new Token(); //adiciona atributos a instancia _token.lineNumber = item.lineNumber; _token.text = hash; //adiciona instancia na lista de token BdTokenList.Add(_token); } hash = null; if ((thischar == ' ')) { //não faz nada } else { hash += thischar; //adiciona operador na lista if (isdoublebreak == true) { hash += nextchar; i++; } //cria uma instancia Token Token _tokenOp = new Token(); //adiciona atributos a instancia _tokenOp.lineNumber = item.lineNumber; _tokenOp.text = hash; //Simbolo nao identificado if (error == true) { _tokenOp.error = true; } //adiciona instancia na lista de token BdTokenList.Add(_tokenOp); hash = null; } } #endregion } } #endregion if (islast && (hash != null)) { //cria uma instancia Token Token _tokenL = new Token(); /// //adiciona atributos a instancia _tokenL.lineNumber = item.lineNumber; _tokenL.text = hash; //adiciona instancia na lista de token BdTokenList.Add(_tokenL); hash = null; } } }
private int sintaxDeclarationVariables(BindingList<Token> list, int i) { // BindingList<Token> varList = new BindingList<Token>(); //throw new NotImplementedException(); if (list[i].category == "identifier") { i++; do { sintaxVar(list, i); if (list[i].category == "doubleComma") { i++; //BindingList<Token> aux = new BindingList<Token>(); if (list[i].category == "integer") { while(list[i].category == "integer"){ Token auxToken = new Token(); auxToken.text = list[i].text; auxToken.error = list[i].error; auxToken.lineNumber = list[i].lineNumber; auxToken.category = list[i].category; variablesInt.Add(auxToken); i++; } i++; if(list[i].category != "end"){ error += "Linha: \t" + Convert.ToString(list[i].lineNumber) + "Esperava-se 'END'. \r\n"; i++; } } else if (list[i].category == "boolean") { while (list[i].category == "boolean") { Token auxToken = new Token(); auxToken.text = list[i].text; auxToken.error = list[i].error; auxToken.lineNumber = list[i].lineNumber; auxToken.category = list[i].category; variablesBool.Add(auxToken); i++; } i++; if (list[i].category != "end") { error += "Linha: \t" + Convert.ToString(list[i].lineNumber) + "Esperava-se 'END'. \r\n"; i++; } } else { error += "Linha: \t" + Convert.ToString(list[i].lineNumber) + "Esperava-se um tipo de variável. \r\n"; i++; } } else { //erro error += "Linha: \t" + Convert.ToString(list[i].lineNumber) + "Esperava-se ':'. \r\n"; i++; } } while (list[i].category == "identifier"); } return i; }