diff --git a/src/compiler.gleam b/src/compiler.gleam index 9078d1c..746dada 100644 --- a/src/compiler.gleam +++ b/src/compiler.gleam @@ -16,6 +16,6 @@ pub fn parse(contents: String) -> Result(glance.Module, String) { pub fn compile(module_contents: String) -> Result(String, String) { module_contents |> parse - |> result.try(transformer.transform) - |> result.try(generator.generate) + |> result.map(transformer.transform) + |> result.map(generator.generate) } diff --git a/src/compiler/generator.gleam b/src/compiler/generator.gleam index 99288ec..96dd69d 100644 --- a/src/compiler/generator.gleam +++ b/src/compiler/generator.gleam @@ -6,7 +6,7 @@ import compiler/python import gleam/string_builder import python_prelude -pub fn generate(module: python.Module) -> Result(String, String) { +pub fn generate(module: python.Module) -> String { string_builder.new() |> string_builder.append(python_prelude.prelude) |> string_builder.append_builder(imports.generate_imports(module.imports)) @@ -21,5 +21,4 @@ pub fn generate(module: python.Module) -> Result(String, String) { "\n\n\n", )) |> string_builder.to_string - |> Ok } diff --git a/src/compiler/transformer.gleam b/src/compiler/transformer.gleam index 72309c7..6b188aa 100644 --- a/src/compiler/transformer.gleam +++ b/src/compiler/transformer.gleam @@ -6,14 +6,12 @@ import glance import gleam/list import gleam/option import gleam/string -import pprint -pub fn transform(input: glance.Module) -> Result(python.Module, String) { +pub fn transform(input: glance.Module) -> python.Module { python.empty_module() |> list.fold(input.imports, _, transform_import) |> list.fold(input.functions, _, transform_function_or_external) |> list.fold(input.custom_types, _, transform_custom_type_in_module) - |> Ok } fn transform_function_or_external( diff --git a/src/macabre.gleam b/src/macabre.gleam index 8e2d61d..3280481 100644 --- a/src/macabre.gleam +++ b/src/macabre.gleam @@ -13,7 +13,13 @@ pub fn usage(message: String) -> Nil { pub fn compile_module(filename: String) -> Result(Nil, String) { simplifile.read(filename) |> result.replace_error("Unable to read '" <> filename <> "'") - |> result.try(compiler.compile) + |> result.try(fn(content) { + content + |> compiler.compile + |> result.map_error(fn(error) { + "Unable to compile " <> filename <> ":\n " <> error + }) + }) |> result.try(output.write(_, output.replace_extension(filename))) |> result.try(fn(_) { // TODO: eventually, this has to be output to a base directory,