Skip to content

Commit

Permalink
python.2: fix self-host by adding fn?, macro?.
Browse files Browse the repository at this point in the history
Also add number? and string? which aren't technically required for
self-host but are easy to implement.
  • Loading branch information
kanaka committed Aug 14, 2024
1 parent c4ab84b commit 0d32585
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions impls/python.2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,12 @@ def nth(list_: MalExpression, index: MalExpression) -> MalExpression:
raise MalIndexError(index.native())
return list_native[index.native()]

def _callable(arg: MalExpression):
return isinstance(arg, (MalFunctionCompiled, MalFunctionRaw))

def apply(args: List[MalExpression]) -> MalExpression:
func = args[0]
assert isinstance(func, MalFunctionCompiled) or isinstance(func, MalFunctionRaw)
assert _callable(func)
rest_args: List[MalExpression] = []
for i in range(1, len(args) - 1):
rest_args.append(args[i])
Expand All @@ -175,7 +177,7 @@ def apply(args: List[MalExpression]) -> MalExpression:


def map_(func: MalExpression, map_list: MalExpression) -> MalExpression:
assert isinstance(func, MalFunctionCompiled) or isinstance(func, MalFunctionRaw)
assert _callable(func)
assert isinstance(map_list, MalList) or isinstance(map_list, MalVector)
result_list: List[MalExpression] = []
for i in range(len(map_list.native())):
Expand Down Expand Up @@ -229,6 +231,17 @@ def readline(arg: MalExpression) -> Union[MalString, MalNil]:
return MalNil()
return MalString(line)

def fn_q(arg: MalExpression) -> MalExpression:
return MalBoolean(_callable(arg) and not arg.is_macro())

def macro_q(arg: MalExpression) -> MalExpression:
return MalBoolean(_callable(arg) and arg.is_macro())

def string_q(arg: MalExpression) -> MalExpression:
return MalBoolean(isinstance(arg, MalString) and not arg.is_keyword())

def number_q(arg: MalExpression) -> MalExpression:
return MalBoolean(isinstance(arg, MalInt))

def not_implemented(func: str) -> MalExpression:
raise MalNotImplementedException(func)
Expand Down Expand Up @@ -355,7 +368,7 @@ def swap(args: List[MalExpression]) -> MalExpression:
atom = args[0]
assert isinstance(atom, MalAtom)
func = args[1]
assert isinstance(func, MalFunctionCompiled) or isinstance(func, MalFunctionRaw)
assert _callable(func)
atom.reset(func.call([atom.native()] + args[2:]))
return atom.native()

Expand Down Expand Up @@ -403,9 +416,10 @@ def swap(args: List[MalExpression]) -> MalExpression:
"time-ms": MalFunctionCompiled(lambda args: MalInt(int(time.time() * 1000))),
"meta": MalFunctionCompiled(lambda args: not_implemented("meta")),
"with-meta": MalFunctionCompiled(lambda args: not_implemented("with-meta")),
"fn?": MalFunctionCompiled(lambda args: not_implemented("fn?")),
"string?": MalFunctionCompiled(lambda args: not_implemented("string?")),
"number?": MalFunctionCompiled(lambda args: not_implemented("number?")),
"fn?": MalFunctionCompiled(lambda args: fn_q(args[0])),
"macro?": MalFunctionCompiled(lambda args: macro_q(args[0])),
"string?": MalFunctionCompiled(lambda args: string_q(args[0])),
"number?": MalFunctionCompiled(lambda args: number_q(args[0])),
"seq": MalFunctionCompiled(lambda args: not_implemented("seq")),
"conj": MalFunctionCompiled(lambda args: not_implemented("conj")),
"get": MalFunctionCompiled(lambda args: get(args[0], args[1])),
Expand Down

0 comments on commit 0d32585

Please sign in to comment.