Advertisement
rplantiko

Internal DSL example

Oct 28th, 2012
1,102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 3.86 KB | None | 0 0
  1. program  zz_computer_config.
  2.  
  3. * PC configuration: An internal DSL example in ABAP
  4. * Using macros, structured constants, and name composition,
  5. * we can make the following code pass as valid ABAP:
  6. *
  7. *    processor :
  8. *      type i386,
  9. *      cores 2.
  10. *
  11. *    disk 1 :
  12. *      size    2000,
  13. *      speed   750,
  14. *      type    sata.
  15. *
  16. *    disk 2 :
  17. *      type    sas.
  18.  
  19.  
  20. start-of-selection.
  21.   perform start.
  22.  
  23. *
  24. class lcl_computer definition.
  25.   public section.
  26.     types:
  27.       begin of ty_disk,
  28.         name    type string,
  29.         size    type i,
  30.         speed   type i,
  31.         type    type string,
  32.       end of ty_disk,
  33.       ty_disk_tab type hashed table of ty_disk with unique key name,
  34.       begin of ty_processor,
  35.         type    type string,
  36.         cores   type i,
  37.       end of ty_processor.
  38.     data:
  39.       gs_processor type ty_processor,
  40.       gt_disks     type ty_disk_tab.
  41.     methods:
  42.       get_disk importing iv_name type any
  43.                returning value(es_disk) type ref to ty_disk,
  44.       get_default_disk returning value(es_disk) type ty_disk,
  45.       dump.
  46. endclass.                    "lcl_computer DEFINITION
  47.  
  48. *
  49. form start.
  50.  
  51.   data: lo_computer type ref to lcl_computer.
  52.   perform get_computer changing lo_computer.
  53.   lo_computer->dump( ).
  54.  
  55. endform.
  56.  
  57.  
  58.  
  59.  
  60. constants:
  61.   begin of gc_processor_type,
  62.     i386     type string value 'i386',
  63.     i686     type string value 'i686',
  64.     ppc      type string value 'PPC',
  65.   end of gc_processor_type,
  66.   begin of gc_disk_type,
  67.     sata         type string value 'SATA',
  68.     sas           type string value 'SAS',
  69.     scsi          type string value 'SCSI',
  70.   end of gc_disk_type.
  71.  
  72. define processor.
  73.   processor_&1 &2.
  74. end-of-definition.
  75.  
  76. define processor_type.
  77.   eo_computer->gs_processor-type = gc_processor_type-&1.
  78. end-of-definition.
  79.  
  80. define processor_cores.
  81.   eo_computer->gs_processor-cores = &1.
  82. end-of-definition.
  83.  
  84. define disk.
  85.   if ls_disk is not bound or ls_disk->name ne &1.
  86.     ls_disk = eo_computer->get_disk( &1 ).
  87.   endif.
  88.   disk_&2 &3.
  89. end-of-definition.
  90.  
  91. define disk_type.
  92.   ls_disk->type = gc_disk_type-&1.
  93. end-of-definition.
  94.  
  95. define disk_size.
  96.   ls_disk->size = &1.
  97. end-of-definition.
  98.  
  99. define disk_speed.
  100.   ls_disk->speed = &1.
  101. end-of-definition.
  102.  
  103. form get_computer changing eo_computer type ref to lcl_computer.
  104.  
  105.   data: ls_disk     type ref to lcl_computer=>ty_disk.
  106.  
  107.   create object eo_computer.
  108.  
  109. * <<< BEGIN DSL
  110. processor :
  111.   type i386,
  112.   cores 2.
  113.  
  114. disk 1 :
  115.   size    2000,
  116.   speed   750,
  117.   type    sata.
  118.  
  119. disk 2 :
  120.   type    sas.
  121. * <<< END DSL
  122.  
  123. endform.
  124.  
  125.  
  126. *
  127. class lcl_computer implementation.
  128.   method get_disk.
  129.     data: ls_disk type ty_disk.
  130.     read table gt_disks reference into es_disk
  131.       with table key name = iv_name.
  132.     if sy-subrc ne 0.
  133.       ls_disk = get_default_disk( ).
  134.       ls_disk-name = iv_name.
  135.       insert ls_disk
  136.         into table gt_disks
  137.         reference into es_disk.
  138.     endif.
  139.   endmethod.                    "get_disk
  140.   method get_default_disk.
  141.     es_disk-speed = 3600.
  142.     es_disk-size  = 300.
  143.     es_disk-type  = gc_disk_type-sata.
  144.   endmethod.
  145.   method dump.
  146.     data: ls_disk type ref to ty_disk.
  147.     format color col_heading.
  148.     write: /1(80) 'Processor'.
  149.     format color col_normal.
  150.     write: /1(80) 'Type:', 10(70) gs_processor-type.
  151.     write: /1(80) 'Cores:', 10(70) gs_processor-cores left-justified.
  152.     skip.
  153.     loop at gt_disks reference into ls_disk.
  154.       format color col_heading.
  155.       write: /1(5) 'Disk', 6(75) ls_disk->name.
  156.       format color col_normal.
  157.       write: /1(10) 'Size',    10(71) ls_disk->size  left-justified.
  158.       write: /1(10) 'Speed',   10(71) ls_disk->speed left-justified.
  159.       write: /1(10) 'Type',    10(71) ls_disk->type  left-justified.
  160.       skip.
  161.     endloop.
  162.  
  163.   endmethod.
  164. endclass.                    "lcl_computer IMPLEMENTATION
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement