Functions.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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("PAD", (FunctionInterface) (ctx, args) -> {
  41. String value = args.get(0);
  42. if (args.size() > 1) {
  43. // System.out.println("PAD:" + args);
  44. value = Utils.Pad(value, args.get(2), "", Integer.parseInt(args.get(1)));
  45. }
  46. return value;
  47. });
  48. }
  49. };
  50. public static void Add(String id, FunctionInterface f) {
  51. functions.put(id, f);
  52. }
  53. public static FunctionInterface Get(String id) throws Exception {
  54. if (!functions.containsKey(id)) {
  55. throw new Exception(String.format("Função de template %s não definida.", id));
  56. }
  57. return functions.get(id);
  58. }
  59. public static String Call(String id, RegistroBase ctx, ArrayList<String> args) throws Exception {
  60. try {
  61. return Get(id).Call(ctx, args);
  62. } catch (Exception e) {
  63. throw new CompileException("Erro na chamada '%s': %s", id, e.getMessage());
  64. }
  65. }
  66. // Utils functions
  67. public static String BIN(ArrayList<String> arguments) {
  68. return BIN(arguments.get(0), arguments.get(1));
  69. }
  70. public static String BIN(String val, String pad) {
  71. return BIN(val, Integer.parseInt(pad));
  72. }
  73. public static String BIN(String val, int pad) {
  74. return Api.num2bin(val, pad);
  75. }
  76. }