Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program zz_computer_config.
- * PC configuration: An internal DSL example in ABAP
- * Using macros, structured constants, and name composition,
- * we can make the following code pass as valid ABAP:
- *
- * processor :
- * type i386,
- * cores 2.
- *
- * disk 1 :
- * size 2000,
- * speed 750,
- * type sata.
- *
- * disk 2 :
- * type sas.
- start-of-selection.
- perform start.
- *
- class lcl_computer definition.
- public section.
- types:
- begin of ty_disk,
- name type string,
- size type i,
- speed type i,
- type type string,
- end of ty_disk,
- ty_disk_tab type hashed table of ty_disk with unique key name,
- begin of ty_processor,
- type type string,
- cores type i,
- end of ty_processor.
- data:
- gs_processor type ty_processor,
- gt_disks type ty_disk_tab.
- methods:
- get_disk importing iv_name type any
- returning value(es_disk) type ref to ty_disk,
- get_default_disk returning value(es_disk) type ty_disk,
- dump.
- endclass. "lcl_computer DEFINITION
- *
- form start.
- data: lo_computer type ref to lcl_computer.
- perform get_computer changing lo_computer.
- lo_computer->dump( ).
- endform.
- constants:
- begin of gc_processor_type,
- i386 type string value 'i386',
- i686 type string value 'i686',
- ppc type string value 'PPC',
- end of gc_processor_type,
- begin of gc_disk_type,
- sata type string value 'SATA',
- sas type string value 'SAS',
- scsi type string value 'SCSI',
- end of gc_disk_type.
- define processor.
- processor_&1 &2.
- end-of-definition.
- define processor_type.
- eo_computer->gs_processor-type = gc_processor_type-&1.
- end-of-definition.
- define processor_cores.
- eo_computer->gs_processor-cores = &1.
- end-of-definition.
- define disk.
- if ls_disk is not bound or ls_disk->name ne &1.
- ls_disk = eo_computer->get_disk( &1 ).
- endif.
- disk_&2 &3.
- end-of-definition.
- define disk_type.
- ls_disk->type = gc_disk_type-&1.
- end-of-definition.
- define disk_size.
- ls_disk->size = &1.
- end-of-definition.
- define disk_speed.
- ls_disk->speed = &1.
- end-of-definition.
- form get_computer changing eo_computer type ref to lcl_computer.
- data: ls_disk type ref to lcl_computer=>ty_disk.
- create object eo_computer.
- * <<< BEGIN DSL
- processor :
- type i386,
- cores 2.
- disk 1 :
- size 2000,
- speed 750,
- type sata.
- disk 2 :
- type sas.
- * <<< END DSL
- endform.
- *
- class lcl_computer implementation.
- method get_disk.
- data: ls_disk type ty_disk.
- read table gt_disks reference into es_disk
- with table key name = iv_name.
- if sy-subrc ne 0.
- ls_disk = get_default_disk( ).
- ls_disk-name = iv_name.
- insert ls_disk
- into table gt_disks
- reference into es_disk.
- endif.
- endmethod. "get_disk
- method get_default_disk.
- es_disk-speed = 3600.
- es_disk-size = 300.
- es_disk-type = gc_disk_type-sata.
- endmethod.
- method dump.
- data: ls_disk type ref to ty_disk.
- format color col_heading.
- write: /1(80) 'Processor'.
- format color col_normal.
- write: /1(80) 'Type:', 10(70) gs_processor-type.
- write: /1(80) 'Cores:', 10(70) gs_processor-cores left-justified.
- skip.
- loop at gt_disks reference into ls_disk.
- format color col_heading.
- write: /1(5) 'Disk', 6(75) ls_disk->name.
- format color col_normal.
- write: /1(10) 'Size', 10(71) ls_disk->size left-justified.
- write: /1(10) 'Speed', 10(71) ls_disk->speed left-justified.
- write: /1(10) 'Type', 10(71) ls_disk->type left-justified.
- skip.
- endloop.
- endmethod.
- endclass. "lcl_computer IMPLEMENTATION
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement