Skip to content

Commit

Permalink
Assignment fixed, one layer of lambda-indirection removed
Browse files Browse the repository at this point in the history
  • Loading branch information
perimosocordiae committed Nov 18, 2010
1 parent 0fdf970 commit 15a944a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
3 changes: 0 additions & 3 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@

fix the assignment mechanism
- save values to a symtab, as before, probably

rewrite builtins as AST nodes
- avoid all the hash lookups at runtime
- allow agressive streamlining / static checking
Expand Down
50 changes: 24 additions & 26 deletions lib/ast_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,21 @@ def run(self):
return exec(self.code)
fix_missing_locations(self.tree)
#print(unparse(self.tree)) # super useful for debugging
exec(compile(self.tree,self.fname,'exec'))
exec(compile(self.tree,self.fname,'exec'),globals(),globals())

class REPLProgram(Program):
def run(self):
assert len(self.tree.body) == 1
#print(unparse(self.tree)) # super useful for debugging
stmt = self.tree.body.pop()
stree = _ast.Expression(body=stmt.value)
fix_missing_locations(stree)
return eval(compile(stree,'<repl>','eval'))
if type(stmt) is _ast.Assign:
stree = _ast.Module(body=[stmt])
fix_missing_locations(stree)
exec(compile(stree,'<repl>','exec'), globals(), globals())
else:
stree = _ast.Expression(body=stmt.value)
fix_missing_locations(stree)
return eval(compile(stree,'<repl>','eval'))

def Statement(line):
c = ast_select('dummy([])') # call w/ empty list as initial seq
Expand All @@ -66,7 +71,6 @@ def Statement(line):
s.col_offset = 1
return s

#TODO: fix!
def Assignment(lhs,rhs):
a = ast_parse(lhs+' = dummy').body[0]
a.value = Expression(rhs)
Expand All @@ -76,18 +80,15 @@ def Assignment(lhs,rhs):
def fold_ast(rpipe):
a = rpipe[-1]
if type(a) is not Literal:
c = ast_select('dummy()')
c.func = a.value
c = a.value
if len(rpipe) > 1:
c.args = [fold_ast(rpipe[:-1])]
elif type(a) is Slurp:
c.args = [] # slurps get no input
else:
c.args = [ast_select('initial')]
c.args[0] = fold_ast(rpipe[:-1])
elif type(a) is not Slurp: # slurps get no input
c.args[0] = ast_select('initial')
return c
elif len(rpipe) == 1:
return a.value
else: raise Exception('Literal atom not at beginning of pipe!')
else: raise Exception('Invalid location for literal: %s'%a)

def ast_select(s):
return ast_parse(s).body[0].value
Expand Down Expand Up @@ -132,14 +133,10 @@ def __init__(self,atom):
self.cmd, args = lex_atom(atom)
assert self.cmd in stdlib
self.args = [parse_atom(a) for a in args]
self.value = ast_select(
'lambda inputs: stdlib[dummy](inputs)')
self.value.body.func.slice.value = _ast.Str(s=self.cmd)
self.value = ast_select('stdlib[dummy]([])') # call-exp
self.value.func.slice.value = _ast.Str(s=self.cmd)
for a in self.args:
if type(a) is Literal:
self.value.body.args.append(a.value)
else:
self.value.body.args.append(ast_ctor('d([])',func=a.value))
self.value.args.append(a.value)

def __str__(self):
return self.cmd+' '+' '.join(map(str,self.args))
Expand All @@ -159,23 +156,24 @@ def __str__(self):

class Reference:
def __init__(self,ref):
self.value=_ast.Name(id=ref,ctx=_ast.Load())
self.value = ast_ctor('dummy([])')
self.value.func.id = ref
def __str__(self):
return '$'+self.value.id
return '$'+self.value.func.id

class Slurp:
def __init__(self,fname):
self.fname = fname
self.value = ast_select("lambda: stdlib['_slurp_']('dummy')")
self.value.body.args[0].s = fname
self.value = ast_select("stdlib['_slurp_'](dummy)")
self.value.args[0] = _ast.Str(s=fname)
def __str__(self):
return '<%s>' % self.fname

class Shell:
def __init__(self,cmd):
self.cmd = cmd
self.value = ast_select("lambda inputs: stdlib['_shell_'](inputs)")
self.value.body.args.append(_ast.Str(s=self.cmd))
self.value = ast_select("stdlib['_shell_']([],dummy)")
self.value.args[1] = _ast.Str(s=self.cmd)
def __str__(self):
return '`%s`'%self.cmd

Expand Down
5 changes: 2 additions & 3 deletions test/stdlib.cj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
# comments should be ignored
[2,3,4,1,2,5,3,2] | sort | inc # from '#' to eol

# assignment is currently broken, with the new AST
#foo = strip | compact
#<stdlib.cj> | $foo | grep /[aeiou]{2}/ | sort | count
foo = strip | compact
<stdlib.cj> | $foo | grep /[aeiou]{2}/ | sort | count

<stdlib.cj> | strip | compact | grep /[aeiou]{2}/ | sort | count

Expand Down

0 comments on commit 15a944a

Please sign in to comment.