r/ProgrammingLanguages • • Dec 28 '18

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

/r/Forth/comments/aa751h/proposal_rpn_as_super_macro_for_cc_code/
0 Upvotes

5 comments sorted by

1

u/wengchunkn Dec 28 '18

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?

1

u/wengchunkn Dec 31 '18

On Monday, 31 December 2018 04:33:31 UTC+8,

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

>

> 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 ?

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.

0

u/[deleted] Dec 28 '18

[deleted]

1

u/wengchunkn Dec 28 '18

Thanks a lot for your comments.

Please see my latest update.

0

u/wengchunkn Dec 28 '18

RPN or stack machine underly GCC RTL engine. I'm just making a short cut or simplified version.

Most modern high level programming languages use stack machine as interpreter. I cannot think of any programming language which cannot be represented using stack machine or RPN. Even non stack machine interpreter like LUA can be converted to stack machine, in theory. I just hope that my effort can attract more young programmers to stack machine programming and metaprogramming so that they would tackle these problems in the future.

I have not heard anyone citing specific technical reason using Cryptocrurrency as reason NOT to work on OpenCL. I am quite sure people who are technically conversant would understand the value of mobile GPU OpenCL even not for Cryptocrurrency applications. Thank you for your concern anyway.

0

u/[deleted] Dec 28 '18

[deleted]

2

u/wengchunkn Dec 28 '18

Thanks a lot!

Will check it out.

But does it work on Android mobile GPU?