Skip to content

Commit

Permalink
Made some recursive procedures iterative. (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
rahularya50 authored and kavigupta committed Apr 15, 2019
1 parent eb201f2 commit e59af85
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 44 deletions.
41 changes: 12 additions & 29 deletions editor/datamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,19 @@ def __init__(self, first: Expression, rest: Expression):
f"Unable to construct a Pair with a cdr of {rest}, expected a Pair, Nil, or Promise.")
self.rest = rest

# def __str__(self):
# if isinstance(self.first, Symbol):
# if isinstance(self.rest, Pair) and self.rest.rest == Nil:
# if self.first.value == "quote":
# return f"'{str(self.rest.first)}"
# elif self.first.value == "unquote":
# return f",{str(self.rest.first)}"
# elif self.first.value == "unquote-splicing":
# return f",@{str(self.rest.first)}"
# elif self.first.value == "quasiquote":
# return f"`{str(self.rest.first)}"
#
# if isinstance(self.rest, Pair):
# rest_str = str(self.rest)
# if rest_str[0] == "(" and rest_str[-1] == ")":
# return f"({self.first} {rest_str[1:-1]})"
# else:
# return f"({self.first} . {rest_str})"
# elif self.rest is Nil:
# return f"({self.first})"
#
# return f"({str(self.first)} . {str(self.rest)})"

def __repr__(self):
if isinstance(self.rest, Pair):
return f"({repr(self.first)} {repr(self.rest)[1:-1]})"
elif self.rest is Nil:
return f"({repr(self.first)})"
else:
return f"({repr(self.first)} . {repr(self.rest)})"
pos = self
out = []
while True:
if isinstance(pos, Pair):
out.append(repr(pos.first))
pos = pos.rest
elif isinstance(pos, NilType):
break
else:
out.append(f". {repr(pos)}")
break
return "(" + " ".join(out) + ")"


class NilType(Expression):
Expand Down
7 changes: 4 additions & 3 deletions editor/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def verify_min_callable_length(operator: Expression, expected: int, actual: int)


def make_list(exprs: List[Expression], last: Expression = Nil) -> Union[Pair, NilType]:
if len(exprs) == 0:
return last
return Pair(exprs[0], make_list(exprs[1:], last))
out = last
for expr in reversed(exprs):
out = Pair(expr, out)
return out
19 changes: 7 additions & 12 deletions editor/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,16 @@ class Append(BuiltIn):
def execute_evaluated(self, operands: List[Expression], frame: Frame) -> Expression:
if len(operands) == 0:
return Nil
out = []
exprs = []
for operand in operands[:-1]:
if not isinstance(operand, Pair) and operand is not Nil:
raise OperandDeduceError(f"Expected operand to be valid list, not {operand}")
out.extend(pair_to_list(operand))
try:
last = operands[-1]
if not isinstance(last, Pair):
raise OperandDeduceError()
last = pair_to_list(last)
except OperandDeduceError:
return make_list(out, operands[-1])
else:
out.extend(last)
return make_list(out)
exprs.extend(pair_to_list(operand))
out = operands[-1]
for expr in reversed(exprs):
out = Pair(expr, out)
return out



@global_attr("car")
Expand Down

0 comments on commit e59af85

Please sign in to comment.