Proposal: RPN as "super macro" for C/C++ code

373 views
Skip to first unread message

Liang Ng

unread,
Dec 27, 2018, 11:50:35 PM12/27/18
to
Proposal: RPN as "super macro" for C/C++ code

RPN (reverse polish notation) is perhaps the only syntax that can be used to simplify C/C++ programs with its macro properties, besides being used to implement a C/C++ interpreter.

RPN macro properties can also be used for two-way translation of code e.g. write RPN macro, then generate C/C++ code.

RPN macros should give better structure for understanding, as well as testing as tests can be made using submodules, and performed interactively.

I will make the RPN macro for C/C++ as close to Forth as possible so that it can be run and tested as Forth programs. Then this can perhaps become a Forth to C/C++ translation standard.

As an analogy, PHP, JavaScript, Java, Python, Ruby etc all use stack machine as interpreters. However, their stack machine opcodes are rarely used by human programmers as a means to edit the programs, as in Forth.

I intend to test if opencl-info.cc from this link can be simplified to a more understandable format, with better test modules and can be executed interactively with a C/C++ interpreter based on stack machine:

https://github.com/supernovaremnant/bazel-android-opencl/blob/master/opencl-info/opencl-info.cc

This is part of my effort to create an interactive development environment based on stack machine to program mobile GPU using OpenCL -- Stack Machine OpenCL (SMOCL):

https://github.com/udexon/5CSM/tree/master/SMOCL

Comments welcome.


References:
https://www.linkedin.com/pulse/multitiered-stack-machine-nsm-using-5gl-fifth-graph-ng-ph-d-/
https://www.facebook.com/groups/483887212122959/ (Project Fire4X -- Forth in Firefox)

Liang Ng

unread,
Dec 28, 2018, 6:14:11 AM12/28/18
to
On Friday, 28 December 2018 12:50:35 UTC+8, Liang Ng wrote:
> Proposal: RPN as "super macro" for C/C++ code
>
> RPN (reverse polish notation) is perhaps the only syntax that can be used to simplify C/C++ programs with its macro properties, besides being used to implement a C/C++ interpreter.

I briefly looked at pycparser -- a bit too complicated for my taste.

I spent an hour or so writing a C parser in PHP. I just made a quick and dirty assumption:

-- Since what I want is a two-way parser generator, I should look for "blocks", marked by "{" and "}".

-- I will use Forth style colon definition to "name" (rename) the blocks -- which will make each block easier to understand and test.

-- Next, I will apply Forth style colon definition for smaller parts within a block, down to an operator.

Not sure if anyone objects this unorthodox approach. Does it sound too simple compare to the formal yacc and lex approach? Maybe that is what you get when you use stack machine?

Pablo Hugo Reda

unread,
Dec 28, 2018, 8:13:15 PM12/28/18
to
in 2005 I had the same idea, which was to replace, but something happened on the way, the macro language exceeds the c in flexibility,

Leave the C and create my language derived from ColorForth.

Liang Ng

unread,
Dec 29, 2018, 9:13:50 PM12/29/18
to
On Saturday, 29 December 2018 09:13:15 UTC+8, Pablo Hugo Reda wrote:
> in 2005 I had the same idea, which was to replace, but something happened on the way, the macro language exceeds the c in flexibility,
>
> Leave the C and create my language derived from ColorForth.

Updates:

https://github.com/udexon/5CSM/blob/master/SMPG/README.md

Stack Machine Parser Generator

This example illustrate how Forth style colon definition can be used "rename" blocks of code of high level programming langauge (e.g. C/C++), thus making it more legible, structured and manageable.

The original post appeared on comp.lang.forth newsgroup.

The following is the output of cparse.php (will be uploaded after clean-up), a C parser written in PHP. It uses one simple rule: look for '{' and '}' as start and end markers, output block label as B_startline_startcol_endline_endcol. Each line starts with the line number, followed by the column number of either '{' or '}'. The number following IN_IF is the nest level of the block.

The input example is taken from https://github.com/supernovaremnant/bazel-android-opencl/blob/master/opencl-info/opencl-info.cc

Even with such a simple example, it shows that the code can quickly grow intelligible, due to fundamental flaws in the design of high level programming languages.

The next step for cparser.php will be to provide the Forth style colon definition to "rename" the block labels B_* -- to give a legible name, to give it more meaning, structure and make in more manageable.

Pablo Hugo Reda

unread,
Dec 30, 2018, 8:06:52 AM12/30/18
to
I'm not understanding well because you translate to c from php.

If you want your forth on the server side, you can make the connection with CGI or something to nginx or apache.

If you want the client side is any forth with access to socket

Anyway, the most interesting thing is when you program in your language, not in the construction of it.

good luck

Pablo Hugo Reda

unread,
Dec 30, 2018, 8:17:07 AM12/30/18
to
I'm not understanding well WHY you translate to c from php.

Liang Ng

unread,
Dec 30, 2018, 1:07:01 PM12/30/18
to
On Sunday, 30 December 2018 21:17:07 UTC+8, Pablo Hugo Reda wrote:
> I'm not understanding well WHY you translate to c from php.

I was not trying to translate to C from PHP. cparse.php is just a parser for C program written in PHP.

NN

unread,
Dec 30, 2018, 3:33:31 PM12/30/18
to
If the two programs are equivalent ( and I havent tried either ) then the
translated version is shorter. Glancing over the output, its also unreadable.
Was that a goal given its machine generated ?

Liang Ng

unread,
Dec 30, 2018, 7:58:39 PM12/30/18
to
I am sorry. The output is a "temporary" file with "block labels", i.e. B_startline_startcol_endline_endcol.

The next step is to "rename" or "colon define" the block labels into meaningful "Forth word" or "block name" -- a feature that does not exist in most high level programming language.

e.g.

: queryDevice B_12_1_15_9 ;
: GPUcount B_18_3_20_4 ;
: GPUfreq B_24_5_27_8;

Then I can rewrite the whole C/C++ program in a Forth like RPN macro:

xxx IF queryDevice THEN
yyy IF GPUcount THEN
zzz IF GPUfreq THEN


After block renaming, we can proceed to do colon definition for each sub-token within a block, down to an atomic token. Forth like colon definition is not just informative, but functional. We can modify the C/C++ code using Forth like code, and generate tests because now every part of the program from an atomic token to block are named using Forth style colon definition -- a feature not available in most high level programming language. I believe this breakthrough is revolutionary.

If you know of similar attempts, please let me know.
Reply all
Reply to author
Forward
0 new messages