Hi
Here is a script I want to be able to write (explanation appears after):
*** start of script ***
import MyCustomMacroLi b # This does the magic I would like help for.
# This is not python, but the module imported above
# will use this block internally and prevent it
# getting to the interpreter.
defmacro MyMacro1:
form = """for <#1> := <#2> to <#3> do begin
<#4multiline>
end"""
subsituteWith = """for <#1> in range(<#2>,<#3> +1):
<#4multiline>"" "
for i := 0 to 3 do begin # This is not python syntax, but the module
print i # imported above makes it so that this
print 'hello' # is first checked against defined macros
end # and substitutions are made if syntax
# matches
""" Should give:
0
hello
1
hello
2
hello
3
hello
"""
*** end of script ***
I want the module "MyCustomMacroL ib" to set up something where I can have
write code that checks each of the statements of this script, and does
macro expansion before passing the result to the python interpreter.
When the (undefined in python) "defmacro" statement is encountered, the
module sets this up in memory as some kind of "macro" object, and as the
remainder of the lines is parsed, a syntax check is made to see if a macro
fits, and if so the expansion is performed and the result is passed to the
interpreter/compiler.
This particular (useless) example sets up a macro where loops can be
written in objectpascal form. I actually have no particular need for this
kind of functionality (I think?), but it looks cool!. I got interested
after reading a couple of Paul Grahams essays about lisp. I also suspect
there actually may be some real problem domains where extending the
language syntax may be beneficial - I just cannot think of any right now :)
I can figure out the minor practical details of things like handling
indention, code objects and the like myself, but I don't know where to
start to try and intercept statements before they get to the interpreter.
Some of the docs I read today appear to indicate that there may also be
differences in the way interactive interpreter and command-line execution
deal with statements. I am lost in the python internals!
Note that I could easily write a script that takes another script as an
argument, preprocesses it, and evaluates the resulting list of python
statments. I do however, prefer a drop-in module as shown above. Plus, I
would also like to do this dynamically in the interpreter.
Any ideas, or is this impossible? (gratuitous guru-bait)
Thanks
Caleb 3 2070
You could try to override __import__ to first parse the source for macro
definitions and do the expansions. Usage would then be:
import DoMagicWithImpo rtModule
import ModuleWithMacro s
(Your code wouldn't work, because python throws a SyntaxError long
before it even tries to execute 'import MyCustomMacroLi b')
The code at http://docs.python.org/lib/examples-imp.html could be
extended to first read the contents of the module (after find_module)
and passes the expanded source (e.g. as a StringIO) to load_module.
This has of course the problem (that many implementations of macro
expansion share) that line numbers in exception dumps have not much
relation to lines of the unexpanded code...
Benjamin Niemann wrote: You could try to override __import__ to first parse the source for macro definitions and do the expansions. Usage would then be:
import DoMagicWithImpo rtModule import ModuleWithMacro s
(Your code wouldn't work, because python throws a SyntaxError long before it even tries to execute 'import MyCustomMacroLi b') The code at http://docs.python.org/lib/examples-imp.html could be extended to first read the contents of the module (after find_module) and passes the expanded source (e.g. as a StringIO) to load_module.
This has of course the problem (that many implementations of macro expansion share) that line numbers in exception dumps have not much relation to lines of the unexpanded code...
If this actually works, a nice application could be rapid prototyping of
syntax extensions to python ('import this module and you can test and
see why my decorator syntax is better than yours' ;)
On Wed, 25 Aug 2004 00:07:22 +0200, Benjamin Niemann <pi**@odahoda.d e>
wrote: ... If this actually works, a nice application could be rapid prototyping of syntax extensions to python ('import this module and you can test and see why my decorator syntax is better than yours' ;)
Well spotted! I didn't think of that. The ongoing decorator thread could
have had some real-world testing being done. Also, the kind of macro
expansion I want to play around could avoid the need for decorators to be
added to the language in the first place... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: mike420 |
last post by:
I think everyone who used Python will agree that its syntax is
the best thing going for it. It is very readable and easy
for everyone to learn. But, Python does not a have very good
macro capabilities, unfortunately. I'd like to know if it may
be possible to add a powerful macro system to Python, while
keeping its amazing syntax, and if it could be possible to
add Pythonistic syntax to Lisp or Scheme, while keeping all
of the...
|
by: Karim Thapa |
last post by:
Why following macro does not work?
#define removebrace(x) x
void Foo(int a, int b, char *txt, int d, int e);
main()
{
|
by: Simon Morgan |
last post by:
Hi,
Can somebody please help me grok the offsetof() macro?
I've found an explanation on
http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031
but I'm afraid it still doesn't make sense to me.
The sticking point seems to be:
|
by: Patrick Kowalzick |
last post by:
Hi all,
Is this valid? I do not find the case in the standard:
void foo( int ) {}
#define FOO foo
int main()
{
|
by: srinu.fsl |
last post by:
there's a MACRO call :
MACRO1(cnf)
and its expansion is :
#define MACRO1(cnf) (((cnf) != TRUE)? (CLEANUP(FAIL)):(err =
SUCCESS));
#define CLEANUP(a)
| |
by: talkaboutquality |
last post by:
Need to define a macro as, say,
#ifdef NEED_FUNCTION
foo(x)
#else
#endif
|
by: Martin Wells |
last post by:
Is there any way of achieving this? Something like:
#define END_SCAN() (EndScan(),CleanUpAfterScan())
(Yes I realise this doesn't work)
I want to be able to invoke it as:
END_SCAN();
|
by: jason |
last post by:
Hi,
I learned my lesson about passing pointers, but now I have a question
about macros.
Why does the function work and the MACRO which is doing the same thing
on the surface, does not work in the following small example ?
#include <stdio.h>
|
by: Francois Grieu |
last post by:
One of the C compiler that I use <OT>(Keil's CX51)</OTbarks at
#define a(b) b
int main(void){return a(
#if 0
#endif
0);}
More generally, this compiler seems confused by any preprocessing
directive in the middle of macro arguments, which comes handy e.g. to
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |