Skip to content

Commit

Permalink
Remove results because any valid glance is able to generate
Browse files Browse the repository at this point in the history
(or else it panics)
  • Loading branch information
dusty-phillips committed Aug 25, 2024
1 parent ef5068c commit 8c48928
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/compiler.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 1 addition & 2 deletions src/compiler/generator.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -21,5 +21,4 @@ pub fn generate(module: python.Module) -> Result(String, String) {
"\n\n\n",
))
|> string_builder.to_string
|> Ok
}
4 changes: 1 addition & 3 deletions src/compiler/transformer.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion src/macabre.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8c48928

Please sign in to comment.