Functions.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package templates;
  7. import API.Api;
  8. import API.Utils;
  9. import common.RegistroBase;
  10. import exception.CompileException;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. /**
  14. *
  15. * @author EUGENIO CARVALHO
  16. */
  17. public class Functions {
  18. protected static HashMap<String, FunctionInterface> functions = new HashMap<String, FunctionInterface>() {
  19. {
  20. // Funcao que inseri tabulações no texto.
  21. // O primeiro parametro indica a quantidade de tabulações
  22. // System.out.println("Tabulation:'" + Utils.Repeat("\t", args.get(0)) + "'");
  23. put("T", (FunctionInterface) (ctx, args) -> Utils.Repeat("\t", args.get(0)));
  24. // Função formata label
  25. put("L", (FunctionInterface) (ctx, args) -> " <" + args.get(0) + ">");
  26. // Converte um numero para binario e adiciona pad informado no segundo parametro
  27. put("BIN", (ctx, args) -> Functions.BIN(args));
  28. //
  29. put("HEX", (FunctionInterface) (ctx, args) -> {
  30. String value = args.get(0);
  31. if (!value.equals("")) {
  32. if (!Utils.IsNumber(value)) {
  33. value = ctx.Get(value);
  34. }
  35. value = Utils.FormatNum(value, "hex");
  36. return Utils.Pad(value, args.get(2), "", Integer.parseInt(args.get(1)));
  37. }
  38. return "";
  39. });
  40. put("DEC", (FunctionInterface) (ctx, args) -> {
  41. String value = args.get(0);
  42. if (!value.equals("")) {
  43. return Utils.Pad(value, args.get(2), "", Integer.parseInt(args.get(1)));
  44. }
  45. return "";
  46. });
  47. put("PAD", (FunctionInterface) (ctx, args) -> {
  48. String value = args.get(0);
  49. if (args.size() > 1) {
  50. // System.out.println("PAD:" + args);
  51. value = Utils.Pad(value, args.get(2), "", Integer.parseInt(args.get(1)));
  52. }
  53. return value;
  54. });
  55. }
  56. };
  57. public static void Add(String id, FunctionInterface f) {
  58. functions.put(id, f);
  59. }
  60. public static FunctionInterface Get(String id) throws Exception {
  61. if (!functions.containsKey(id)) {
  62. throw new Exception(String.format("Função de template %s não definida.", id));
  63. }
  64. return functions.get(id);
  65. }
  66. public static String Call(String id, RegistroBase ctx, ArrayList<String> args) throws Exception {
  67. try {
  68. return Get(id).Call(ctx, args);
  69. } catch (Exception e) {
  70. throw new CompileException("Erro na chamada '%s': %s", id, e.getMessage());
  71. }
  72. }
  73. // Utils functions
  74. public static String BIN(ArrayList<String> arguments) {
  75. return BIN(arguments.get(0), arguments.get(1));
  76. }
  77. public static String BIN(String val, String pad) {
  78. return BIN(val, Integer.parseInt(pad));
  79. }
  80. public static String BIN(String val, int pad) {
  81. return Api.num2bin(val, pad);
  82. }
  83. }