Pythonic macro syntax

2019-01-21 11:08发布

I've been working on an alternative compiler front-end for Python where all syntax is parsed via macros. I'm finally to the point with its development that I can start work on a superset of the Python language where macros are an integral component.

My problem is that I can't come up with a pythonic macro definition syntax. I've posted several examples in two different syntaxes in answers below. Can anyone come up with a better syntax? It doesn't have to build off the syntax I've proposed in any way -- I'm completely open here. Any comments, suggestions, etc would be helpful, as would alternative syntaxes showing the examples I've posted.

A note about the macro structure, as seen in the examples I've posted: The use of MultiLine/MLMacro and Partial/PartialMacro tell the parser how the macro is applied. If it's multiline, the macro will match multiple line lists; generally used for constructs. If it's partial, the macro will match code in the middle of a list; generally used for operators.

9条回答
别忘想泡老子
2楼-- · 2019-01-21 12:06

If you're only asking about the syntax (not implementation) of macros within Python, then I believe the answer is obvious. The syntax should closely match what Python already has (i.e., the "def" keyword).

Whether you implement this as one of the following is up to you:

def macro largest(lst):
defmac largest(lst):
macro largest(lst):

but I believe it should be exactly the same as a normal function with respect to the rest so that:

def twice_second(a,b):
    glob_i = glob_i + 1
    return b * 2
x = twice_second (1,7);

and

defmac twice_second(a,b):
    glob_i = glob_i + 1
    return b * 2
x = twice_second (1,7);

are functionally equivalent.

The way I would implement this is with a pre-processor (a la C) which would:

  • replace all defmac's with defs in the input file.
  • pass it through Python to check syntax (sneaky bit, this).
  • put the defmac's back in.
  • find all uses of each macro and "inline" them, using your own reserved variables, such as converting local var a with __macro_second_local_a.
  • return value should be a special variable as well (macro_second_retval).
  • global variables would keep their real names.
  • parameter can be given _macro_second_param_XXX names.
  • once all inlining is done, remove the defmac 'functions' entirely.
  • pass the resultant file through Python.

No doubt there'll be some nigglies to take care of (like tuples or multiple return points) but Python is sufficiently robust to handle that in my opinion.

So:

x = twice_second (1,7);

becomes:

# These lines are the input params.
__macro_second_param_a = 1
__macro_second_param_b = 7

# These lines are the inlined macro.
glob_i = glob_i + 1
__macro_second_retval = __macro_second_param_b * 2

# Modified call to macro.
x = __macro_second_retval
查看更多
对你真心纯属浪费
3楼-- · 2019-01-21 12:10

This is a new macro syntax I've come up with based on Kent Fredric's ideas. It parses the syntax into a list just like the code is parsed.

Print macro:

macro PrintMacro:
    syntax:
      print $stmts

  if not isinstance(stmts, list):
    stmts = [stmts]
  return Printnl(stmts, None)

If macro:

@MultiLine
macro IfMacro:
  syntax:
    @if_ = if $cond: $body
    @elifs = ZeroOrMore(elif $cond: $body)
    Optional(else: $elseBody)

  return If(
      [(cond, Stmt(body)) for cond, body in [if_] + elifs],
      elseBody != None and Stmt(elseBody) or None
    )

X to Y [inclusive] [step Z] macro:

@Partial
macro ToMacro:
  syntax:
    $start to $end Optional(inclusive) Optional(step $step)

  if step == None:
    step = quote 1
  if inclusive:
    return quote:
      xrange($start, $end+1, $step)
  else:
    return quote:
      xrange($start, $end, $step)

Aside from the minor issue of using decorators to identify macro type, my only real issue with this is the way you can name groups, e.g. in the if case. I'm using @name = ..., but this just reeks of Perl. I don't want to just use name = ... because that could conflict with a macro pattern to match. Any ideas?

查看更多
我命由我不由天
4楼-- · 2019-01-21 12:12

You should take a look at MetaPython to see if it accomplishes what you're looking for.

查看更多
登录 后发表回答