123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|681|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> Misc Languages -> RPN Enbafflement

Tue, 06 Apr 2021, 06:45
Jayenkai

RPN Enbafflement


Wrapping myself up into knots, trying to figure this one out.

Anyone want to give it a try!?!

The Challenge


Rules
1. Must take BODMAS into account.
2. Must ignore strings, and parse those at the correct time.
3. Should wrap everything into brackets, because apparently my script engine needs brackets around any maths!!
4. Must not cause my hair to fall out.

Have at, code army!!

-=-=-
''Load, Next List!''
Tue, 06 Apr 2021, 07:32
cyangames
Ohh, is this for your scripting engine?
Tue, 06 Apr 2021, 09:05
Jayenkai
Yeah, it was something I left until later, 'cos I thought it'd be a case of "Meh, just swapping bits about... that's EASY!!"

.. D'oh!

-=-=-
''Load, Next List!''
Sun, 11 Apr 2021, 18:20
shroom_monk
I've taken a stab at this, but either I'm missing something or I think I've identified a few issues with the problem as stated.

Firstly, to be truly RPN I think that all functions need to be treated like operators. So

should be

for example, rather than


Secondly, the meaning of the + operator is context dependent in your example: it means both addition of numbers and concatenation of strings. On its own this isn't a problem if you only care about one type or the other, but because you can mix and match types, you can get problems with reordering. For example:

wants to be interpreted something like:

But because - is technically lower precedence in BODMAS than +, then a RPN parser will want to do the subtraction in the above calculation last, as it can't tell the difference between addition + and concatenation +. Solving this requires the context of each + to be figured out ahead of time, which is painful.

As a final note, I think true RPN doesn't require brackets at all? In theory you can add them back in afterwards, but the fact that functions (e.g. Sin) still require brackets in your notation makes this trickier.

So I think that might be why you've got yourself tangled in knots!

Anyway, here's my attempted solution to the problem, in Python. There are two steps. First we 'tokenise' the input, i.e. split it up into an array of strings where each string is a meaningful 'token', which we label. Secondly we convert these tokens into RPN using the Shunting-yard Algorithm, except modified to account for aforementioned quirks. Unfortunately it doesn't quite work on your sample input due to the issue with operator precedence I mentioned! And I didn't do the bracketing thing because of the trickiness with functions not being in reverse.



Maybe you can adapt the above into something useful! Addressing the issues noted might make things easier or there might be some other way around it (or maybe I'm totally wrong, it is 1am after all).

As an aside, I vaguely recall that stuff like Context Free Grammars get called out as good ways to parse programming languages, but I've forgotten too many of the important details to be able to blindly recommend those. Could be worth researching if you're interested though.

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Sun, 11 Apr 2021, 18:31
Jayenkai
I'll have a try in the morning, thanks Shroomy!

FWIW, the command is most definitely coming at the end. What I'm doing is parsing a line, popping elements into a mini-script, and then running through that.
Print 1+2+3
becomes
1 - n1
2 - n2
3 - m+ (adds 1 and 2, puts into 1)
4 - n3
5 - m+ (adds 1 and 4, puts into 1)
6 - Print (value at 1)

It's incredibly convoluted, and the code is becoming completely unmanageable.
I'll have a read through your post and see if it clicks anything important.

Thanks again.
*distant ascii hugs*

-=-=-
''Load, Next List!''
Mon, 12 Apr 2021, 05:06
Pakz
I have this book called "language implementation patterns" It is a book on how to write your own various compilers/parsers. I only read a bit and need to really sit down and read it when I want to do something like that. I was thinking of creating some kind of scratch interface for a minecraft game and/or civ game.

From one thing in there I noticed the author took the lines of code apart and placed them into a stack list and handled the logic from the stack. It is still way out of my understanding though.
Mon, 12 Apr 2021, 07:04
Jayenkai
Got it to this stage..



Nnngh.
You're definitely right about those bracketed command'y bits!
I'll study more!!

-=-=-
''Load, Next List!''
Mon, 12 Apr 2021, 10:43
Jayenkai
Almost got things working, but a few stragglers are clinging on..

Print 5=<5
isn't working, because the parser is currently splitting the = and the <.. Should be an easy enough fix... I hope!

Rect 32,32,100-32,100-32
isn't working, because the maths in each parameter is still trying to access "value 0", which is the first parameter.
I need to rejig my parameter stuff, and hope I can figure it out.

Rect 32,32,Pyth(90,100-10),Pyth(80,70-5)
makes things infinitely worse, with parameter-based maths and function calls and more, all confusing the hell out of the poor script!!






-=-=-
''Load, Next List!''