inline.inc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. ; vim:ft=fasm:
  2. ; taken from: https://github.com/tgrysztar/fasmg
  3. ; Simple preprocessor for inline macros.
  4. ; Example of use:
  5. ;
  6. ; inlinemacro oddflip(number)
  7. ; return = (number) xor 1
  8. ; end inlinemacro
  9. ;
  10. ; db oddflip(3), oddflip(oddflip(0) shl 1)
  11. include 'xcalm.inc'
  12. define inlinemacro? inlinemacro
  13. calminstruction inlinemacro?! declaration&
  14. local name
  15. match name(arguments?), declaration
  16. jyes define
  17. match name= arguments?, declaration
  18. jyes define
  19. match name arguments?, declaration
  20. define:
  21. arrange tmp, =__inline__.name
  22. arrange name, =inlinemacro.name
  23. publish name, tmp
  24. arrange tmp, =struc (=return?) name arguments
  25. assemble tmp
  26. end calminstruction
  27. macro end?.inlinemacro?!
  28. end struc
  29. end macro
  30. macro inlinemacro?.enable?
  31. calminstruction ?! text&
  32. local head, tail, name, arguments, more, i
  33. init i, 0
  34. match =inlinemacro? more, text
  35. jyes ready
  36. transform text, inlinemacro
  37. jno ready
  38. match =else? more, text
  39. jno preprocess
  40. compute i, i+1
  41. arrange text, =__inline__.(=else =if 1) =__inline__.(=__return__.i==1) text =__inline__.(=end =if) =__inline__.(=if ~=definite =__return__.i)
  42. preprocess:
  43. match head? =__inline__.name?(tail?, text
  44. jno ready
  45. match arguments?) tail?, tail
  46. jno ready ; syntax error
  47. collect:
  48. match arguments?, arguments, ()
  49. jyes inline
  50. match more?) tail?, tail
  51. jno ready ; syntax error
  52. arrange arguments, arguments) more
  53. jump collect
  54. inline:
  55. match , name
  56. jyes special
  57. local tmp, return
  58. compute i, i+1
  59. arrange return, =__return__.i
  60. arrange tmp, return =inlinemacro.name arguments
  61. arrange text, head return tail
  62. take text, tmp
  63. jump preprocess
  64. special:
  65. arrange text, head tail
  66. take text, arguments
  67. jump preprocess
  68. ready:
  69. assemble text
  70. take , text
  71. take text, text
  72. jyes preprocess
  73. end calminstruction
  74. end macro
  75. ;macro include? file*
  76. ; include file, inlinemacro.enable
  77. ; purge ?
  78. ;end macro
  79. inlinemacro.enable