Skip to content

Commit

Permalink
make: merge eval-ast into eval (part of kanaka#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon committed Feb 1, 2022
1 parent 0382550 commit dea2c90
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 268 deletions.
10 changes: 2 additions & 8 deletions impls/make/env.mk
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,8 @@ endef
# $(2): list/vector object of bind forms
# $(3): space separated list of expressions to bind
ENV = $(strip $(foreach new_env,$(call _assoc!,$(call _hash_map),__outer__,$(if $(1),$(1),$(__nil))),$(if $(2),$(call BIND_ARGS,$(new_env),$(2),$(3)),$(new_env))))
ENV_FIND = $(strip \
$(if $(call _contains?,$(1),$(subst =,$(__equal),$(2))),\
$(1),\
$(if $(call _EQ,$(__nil),$(call _get,$(1),__outer__)),\
,\
$(call ENV_FIND,$(call _get,$(1),__outer__),$(2)))))

ENV_GET = $(foreach env,|$(call ENV_FIND,$(1),$(2))|,$(if $(call _EQ,||,$(env)),$(call _error,'$(2)' not found)$(__nil),$(call _get,$(strip $(subst |,,$(env))),$(subst =,$(__equal),$(2)))))

ENV_GET = $(if $(call _EQ,$(1),$(__nil)),,$(or $(_get),$(call ENV_GET,$(call _get,$(1),__outer__),$(2))))

ENV_SET = $(if $(call _assoc!,$(1),$(subst =,$(__equal),$(2)),$(3)),$(1),)

Expand Down
28 changes: 10 additions & 18 deletions impls/make/step2_eval.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@ define READ
$(if $(READLINE_EOF)$(__ERROR),,$(call READ_STR,$(if $(1),$(1),$(call READLINE,"user> "))))
endef

define EVAL_INVOKE
$(if $(__ERROR),,\
$(and $(EVAL_DEBUG),$(info EVAL_INVOKE: $(call _pr_str,$(1))))\
$(foreach el,$(call _smap,EVAL,$(1),$(2)),\
$(call _apply,$(call sfirst,$(el)),$(call srest,$(el)))))
endef

# EVAL: evaluate the parameter
define EVAL_AST
$(strip \
define EVAL
$(strip $(if $(__ERROR),,\
$(and $(EVAL_DEBUG),$(info EVAL_AST: $(call _pr_str,$(1))))\
$(if $(call _symbol?,$(1)),\
$(foreach key,$($(1)_value),\
$(if $(call _contains?,$(2),$(key)),\
$(call _get,$(2),$(key)),\
$(call _error,'$(key)' not found in REPL_ENV))),\
$(if $(call _list?,$(1)),\
$(call _smap,EVAL,$(1),$(2)),\
$(if $(call _vector?,$(1)),\
$(call _smap_vec,EVAL,$(1),$(2)),\
$(if $(call _hash_map?,$(1)),\
Expand All @@ -35,24 +40,11 @@ $(strip \
$(eval $(v:$(1)_%=$(new_hmap)_%) := $(call EVAL,$($(v)),$(2))))\
$(eval $(new_hmap)_size := $($(1)_size))\
$(new_hmap)),\
$(1))))))
endef

define EVAL_INVOKE
$(if $(__ERROR),,\
$(and $(EVAL_DEBUG),$(info EVAL_INVOKE: $(call _pr_str,$(1))))\
$(foreach el,$(call EVAL_AST,$(1),$(2)),\
$(call _apply,$(call sfirst,$(el)),$(call srest,$(el)))))
endef

define EVAL
$(strip $(if $(__ERROR),,\
$(and $(EVAL_DEBUG),$(info EVAL: $(call _pr_str,$(1))))\
$(if $(call _list?,$(1)),\
$(if $(call _EQ,0,$(call _count,$(1))),\
$(1),\
$(strip $(call EVAL_INVOKE,$(1),$(2)))),\
$(call EVAL_AST,$(1),$(2)))))
$(1)))))))
endef


