2021-08-09 23:58:40 -04:00
|
|
|
(with_import "./collections.kp"
|
|
|
|
|
(let (
|
|
|
|
|
; This partial eval only works via the root env,
|
|
|
|
|
; since we can't tell from outside if something takes an env
|
|
|
|
|
; or not
|
|
|
|
|
; Also, based on that, we have to switch on every primitive...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; Here is every form in k'
|
|
|
|
|
; True
|
|
|
|
|
; False
|
|
|
|
|
; Env: *KPEnv
|
|
|
|
|
; Combiner: KPCombiner / BuiltinCombiner: KPBuiltinCombiner
|
|
|
|
|
; String: str
|
|
|
|
|
; Symbol: str
|
|
|
|
|
; Int: int
|
|
|
|
|
; Array: rc<vec<KPValue>>
|
|
|
|
|
; Nil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; Ok, some more things we need / need to change
|
|
|
|
|
|
|
|
|
|
; Arrays need to be able to contain some combination of now and later that
|
|
|
|
|
; can be extracted correctly by the relevent operators (idx, slice)
|
|
|
|
|
|
|
|
|
|
; Vau needs to be handled, obv, which will require a handling of it
|
|
|
|
|
; pre-evaluating the parameters. The more I think about it, the more
|
|
|
|
|
; straight forward it seems actually - it can mostly mirror actual vau?
|
|
|
|
|
|
|
|
|
|
; how should empty-env be handled? More generally, how much do we reify enviornments?
|
|
|
|
|
|
|
|
|
|
; meta...
|
|
|
|
|
|
|
|
|
|
; I think now/later will have to be split into
|
|
|
|
|
; later/now_value/array/comb(wrap_level / dynamic_env_usage?)/env
|
|
|
|
|
; Env will need special checking for escaping, including when contained inside of an array or other combinator
|
|
|
|
|
; Actually, that extends to checking escaping for combinators in general, since they may close over laters
|
|
|
|
|
|
2021-08-10 22:56:12 -04:00
|
|
|
; Ok, instead of just ['now v] and ['later v], we have these marked values
|
|
|
|
|
; ['val v] (v can be an array)
|
|
|
|
|
; ['later c]
|
|
|
|
|
; ['marked_array a] (a contains marked values)
|
|
|
|
|
; ['comb wrap_level de? se params body <actual_function>]
|
|
|
|
|
; ['prim_comb <handler_function> <actual_function>]
|
|
|
|
|
; ['env [ ['symbol marked_value ]... ] <name_it_can_be_found_as(changes, must not escape)>]
|
2021-08-09 23:58:40 -04:00
|
|
|
|
2021-08-10 22:56:12 -04:00
|
|
|
|
|
|
|
|
val? (lambda (x) (= 'val (idx x 0)))
|
|
|
|
|
.val (lambda (x) (idx x 1))
|
|
|
|
|
later? (lambda (x) (= 'later (idx x 0)))
|
|
|
|
|
.later (lambda (x) (idx x 1))
|
|
|
|
|
marked_array? (lambda (x) (= 'marked_array (idx x 0)))
|
|
|
|
|
comb? (lambda (x) (= 'comb (idx x 0)))
|
|
|
|
|
.comb (lambda (x) (slice x 1 -1))
|
|
|
|
|
prim_comb? (lambda (x) (= 'prim_comb (idx x 0)))
|
|
|
|
|
.prim_comb (lambda (x) (idx x 1))
|
|
|
|
|
marked_env? (lambda (x) (= 'env (idx x 0)))
|
|
|
|
|
|
|
|
|
|
strip (rec-lambda recurse (x)
|
|
|
|
|
(cond (val? x) (.val x)
|
|
|
|
|
(later? x) (.later x)
|
|
|
|
|
(marked_array? x) (map recurse (idx x 1))
|
|
|
|
|
(comb? x) (idx x 6)
|
|
|
|
|
(prim_comb? x) (idx x 2)
|
|
|
|
|
(makred_env? x) (error "Env escaped to strip!")
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
; partial_eval_helper always takes in unmarked expressions and makes marked ones, maintaining marked envs
|
2021-08-09 23:58:40 -04:00
|
|
|
partial_eval_helper (rec-lambda recurse (x env)
|
2021-08-10 22:56:12 -04:00
|
|
|
(cond (= x true) ['val true ]
|
|
|
|
|
(= x false) ['val false]
|
2021-08-09 23:58:40 -04:00
|
|
|
(env? x) (error "called partial_eval with an env " x)
|
|
|
|
|
(combiner? x) (error "called partial_eval with a combiner, not yet supported (assuming just parsed symbols etc) " x)
|
2021-08-10 22:56:12 -04:00
|
|
|
(string? x) ['val x]
|
2021-08-09 23:58:40 -04:00
|
|
|
(symbol? x) (get-value env x)
|
2021-08-10 22:56:12 -04:00
|
|
|
(int? x) ['val x]
|
2021-08-09 23:58:40 -04:00
|
|
|
(and (array? x) (= 0 (len x))) (error "Partial eval on empty array")
|
2021-08-10 22:56:12 -04:00
|
|
|
(array? x) (let ( comb (recurse (idx x 0) env) )
|
|
|
|
|
(cond (later? comb) ['later x]
|
|
|
|
|
(prim_comb? comb) ((.prim_comb comb) env (slice x 1 -1))
|
|
|
|
|
(comb? comb) (let (
|
|
|
|
|
[wrap_level de? se params body actual_function] (.comb comb)
|
|
|
|
|
;subs (map (lambda (y) (recurse y env)) x)
|
|
|
|
|
) ['later x])
|
|
|
|
|
true (error (str "Partial eval noticed that you will likely call not a function " x))))
|
|
|
|
|
(nil? x) ['val x]
|
2021-08-09 23:58:40 -04:00
|
|
|
|
|
|
|
|
)
|
|
|
|
|
)
|
2021-08-10 22:56:12 -04:00
|
|
|
needs_params_val_lambda (vau de (f_sym) (let (
|
|
|
|
|
actual_function (eval f_sym de)
|
|
|
|
|
handler (lambda (de params) (let (evaled_params (map (lambda (x) (partial_eval_helper x de)) params))
|
|
|
|
|
(if (foldl (lambda (a x) (and a (val? x))) true evaled_params) ['val (lapply actual_function (map .val evaled_params))]
|
|
|
|
|
['later (cons actual_function params)])))
|
|
|
|
|
) [f_sym ['prim_comb handler actual_function]]))
|
|
|
|
|
|
2021-08-09 23:58:40 -04:00
|
|
|
partial_eval (lambda (x) (partial_eval_helper x [
|
|
|
|
|
; vau
|
|
|
|
|
; eval
|
|
|
|
|
; cond
|
2021-08-10 22:56:12 -04:00
|
|
|
(needs_params_val_lambda symbol?)
|
|
|
|
|
(needs_params_val_lambda int?)
|
|
|
|
|
(needs_params_val_lambda string?)
|
2021-08-09 23:58:40 -04:00
|
|
|
; combiner?
|
|
|
|
|
; env?
|
2021-08-10 22:56:12 -04:00
|
|
|
(needs_params_val_lambda nil?)
|
|
|
|
|
(needs_params_val_lambda bool?)
|
2021-08-09 23:58:40 -04:00
|
|
|
; array?
|
2021-08-10 22:56:12 -04:00
|
|
|
(needs_params_val_lambda str-to-symbol)
|
|
|
|
|
(needs_params_val_lambda get-text)
|
2021-08-09 23:58:40 -04:00
|
|
|
; array
|
|
|
|
|
; len
|
|
|
|
|
; idx
|
|
|
|
|
; slice
|
|
|
|
|
; concat
|
2021-08-10 22:56:12 -04:00
|
|
|
(needs_params_val_lambda +)
|
|
|
|
|
(needs_params_val_lambda -)
|
|
|
|
|
(needs_params_val_lambda *)
|
|
|
|
|
(needs_params_val_lambda /)
|
|
|
|
|
(needs_params_val_lambda %)
|
|
|
|
|
(needs_params_val_lambda &)
|
|
|
|
|
(needs_params_val_lambda |)
|
|
|
|
|
(needs_params_val_lambda <<)
|
|
|
|
|
(needs_params_val_lambda >>)
|
|
|
|
|
(needs_params_val_lambda =)
|
|
|
|
|
(needs_params_val_lambda !=)
|
|
|
|
|
(needs_params_val_lambda <)
|
|
|
|
|
(needs_params_val_lambda <=)
|
|
|
|
|
(needs_params_val_lambda >)
|
|
|
|
|
(needs_params_val_lambda >=)
|
2021-08-09 23:58:40 -04:00
|
|
|
|
|
|
|
|
; Don't forget, these short-circut with the truthy/falsey values
|
|
|
|
|
; and
|
|
|
|
|
; or
|
|
|
|
|
|
|
|
|
|
; pr-str
|
2021-08-10 22:56:12 -04:00
|
|
|
(needs_params_val_lambda str)
|
|
|
|
|
(needs_params_val_lambda prn)
|
2021-08-09 23:58:40 -04:00
|
|
|
; println
|
|
|
|
|
; meta
|
|
|
|
|
; with-meta
|
|
|
|
|
; wrap
|
|
|
|
|
; unwrap
|
|
|
|
|
; error
|
|
|
|
|
; recover
|
|
|
|
|
; read-string
|
|
|
|
|
; slurp
|
|
|
|
|
; get_line
|
|
|
|
|
; write_file
|
|
|
|
|
; empty_env
|
|
|
|
|
]))
|
|
|
|
|
)
|
2021-08-10 22:56:12 -04:00
|
|
|
(provide partial_eval strip)
|
2021-08-09 23:58:40 -04:00
|
|
|
))
|