Updating for numbers
This commit is contained in:
parent
3b7caa92e2
commit
0081d4fdfc
6
hy/lang/number.py
Normal file
6
hy/lang/number.py
Normal file
@ -0,0 +1,6 @@
|
||||
from hy.lang.hyobj import HYObject
|
||||
|
||||
|
||||
class HYNumber(int, HYObject):
|
||||
def __init__(self, number):
|
||||
self = number
|
@ -6,6 +6,4 @@ class HYSymbol(unicode, HYObject):
|
||||
self += string
|
||||
|
||||
def eval(self, *args, **kwargs):
|
||||
if self.isdigit():
|
||||
return int(self)
|
||||
return self.lookup(self)
|
||||
|
@ -2,6 +2,7 @@ from hy.lang.expression import HYExpression
|
||||
from hy.lex.errors import LexException
|
||||
from hy.lang.string import HYString
|
||||
from hy.lang.symbol import HYSymbol
|
||||
from hy.lang.number import HYNumber
|
||||
from hy.lex.machine import Machine
|
||||
from hy.lang.list import HYList
|
||||
from hy.lang.map import HYMap
|
||||
@ -10,6 +11,12 @@ from hy.lang.map import HYMap
|
||||
WHITESPACE = [" ", "\t", "\n", "\r"]
|
||||
|
||||
|
||||
def _resolve_atom(value):
|
||||
if value.isdigit():
|
||||
return HYNumber(value)
|
||||
return HYSymbol(value)
|
||||
|
||||
|
||||
class State(object):
|
||||
def __init__(self, machine):
|
||||
self.machine = machine
|
||||
@ -58,13 +65,13 @@ class Expression(State):
|
||||
|
||||
def exit(self):
|
||||
if self.bulk:
|
||||
self.nodes.append(HYSymbol(self.bulk))
|
||||
self.nodes.append(_resolve_atom(self.bulk))
|
||||
|
||||
self.machine.nodes.append(self.nodes)
|
||||
|
||||
def commit(self):
|
||||
if self.bulk.strip() != "":
|
||||
self.nodes.append(HYSymbol(self.bulk))
|
||||
self.nodes.append(_resolve_atom(self.bulk))
|
||||
self.bulk = ""
|
||||
|
||||
def p(self, x):
|
||||
@ -85,12 +92,12 @@ class List(State):
|
||||
|
||||
def exit(self):
|
||||
if self.bulk:
|
||||
self.nodes.append(HYSymbol(self.bulk))
|
||||
self.nodes.append(_resolve_atom(self.bulk))
|
||||
self.machine.nodes.append(self.nodes)
|
||||
|
||||
def commit(self):
|
||||
if self.bulk.strip() != "":
|
||||
self.nodes.append(HYSymbol(self.bulk))
|
||||
self.nodes.append(_resolve_atom(self.bulk))
|
||||
self.bulk = ""
|
||||
|
||||
def p(self, x):
|
||||
@ -111,7 +118,7 @@ class Map(State):
|
||||
|
||||
def exit(self):
|
||||
if self.bulk:
|
||||
self.nodes.append(HYSymbol(self.bulk))
|
||||
self.nodes.append(_resolve_atom(self.bulk))
|
||||
|
||||
if (len(self.nodes) % 2) != 0:
|
||||
raise Exception("Hash map is fucked")
|
||||
@ -125,7 +132,7 @@ class Map(State):
|
||||
|
||||
def commit(self):
|
||||
if self.bulk.strip() != "":
|
||||
self.nodes.append(HYSymbol(self.bulk))
|
||||
self.nodes.append(_resolve_atom(self.bulk))
|
||||
self.bulk = ""
|
||||
|
||||
def p(self, x):
|
||||
|
@ -23,7 +23,7 @@ def test_fn_split():
|
||||
assert one.get_invocation() == {
|
||||
"function": "+",
|
||||
"args": [
|
||||
"1", "1"
|
||||
1, 1
|
||||
]
|
||||
}
|
||||
assert two.get_invocation() == {
|
||||
|
@ -4,15 +4,15 @@ from hy.lex.tokenize import tokenize
|
||||
def test_simple_tokenize():
|
||||
"""Checking we can still handle something simple."""
|
||||
|
||||
assert [["+", "1", "1"]] == tokenize("(+ 1 1)")
|
||||
assert [["+", 1, 1]] == tokenize("(+ 1 1)")
|
||||
|
||||
|
||||
def test_double_tokenize():
|
||||
"""Checking if we can lex two things at once."""
|
||||
|
||||
assert [
|
||||
["+", "1", "2"],
|
||||
["-", "1", "1"]
|
||||
["+", 1, 2],
|
||||
["-", 1, 1]
|
||||
] == tokenize("(+ 1 2) (- 1 1)")
|
||||
|
||||
|
||||
@ -59,15 +59,15 @@ def test_full_recurse():
|
||||
['fn',
|
||||
'el',
|
||||
['+',
|
||||
'1',
|
||||
'2',
|
||||
1,
|
||||
2,
|
||||
['==',
|
||||
'1',
|
||||
'20'
|
||||
1,
|
||||
20
|
||||
],
|
||||
['-',
|
||||
'1',
|
||||
'1'
|
||||
1,
|
||||
1
|
||||
],
|
||||
]
|
||||
],
|
||||
|
@ -5,7 +5,7 @@ def test_list_lex():
|
||||
"""test basic lexing of lists"""
|
||||
fn = tokenize("(fn [1 2 3 4])")[0]
|
||||
assert fn == [
|
||||
"fn", ["1", "2", "3", "4"]
|
||||
"fn", [1, 2, 3, 4]
|
||||
]
|
||||
|
||||
|
||||
@ -13,7 +13,7 @@ def test_list_recurse():
|
||||
""" test we can recurse lists """
|
||||
fn = tokenize("(fn [1 2 3 4 [5 6 7]])")[0]
|
||||
assert fn == [
|
||||
"fn", ["1", "2", "3", "4", ["5", "6", "7"]]
|
||||
"fn", [1, 2, 3, 4, [5, 6, 7]]
|
||||
]
|
||||
|
||||
|
||||
@ -21,7 +21,7 @@ def test_double_rainbow():
|
||||
""" DOUBLE LISTS """
|
||||
fn = tokenize("(fn [1 2 3 4] [5 6 7])")[0]
|
||||
assert fn == [
|
||||
"fn", ["1", "2", "3", "4"], ["5", "6", "7"]
|
||||
"fn", [1, 2, 3, 4], [5, 6, 7]
|
||||
]
|
||||
|
||||
|
||||
@ -29,7 +29,7 @@ def test_string_in_list():
|
||||
""" String in list """
|
||||
fn = tokenize('(fn [1 2 "three four" 5])')[0]
|
||||
assert fn == [
|
||||
"fn", ["1", "2", "three four", "5"]
|
||||
"fn", [1, 2, "three four", 5]
|
||||
]
|
||||
|
||||
|
||||
@ -40,5 +40,5 @@ def test_list_recurse_with_comment():
|
||||
2 3 4 [5 6 7]])
|
||||
""")[0]
|
||||
assert fn == [
|
||||
"fn", ["1", "2", "3", "4", ["5", "6", "7"]]
|
||||
"fn", [1, 2, 3, 4, [5, 6, 7]]
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user