Skip to content

Commit

Permalink
More refactoring to become truly Pythonic.
Browse files Browse the repository at this point in the history
  • Loading branch information
ingydotnet committed Mar 11, 2010
1 parent 758d669 commit 4ac8f68
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 98 deletions.
19 changes: 2 additions & 17 deletions lib/cdent/__init__.py
@@ -1,21 +1,6 @@
"""\
Meta class for C'Dent
C'Dent runtime module used by all modules *written* in C'Dent/Python
(module.cd.py).
"""

version = '0.01'

id_to_language = {
'cd': "C'Dent",
'cpp': 'C++',
'java': 'Java',
'js': 'JavaScript',
'php': 'PHP',
'pm': 'Perl',
'pm6': 'Perl 6',
'py': 'Python',
'py3': 'Python 3',
'rb': 'Ruby',
}

def language(id):
return id_to_language[id]
10 changes: 5 additions & 5 deletions lib/cdent/command.py
Expand Up @@ -10,7 +10,7 @@ class Command():
def __init__(self, args):
sys.argv = args
self.action = None
self.src = None
self.from_ = None
self.to = None
self.parser = None
self.emitter = None
Expand Down Expand Up @@ -41,7 +41,7 @@ def cb_from(option, opt, value, parser):
raise OptionError('--from used before --compile')
exec "from cdent.parser." + value + " import Parser" in globals()
self.parser = Parser()
self.src = value
self.from_ = value
parser.add_option(
"--from", type="choice",
choices=['cd', 'java', 'js', 'pm', 'py', 'rb'],
Expand Down Expand Up @@ -139,7 +139,7 @@ def cb_version(option, opt, value, parser):
if (not self.action):
raise OptionError('is required', '--compile | --help | --version')
if self.action == 'compile':
if (not self.src):
if (not self.from_):
raise OptionError('is required', '--from')
if (not self.to):
raise OptionError('is required', '--to')
Expand All @@ -154,5 +154,5 @@ def compile(self):
self.emitter.create_module(ast)

def version(self):
import cdent
print "C'Dent version '%s'" % cdent.version
import cdent.compiler
print "C'Dent version '%s'" % cdent.compiler.version()
22 changes: 22 additions & 0 deletions lib/cdent/compiler.py
@@ -0,0 +1,22 @@
"""\
Meta class for the C'Dent compiler
"""

__version__ = '0.01'

id_to_language = {
'cd': "C'Dent",
'cpp': 'C++',
'java': 'Java',
'js': 'JavaScript',
'php': 'PHP',
'pm': 'Perl',
'pm6': 'Perl 6',
'py': 'Python',
'py3': 'Python 3',
'rb': 'Ruby',
}

def language(id):
return id_to_language[id]

13 changes: 8 additions & 5 deletions lib/cdent/emitter/__init__.py
Expand Up @@ -2,7 +2,7 @@
Code emitter base class for C'Dent
"""

import cdent
import cdent.compiler

class Emitter():
# Default comment constants
Expand Down Expand Up @@ -91,7 +91,10 @@ def emit_string(self, string):
self.write('"' + string.val + '"')

def cdent_header(self, ast):
header = "C'Dent generated %s module." % cdent.language(self.LANGUAGE_ID)
header = (
"C'Dent generated %s module." %
cdent.compiler.language(self.LANGUAGE_ID)
)
if self.emit_trailer:
header += " See trailer at end of file for details."
self.write_line_comment(header)
Expand All @@ -103,13 +106,13 @@ def cdent_header(self, ast):

def cdent_trailer(self, ast):
from time import strftime
cdent_version = cdent.version
cdent_version = cdent.compiler.__version__
module_name = ast.module.name
module_lang = cdent.language(self.LANGUAGE_ID)
module_lang = cdent.compiler.language(self.LANGUAGE_ID)
compile_time = strftime("%Y-%m-%d %H:%M:%S")
compiled_by = ast.user
input_path = getattr(ast, 'from')['path']
input_lang = cdent.language(getattr(ast, 'from')['lang'])
input_lang = cdent.compiler.language(getattr(ast, 'from')['lang'])
self.writeln()
self.write_block_comment(self.trailer_text() % locals())

Expand Down
6 changes: 3 additions & 3 deletions lib/cdent/emitter/js.py
Expand Up @@ -11,10 +11,10 @@ class Emitter(Base):
BLOCK_COMMENT_PREFIX = ' * '
BLOCK_COMMENT_END = ' */\n'

def emit_class(self, klass):
name = klass.name
def emit_class(self, class_):
name = class_.name
self.writeln('(this.%s = function() {}).prototype = {' % name)
self.emit(klass.has, indent=True)
self.emit(class_.has, indent=True)
self.writeln('}')

def emit_method(self, method):
Expand Down
6 changes: 3 additions & 3 deletions lib/cdent/emitter/pm.py
Expand Up @@ -7,12 +7,12 @@
class Emitter(Base):
LANGUAGE_ID = 'pm'

def emit_class(self, klass):
name = klass.name
def emit_class(self, class_):
name = class_.name
self.writeln('package %s;' % name)
self.writeln('use Moose;')
self.writeln()
self.emit(klass.has)
self.emit(class_.has)
self.writeln()
self.writeln('1;')

Expand Down
6 changes: 3 additions & 3 deletions lib/cdent/emitter/py.py
Expand Up @@ -10,10 +10,10 @@ class Emitter(Base):
BLOCK_COMMENT_PREFIX = ''
BLOCK_COMMENT_END = '"""\n'

def emit_class(self, klass):
name = klass.name
def emit_class(self, class_):
name = class_.name
self.writeln('class %s():' % name)
self.emit(klass.has, indent=True)
self.emit(class_.has, indent=True)

def emit_method(self, method):
name = method.name
Expand Down
6 changes: 3 additions & 3 deletions lib/cdent/emitter/rb.py
Expand Up @@ -7,10 +7,10 @@
class Emitter(Base):
LANGUAGE_ID = 'rb'

def emit_class(self, klass):
name = klass.name
def emit_class(self, class_):
name = class_.name
self.writeln('class %s' % name)
self.emit(klass.has, indent=True)
self.emit(class_.has, indent=True)
self.writeln('end')

def emit_method(self, method):
Expand Down
7 changes: 2 additions & 5 deletions lib/cdent/parser/cd.py
Expand Up @@ -13,14 +13,11 @@

class Parser(Base):
def parse_module(self):
ast = load(self.input)
return ast
return load(self.input)

def multi_constructor(loader, type, node):
map = loader.construct_mapping(node)
obj = getattr(cdent.ast, 'ast_' + type)()
for k in map:
setattr(obj, k, map[k])
obj.__dict__.update(loader.construct_mapping(node))
return obj

Loader.add_multi_constructor('tag:cdent.org,2010:', multi_constructor)
5 changes: 0 additions & 5 deletions src/cpan/Changes

This file was deleted.

16 changes: 0 additions & 16 deletions src/cpan/MANIFEST

This file was deleted.

5 changes: 0 additions & 5 deletions src/cpan/Makefile.PL

This file was deleted.

22 changes: 0 additions & 22 deletions src/cpan/README

This file was deleted.

6 changes: 0 additions & 6 deletions src/cpan/t/test.t

This file was deleted.

File renamed without changes.
File renamed without changes.

0 comments on commit 4ac8f68

Please sign in to comment.