Strict Standards: Non-static method Soojung::addReferer() should not be called statically in /home/lifthrasiir/sites/sapzil.info/soojung/settings.php on line 79

Warning: Cannot modify header information - headers already sent by (output started at /home/lifthrasiir/sites/sapzil.info/soojung/settings.php:79) in /home/lifthrasiir/sites/sapzil.info/soojung/classes/Counter.class.php on line 63

Strict Standards: Non-static method Entry::getEntry() should not be called statically in /home/lifthrasiir/sites/sapzil.info/soojung/entry.php on line 51

Strict Standards: Non-static method Soojung::entryIdToFilename() should not be called statically in /home/lifthrasiir/sites/sapzil.info/soojung/classes/Entry.class.php on line 182

Strict Standards: Non-static method Soojung::queryFilenameMatch() should not be called statically in /home/lifthrasiir/sites/sapzil.info/soojung/classes/Soojung.class.php on line 55
TokigunStudio3 | 블로그: 오늘의 삽질

내용으로 바로 넘어 가기


TokigunStudio3

228 / 3282   


더 이상 이 블로그는 운영되지 않습니다. 새 블로그로 가 주세요.

오늘의 삽질

2005/05/03 AM 01:47 | 일상/삽질 열전 | 4 comments | 0 trackbacks | AllBlog: vote, to pocket

TiniCube 만든다고 여러 가지 삽질을 하고 있다. 생전 이런 종류의 프로그램에서 쓸 것 같지 않던 weakref 모듈을 쓰질 않나(-_-) 이번에는 compiler 모듈을 건들어 보는 삽질을 감행하고 있다.

그렇게 해서 만들고 있는 코드의 일부.
from compiler import parse, syntax, misc, pycodegen
from compiler.ast import *
import time, sys

def eval_parse(str):
    return parse(str, 'eval')

def eval_compile(nodes, filename=''):
    misc.set_filename(filename, nodes)
    if syntax.check(nodes): raise SyntaxError
    return pycodegen.ExpressionCodeGenerator(nodes).getCode()

def wrap(n):
    cls = n.__class__
    c = cls.__name__
    if cls in ('Add','Div','FloorDiv','LeftShift','Mod','Mul','Power','RightShift','Sub'):
        return cls((wrap(n.left), wrap(n.right)), n.lineno)
    elif cls in ('And','Bitand','Bitor','Bitxor','Decorators','List','Or','Sliceobj','Stmt','Tuple'):
        return cls(map(wrap, n.nodes), n.lineno)
    elif c == 'AssAttr':
        return cls(wrap(n.expr), n.attrname, n.flags, n.lineno)
    elif c == 'AssList' or c == 'AssTuple':
        return cls(wrap(n.nodes), n.lineno)
    # ....
    elif c == 'If':
        return cls([(wrap(test), wrap(stmt)) for test, stmt in n.tests], wrap(n.else_), n.lineno)
    elif c == 'Import':
        return cls(n.names, n.lineno)
    elif c == 'Keyword':
        return cls(n.name, wrap(n.expr), n.lineno)
    # ....
    elif c == 'TryExcept':
        return cls(wrap(n.body), [(wrap(expr1), wrap(expr2), wrap(stmt)) for expr1, expr2, stmt in n.handlers], wrap(n.else_), n.lineno)
    elif c == 'TryFinally':
        return cls(wrap(n.body), wrap(n.final), n.lineno)
    elif c == 'While':
        return cls(wrap(n.test), wrap(n.body), wrap(n.else_), n.lineno)
    return n
...파이썬의 compiler 모듈(documentation 별로 안 되어 있어서 무진장 골때린다. 직접 코드 보고서야 이해했다.)에 있는 AST, 즉 추상 소스 트리를 헤집어 가면서 문제가 될 수 있는 부분을 죄다 뜯어 고치는 과정. 아직 본격적으로 들어 가진 않아서 세부적인 부분은 다 빠져 있고 wrap 함수만 재귀적으로 돌리게 되어 있다.

위와 같이 AST에서 나올 수 있는 모든 node에 대해 작업을 하도록 했는데, compiler.ast에 있는 클래스 API가 2.3과 2.4가 다르다. -_-;;;;; 결국 2.3 용으로도 코드를 따로 만들어 버렸다.

...앞으로 삽질길이 험난하구먼.

TrackBack URL: http://sapzil.info/soojung/trackback.php?blogid=592

Comment: toz (2005/05/03 PM 05:12)

역시 촉수 토끼옹!

Comment: 토끼군 (2005/05/03 PM 05:21)

toz: ;;;;

아무튼 어제의 삽질을 통해 +-eval 명령을 다시 구현하니까 좋군요. 아직 처리 안 된 자료형이 한참 남아 있지만.

Comment: 인클루드 (2005/05/04 AM 05:01)

뭐가 뭔지 도통 모르겠... @.@

Comment: 토끼군 (2005/05/04 AM 07:11)

인클루드: 파이썬 흑마법~ =_=

Copyright (c) 1995-2005, Kang Seonghoon (Tokigun).