Expand Down
40 changes: 17 additions & 23 deletions impls/make/step3_env.mk
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,6 @@ $(strip \
$(call LET,$(left),$(2))))))))
endef

define EVAL_AST
$(strip \
$(and $(EVAL_DEBUG),$(info EVAL_AST: $(call _pr_str,$(1))))\
$(if $(call _symbol?,$(1)),\
$(foreach key,$($(1)_value),\
$(call ENV_GET,$(2),$(key))),\
$(if $(call _list?,$(1)),\
$(call _smap,EVAL,$(1),$(2)),\
$(if $(call _vector?,$(1)),\
$(call _smap_vec,EVAL,$(1),$(2)),\
$(if $(call _hash_map?,$(1)),\
$(foreach new_hmap,$(call __new_obj,hmap),\
$(foreach v,$(call __get_obj_values,$(1)),\
$(eval $(v:$(1)_%=$(new_hmap)_%) := $(call EVAL,$($(v)),$(2))))\
$(eval $(new_hmap)_size := $($(1)_size))\
$(new_hmap)),\
$(1))))))
endef

define EVAL_INVOKE
$(if $(__ERROR),,\
$(and $(EVAL_DEBUG),$(info EVAL_INVOKE: $(call _pr_str,$(1))))
Expand All @@ -63,18 +44,31 @@ $(if $(__ERROR),,\
$(foreach a1,$(call _nth,$(1),1),\
$(foreach a2,$(call _nth,$(1),2),\
$(call EVAL,$(a2),$(call LET,$(a1),$(call ENV,$(2)))))),\
$(foreach el,$(call EVAL_AST,$(1),$(2)),\
$(foreach el,$(call _smap,EVAL,$(1),$(2)),\
$(call _apply,$(call sfirst,$(el)),$(call srest,$(el))))))))
endef

define EVAL
$(strip $(if $(__ERROR),,\
$(and $(EVAL_DEBUG),$(info EVAL: $(call _pr_str,$(1))))\
$(if $(filter-out false nil,$(call _obj_type,$(or $(call ENV_GET,$(2),DEBUG-EVAL),$(__nil)))),\
$(info EVAL: $(_pr_str)))\
$(if $(call _symbol?,$(1)),\
$(foreach key,$($(1)_value),\
$(or $(call ENV_GET,$(2),$(key)),\
$(call _error,'$(key)' not found)$(__nil))),\
$(if $(call _vector?,$(1)),\
$(call _smap_vec,EVAL,$(1),$(2)),\
$(if $(call _hash_map?,$(1)),\
$(foreach new_hmap,$(call __new_obj,hmap),\
$(foreach v,$(call __get_obj_values,$(1)),\
$(eval $(v:$(1)_%=$(new_hmap)_%) := $(call EVAL,$($(v)),$(2))))\
$(eval $(new_hmap)_size := $($(1)_size))\
$(new_hmap)),\
$(if $(call _list?,$(1)),\
$(if $(call _EQ,0,$(call _count,$(1))),\
$(1),\
$(strip $(call EVAL_INVOKE,$(1),$(2)))),\
$(call EVAL_AST,$(1),$(2)))))
$(word 1,$(strip $(call EVAL_INVOKE,$(1),$(2)) $(__nil)))),\
$(1)))))))
endef


Expand Down
40 changes: 17 additions & 23 deletions impls/make/step4_if_fn_do.mk
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,6 @@ $(strip \
$(call LET,$(left),$(2))))))))
endef

define EVAL_AST
$(strip \
$(and $(EVAL_DEBUG),$(info EVAL_AST: $(call _pr_str,$(1))))\
$(if $(call _symbol?,$(1)),\
$(foreach key,$($(1)_value),\
$(call ENV_GET,$(2),$(key))),\
$(if $(call _list?,$(1)),\
$(call _smap,EVAL,$(1),$(2)),\
$(if $(call _vector?,$(1)),\
$(call _smap_vec,EVAL,$(1),$(2)),\
$(if $(call _hash_map?,$(1)),\
$(foreach new_hmap,$(call __new_obj,hmap),\
$(foreach v,$(call __get_obj_values,$(1)),\
$(eval $(v:$(1)_%=$(new_hmap)_%) := $(call EVAL,$($(v)),$(2))))\
$(eval $(new_hmap)_size := $($(1)_size))\
$(new_hmap)),\
$(1))))))
endef

