Skip to content

Commit

Permalink
PEP-8 style cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpressey committed Jun 30, 2021
1 parent f7968f1 commit 022f240
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/castile/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def __init__(self, tag, children=None, value=None, type=None, aux=None):
assert isinstance(self.children, list)
for child in self.children:
assert isinstance(child, AST), \
"child %r of %r is not an AST node" % (child, self)
#print "created %r" % self
"child %r of %r is not an AST node" % (child, self)
# print("created %r" % self)

def copy(self, children=None, value=None, type=None, aux=None):
if children is None:
Expand Down
1 change: 0 additions & 1 deletion src/castile/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import castile.backends.c
#import castile.backends.cil
import castile.backends.javascript
import castile.backends.stackmac
import castile.backends.ruby
15 changes: 7 additions & 8 deletions src/castile/backends/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from castile.transformer import VarDeclTypeAssigner

OPS = {
'and': '&&',
'or': '||',
'and': '&&',
'or': '||',
}

PRELUDE = r"""
Expand Down Expand Up @@ -118,7 +118,7 @@ def c_type(self, type):
def c_decl(self, type, name, args=True):
if isinstance(type, Function):
s = ''
s += self.c_type(type.return_type)
s += self.c_type(type.return_type)
s += ' %s' % name
if args:
s += '('
Expand Down Expand Up @@ -304,11 +304,13 @@ def compile(self, ast):
self.write(' = ')
self.compile(ast.children[1])
elif ast.tag == 'Make':
self.write('make_%s(' % ast.type.name)

def find_field(name):
for field in ast.children[1:]:
if field.value == name:
return field

self.write('make_%s(' % ast.type.name)
ordered_fields = []
for field_name in ast.type.defn.field_names_in_order():
ordered_fields.append(find_field(field_name))
Expand Down Expand Up @@ -337,12 +339,9 @@ def find_field(name):
self.compile(ast.children[0])
self.write('->value);\n')
self.typecasing.add(ast.children[0].value)
#self.write('/*')
#self.write(repr(ast.children[0]))
#self.write('*/')
self.compile(ast.children[2])
self.typecasing.remove(ast.children[0].value)
self.indent -= 1;
self.indent -= 1
self.write_indent('}\n')
else:
raise NotImplementedError(repr(ast))
14 changes: 8 additions & 6 deletions src/castile/backends/stackmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ def compile(self, ast):
# first arg passed is DEEPEST, so go backwards.
pos = 0 - self.fun_argsize
for child in ast.children:
self.out.write('%s_local_%s=%d\n' %
(self.fun_lit, child.value, pos))
self.out.write(
'%s_local_%s=%d\n' % (self.fun_lit, child.value, pos)
)
pos += self.size_of(ast.type)
elif ast.tag == 'Body':
self.compile(ast.children[0])
Expand All @@ -150,8 +151,9 @@ def compile(self, ast):
self.compile(child)
elif ast.tag == 'VarDecl':
self.out.write('push 0\n')
self.out.write('%s_local_%s=%s\n' %
(self.fun_lit, ast.value, self.local_pos))
self.out.write(
'%s_local_%s=%s\n' % (self.fun_lit, ast.value, self.local_pos)
)
self.local_pos += 1
elif ast.tag == 'Block':
for child in ast.children:
Expand Down Expand Up @@ -257,12 +259,12 @@ def compile(self, ast):
self.out.write('get_value\n')
assert ast.children[0].tag == 'VarRef'
self.out.write('set_local %s_local_%s\n' % (self.fun_lit, ast.children[0].value))

self.compile(ast.children[2])
# now restore the value, with what was saved on the stack
self.out.write('dup\n')
self.out.write('set_local %s_local_%s\n' % (self.fun_lit, ast.children[0].value))

self.out.write('%s:\n' % end_typecase)
self.out.write('pop 1\n')
else:
Expand Down
25 changes: 14 additions & 11 deletions src/castile/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def field_names_in_order(self):
l.append(m[i])
return l


class TypeChecker(object):
def __init__(self):
global_context = {}
Expand Down Expand Up @@ -72,12 +73,12 @@ def collect_struct(self, ast):
self.structs[name] = StructDefinition(ast.value, struct_fields, te)

def resolve_structs(self, ast):
if isinstance(ast.type, Struct):
if ast.type.name not in self.structs:
raise CastileTypeError('undefined struct %s' % name)
ast.type.defn = self.structs[ast.type.name]
for child in ast.children:
self.resolve_structs(child)
if isinstance(ast.type, Struct):
if ast.type.name not in self.structs:
raise CastileTypeError('undefined struct %s' % name)
ast.type.defn = self.structs[ast.type.name]
for child in ast.children:
self.resolve_structs(child)

# context is modified as side-effect of traversal
def type_of(self, ast):
Expand Down Expand Up @@ -144,8 +145,9 @@ def type_of(self, ast):
ast.type = Void()
elif ast.tag == 'FunType':
return_type = self.type_of(ast.children[0])
ast.type = Function([self.type_of(c) for c in ast.children[1:]],
return_type)
ast.type = Function(
[self.type_of(c) for c in ast.children[1:]], return_type
)
elif ast.tag == 'UnionType':
ast.type = Union([self.type_of(c) for c in ast.children])
elif ast.tag == 'StructType':
Expand All @@ -158,7 +160,7 @@ def type_of(self, ast):
elif ast.tag == 'FunCall':
t1 = self.type_of(ast.children[0])
assert isinstance(t1, Function), \
'%r is not a function' % t1
'%r is not a function' % t1
if len(t1.arg_types) != len(ast.children) - 1:
raise CastileTypeError("argument mismatch")
i = 0
Expand Down Expand Up @@ -297,8 +299,9 @@ def type_of(self, ast):
if not isinstance(uni_t, Union):
raise CastileTypeError('bad cast, not a union: %s' % uni_t)
if not uni_t.contains(val_t):
raise CastileTypeError('bad cast, %s does not include %s' %
(uni_t, val_t))
raise CastileTypeError(
'bad cast, %s does not include %s' % (uni_t, val_t)
)
ast.type = uni_t
else:
raise NotImplementedError(repr(ast))
Expand Down
2 changes: 1 addition & 1 deletion src/castile/eval.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from castile.builtins import BUILTINS, TaggedValue


### Evaluator ###
# ### Evaluator ### #


OPS = {
Expand Down
27 changes: 16 additions & 11 deletions src/castile/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, text):
# for parser...
self.locals = None

### SCANNER ###
# ### SCANNER ### #

def scan_pattern(self, pattern, type, token_group=1, rest_group=2):
pattern = r'^(' + pattern + r')(.*?)$'
Expand All @@ -42,7 +42,7 @@ def scan_pattern(self, pattern, type, token_group=1, rest_group=2):
self.type = type
self.token = match.group(token_group)
self.text = match.group(rest_group)
#print self.type, self.token
# print(self.type, self.token)
return True

def scan(self):
Expand Down Expand Up @@ -90,8 +90,9 @@ def expect(self, token):
if self.token == token:
self.scan()
else:
raise CastileSyntaxError("Expected '%s', but found '%s'" %
(token, self.token))
raise CastileSyntaxError(
"Expected '%s', but found '%s'" % (token, self.token)
)

def expect_type(self, type):
self.check_type(type)
Expand All @@ -107,8 +108,9 @@ def on_type(self, type):

def check_type(self, type):
if not self.type == type:
raise CastileSyntaxError("Expected %s, but found %s ('%s')" %
(type, self.type, self.token))
raise CastileSyntaxError(
"Expected %s, but found %s ('%s')" % (type, self.type, self.token)
)

def consume(self, token):
if self.token == token:
Expand All @@ -125,7 +127,7 @@ def consume_type(self, type):
else:
return None

### PARSER ###
# ### PARSER ### #

def program(self):
defns = []
Expand Down Expand Up @@ -239,7 +241,8 @@ def body(self):
elif last is not None and last.tag not in self.STMT_TAGS:
stmts[-1] = AST('Return', [stmts[-1]])
self.expect('}')
vardecls = AST('VarDecls',
vardecls = AST(
'VarDecls',
[AST('VarDecl', value=name) for name in self.locals]
)
stmts = AST('Block', stmts)
Expand Down Expand Up @@ -335,9 +338,11 @@ def expr4(self):
return e

def expr5(self):
if (self.on_type('string literal') or self.on('-') or
self.on_type('integer literal') or self.on('fun')
or self.on('true') or self.on('false') or self.on('null')):
if (
self.on_type('string literal') or self.on('-') or
self.on_type('integer literal') or self.on('fun') or
self.on('true') or self.on('false') or self.on('null')
):
return self.literal()
elif self.consume('not'):
return AST('Not', [self.expr1()])
Expand Down
1 change: 1 addition & 0 deletions src/castile/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from castile.ast import AST


class FunctionLifter(object):
"""Bring all function definitions up to the toplevel (for target
languages like C).
Expand Down

0 comments on commit 022f240

Please sign in to comment.