Skip to content

Commit

Permalink
Rename program to package
Browse files Browse the repository at this point in the history
This is in preparation for dependent packages
  • Loading branch information
dusty-phillips committed Aug 27, 2024
1 parent b85dad6 commit 87fde3a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 58 deletions.
16 changes: 8 additions & 8 deletions src/compiler.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import compiler/generator
import compiler/program
import compiler/package
import compiler/transformer
import glance
import gleam/dict
Expand All @@ -14,16 +14,16 @@ pub fn compile_module(glance_module: glance.Module) -> String {
|> generator.generate
}

pub fn compile_program(program: program.GleamProgram) -> program.CompiledProgram {
program.CompiledProgram(
base_directory: program.base_directory,
main_module: dict.get(program.modules, program.main_module)
pub fn compile_package(package: package.GleamPackage) -> package.CompiledPackage {
package.CompiledPackage(
base_directory: package.base_directory,
main_module: dict.get(package.modules, package.main_module)
|> result.try(fn(mod) { mod.functions |> has_main_function })
|> result.replace(program.main_module |> string.drop_right(6))
|> result.replace(package.main_module |> string.drop_right(6))
|> option.from_result,
modules: program.modules
modules: package.modules
|> dict.map_values(fn(_key, value) { compile_module(value) }),
external_import_files: program.external_import_files,
external_import_files: package.external_import_files,
)
}

Expand Down
50 changes: 25 additions & 25 deletions src/compiler/program.gleam → src/compiler/package.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ import gleam/set
import gleam/string
import simplifile

pub type GleamProgram {
GleamProgram(
pub type GleamPackage {
GleamPackage(
base_directory: String,
main_module: String,
modules: dict.Dict(String, glance.Module),
external_import_files: set.Set(String),
)
}

pub type CompiledProgram {
CompiledProgram(
pub type CompiledPackage {
CompiledPackage(
base_directory: String,
main_module: option.Option(String),
modules: dict.Dict(String, String),
Expand All @@ -36,16 +36,16 @@ pub type CompiledProgram {

/// Load the entry_point file and recursively load and parse any modules it
///returns.
pub fn load_program(
pub fn load_package(
source_directory: String,
) -> Result(GleamProgram, errors.Error) {
) -> Result(GleamPackage, errors.Error) {
source_directory
|> simplifile.is_directory
|> result.map_error(errors.FileOrDirectoryNotFound(source_directory, _))
|> result.try(fn(_) { find_entrypoint(source_directory) })
|> result.try(fn(entrypoint) {
load_module(
GleamProgram(
GleamPackage(
source_directory,
entrypoint,
dict.new(),
Expand All @@ -72,17 +72,17 @@ pub fn build_directory(base_directory: String) -> String {
filepath.join(base_directory, "build")
}

/// Parse the module and add it to the program's modules, if it can be parsed.
/// Parse the module and add it to the package's modules, if it can be parsed.
/// Then recursively parse any modules it imports.
fn load_module(
program: GleamProgram,
package: GleamPackage,
module_path: String,
) -> Result(GleamProgram, errors.Error) {
case dict.get(program.modules, module_path) {
Ok(_) -> Ok(program)
) -> Result(GleamPackage, errors.Error) {
case dict.get(package.modules, module_path) {
Ok(_) -> Ok(package)
Error(_) -> {
let module_result =
program.base_directory
package.base_directory
|> source_directory
|> filepath.join(module_path)
|> simplifile.read
Expand All @@ -92,7 +92,7 @@ fn load_module(
case module_result {
Error(err) -> Error(err)
Ok(module_contents) -> {
add_module(program, module_path, module_contents)
add_module(package, module_path, module_contents)
|> Ok
|> list.fold(module_contents.imports, _, fold_load_module)
}
Expand All @@ -102,13 +102,13 @@ fn load_module(
}

fn fold_load_module(
program_result: Result(GleamProgram, errors.Error),
package_result: Result(GleamPackage, errors.Error),
import_def: glance.Definition(glance.Import),
) -> Result(GleamProgram, errors.Error) {
case program_result {
) -> Result(GleamPackage, errors.Error) {
case package_result {
Error(error) -> Error(error)
Ok(program) ->
load_module(program, import_def.definition.module <> ".gleam")
Ok(package) ->
load_module(package, import_def.definition.module <> ".gleam")
}
}

Expand All @@ -121,16 +121,16 @@ fn parse(
}

fn add_module(
program: GleamProgram,
package: GleamPackage,
module_path: String,
module_contents: glance.Module,
) -> GleamProgram {
GleamProgram(
..program,
modules: dict.insert(program.modules, module_path, module_contents),
) -> GleamPackage {
GleamPackage(
..package,
modules: dict.insert(package.modules, module_path, module_contents),
external_import_files: set.union(
python_external_modules(module_contents.functions),
program.external_import_files,
package.external_import_files,
),
)
}
Expand Down
22 changes: 11 additions & 11 deletions src/macabre.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import argv
import compiler
import compiler/program
import compiler/package
import errors
import filepath
import gleam/dict
Expand All @@ -14,9 +14,9 @@ pub fn main() {
[] -> usage("Not enough arguments")
[directory] ->
directory
|> program.load_program
|> result.map(compiler.compile_program)
|> result.try(write_program)
|> package.load_package
|> result.map(compiler.compile_package)
|> result.try(write_package)
|> result.map_error(output.write_error)
|> result.unwrap_both
// both nil
Expand All @@ -28,26 +28,26 @@ pub fn usage(message: String) -> Nil {
io.println("Usage: macabre <filename.gleam>\n\n" <> message)
}

pub fn write_program(
program: program.CompiledProgram,
pub fn write_package(
package: package.CompiledPackage,
) -> Result(Nil, errors.Error) {
let build_directory = program.build_directory(program.base_directory)
let source_directory = program.source_directory(program.base_directory)
let build_directory = package.build_directory(package.base_directory)
let source_directory = package.source_directory(package.base_directory)
output.delete(build_directory)
|> result.try(fn(_) { output.create_directory(build_directory) })
|> result.try(fn(_) { output.write_prelude_file(build_directory) })
|> result.try(fn(_) {
output.write_py_main(build_directory, program.main_module)
output.write_py_main(build_directory, package.main_module)
})
|> result.try(fn(_) {
output.copy_externals(
build_directory,
source_directory,
program.external_import_files |> set.to_list,
package.external_import_files |> set.to_list,
)
})
|> result.try(fn(_) {
dict.fold(program.modules, Ok(Nil), fn(state, name, module) {
dict.fold(package.modules, Ok(Nil), fn(state, name, module) {
result.try(state, fn(_) {
build_directory
|> filepath.join(name)
Expand Down
28 changes: 14 additions & 14 deletions test/compiler/directory_structure_tests.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import compiler
import compiler/program
import compiler/package
import filepath
import gleam/dict
import gleam/list
Expand Down Expand Up @@ -51,7 +51,7 @@ fn init_folders(
use_function(project_files)
}

pub fn program_compile_test_with_nested_folders_test() {
pub fn package_compile_test_with_nested_folders_test() {
// src/<dirname.gleam>
// src/baz.py
// src/foo/bar.gleam
Expand Down Expand Up @@ -94,38 +94,38 @@ pub fn program_compile_test_with_nested_folders_test() {
)
|> should.be_ok

let gleam_program =
program.load_program(project_files.base_dir)
let gleam_package =
package.load_package(project_files.base_dir)
|> should.be_ok

// load

should.equal(gleam_program.base_directory, project_files.base_dir)
should.equal(gleam_package.base_directory, project_files.base_dir)
should.equal(
gleam_program.main_module,
gleam_package.main_module,
filepath.base_name(project_files.main_path),
)
gleam_program.modules
gleam_package.modules
|> dict.size
|> should.equal(2)
gleam_program.external_import_files |> set.size |> should.equal(2)
gleam_package.external_import_files |> set.size |> should.equal(2)

// --- compile
let compiled_program = compiler.compile_program(gleam_program)
should.equal(compiled_program.base_directory, project_files.base_dir)
let compiled_package = compiler.compile_package(gleam_package)
should.equal(compiled_package.base_directory, project_files.base_dir)
should.equal(
compiled_program.main_module,
compiled_package.main_module,
option.Some(
filepath.base_name(project_files.main_path) |> string.drop_right(6),
),
)
compiled_program.modules
compiled_package.modules
|> dict.size
|> should.equal(2)
compiled_program.external_import_files |> set.size |> should.equal(2)
compiled_package.external_import_files |> set.size |> should.equal(2)

// --- write output
macabre.write_program(compiled_program) |> should.be_ok
macabre.write_package(compiled_package) |> should.be_ok

simplifile.read_directory(project_files.build_dir)
|> should.be_ok
Expand Down

0 comments on commit 87fde3a

Please sign in to comment.