define EVAL_INVOKE
$(if $(__ERROR),,\
$(and $(EVAL_DEBUG),$(info EVAL_INVOKE: $(call _pr_str,$(1))))
Expand All @@ -64,7 +45,7 @@ $(if $(__ERROR),,\
$(foreach a2,$(call _nth,$(1),2),\
$(call EVAL,$(a2),$(call LET,$(a1),$(call ENV,$(2)))))),\
$(if $(call _EQ,do,$($(a0)_value)),\
$(call slast,$(call EVAL_AST,$(call srest,$(1)),$(2))),\
$(call slast,$(call _smap,EVAL,$(call srest,$(1)),$(2))),\
$(if $(call _EQ,if,$($(a0)_value)),\
$(foreach a1,$(call _nth,$(1),1),\
$(foreach a2,$(call _nth,$(1),2),\
Expand All @@ -76,7 +57,7 @@ $(if $(__ERROR),,\
$(foreach a1,$(call _nth,$(1),1),\
$(foreach a2,$(call _nth,$(1),2),\
$(call _function,$$(call EVAL,$(a2),$$(call ENV,$(2),$(a1),$$1))))),\
$(foreach el,$(call EVAL_AST,$(1),$(2)),\
$(foreach el,$(call _smap,EVAL,$(1),$(2)),\
$(and $(EVAL_DEBUG),$(info invoke: $(call _pr_str,$(el))))\
$(foreach f,$(call sfirst,$(el)),\
$(foreach args,$(call srest,$(el)),\
Expand All @@ -85,12 +66,25 @@ endef

define EVAL
$(strip $(if $(__ERROR),,\
$(and $(EVAL_DEBUG),$(info EVAL: $(call _pr_str,$(1))))\
$(if $(filter-out false nil,$(call _obj_type,$(or $(call ENV_GET,$(2),DEBUG-EVAL),$(__nil)))),\
$(info EVAL: $(_pr_str)))\
$(if $(call _symbol?,$(1)),\
$(foreach key,$($(1)_value),\
$(or $(call ENV_GET,$(2),$(key)),\
$(call _error,'$(key)' not found)$(__nil))),\
$(if $(call _vector?,$(1)),\
$(call _smap_vec,EVAL,$(1),$(2)),\
$(if $(call _hash_map?,$(1)),\
$(foreach new_hmap,$(call __new_obj,hmap),\
$(foreach v,$(call __get_obj_values,$(1)),\
$(eval $(v:$(1)_%=$(new_hmap)_%) := $(call EVAL,$($(v)),$(2))))\
$(eval $(new_hmap)_size := $($(1)_size))\
$(new_hmap)),\
$(if $(call _list?,$(1)),\
$(if $(call _EQ,0,$(call _count,$(1))),\
$(1),\
$(word 1,$(strip $(call EVAL_INVOKE,$(1),$(2)) $(__nil)))),\
$(call EVAL_AST,$(1),$(2)))))
$(1)))))))
endef


Expand Down
40 changes: 17 additions & 23 deletions impls/make/step6_file.mk
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,6 @@ $(strip \
$(call LET,$(left),$(2))))))))
endef

define EVAL_AST
$(strip \
$(and $(EVAL_DEBUG),$(info EVAL_AST: $(call _pr_str,$(1))))\
$(if $(call _symbol?,$(1)),\
$(foreach key,$($(1)_value),\
$(call ENV_GET,$(2),$(key))),\
$(if $(call _list?,$(1)),\
$(call _smap,EVAL,$(1),$(2)),\
$(if $(call _vector?,$(1)),\
$(call _smap_vec,EVAL,$(1),$(2)),\
$(if $(call _hash_map?,$(1)),\
$(foreach new_hmap,$(call __new_obj,hmap),\
$(foreach v,$(call __get_obj_values,$(1)),\
$(eval $(v:$(1)_%=$(new_hmap)_%) := $(call EVAL,$($(v)),$(2))))\
$(eval $(new_hmap)_size := $($(1)_size))\
$(new_hmap)),\
$(1))))))
endef

define EVAL_INVOKE
$(if $(__ERROR),,\
$(and $(EVAL_DEBUG),$(info EVAL_INVOKE: $(call _pr_str,$(1))))
Expand All @@ -64,7 +45,7 @@ $(if $(__ERROR),,\
$(foreach a2,$(call _nth,$(1),2),\
$(call EVAL,$(a2),$(call LET,$(a1),$(call ENV,$(2)))))),\
$(if $(call _EQ,do,$($(a0)_value)),\
$(call slast,$(call EVAL_AST,$(call srest,$(1)),$(2))),\
$(call slast,$(call _smap,EVAL,$(call srest,$(1)),$(2))),\
$(if $(call _EQ,if,$($(a0)_value)),\
$(foreach a1,$(call _nth,$(1),1),\
$(foreach a2,$(call _nth,$(1),2),\
Expand All @@ -76,7 +57,7 @@ $(if $(__ERROR),,\
$(foreach a1,$(call _nth,$(1),1),\
$(foreach a2,$(call _nth,$(1),2),\
$(call _function,$$(call EVAL,$(a2),$$(call ENV,$(2),$(a1),$$1))))),\
$(foreach el,$(call EVAL_AST,$(1),$(2)),\
$(foreach el,$(call _smap,EVAL,$(1),$(2)),\
$(and $(EVAL_DEBUG),$(info invoke: $(call _pr_str,$(el))))\
$(foreach f,$(call sfirst,$(el)),\
$(foreach args,$(call srest,$(el)),\
Expand All @@ -85,12 +66,25 @@ endef

define EVAL
$(strip $(if $(__ERROR),,\
$(and $(EVAL_DEBUG),$(info EVAL: $(call _pr_str,$(1))))\
$(if $(filter-out false nil,$(call _obj_type,$(or $(call ENV_GET,$(2),DEBUG-EVAL),$(__nil)))),\
$(info EVAL: $(_pr_str)))\
$(if $(call _symbol?,$(1)),\
$(foreach key,$($(1)_value),\
$(or $(call ENV_GET,$(2),$(key)),\
$(call _error,'$(key)' not found)$(__nil))),\
$(if $(call _vector?,$(1)),\
$(call _smap_vec,EVAL,$(1),$(2)),\
$(if $(call _hash_map?,$(1)),\
$(foreach new_hmap,$(call __new_obj,hmap),\
$(foreach v,$(call __get_obj_values,$(1)),\
$(eval $(v:$(1)_%=$(new_hmap)_%) := $(call EVAL,$($(v)),$(2))))\
$(eval $(new_hmap)_size := $($(1)_size))\
$(new_hmap)),\
$(if $(call _list?,$(1)),\
$(if $(call _EQ,0,$(call _count,$(1))),\
$(1),\
$(word 1,$(strip $(call EVAL_INVOKE,$(1),$(2)) $(__nil)))),\
$(call EVAL_AST,$(1),$(2)))))
$(1)))))))
endef


Expand Down
44 changes: 18 additions & 26 deletions impls/make/step7_quote.mk
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,6 @@ $(strip \
$(call LET,$(left),$(2))))))))
endef

define EVAL_AST
$(strip \
$(and $(EVAL_DEBUG),$(info EVAL_AST: $(call _pr_str,$(1))))\
$(if $(call _symbol?,$(1)),\
$(foreach key,$($(1)_value),\
$(call ENV_GET,$(2),$(key))),\
$(if $(call _list?,$(1)),\
$(call _smap,EVAL,$(1),$(2)),\
$(if $(call _vector?,$(1)),\
$(call _smap_vec,EVAL,$(1),$(2)),\
$(if $(call _hash_map?,$(1)),\
$(foreach new_hmap,$(call __new_obj,hmap),\
$(foreach v,$(call __get_obj_values,$(1)),\
$(eval $(v:$(1)_%=$(new_hmap)_%) := $(call EVAL,$($(v)),$(2))))\
$(eval $(new_hmap)_size := $($(1)_size))\
$(new_hmap)),\
$(1))))))
endef

define EVAL_INVOKE
$(if $(__ERROR),,\
$(and $(EVAL_DEBUG),$(info EVAL_INVOKE: $(call _pr_str,$(1))))
Expand All @@ -84,12 +65,10 @@ $(if $(__ERROR),,\
$(call EVAL,$(a2),$(call LET,$(a1),$(call ENV,$(2)))))),\
$(if $(call _EQ,quote,$($(a0)_value)),\
$(call _nth,$(1),1),\
$(if $(call _EQ,quasiquoteexpand,$($(a0)_value)),\
$(call QUASIQUOTE,$(call _nth,$(1),1)),\
$(if $(call _EQ,quasiquote,$($(a0)_value)),\
$(call EVAL,$(call QUASIQUOTE,$(call _nth,$(1),1)),$(2)),\
$(if $(call _EQ,do,$($(a0)_value)),\
$(call slast,$(call EVAL_AST,$(call srest,$(1)),$(2))),\
$(call slast,$(call _smap,EVAL,$(call srest,$(1)),$(2))),\
$(if $(call _EQ,if,$($(a0)_value)),\
$(foreach a1,$(call _nth,$(1),1),\
$(foreach a2,$(call _nth,$(1),2),\
Expand All @@ -101,21 +80,34 @@ $(if $(__ERROR),,\
$(foreach a1,$(call _nth,$(1),1),\
$(foreach a2,$(call _nth,$(1),2),\
$(call _function,$$(call EVAL,$(a2),$$(call ENV,$(2),$(a1),$$1))))),\
$(foreach el,$(call EVAL_AST,$(1),$(2)),\
$(foreach el,$(call _smap,EVAL,$(1),$(2)),\
$(and $(EVAL_DEBUG),$(info invoke: $(call _pr_str,$(el))))\
$(foreach f,$(call sfirst,$(el)),\
$(foreach args,$(call srest,$(el)),\
$(call apply,$(f),$(args)))))))))))))))
$(call apply,$(f),$(args))))))))))))))
endef

