Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
joltdx
Active Contributor
The year was 2020 and it was the first year of the pandemic. In december, though, I had the most fun I ever had with ABAP as I participated in the annual Advent of Code event. It's like an advent calendar with a two part programming challenge/puzzle/exercise each day from December 1st to December 25th. The puzzles are not programming language specific but can be solved in any language. Some people join in like a competition for speed solving. Some to learn a new programming language or increase their knowledge in one. Some have done each day in a new language. Others just join for the sheer fun and awesomeness of it. 😊

The puzzles are released each specific day, but after that they are available indefinitely, so they can be accessed at any time.

 

Advent of Code 2020 in ABAP


I decided earlier that I should try it out in ABAP this year, mainly for the fun of it since it was a long time since I did anything similar, and also to find new aspects and uses of the ABAP language as well as to improve my current skills in general. The puzzles are not like what my customers and clients typically want or need implemented in ABAP.

I saw a blog post by lars.hvam where he shared a template with empty ABAP classes to use for solving the puzzles running ABAP on Node.js. Hah. Well. Challenge accepted and now over a month have passed and I don't regret a single thing. And I learned a lot while having so much fun.

 

Node.js, no server? Wait, what?


Yes, so what was actually happening was that I wrote ABAP code in local files on my laptop using Visual Studio Code. Using the abaplint plugin I had ABAP syntax highlighting, and indication of syntax and programming errors, as we would expect in ADT or SE80. And obviously my laptop is not a SAP NetWeaver stack or SCP running 'Steampunk' so in order to run the code I wrote, three things needed to happen behind the scenes.

To demonstrate, here is an example from day 23 where I implemented a linked list in ABAP as part of the solution. One method new_cup looked like this:


Modern ABAP syntax


In order to make this runnable I issue a npm command to Node which will take care of some automated steps. First step is to downport this somewhat modern syntax to a 702 version, getting rid of the inline declaration, the NEW constructor operator and the VALUE operator. The result is written to a new file:


ABAP downported to v702


Next up, the downported code is run through a transpiler, generating JavaScript code supported by an ABAP runtime component written in JavaScript/TypeScript. Yet again a new file I have the possibility to view if I choose to:


Transpiled ABAP code


And this is the code that will finally run on my machine to get my puzzle outputs for my specific input. How awesome is that? 😎

 

Wait. But why?


Actually, is it really that awesome? Typically as ABAPers we work in an SAP system running ABAP just fine. Why would we need ABAP to run outside of SAP, as it is for SAP systems we write our code? So this is totally meaningless for us, right?

Well, yes, but actually no. 😊

Some of the use cases for the ABAP linter include the possibility for extended custom checks in the Code Inspector and ABAP Test Cockpit as well as syntax highlighting and checks for Visual Studio Code and SAP Business Application Studio.

The abaplint transpiler obviously relies heavily on the linter and the uses of it can include removing the need for an ABAP system to perform automatic unit testing of open source ABAP repositories on GitHub. This means that when you find ABAP open source you want to use, you could verify the unit tests that is a minimum requirement for it and be sure that any future upgrade won't break those, if they are run automatically on pull requests. This would be possible on request, without needing to launch an entire SAP system, or have one standing by, to run these unit tests.

It would also be a means of being able to develop and test software without needing an SAP server at that time. This would be limited, of course, to cases where no parts of existing SAP modules and processes are needed.

Also, having the option of testing the syntax, getting a better understanding of the language, have easy access to learning and experimenting with ABAP for new developers as well as old, getting fresh blood into the ABAP sphere. I see all those things as positive!

Maybe somebody totally new to ABAP decides to try solving one of the AoC puzzles in ABAP next year?

 

How competent is the transpiler?


The abaplint transpiler is not yet able to do everything that ABAP on a SAP system can. It is a work in progress but I would say that the base and foundation is really competent and stable. There were some features or aspects that were not in place when I needed them, but given the current implementation level, they were not far away and could quickly be implemented when I reported an issue.

