(.using [library [lux "*" ["@" target] ["[0]" meta] [abstract [monad {"+" do}]] [control ["[0]" io] ["[0]" maybe] ["<>" parser ("[1]#[0]" monad) ["<[0]>" code {"+" Parser}]]] [data ["[0]" product] ["[0]" text ["%" format]] [collection ["[0]" list ("[1]#[0]" functor mix)]]] [type abstract] [macro {"+" with_symbols} [syntax {"+" syntax:}] ["[0]" code] ["[0]" template]]]]) (abstract: .public (Object brand) Any) (template [] [(with_expansions [ (template.symbol [ "'"])] (abstract: Any) (type: .public (..Object )))] [Nil] [Function] [Table] ) (template [ ] [(type: .public )] [Boolean Bit] [Integer Int] [Float Frac] [String Text] ) (type: Nilable [Bit Code]) (def: nilable (Parser Nilable) (let [token (' "?")] (<| (<>.and (<>.parses? (.this! token))) (<>.after (<>.not (.this! token))) .any))) (type: Field [Bit Text Nilable]) (def: static! (Parser Any) (.this! (' "static"))) (def: field (Parser Field) (.form ($_ <>.and (<>.parses? ..static!) .local_symbol ..nilable))) (def: constant (Parser Field) (.form ($_ <>.and (<>#in true) .local_symbol ..nilable))) (type: Common_Method (Record [#name Text #alias (Maybe Text) #inputs (List Nilable) #io? Bit #try? Bit #output Nilable])) (type: Static_Method Common_Method) (type: Virtual_Method Common_Method) (type: Method (Variant {#Static Static_Method} {#Virtual Virtual_Method})) (def: common_method (Parser Common_Method) ($_ <>.and .local_symbol (<>.maybe (<>.after (.this! (' "as")) .local_symbol)) (.tuple (<>.some ..nilable)) (<>.parses? (.this! (' "io"))) (<>.parses? (.this! (' "try"))) ..nilable)) (def: static_method (<>.after ..static! ..common_method)) (def: method (Parser Method) (.form (<>.or ..static_method ..common_method))) (type: Member (Variant {#Field Field} {#Method Method})) (def: member (Parser Member) ($_ <>.or ..field ..method )) (def: input_variables (-> (List Nilable) (List [Bit Code])) (|>> list.enumeration (list#each (function (_ [idx [nilable? type]]) [nilable? (|> idx %.nat code.local_symbol)])))) (def: (nilable_type [nilable? type]) (-> Nilable Code) (if nilable? (` (.Maybe (~ type))) type)) (def: (with_nil g!temp [nilable? input]) (-> Code [Bit Code] Code) (if nilable? (` (case (~ input) {.#Some (~ g!temp)} (~ g!temp) {.#None} ("lua object nil"))) input)) (def: (without_nil g!temp [nilable? outputT] output) (-> Code Nilable Code Code) (if nilable? (` (let [(~ g!temp) (~ output)] (if ("lua object nil?" (~ g!temp)) {.#None} {.#Some (~ g!temp)}))) (` (let [(~ g!temp) (~ output)] (if (not ("lua object nil?" (~ g!temp))) (~ g!temp) (.panic! "Nil is an invalid value!")))))) (type: Import (Variant {#Class [Text Text (List Member)]} {#Function Static_Method} {#Constant Field})) (def: import ($_ <>.or (<>.and .local_symbol (<>.else ["" (list)] (.tuple (<>.and .text (<>.some member))))) (.form ..common_method) ..constant )) (def: (with_io with? without) (-> Bit Code Code) (if with? (` ((~! io.io) (~ without))) without)) (def: (io_type io? rawT) (-> Bit Code Code) (if io? (` ((~! io.IO) (~ rawT))) rawT)) (def: (with_try with? without_try) (-> Bit Code Code) (if with? (` (.try (~ without_try))) without_try)) (def: (try_type try? rawT) (-> Bit Code Code) (if try? (` (.Either .Text (~ rawT))) rawT)) (def: (make_function g!method g!temp source inputsT io? try? outputT) (-> Code Code Code (List Nilable) Bit Bit Nilable Code) (let [g!inputs (input_variables inputsT)] (` (def: ((~ g!method) [(~+ (list#each product.right g!inputs))]) (-> [(~+ (list#each nilable_type inputsT))] (~ (|> (nilable_type outputT) (try_type try?) (io_type io?)))) (:expected (~ (<| (with_io io?) (with_try try?) (without_nil g!temp outputT) (` ("lua apply" (:as ..Function (~ source)) (~+ (list#each (with_nil g!temp) g!inputs))))))))))) (syntax: .public (import: [import ..import]) (with_symbols [g!temp] (case import {#Class [class format members]} (with_symbols [g!object] (let [qualify (: (-> Text Code) (function (_ member_name) (|> format (text.replaced "[1]" class) (text.replaced "[0]" member_name) code.local_symbol))) g!type (code.local_symbol class) real_class (text.replaced "/" "." class) imported (case (text.all_split_by "/" class) {.#Item head tail} (list#mix (function (_ sub super) (` ("lua object get" (~ (code.text sub)) (:as (..Object .Any) (~ super))))) (` ("lua import" (~ (code.text head)))) tail) {.#End} (` ("lua import" (~ (code.text class)))))] (in (list& (` (type: (~ g!type) (..Object (Primitive (~ (code.text real_class)))))) (list#each (function (_ member) (case member {#Field [static? field fieldT]} (if static? (` ((~! syntax:) ((~ (qualify field)) []) (# (~! meta.monad) (~' in) (list (` (.:as (~ (nilable_type fieldT)) ("lua object get" (~ (code.text field)) (:as (..Object .Any) (~ imported))))))))) (` (def: ((~ (qualify field)) (~ g!object)) (-> (~ g!type) (~ (nilable_type fieldT))) (:expected (~ (without_nil g!temp fieldT (` ("lua object get" (~ (code.text field)) (:as (..Object .Any) (~ g!object)))))))))) {#Method method} (case method {#Static [method alias inputsT io? try? outputT]} (..make_function (qualify (maybe.else method alias)) g!temp (` ("lua object get" (~ (code.text method)) (:as (..Object .Any) (~ imported)))) inputsT io? try? outputT) {#Virtual [method alias inputsT io? try? outputT]} (let [g!inputs (input_variables inputsT)] (` (def: ((~ (qualify (maybe.else method alias))) [(~+ (list#each product.right g!inputs))] (~ g!object)) (-> [(~+ (list#each nilable_type inputsT))] (~ g!type) (~ (|> (nilable_type outputT) (try_type try?) (io_type io?)))) (:expected (~ (<| (with_io io?) (with_try try?) (without_nil g!temp outputT) (` ("lua object do" (~ (code.text method)) (~ g!object) (~+ (list#each (with_nil g!temp) g!inputs))))))))))))) members))))) {#Function [name alias inputsT io? try? outputT]} (in (list (..make_function (code.local_symbol (maybe.else name alias)) g!temp (` ("lua constant" (~ (code.text (text.replaced "/" "." name))))) inputsT io? try? outputT))) {#Constant [_ name fieldT]} (in (list (` ((~! syntax:) ((~ (code.local_symbol name)) []) (# (~! meta.monad) (~' in) (list (` (.:as (~ (nilable_type fieldT)) ("lua constant" (~ (code.text (text.replaced "/" "." name)))))))))))) ))) (template: .public (closure ) [(.:as ..Function (`` ("lua function" (~~ (template.amount )) (.function (_ []) ))))])