Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsol committed Jan 30, 2020
1 parent 82d1387 commit e64a08e
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 129 deletions.
2 changes: 1 addition & 1 deletion src/Pyxell.g4
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ expr
| '[' (expr ',')* expr? ']' # ExprArray
| '[' expr 'step' expr ']' # ExprArrayRangeStep
| '[' expr comprehension+ ']' # ExprArrayComprehension
| expr safe='?'? '.' ID # ExprAttr
| expr safe='?'? '[' tuple_expr ']' # ExprIndex
| expr '[' e1=expr? (':' e2=expr? (':' e3=expr?)?) ']' # ExprSlice
| expr safe='?'? '.' ID # ExprAttr
| expr '(' (call_arg ',')* call_arg? ')' # ExprCall
| expr op='!' # ExprUnaryOp
| <assoc=right> expr op=('^' | '^^') expr # ExprBinaryOp
Expand Down
2 changes: 1 addition & 1 deletion src/antlr/Pyxell.interp

Large diffs are not rendered by default.

192 changes: 96 additions & 96 deletions src/antlr/PyxellParser.py

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions src/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@ def visitComprehensionFilter(self, ctx):
'expr': self.visit(ctx.expr()),
}

def visitExprAttr(self, ctx):
return {
**_node(ctx, 'ExprAttr'),
'expr': self.visit(ctx.expr()),
'attr': self.visit(ctx.ID()),
'safe': bool(ctx.safe),
}

def visitExprIndex(self, ctx):
return {
**_node(ctx, 'ExprIndex'),
Expand All @@ -243,14 +251,6 @@ def visitExprSlice(self, ctx):
'slice': self.visit([ctx.e1, ctx.e2, ctx.e3]),
}

def visitExprAttr(self, ctx):
return {
**_node(ctx, 'ExprAttr'),
'expr': self.visit(ctx.expr()),
'attr': self.visit(ctx.ID()),
'safe': bool(ctx.safe),
}

def visitExprCall(self, ctx):
return {
**_node(ctx, 'ExprCall'),
Expand Down
46 changes: 23 additions & 23 deletions src/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,12 @@ def lvalue(self, node, expr, declare=None, override=False, initialize=False):

return self.env[id]

elif expr['node'] == 'ExprIndex' and not expr.get('safe'):
return self.index(node, *map(self.compile, expr['exprs']), lvalue=True)

elif expr['node'] == 'ExprAttr' and not expr.get('safe'):
return self.member(node, self.compile(expr['expr']), expr['attr'], lvalue=True)

elif expr['node'] == 'ExprIndex' and not expr.get('safe'):
return self.index(node, *map(self.compile, expr['exprs']), lvalue=True)

self.throw(node, err.NotLvalue())

def assign(self, node, expr, value):
Expand Down Expand Up @@ -963,12 +963,6 @@ def convert_expr(expr):
**expr,
'exprs': lmap(convert_expr, expr['exprs']),
}
if node == 'ExprSlice':
return {
**expr,
'expr': convert_expr(expr['expr']),
'slice': lmap(convert_expr, expr['slice']),
}
if node == 'ExprArrayComprehension':
return {
**expr,
Expand All @@ -981,16 +975,22 @@ def convert_expr(expr):
'iterables': lmap(convert_expr, expr['iterables']),
'steps': lmap(convert_expr, expr['steps']),
}
if node == 'ExprCall':
if node in {'ComprehensionFilter', 'ExprAttr', 'CallArg', 'ExprUnaryOp'}:
return {
**expr,
'expr': convert_expr(expr['expr']),
'args': lmap(convert_expr, expr['args']),
}
if node in {'ComprehensionFilter', 'ExprAttr', 'CallArg', 'ExprUnaryOp'}:
if node == 'ExprSlice':
return {
**expr,
'expr': convert_expr(expr['expr']),
'slice': lmap(convert_expr, expr['slice']),
}
if node == 'ExprCall':
return {
**expr,
'expr': convert_expr(expr['expr']),
'args': lmap(convert_expr, expr['args']),
}
if node == 'AtomString':
expr = self.convert_string(expr, expr['string'])
Expand Down Expand Up @@ -1614,6 +1614,17 @@ def compileExprArrayComprehension(self, node):

return result

def compileExprAttr(self, node):
expr = node['expr']
attr = node['attr']

if node.get('safe'):
obj = self.compile(expr)
return self.safe(node, obj, lambda: self.nullable(self.attr(node, self.extract(obj), attr)), vNull)

obj, value = self.attribute(node, expr, attr)
return value

def compileExprIndex(self, node):
exprs = node['exprs']

Expand Down Expand Up @@ -1668,17 +1679,6 @@ def compileExprSlice(self, node):
# `CharArray_asString` is used directly, because `.join` would copy the array redundantly.
return self.builder.call(self.get(node, 'CharArray_asString'), [result]) if type == tString else result

def compileExprAttr(self, node):
expr = node['expr']
attr = node['attr']

if node.get('safe'):
obj = self.compile(expr)
return self.safe(node, obj, lambda: self.nullable(self.attr(node, self.extract(obj), attr)), vNull)

obj, value = self.attribute(node, expr, attr)
return value

def compileExprCall(self, node):
expr = node['expr']

Expand Down
1 change: 1 addition & 0 deletions test/bad/modules/attr02.err
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Undeclared identifier `package`.
2 changes: 2 additions & 0 deletions test/bad/modules/attr02.px
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

print package.something

0 comments on commit e64a08e

Please sign in to comment.