IvannosysVisitor.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package frontend.Ivannosys;
  2. import API.Imports;
  3. import common.Instruction;
  4. import common.Log;
  5. import compiler.IvannosysCompiler;
  6. import grammar.IvannosysGrammarBaseVisitor;
  7. import grammar.IvannosysGrammarParser;
  8. import grammar.IvannosysGrammarVisitor;
  9. import java.util.LinkedList;
  10. import org.antlr.v4.runtime.tree.ParseTree;
  11. /**
  12. *
  13. * @author Eugenio ThiS claSS provideS an empty implementation of
  14. * {@link IvannosysGrammarVisitor}, which can be extended to create a viSitor
  15. * which only needS to handle a SubS of the available methodS.
  16. *
  17. * operationS with no return type.
  18. */
  19. public class IvannosysVisitor extends IvannosysGrammarBaseVisitor<String> {
  20. protected IvannosysCompiler compiler;
  21. protected LinkedList<String> importSequence = new LinkedList<>();
  22. protected String extensionLibrary = "go";
  23. protected Log errors;
  24. public IvannosysVisitor(ParseTree tree, String[] tokens, IvannosysCompiler comp) throws Exception {
  25. super();
  26. errors = new Log();
  27. compiler = comp;
  28. System.out.println("IvannosysVisitor");
  29. this.visit(tree);
  30. }
  31. @Override
  32. public String visitImport_part(IvannosysGrammarParser.Import_partContext ctx) {
  33. String alias = "";
  34. if (ctx.ID() != null) {
  35. alias = ctx.ID().getText();
  36. }
  37. try {
  38. String path = ctx.T_STRING().getText().replace("\"", "");
  39. // Verificar importacao ciclica
  40. if (importSequence.contains(path)) {
  41. return "";
  42. }
  43. importSequence.add(path);
  44. Imports.Add(path, alias);
  45. // System.out.println("Visitor import " + alias + " >> " + path);
  46. // compiler.LoadPackage(alias, path.replace("\"", ""));
  47. // throw new Exception(String.format("O pacote '%Set' não foi encontrado no path.", path));
  48. } catch (Exception ex) {
  49. errors.AddError(
  50. (Instruction) new Instruction()
  51. .Set("class", "Import")
  52. .Set("msg", ex.getMessage())
  53. .Set("line", ctx.getStart().getLine())
  54. .Set("col", ctx.getStart().getCharPositionInLine()));
  55. } finally {
  56. return super.visitImport_part(ctx); //To change body of generated methods, choose Tools | Templates.
  57. }
  58. }
  59. public Log Error() {
  60. return errors;
  61. }
  62. }
  63. // for (String token : tokens) {
  64. // if (!token.startsWith("'")) {
  65. // continue;
  66. // }
  67. // ts.addPalavraReservada(token);
  68. // }