2013-03-03 22:26:17 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2013-03-15 02:03:33 +01:00
|
|
|
import hy
|
2013-03-26 01:31:58 +01:00
|
|
|
|
2013-03-07 04:09:13 +01:00
|
|
|
import sys
|
2013-03-26 01:31:58 +01:00
|
|
|
import os
|
2013-03-15 02:03:33 +01:00
|
|
|
|
2013-03-15 17:38:44 +01:00
|
|
|
|
2013-03-15 02:03:33 +01:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
from hy.importer import import_file_to_module
|
2013-03-15 18:00:28 +01:00
|
|
|
sys.argv.pop(0)
|
|
|
|
import_file_to_module("__main__", sys.argv[0])
|
2013-03-15 02:03:33 +01:00
|
|
|
sys.exit(0) # right?
|
|
|
|
|
|
|
|
|
2013-03-14 22:36:38 +01:00
|
|
|
import readline
|
2013-03-26 01:31:58 +01:00
|
|
|
import code
|
|
|
|
import ast
|
2013-03-15 00:44:33 +01:00
|
|
|
|
2013-03-14 22:36:38 +01:00
|
|
|
from hy.lex.states import Idle, LexException
|
|
|
|
from hy.lex.machine import Machine
|
|
|
|
from hy.compiler import hy_compile
|
|
|
|
from hy.core import process
|
2013-03-07 04:09:13 +01:00
|
|
|
|
2013-03-26 01:31:58 +01:00
|
|
|
from hy.macros import macro
|
|
|
|
from hy.models.expression import HyExpression
|
|
|
|
from hy.models.string import HyString
|
|
|
|
from hy.models.symbol import HySymbol
|
|
|
|
|
2013-03-15 17:38:44 +01:00
|
|
|
|
2013-03-14 22:36:38 +01:00
|
|
|
_machine = Machine(Idle, 1, 0)
|
|
|
|
|
|
|
|
|
|
|
|
class HyREPL(code.InteractiveConsole):
|
|
|
|
def runsource(self, source, filename='<input>', symbol='single'):
|
|
|
|
global _machine
|
|
|
|
|
|
|
|
try:
|
|
|
|
_machine.process(source + "\n")
|
|
|
|
except LexException as e:
|
|
|
|
_machine = Machine(Idle, 1, 0)
|
|
|
|
self.showsyntaxerror(filename)
|
|
|
|
return False
|
|
|
|
|
|
|
|
if type(_machine.state) != Idle:
|
|
|
|
_machine = Machine(Idle, 1, 0)
|
|
|
|
return True
|
|
|
|
|
|
|
|
tokens = process(_machine.nodes)
|
|
|
|
|
|
|
|
_machine = Machine(Idle, 1, 0)
|
|
|
|
_ast = hy_compile(tokens, root=ast.Interactive)
|
|
|
|
code = compile(_ast, filename, symbol)
|
|
|
|
|
|
|
|
self.runcode(code)
|
|
|
|
return False
|
|
|
|
|
2013-03-15 17:38:44 +01:00
|
|
|
|
2013-03-14 22:36:38 +01:00
|
|
|
sys.ps1 = "=> "
|
|
|
|
sys.ps2 = "... "
|
|
|
|
|
2013-03-26 00:10:59 +01:00
|
|
|
history = os.path.expanduser("~/.hy-history")
|
|
|
|
readline.parse_and_bind("set blink-matching-paren on")
|
|
|
|
|
2013-03-26 01:31:58 +01:00
|
|
|
|
|
|
|
@macro("koan")
|
|
|
|
def koan_macro(tree):
|
|
|
|
return HyExpression([HySymbol('print'),
|
|
|
|
HyString("""
|
|
|
|
Ummon asked the head monk, "What sutra are you lecturing on?"
|
|
|
|
"The Nirvana Sutra."
|
|
|
|
"The Nirvana Sutra has the Four Virtues, hasn't it?"
|
|
|
|
"It has."
|
|
|
|
Ummon asked, picking up a cup, "How many virtues has this?"
|
|
|
|
"None at all, " said the monk.
|
|
|
|
"But ancient people said it had, didn't they?" said Ummon.
|
|
|
|
"Whatdo you think of what they said?"
|
|
|
|
Ummon struck the cup and asked, "You understand?"
|
|
|
|
"No," said the monk.
|
|
|
|
"Then," said Ummon, "You'd better go on with your lectures on the sutra."
|
|
|
|
""")])
|
|
|
|
|
|
|
|
|
2013-03-26 01:54:36 +01:00
|
|
|
@macro("ideas")
|
|
|
|
def koan_macro(tree):
|
|
|
|
return HyExpression([HySymbol('print'),
|
|
|
|
HyString("""
|
|
|
|
|
|
|
|
=> (import-from sh figlet)
|
|
|
|
=> (figlet "Hi, Hy!")
|
|
|
|
_ _ _ _ _ _
|
|
|
|
| | | (_) | | | |_ _| |
|
|
|
|
| |_| | | | |_| | | | | |
|
|
|
|
| _ | |_ | _ | |_| |_|
|
|
|
|
|_| |_|_( ) |_| |_|\__, (_)
|
|
|
|
|/ |___/
|
|
|
|
|
|
|
|
|
|
|
|
;;; string things
|
|
|
|
(.join ", " ["what" "the" "heck"])
|
|
|
|
|
|
|
|
|
|
|
|
;;; this one plays with command line bits
|
|
|
|
(import-from sh cat grep)
|
|
|
|
(-> (cat "/usr/share/dict/words") (grep "-E" "bro$"))
|
|
|
|
|
|
|
|
|
|
|
|
;;; filtering a list w/ a lambda
|
|
|
|
(filter (lambda [x] (= (% x 2) 0)) (range 0 10))
|
|
|
|
|
|
|
|
|
|
|
|
;;; swaggin' functional bits (Python rulez)
|
|
|
|
(max (map (lambda [x] (len x)) ["hi" "my" "name" "is" "paul"]))
|
|
|
|
|
|
|
|
""")])
|
|
|
|
|
|
|
|
|
2013-03-26 00:10:59 +01:00
|
|
|
try:
|
|
|
|
readline.read_history_file(history)
|
|
|
|
except IOError:
|
|
|
|
open(history, 'wa').close()
|
2013-03-15 17:38:44 +01:00
|
|
|
|
2013-03-28 03:20:16 +01:00
|
|
|
# readline.parse_and_bind("tab: complete")
|
2013-03-26 01:31:58 +01:00
|
|
|
|
2013-03-14 22:36:38 +01:00
|
|
|
hr = HyREPL()
|
2013-03-15 00:44:33 +01:00
|
|
|
hr.interact("{appname} {version}".format(
|
|
|
|
appname=hy.__appname__,
|
|
|
|
version=hy.__version__
|
|
|
|
))
|
2013-03-26 00:10:59 +01:00
|
|
|
readline.write_history_file(history)
|