Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def cli():
- print "Welcome to CLI v0.0."
- print "This may be extremely buggy. Enter q to quit."
- stack,functions,prescision=[],{},3
- while True:
- cmd=raw_input('>>')
- if cmd=='':
- pass
- elif cmd.count(':') == 1:
- z=cmd.index(':')
- a,b=cmd[:z],cmd[z+1:]
- m=errorcheck(b,functions)
- if m==1:
- functions[a]=b
- elif cmd[0]=='p':
- if len(cmd[1:])==0:
- print "The prescision is currently set to {0}.".format(prescision)
- elif len(filter(lambda x: not x in ' 0123456789',cmd[1:]))==0:
- prescision=int(cmd[1:])
- print 'The prescision has been set to {0}.'.format(prescision)
- else:
- print 'Invalid requested prescision.'
- elif cmd=='q':
- print "Goodbye!"
- break
- elif cmd=='c':
- v=raw_input('Are you sure you want to clear the stack?\n-->')
- if v in ['y','yes']:
- stack=[]
- else:
- initialstack=stack
- z=run(cmd,stack,prescision,functions)
- if z==0:
- stack=initialstack
- else:
- stack=z
- if len(stack)>5:
- print '...'
- for i in range(5):
- print stack[i+len(stack)-5]
- else:
- for i in stack:
- print i
- def run(cmds, stack, prescision, functions):
- l,z=cmds.rsplit(' '),errorcheck(cmds,functions)
- if z==0:
- return 0
- for i in l:
- if stack==0:
- break
- elif i in functions.keys():
- stack = run(functions[i],stack,prescision,functions)
- elif i in '+-*/' or i=='**':
- if len(stack)<2:
- print 'Stack underflow error'
- return 0
- else:
- a=stack.pop()
- b=stack.pop()
- stack.append(round(eval(str(b)+i+str(a)),prescision))
- elif i=='$':
- if len(stack)==0:
- print 'Stack underflow error'
- return 0
- else:
- stack.append(stack[-1])
- elif i=='@':
- if len(stack)<3:
- print 'Stack underflow error'
- return 0
- else:
- z=stack[:-3]
- z.append(stack[-2])
- z.append(stack[-1])
- z.append(stack[-3])
- stack=z
- elif i=='\\':
- if len(stack)<2:
- print 'Stack underflow error'
- return 0
- else:
- a=stack.pop()
- b=stack.pop()
- stack.append(a)
- stack.append(b)
- elif i=='%':
- if len(stack)==0:
- print 'Stack underflow error'
- return 0
- else:
- stack.pop()
- elif len(filter(lambda x: not x in '.0123456789',i))==0:
- stack.append(round(float(i),prescision))
- return stack
- def errorcheck(cmds, functions):
- l=cmds.rsplit(' ')
- for i in l:
- if i.count('.')>1:
- print 'Invalid decimal placing.'
- return 0
- if not i in '\\$%@+-*/' and i!='**' and not i in functions.keys():
- if len(filter(lambda x: not x in '.0123456789',i))!=0:
- print "Undefined command {0}".format(i)
- return 0
- return 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement