From 94f60efe5745accce36c5d6824c71f00a12dd384 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Thu, 24 Jan 2019 00:16:16 -0400 Subject: Added dynamic typing. --- stdlib/source/lux/type/dynamic.lux | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 stdlib/source/lux/type/dynamic.lux (limited to 'stdlib/source') diff --git a/stdlib/source/lux/type/dynamic.lux b/stdlib/source/lux/type/dynamic.lux new file mode 100644 index 000000000..d57669213 --- /dev/null +++ b/stdlib/source/lux/type/dynamic.lux @@ -0,0 +1,39 @@ +(.module: + [lux #* + [control + ["ex" exception (#+ exception:)]] + [data + ["." error] + [text + format]] + [macro (#+ with-gensyms) + ["." syntax (#+ syntax:)]] + ["." type + abstract]]) + +(exception: #export (wrong-type {expected Type} {actual Type}) + (ex.report ["Expected" (%type expected)] + ["Actual" (%type actual)])) + +(abstract: #export Dynamic + {} + + [Type Any] + + (def: dynamic-abstraction (-> [Type Any] Dynamic) (|>> :abstraction)) + (def: dynamic-representation (-> Dynamic [Type Any]) (|>> :representation)) + + (syntax: #export (:dynamic value) + (with-gensyms [g!value] + (wrap (list (` (let [(~ g!value) (~ value)] + ((~! ..dynamic-abstraction) [(:of (~ g!value)) (~ g!value)]))))))) + + (syntax: #export (:check type value) + (with-gensyms [g!type g!value] + (wrap (list (` (let [[(~ g!type) (~ g!value)] ((~! ..dynamic-representation) (~ value))] + (: ((~! error.Error) (~ type)) + (if (:: (~! type.Equivalence) (~' =) + (.type (~ type)) (~ g!type)) + (#error.Success (:coerce (~ type) (~ g!value))) + ((~! ex.throw) ..wrong-type [(.type (~ type)) (~ g!type)]))))))))) + ) -- cgit v1.2.3