nextem.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #ifndef NORCSID
  2. static char rcsid[] = "$Id$";
  3. #endif
  4. #include <em_spec.h>
  5. #include <em_flag.h>
  6. #include "assert.h"
  7. #include "param.h"
  8. #include "tables.h"
  9. #include "types.h"
  10. #include <cg_pattern.h>
  11. #include "data.h"
  12. #include "result.h"
  13. #include "extern.h"
  14. /*
  15. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  16. * See the copyright notice in the ACK home directory, in the file "Copyright".
  17. *
  18. * Author: Hans van Staveren
  19. */
  20. #ifndef NDEBUG
  21. #include <stdio.h>
  22. extern char em_mnem[][4];
  23. #endif
  24. byte *trypat(bp,len) register byte *bp; {
  25. register patlen,i;
  26. result_t result;
  27. getint(patlen,bp);
  28. if (len == 3) {
  29. if (patlen < 3)
  30. return(0);
  31. } else {
  32. if (patlen != len)
  33. return(0);
  34. }
  35. for(i=0;i<patlen;i++)
  36. if (emp[i].em_instr != (*bp++&BMASK))
  37. return(0);
  38. for (i=0;i<patlen;i++)
  39. if (emp[i].em_optyp==OPNO)
  40. dollar[i].e_typ=EV_UNDEF;
  41. else if ((dollar[i].e_typ=argtyp(emp[i].em_instr))==EV_INT)
  42. dollar[i].e_v.e_con=emp[i].em_u.em_ioper;
  43. else
  44. dollar[i].e_v.e_str=emp[i].em_soper;
  45. getint(i,bp);
  46. if (i!=0) {
  47. struct emline *svp = saveemp;
  48. saveemp = emp;
  49. result = compute(&enodes[i]);
  50. if (result.e_typ != EV_INT || result.e_v.e_con == 0) {
  51. saveemp = svp;
  52. return(0);
  53. }
  54. }
  55. #ifndef NDEBUG
  56. if (Debug) {
  57. fprintf(stderr,"Matched:");
  58. for (i=0;i<patlen;i++)
  59. fprintf(stderr," %3.3s",em_mnem[emp[i].em_instr-sp_fmnem]);
  60. fprintf(stderr,"\n");
  61. }
  62. #endif
  63. saveemp = emp;
  64. emp += patlen;
  65. return(bp);
  66. }
  67. extern char em_flag[];
  68. argtyp(mn) {
  69. switch(em_flag[mn-sp_fmnem]&EM_PAR) {
  70. case PAR_W:
  71. case PAR_S:
  72. case PAR_Z:
  73. case PAR_O:
  74. case PAR_N:
  75. case PAR_L:
  76. case PAR_F:
  77. case PAR_R:
  78. case PAR_C:
  79. return(EV_INT);
  80. default:
  81. return(EV_STR);
  82. }
  83. }
  84. byte *nextem(toplevel) {
  85. register i;
  86. short hash[3];
  87. register byte *bp;
  88. byte *cp;
  89. int index;
  90. register struct emline *ep;
  91. if (toplevel) {
  92. if (nemlines && emp>emlines) {
  93. nemlines -= emp-emlines;
  94. for (i=0,ep=emlines;i<nemlines;i++)
  95. *ep++ = *emp++;
  96. emp=emlines;
  97. }
  98. fillemlines();
  99. }
  100. hash[0] = emp[0].em_instr;
  101. hash[1] = (hash[0]<<4) ^ emp[1].em_instr;
  102. hash[2] = (hash[1]<<4) ^ emp[2].em_instr;
  103. for (i=2;i>=0;i--) {
  104. index = pathash[hash[i]&BMASK];
  105. while (index != 0) {
  106. bp = &pattern[index];
  107. if ( bp[PO_HASH] == (hash[i]>>8))
  108. if ((cp=trypat(&bp[PO_MATCH],i+1)) != 0)
  109. return(cp);
  110. index = (bp[PO_NEXT]&BMASK) | (bp[PO_NEXT+1]<<8);
  111. }
  112. }
  113. return(0);
  114. }