From 0c32c7f03ad1f8f0db54b623dc407713bbf8cacd Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Wed, 6 Jul 2022 12:05:43 -0400 Subject: Moved compiler machinery under lux/meta. --- .../lux/meta/compiler/language/lux/analysis.lux | 132 +++++++++++++++++ .../lux/meta/compiler/language/lux/synthesis.lux | 161 ++++++++++++++++++++ .../lux/tool/compiler/language/lux/analysis.lux | 133 ----------------- .../lux/tool/compiler/language/lux/synthesis.lux | 162 --------------------- 4 files changed, 293 insertions(+), 295 deletions(-) create mode 100644 stdlib/source/parser/lux/meta/compiler/language/lux/analysis.lux create mode 100644 stdlib/source/parser/lux/meta/compiler/language/lux/synthesis.lux delete mode 100644 stdlib/source/parser/lux/tool/compiler/language/lux/analysis.lux delete mode 100644 stdlib/source/parser/lux/tool/compiler/language/lux/synthesis.lux (limited to 'stdlib/source/parser') diff --git a/stdlib/source/parser/lux/meta/compiler/language/lux/analysis.lux b/stdlib/source/parser/lux/meta/compiler/language/lux/analysis.lux new file mode 100644 index 000000000..999fca556 --- /dev/null +++ b/stdlib/source/parser/lux/meta/compiler/language/lux/analysis.lux @@ -0,0 +1,132 @@ +(.require + [library + [lux (.except nat int rev local) + [abstract + [monad (.only do)]] + [control + ["//" parser] + ["[0]" try (.only Try)] + ["[0]" exception (.only exception)]] + [data + ["[0]" bit] + ["[0]" text (.only) + ["%" \\format (.only format)]] + [collection + ["[0]" list (.use "[1]#[0]" functor)]]] + [math + [number + ["[0]" i64] + ["[0]" nat] + ["[0]" int] + ["[0]" rev] + ["[0]" frac]]] + [meta + ["[0]" symbol] + [macro + ["[0]" template]] + [compiler + [arity (.only Arity)] + [reference (.only) + [variable (.only)]]]]]] + ["/" \\library (.only Environment Analysis)]) + +(def (remaining_inputs asts) + (-> (List Analysis) Text) + (format text.new_line "Remaining input: " + (|> asts + (list#each /.format) + (text.interposed " ")))) + +(exception .public (cannot_parse [input (List Analysis)]) + (exception.report + "Input" (exception.listing /.format input))) + +(exception .public (unconsumed_input [input (List Analysis)]) + (exception.report + "Input" (exception.listing /.format input))) + +(type .public Parser + (//.Parser (List Analysis))) + +(def .public (result parser input) + (All (_ a) (-> (Parser a) (List Analysis) (Try a))) + (case (parser input) + {try.#Failure error} + {try.#Failure error} + + {try.#Success [{.#End} value]} + {try.#Success value} + + {try.#Success [unconsumed _]} + (exception.except ..unconsumed_input unconsumed))) + +(def .public any + (Parser Analysis) + (function (_ input) + (case input + {.#End} + (exception.except ..cannot_parse input) + + {.#Item [head tail]} + {try.#Success [tail head]}))) + +(def .public end + (Parser Any) + (function (_ tokens) + (case tokens + {.#End} {try.#Success [tokens []]} + _ {try.#Failure (format "Expected list of tokens to be empty!" + (remaining_inputs tokens))}))) + +(def .public end? + (Parser Bit) + (function (_ tokens) + {try.#Success [tokens (case tokens + {.#End} true + _ false)]})) + +(with_template [ ] + [(`` (these (def .public + (Parser ) + (function (_ input) + (case input + (list.partial ( x) input') + {try.#Success [input' x]} + + _ + (exception.except ..cannot_parse input)))) + + (def .public ( expected) + (-> (Parser Any)) + (function (_ input) + (case input + (list.partial ( actual) input') + (if (at = expected actual) + {try.#Success [input' []]} + (exception.except ..cannot_parse input)) + + _ + (exception.except ..cannot_parse input))))))] + + [bit this_bit /.bit Bit bit.equivalence] + [nat this_nat /.nat Nat nat.equivalence] + [int this_int /.int Int int.equivalence] + [rev this_rev /.rev Rev rev.equivalence] + [frac this_frac /.frac Frac frac.equivalence] + [text this_text /.text Text text.equivalence] + [local this_local /.local Nat nat.equivalence] + [foreign this_foreign /.foreign Nat nat.equivalence] + [constant this_constant /.constant Symbol symbol.equivalence] + ) + +(def .public (tuple parser) + (All (_ a) (-> (Parser a) (Parser a))) + (function (_ input) + (case input + (list.partial (/.tuple head) tail) + (do try.monad + [output (..result parser head)] + {try.#Success [tail output]}) + + _ + (exception.except ..cannot_parse input)))) diff --git a/stdlib/source/parser/lux/meta/compiler/language/lux/synthesis.lux b/stdlib/source/parser/lux/meta/compiler/language/lux/synthesis.lux new file mode 100644 index 000000000..76248a4af --- /dev/null +++ b/stdlib/source/parser/lux/meta/compiler/language/lux/synthesis.lux @@ -0,0 +1,161 @@ +(.require + [library + [lux (.except function loop i64 local) + [abstract + [monad (.only do)]] + [control + ["//" parser] + ["[0]" try (.only Try)] + ["[0]" exception (.only exception)]] + [data + ["[0]" bit] + ["[0]" text (.only) + ["%" \\format (.only format)]] + [collection + ["[0]" list]]] + [math + [number + ["n" nat] + ["[0]" i64] + ["[0]" frac]]] + [meta + ["[0]" symbol] + [compiler + [reference (.only) + [variable (.only Register)]] + [arity (.only Arity)] + [language + [lux + [analysis (.only Environment)]]]]]]] + [\\library + ["[0]" / (.only Synthesis Abstraction)]]) + +(exception .public (cannot_parse [input (List Synthesis)]) + (exception.report + "Input" (exception.listing /.%synthesis input))) + +(exception .public (unconsumed_input [input (List Synthesis)]) + (exception.report + "Input" (exception.listing /.%synthesis input))) + +(exception .public (expected_empty_input [input (List Synthesis)]) + (exception.report + "Input" (exception.listing /.%synthesis input))) + +(exception .public (wrong_arity [expected Arity + actual Arity]) + (exception.report + "Expected" (%.nat expected) + "Actual" (%.nat actual))) + +(exception .public empty_input) + +(type .public Parser + (//.Parser (List Synthesis))) + +(def .public (result parser input) + (All (_ a) (-> (Parser a) (List Synthesis) (Try a))) + (case (parser input) + {try.#Failure error} + {try.#Failure error} + + {try.#Success [{.#End} value]} + {try.#Success value} + + {try.#Success [unconsumed _]} + (exception.except ..unconsumed_input unconsumed))) + +(def .public any + (Parser Synthesis) + (.function (_ input) + (case input + {.#End} + (exception.except ..empty_input []) + + {.#Item [head tail]} + {try.#Success [tail head]}))) + +(def .public end + (Parser Any) + (.function (_ tokens) + (case tokens + {.#End} {try.#Success [tokens []]} + _ (exception.except ..expected_empty_input [tokens])))) + +(def .public end? + (Parser Bit) + (.function (_ tokens) + {try.#Success [tokens (case tokens + {.#End} true + _ false)]})) + +(with_template [ ] + [(`` (def .public + (Parser ) + (.function (_ input) + (case input + (list.partial ( x) input') + {try.#Success [input' x]} + + _ + (exception.except ..cannot_parse input))))) + + (`` (def .public ( expected) + (-> (Parser Any)) + (.function (_ input) + (case input + (list.partial ( actual) input') + (if (at = expected actual) + {try.#Success [input' []]} + (exception.except ..cannot_parse input)) + + _ + (exception.except ..cannot_parse input)))))] + + [bit this_bit /.bit Bit bit.equivalence] + [i64 this_i64 /.i64 I64 i64.equivalence] + [f64 this_f64 /.f64 Frac frac.equivalence] + [text this_text /.text Text text.equivalence] + [local this_local /.variable/local Nat n.equivalence] + [foreign this_foreign /.variable/foreign Nat n.equivalence] + [constant this_constant /.constant Symbol symbol.equivalence] + ) + +(def .public (tuple parser) + (All (_ a) (-> (Parser a) (Parser a))) + (.function (_ input) + (case input + (list.partial (/.tuple head) tail) + (do try.monad + [output (..result parser head)] + {try.#Success [tail output]}) + + _ + (exception.except ..cannot_parse input)))) + +(def .public (function expected parser) + (All (_ a) (-> Arity (Parser a) (Parser [(Environment Synthesis) a]))) + (.function (_ input) + (case input + (list.partial (/.function/abstraction [environment actual body]) tail) + (if (n.= expected actual) + (do try.monad + [output (..result parser (list body))] + {try.#Success [tail [environment output]]}) + (exception.except ..wrong_arity [expected actual])) + + _ + (exception.except ..cannot_parse input)))) + +(def .public (loop init_parsers iteration_parser) + (All (_ a b) (-> (Parser a) (Parser b) (Parser [Register a b]))) + (.function (_ input) + (case input + (list.partial (/.loop/scope [start inits iteration]) tail) + (do try.monad + [inits (..result init_parsers inits) + iteration (..result iteration_parser (list iteration))] + {try.#Success [tail [start inits iteration]]}) + + _ + (exception.except ..cannot_parse input)))) diff --git a/stdlib/source/parser/lux/tool/compiler/language/lux/analysis.lux b/stdlib/source/parser/lux/tool/compiler/language/lux/analysis.lux deleted file mode 100644 index 5e818aa29..000000000 --- a/stdlib/source/parser/lux/tool/compiler/language/lux/analysis.lux +++ /dev/null @@ -1,133 +0,0 @@ -(.require - [library - [lux (.except nat int rev local) - [abstract - [monad (.only do)]] - [control - ["//" parser] - ["[0]" try (.only Try)] - ["[0]" exception (.only exception)]] - [data - ["[0]" bit] - ["[0]" text (.only) - ["%" \\format (.only format)]] - [collection - ["[0]" list (.use "[1]#[0]" functor)]]] - [math - [number - ["[0]" i64] - ["[0]" nat] - ["[0]" int] - ["[0]" rev] - ["[0]" frac]]] - [meta - ["[0]" symbol] - [macro - ["[0]" template]]] - [tool - [compiler - [arity (.only Arity)] - [reference (.only) - [variable (.only)]]]]]] - ["/" \\library (.only Environment Analysis)]) - -(def (remaining_inputs asts) - (-> (List Analysis) Text) - (format text.new_line "Remaining input: " - (|> asts - (list#each /.format) - (text.interposed " ")))) - -(exception .public (cannot_parse [input (List Analysis)]) - (exception.report - "Input" (exception.listing /.format input))) - -(exception .public (unconsumed_input [input (List Analysis)]) - (exception.report - "Input" (exception.listing /.format input))) - -(type .public Parser - (//.Parser (List Analysis))) - -(def .public (result parser input) - (All (_ a) (-> (Parser a) (List Analysis) (Try a))) - (case (parser input) - {try.#Failure error} - {try.#Failure error} - - {try.#Success [{.#End} value]} - {try.#Success value} - - {try.#Success [unconsumed _]} - (exception.except ..unconsumed_input unconsumed))) - -(def .public any - (Parser Analysis) - (function (_ input) - (case input - {.#End} - (exception.except ..cannot_parse input) - - {.#Item [head tail]} - {try.#Success [tail head]}))) - -(def .public end - (Parser Any) - (function (_ tokens) - (case tokens - {.#End} {try.#Success [tokens []]} - _ {try.#Failure (format "Expected list of tokens to be empty!" - (remaining_inputs tokens))}))) - -(def .public end? - (Parser Bit) - (function (_ tokens) - {try.#Success [tokens (case tokens - {.#End} true - _ false)]})) - -(with_template [ ] - [(`` (these (def .public - (Parser ) - (function (_ input) - (case input - (list.partial ( x) input') - {try.#Success [input' x]} - - _ - (exception.except ..cannot_parse input)))) - - (def .public ( expected) - (-> (Parser Any)) - (function (_ input) - (case input - (list.partial ( actual) input') - (if (at = expected actual) - {try.#Success [input' []]} - (exception.except ..cannot_parse input)) - - _ - (exception.except ..cannot_parse input))))))] - - [bit this_bit /.bit Bit bit.equivalence] - [nat this_nat /.nat Nat nat.equivalence] - [int this_int /.int Int int.equivalence] - [rev this_rev /.rev Rev rev.equivalence] - [frac this_frac /.frac Frac frac.equivalence] - [text this_text /.text Text text.equivalence] - [local this_local /.local Nat nat.equivalence] - [foreign this_foreign /.foreign Nat nat.equivalence] - [constant this_constant /.constant Symbol symbol.equivalence] - ) - -(def .public (tuple parser) - (All (_ a) (-> (Parser a) (Parser a))) - (function (_ input) - (case input - (list.partial (/.tuple head) tail) - (do try.monad - [output (..result parser head)] - {try.#Success [tail output]}) - - _ - (exception.except ..cannot_parse input)))) diff --git a/stdlib/source/parser/lux/tool/compiler/language/lux/synthesis.lux b/stdlib/source/parser/lux/tool/compiler/language/lux/synthesis.lux deleted file mode 100644 index 2117dabd0..000000000 --- a/stdlib/source/parser/lux/tool/compiler/language/lux/synthesis.lux +++ /dev/null @@ -1,162 +0,0 @@ -(.require - [library - [lux (.except function loop i64 local) - [abstract - [monad (.only do)]] - [control - ["//" parser] - ["[0]" try (.only Try)] - ["[0]" exception (.only exception)]] - [data - ["[0]" bit] - ["[0]" text (.only) - ["%" \\format (.only format)]] - [collection - ["[0]" list]]] - [math - [number - ["n" nat] - ["[0]" i64] - ["[0]" frac]]] - [meta - ["[0]" symbol]] - [tool - [compiler - [reference (.only) - [variable (.only Register)]] - [arity (.only Arity)] - [language - [lux - [analysis (.only Environment)]]]]]]] - [\\library - ["[0]" / (.only Synthesis Abstraction)]]) - -(exception .public (cannot_parse [input (List Synthesis)]) - (exception.report - "Input" (exception.listing /.%synthesis input))) - -(exception .public (unconsumed_input [input (List Synthesis)]) - (exception.report - "Input" (exception.listing /.%synthesis input))) - -(exception .public (expected_empty_input [input (List Synthesis)]) - (exception.report - "Input" (exception.listing /.%synthesis input))) - -(exception .public (wrong_arity [expected Arity - actual Arity]) - (exception.report - "Expected" (%.nat expected) - "Actual" (%.nat actual))) - -(exception .public empty_input) - -(type .public Parser - (//.Parser (List Synthesis))) - -(def .public (result parser input) - (All (_ a) (-> (Parser a) (List Synthesis) (Try a))) - (case (parser input) - {try.#Failure error} - {try.#Failure error} - - {try.#Success [{.#End} value]} - {try.#Success value} - - {try.#Success [unconsumed _]} - (exception.except ..unconsumed_input unconsumed))) - -(def .public any - (Parser Synthesis) - (.function (_ input) - (case input - {.#End} - (exception.except ..empty_input []) - - {.#Item [head tail]} - {try.#Success [tail head]}))) - -(def .public end - (Parser Any) - (.function (_ tokens) - (case tokens - {.#End} {try.#Success [tokens []]} - _ (exception.except ..expected_empty_input [tokens])))) - -(def .public end? - (Parser Bit) - (.function (_ tokens) - {try.#Success [tokens (case tokens - {.#End} true - _ false)]})) - -(with_template [ ] - [(`` (def .public - (Parser ) - (.function (_ input) - (case input - (list.partial ( x) input') - {try.#Success [input' x]} - - _ - (exception.except ..cannot_parse input))))) - - (`` (def .public ( expected) - (-> (Parser Any)) - (.function (_ input) - (case input - (list.partial ( actual) input') - (if (at = expected actual) - {try.#Success [input' []]} - (exception.except ..cannot_parse input)) - - _ - (exception.except ..cannot_parse input)))))] - - [bit this_bit /.bit Bit bit.equivalence] - [i64 this_i64 /.i64 I64 i64.equivalence] - [f64 this_f64 /.f64 Frac frac.equivalence] - [text this_text /.text Text text.equivalence] - [local this_local /.variable/local Nat n.equivalence] - [foreign this_foreign /.variable/foreign Nat n.equivalence] - [constant this_constant /.constant Symbol symbol.equivalence] - ) - -(def .public (tuple parser) - (All (_ a) (-> (Parser a) (Parser a))) - (.function (_ input) - (case input - (list.partial (/.tuple head) tail) - (do try.monad - [output (..result parser head)] - {try.#Success [tail output]}) - - _ - (exception.except ..cannot_parse input)))) - -(def .public (function expected parser) - (All (_ a) (-> Arity (Parser a) (Parser [(Environment Synthesis) a]))) - (.function (_ input) - (case input - (list.partial (/.function/abstraction [environment actual body]) tail) - (if (n.= expected actual) - (do try.monad - [output (..result parser (list body))] - {try.#Success [tail [environment output]]}) - (exception.except ..wrong_arity [expected actual])) - - _ - (exception.except ..cannot_parse input)))) - -(def .public (loop init_parsers iteration_parser) - (All (_ a b) (-> (Parser a) (Parser b) (Parser [Register a b]))) - (.function (_ input) - (case input - (list.partial (/.loop/scope [start inits iteration]) tail) - (do try.monad - [inits (..result init_parsers inits) - iteration (..result iteration_parser (list iteration))] - {try.#Success [tail [start inits iteration]]}) - - _ - (exception.except ..cannot_parse input)))) -- cgit v1.2.3