One example was that the bit operators BIT-AND, BIT-OR and BIT-XOR was not yet implemented in the ABAP runtime. I don't think I ever used them in ABAP before but I needed them now. 😀 So, again, challenge accepted, and I managed to implement those, learning a lot in the process in many areas, and now they're there...

Other bumps in the road for me was that string offset and length did not yet work on field-symbols, and there were also a situation for me with using some specific conditions in a LOOP. All of that was simply not yet implemented at that time, but it is now along with other improvements and fixes.

I did have to resort to my 'Steampunk' trial to run one of the solutions, though. (Spoiler in this paragraph if you intend do do the puzzles). Day 15 was a numbers game where it was needed to keep track of previously spoken numbers and the last two turns in the game where they were spoken. Fine for part 1 where the game went on for 2020 rounds, but as part 2 was running a stunning 30 million rounds, that one ran really really slow since there is not yet support for a hashed table type in the runtime. The solution ran, but I cancelled it the next morning after letting it run throughout the night. Instead I copy/pasted my code to my Steampunk trial and ran it there, with a hashed table, to get the result in a reasonable time.

Basically I'm super impressed by the downporter and transpiler in abaplint!

 

My solutions to the puzzles


I did not compete for time and I did not even complete all of the puzzles on the same day they were released. (Which by no means is a requirement anyway). But by December 25th I had actually completed them all and collected all of my 50 stars.  And I felt the most surprising fulfillment which is why I want to share this with you.

If you're interested you can find all my solutions in my GitHub repository. They are not optimized for speed, cleanliness, maintainability, readability, or anything other than pure fun and accomplishing the objective of solving the puzzles. In some of the earlier solutions I have some strange workaround where I needed something that was not yet implemented, and I think I will go back some day to revisit them and update according to the progress of the transpiler.

I know others did AoC in ABAP this year, and also previous years. If you did Advent of Code in full or in parts, in ABAP or any other way, please, if you are willing, share a link to your solution repository in the comments if possible. It would be interesting to compare solutions.

 

In conclusion


In conclusion, I'm so happy I decided to take the time in december to do the Advent of Code 2020. And I'm so happy i decided to do it in Node.js for that extra challenge.

AoC 2020 was basically completed in ABAP, running on a non-ABAP system (well 49/50 ran to the end)

I'm also stoked on contributing to open source, so I'll definitely continue doing that.

Before the next AoC event, I will try to push for this ahead of time in case you want to join in the fun.

 

Try the abaplint transpiler online and live


Try it yourselfy by transpiling ABAP live in your browser at transpiler.abaplint.org. Note that this does not include the downporting so please write v702 ABAP...

You could try pasting this code in the left ABAP-pane:
DATA str TYPE string VALUE 'Remote tsewaba pies'.
DATA tab TYPE STANDARD TABLE OF string.
DATA tmp1 TYPE string.
DATA tmp2 TYPE string.
FIELD-SYMBOLS <fs1> TYPE string.
tmp1 = to_upper( str+11(5) ).
CONDENSE tmp1 NO-GAPS.
WRITE / tmp1.
FIND REGEX 'p(i)e(s)' IN str SUBMATCHES tmp1 tmp2.
WRITE | { tmp1 }{ tmp2 } |.
SPLIT str AT | | INTO TABLE tab.
LOOP AT tab INTO tmp1 FROM 2 TO 2.
WRITE reverse( tmp1+1(4) ).
ENDLOOP.
READ TABLE tab INDEX 1 ASSIGNING <fs1>.
IF sy-subrc = 0.
WRITE reverse( '!' && <fs1>+1(3) ).
ENDIF.

 

Resources


Advent of Code

Abaplint transpiler playground

abaplint extension for Visual Studio Code

abaplint extension for SAP Business Application Studio

Other abaplint tools

Open Source ABAP projects
12 Comments