Skip to content

Commit

Permalink
Start adding a prelude
Browse files Browse the repository at this point in the history
move dataclasses import to it
  • Loading branch information
dusty-phillips committed Aug 20, 2024
1 parent 0657cc5 commit 058727f
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 61 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ PRs are welcome.
- only compiles one module at a time
- eliminate all todos in source code
- panic should probably raise a custom/stdlib exception
- generate **main** if a module has a main function
1 change: 1 addition & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ gleam_stdlib = ">= 0.34.0 and < 2.0.0"
glance = ">= 0.11.0 and < 1.0.0"
argv = ">= 1.0.2 and < 2.0.0"
simplifile = ">= 2.0.1 and < 3.0.0"
filepath = ">= 1.0.0 and < 2.0.0"

[dev-dependencies]
gleescript = ">= 1.4.0 and < 2.0.0"
Expand Down
1 change: 1 addition & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ packages = [

[requirements]
argv = { version = ">= 1.0.2 and < 2.0.0" }
filepath = { version = ">= 1.0.0 and < 2.0.0" }
glance = { version = ">= 0.11.0 and < 1.0.0" }
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleescript = { version = ">= 1.4.0 and < 2.0.0" }
Expand Down
4 changes: 3 additions & 1 deletion src/generator.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import gleam/option
import gleam/string_builder.{type StringBuilder}
import pprint
import python
import python_prelude

pub type Foo

Expand Down Expand Up @@ -172,7 +173,7 @@ fn generate_type_fields(field: python.Field(python.Type)) -> StringBuilder {

fn generate_type_variant(variant: python.Variant) -> StringBuilder {
string_builder.new()
|> string_builder.append("@dataclass(frozen=True)\n")
|> string_builder.append("@dataclasses.dataclass(frozen=True)\n")
|> string_builder.append("class ")
|> string_builder.append(variant.name)
|> string_builder.append(":\n")
Expand Down Expand Up @@ -233,6 +234,7 @@ fn generate_plural(

pub fn generate(module: python.Module) -> Result(String, String) {
string_builder.new()
|> string_builder.append(python_prelude.prelude)
|> string_builder.append_builder(generate_imports(module.imports))
|> string_builder.append_builder(generate_plural(
module.custom_types,
Expand Down
21 changes: 20 additions & 1 deletion src/macabre.gleam
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import argv
import filepath
import generator
import glance
import gleam/io
import gleam/result
import gleam/string
import pprint
import python_prelude
import simplifile
import transformer

Expand All @@ -27,7 +29,11 @@ pub fn write_output(contents: String, filename: String) -> Result(Nil, String) {
}

pub fn replace_extension(filename: String) -> String {
filename |> string.drop_right(5) <> "py"
filename |> filepath.strip_extension <> ".py"
}

pub fn replace_file(path: String, filename: String) -> String {
path |> filepath.directory_name |> filepath.join(filename)
}

pub fn output_result(filename: String, result: Result(Nil, String)) -> Nil {
Expand All @@ -44,6 +50,12 @@ pub fn output_result(filename: String, result: Result(Nil, String)) -> Nil {
}
}

pub fn output_prelude_file(filepath: String) -> Result(Nil, String) {
filepath
|> simplifile.write(python_prelude.gleam_builtins)
|> result.replace_error("Unable to write prelude")
}

pub fn compile(module_contents: String) -> Result(String, String) {
module_contents
|> parse
Expand All @@ -57,6 +69,13 @@ pub fn compile_module(filename: String) -> Result(Nil, String) {
|> result.try(compile)
|> pprint.debug
|> result.try(write_output(_, replace_extension(filename)))
|> result.try(fn(_) {
// TODO: eventually, this has to be output to a base directory,
// not one copy per module.
filename
|> replace_file("gleam_builtins.py")
|> output_prelude_file
})
}

pub fn main() {
Expand Down
4 changes: 4 additions & 0 deletions src/python.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import gleam/option

pub type Context(a) {
Context(imports: List(Import), item: a)
}

pub type Import {
UnqualifiedImport(module: String, name: String, alias: option.Option(String))
}
Expand Down
5 changes: 5 additions & 0 deletions src/python_prelude.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub const gleam_builtins = "
import dataclasses
"

pub const prelude = "from gleam_builtins import *\n\n"
9 changes: 0 additions & 9 deletions src/transformer.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,8 @@ fn transform_custom_type_in_module(
module: python.Module,
custom_type: glance.Definition(glance.CustomType),
) -> python.Module {
let has_dataclass_import =
list.find(module.imports, fn(imp) { imp == python.dataclass_import })

let imports = case has_dataclass_import {
Ok(_) -> module.imports
Error(_) -> [python.dataclass_import, ..module.imports]
}

python.Module(
..module,
imports: imports,
custom_types: [
transform_custom_type(custom_type.definition),
..module.custom_types
Expand Down
8 changes: 6 additions & 2 deletions test/assignment_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ pub fn simple_assignment_test() {
|> macabre.compile
|> should.be_ok
|> should.equal(
"def main():
"from gleam_builtins import *
def main():
a = \"hello world\"",
)
}
Expand All @@ -23,7 +25,9 @@ pub fn mulitple_simple_assignment_test() {
|> macabre.compile
|> should.be_ok
|> should.equal(
"def main():
"from gleam_builtins import *
def main():
a = \"hello world\"
b = 42",
)
Expand Down
12 changes: 5 additions & 7 deletions test/custom_types_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ pub fn single_variant_custom_type_test() {
|> macabre.compile
|> should.be_ok
|> should.equal(
"from dataclasses import dataclass
"from gleam_builtins import *
class Foo:
@dataclass(frozen=True)
@dataclasses.dataclass(frozen=True)
class Bar:
a: int
Expand All @@ -31,15 +30,14 @@ pub fn multi_variant_custom_type_test() {
|> macabre.compile
|> should.be_ok
|> should.equal(
"from dataclasses import dataclass
"from gleam_builtins import *
class Foo:
@dataclass(frozen=True)
@dataclasses.dataclass(frozen=True)
class Bar:
a: int
@dataclass(frozen=True)
@dataclasses.dataclass(frozen=True)
class Baz:
a: str
Expand Down
Loading

0 comments on commit 058727f

Please sign in to comment.