Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add multiple arithmatic operator and multiple logical operator #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added rlispy/__pycache__/eval.cpython-38.pyc
Binary file not shown.
Binary file added rlispy/__pycache__/parse.cpython-38.pyc
Binary file not shown.
57 changes: 53 additions & 4 deletions rlispy/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,69 @@ def foldl(combine, acc, lst):
lst[1:]
)

#there don't have apply in python3, so I need to define one
def apply(proc, *args, **kwargs):
return proc(*args, **kwargs)

#define multiobjective arithmetic operators here
def _add(*operands):
result = 0
for operand in operands:
result = result + operand
return result

def _sub(*operands):
result = 0
for operand in operands:
if len(operands) > 1 and result == 0:
result = operand
else: result = result - operand
return result

def _mul(*operands):
result = 1
for operand in operands:
result = result * operand
return result

def _truediv(*operands):
result = 1
for operand in operands:
#first step in truediv, we need to let the first num in our operands tuple to be our dividend
if result == 1:
result = operand
else: result = result / operand
#and if divisor is 0, python will throw appropriate error to us
return result

#define multiobjective logical operators here
def _and(*args):
result = True
for arg in args:
result = result and arg
return result

def _or(*args):
result = False
for arg in args:
result = result or arg
return result
# An environment with some Scheme standard procedures."
def standard_env():
env = Env()
env.update(vars(math))
env.update({
'+':op.add,
'-':op.sub,
'*':op.mul,
'/':op.div,
'+':_add,
'-':_sub,
'*':_mul,
'/':_truediv,
'>':op.gt,
'<':op.lt,
'>=':op.ge,
'<=':op.le,
'=':op.eq,
'and':_and,
'or':_or,
'abs': abs,
'append': op.add,
'apply': apply,
Expand Down
4 changes: 2 additions & 2 deletions rlispy/lis.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
def repl(prompt='Code: '):
print("Lispy Version 3.0\n Get Coding!\n")
while True:
inpt = raw_input(prompt)
inpt = input(prompt)
try:
if inpt == "quit": break
val = eval(parse(inpt))
if val is not None:
print(schemestr(val))
except Exception as e:
print '%s: %s' % (type(e).__name__, e)
print ('%s: %s' % (type(e).__name__, e))

# Convert a Python object back into a Scheme-readable string.
def schemestr(exp):
Expand Down
2 changes: 1 addition & 1 deletion rlispy/parse.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Parsing Module

from __future__ import division
import re, sys, StringIO
import re, sys

# A Scheme List is implemented as a Python list
# A Scheme Number is implemented as a Python int or float
Expand Down