Partial Evaluation for Parsing

I am working on a macro system for Python ( as discussed here ), and one of the things I looked at is units. Although units can be implemented without macros or using static macros (for example, to identify all your blocks in advance), I love the idea of ​​being able to dynamically expand the syntax at runtime.

To do this, I am considering using partial code evaluation at compile time. If parsing is not performed for this expression, because the macro is not available for its syntax, the compiler stops evaluating the function / block and generates the code that it already has, with a stub where the unknown expression is located. When this stub gets into runtime, the function is recompiled with the current set of macros. If this compilation fails, a parsing error will be output because execution cannot continue. If compilation succeeds, the new function replaces the old one, and execution continues.

The biggest problem that I see is that you cannot find the analysis errors until the affected code is run. However, this will not affect many cases, for example. such as [], {}, () and `` should still be conjugated (the requirement of my tokenizer / list parser), and top-level syntax such as classes and functions will not be affected, since their “run time” is indeed load the time where the syntax is evaluated and their objects are generated.

Besides the complexity of the execution and the problem described above, what problems exist with this idea?

+3
source share
6 answers

Here are some possible issues:

  • . , .
  • .

, / Perl 6, . ( ):

1960- . , . , . : , . , - , , . , , -, . , . , .

: Perl 6 6: , , , ; "macro". , , , Perl 6 . Perl 6 , - , AST, ; Perl 6 , . , , , .

+3

, "" , . - . , , :

def fun():
   not implemented yet

try:
  fun()
except:
  pass

, , . , , .

, , , , . , , , , (, ). , , , . ...

, , , .

+2

, , , ( ), . Python 2.5 , , .

, . , .

+1

, . . : - , (, , python). , . , , . , , , , .

: bottom-up . . , . , , :

S -> NP . VP

( NP, VP). (, X- > NP). , , LALR LL- (, ) - O (n ^ 3) O (n), - , . - , . , .

+1

, , , , . , .

, , .

( , ).

0

, . , :

define some syntax M1 with definition D1
if _whatever_:
    define M1 to do D2
else:
    define M1 to do D3
code that uses M1

, , , , ( , M1, D1). , , , . - - , Python : D.

, , , "". , , , .

, :

...function definition fun1 that calls fun2...
define M1 (at runtime)
use M1
...function definition for fun2

Technically, when you use M1, you cannot parse it, so you need to leave the rest of the program (including the definition of the fun2 function) in the source code. When you run the entire program, you will see a call to fun2, which you cannot call, even if it is defined.

0
source

Source: https://habr.com/ru/post/1702156/


All Articles