define EVAL
$(strip $(if $(__ERROR),,\
$(and $(EVAL_DEBUG),$(info EVAL: $(call _pr_str,$(1))))\
$(if $(filter-out false nil,$(call _obj_type,$(or $(call ENV_GET,$(2),DEBUG-EVAL),$(__nil)))),\
$(info EVAL: $(_pr_str)))\
$(if $(call _symbol?,$(1)),\
$(foreach key,$($(1)_value),\
$(or $(call ENV_GET,$(2),$(key)),\
$(call _error,'$(key)' not found)$(__nil))),\
$(if $(call _vector?,$(1)),\
$(call _smap_vec,EVAL,$(1),$(2)),\
$(if $(call _hash_map?,$(1)),\
$(foreach new_hmap,$(call __new_obj,hmap),\
$(foreach v,$(call __get_obj_values,$(1)),\
$(eval $(v:$(1)_%=$(new_hmap)_%) := $(call EVAL,$($(v)),$(2))))\
$(eval $(new_hmap)_size := $($(1)_size))\
$(new_hmap)),\
$(if $(call _list?,$(1)),\
$(if $(call _EQ,0,$(call _count,$(1))),\
$(1),\
$(word 1,$(strip $(call EVAL_INVOKE,$(1),$(2)) $(__nil)))),\
$(call EVAL_AST,$(1),$(2)))))
$(1)))))))
endef


Expand Down
Loading

0 comments on commit dea2c90

Please sign in to comment.