This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
catlang.py
executable file
·95 lines (81 loc) · 2.75 KB
/
catlang.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# catlang.py - Andrew Nelis <[email protected]>
# - extended by Wayne Wiitanen <[email protected]> June, 2012
#
# An interpreter for the cat language.
#
# Usage:
#
# ./catlang.py --eval "code" - Evaluate the given source
# ./catlang.py - (no arguments) Start an interactive session.
#
# If you add a new function, be sure to add a test (or two) to the runtest
# function.
#
# LICENSE: LGPL
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License (LGPL) as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This software is distributed in the hope that it will be useful.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA,
# or see http://www.gnu.org/copyleft/lesser.html
__version__ = '0.7'
import sys, platform
import readline
from cat.repl import REPL
from cat.eval import CatEval
# set up to handle colored text to console
if platform.system().lower() == 'windows' :
try :
from termcolor import colored
from colorama import init
colorama.init()
except ImportError:
def colored( text, _ ) :
return text
elif platform.system().lower() == 'darwin' :
try :
from termcolor import colored
except ImportError:
def colored( text, _ ) :
return text
else :
try:
from colorama import init, Fore
init(autoreset=True)
def colored(text, color=None):
if color:
fg_color = getattr(Fore, color.upper())
else:
fg_color = ''
return fg_color + text
except ImportError:
def colored(text, _): # NOQA
return text
if __name__ == '__main__':
cat = CatEval(output_fn=colored)
if len(sys.argv) > 1:
if sys.argv[1] in ('-e', '--eval'):
cat.eval(' '.join(sys.argv[2:]))
print cat
else:
r = REPL(cat)
r.run()