0

I want to know whether I can make a normal text to have a functionality like a reserved word. For example : "and" is a reserved keyword in python and what I want to do is, give the same power as of "and" to another thing, let's say "abc".

So

w, i = 0,0
if w == 0 and i == 0:
   print("True")
w, i = 0,0
if w == 0 abc i == 0:
   print("True")

will be the same. Anyone have any idea if this is possible and how can I do that.

Thank You.

Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
  • Not without forking the interpreter itself and going to add your operator over there, no. [Some operations and operators already have Python hooks](https://docs.python.org/3/reference/datamodel.html#special-method-names), but usually you'd just write a function. – Masklinn Mar 23 '21 at 13:42
  • Hi, just by curiosity why do you need such a functionnality ? – LCMa Mar 23 '21 at 13:42
  • 1
    There is no python equivalent for c++ macros AFAIK. – Kevin Mar 23 '21 at 13:46
  • @LCMa, Just to know if it is possible or not – Discormoli Mar 23 '21 at 13:53
  • @Masklinn , I found my answer in a library called SLY, it's library that help to make Lexer and Parser, how ever by using it I can implement new syntax and new keywords that works similar to python or any other language, we can add them to whatever program we need not only when making a new language. – Discormoli Mar 24 '21 at 06:25

1 Answers1

0

This is going to involve either a preprocessor that converts from your weirdo syntax to legal Python, or editing and rebuilding the Python interpreter from scratch with support for your new keyword. And odds are, it'll break something, e.g. in this case abc is already a module that ships with Python, and making import abc parse as import and is going to cause major problems (IIRC, the core interpreter effectively depends on the abc module as ABCs are used all over the place).

So the real answer to your question is:

  1. Don't do this
  2. If you do, it's a project way beyond the scope of StackOverflow (we're not going to rewrite the interpreter for you)
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • hehe, I also think so, but ask just for curiosity Thanks for answering – Discormoli Mar 23 '21 at 13:52
  • 3. See bullet #1 – sizzzzlerz Mar 23 '21 at 14:29
  • I found my answer in a library called SLY, it's library that help to make Lexer and Parser, how ever by using it I can implement new syntax and new keywords that works similar to python or any other language, we can add them to whatever program we need not only when making a new language. – Discormoli Mar 24 '21 at 06:24