Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion patch/gleam/bit_array.patch.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn unsafe_to_string(a: BitArray) -> String {
}

@external(c, "", "gleam_bit_array_from_string")
fn from_string(x: String) -> BitArray
pub fn from_string(x: String) -> BitArray

fn is_utf8_loop(bits: BitArray) -> Bool {
case bits {
Expand Down
2 changes: 1 addition & 1 deletion patch/gleam/io.patch.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn println(string: String) -> Nil {
do_print("\n")
}

fn println_error(string: String) -> Nil {
pub fn println_error(string: String) -> Nil {
do_print_error(string)
do_print_error("\n")
}
Expand Down
2 changes: 1 addition & 1 deletion patch/gleam/string.patch.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn append_pairs(strings: List(String)) {
fn unsafe_int_to_utf_codepoint(a: Int) -> UtfCodepoint

@external(c, "", "gleam_string_from_utf_codepoints")
fn from_utf_codepoints(utf_codepoints: List(UtfCodepoint)) -> String
pub fn from_utf_codepoints(utf_codepoints: List(UtfCodepoint)) -> String

@external(c, "", "gleam_string_do_to_utf_codepoints")
fn do_to_utf_codepoints(string: String) -> List(UtfCodepoint)
Expand Down
46 changes: 26 additions & 20 deletions src/gig/compiler.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,17 @@ pub fn compile(gleam_file_name: String, options: CompileOptions) {
// process the prelude
let assert Ok(source_text) = read_source(sources, "gleam")
let assert Ok(prelude) = glance.module(source_text)
let typed = typed_ast.new_context()
let assert Ok(typed) =
typed_ast.infer_module(typed, prelude, "gleam", source_text)
let assert Ok(typed) = typed_ast.infer_module(dict.new(), prelude, "gleam")

// parse and typecheck input (recursively)
let #(typed, _done) = infer_file(sources, typed, ["gleam"], module_id)
let typed =
infer_file(
sources,
dict.from_list([#("gleam", #(typed, typed_ast.interface(typed)))]),
module_id,
)

let core = core.lower_context(typed)
let core = core.lower_modules(dict.map_values(typed, fn(_, p) { p.0 }))

case options.headers {
True -> generate_headers_only(core, sources)
Expand All @@ -123,7 +126,7 @@ pub fn compile(gleam_file_name: String, options: CompileOptions) {
}

fn generate_headers_only(
core: core.Context,
core: core.Module,
sources: dict.Dict(String, String),
) -> String {
headers.module_headers(core)
Expand All @@ -150,7 +153,7 @@ fn generate_headers_only(
}

fn compile_to_binary(
core: core.Context,
core: core.Module,
sources: dict.Dict(String, String),
module_id: String,
target_path: String,
Expand Down Expand Up @@ -277,12 +280,9 @@ fn offset_to_line_col(text: String, offset: Int) {

fn infer_file(
sources: dict.Dict(String, String),
c: typed_ast.Context,
done: List(String),
modules: dict.Dict(String, #(typed_ast.Module, typed_ast.ModuleInterface)),
module_id: String,
) -> #(typed_ast.Context, List(String)) {
// add module to "done" list
let done = [module_id, ..done]
) -> dict.Dict(String, #(typed_ast.Module, typed_ast.ModuleInterface)) {
let assert Ok(source) = dict.get(sources, module_id)

io.println("Parse " <> source)
Expand All @@ -300,20 +300,26 @@ fn infer_file(
}

// infer imports
let #(c, done) =
list.fold(module.imports, #(c, done), fn(acc, i) {
let #(c, done) = acc
let modules =
list.fold(module.imports, modules, fn(modules, i) {
let module_id = i.definition.module
case list.contains(done, module_id) {
True -> #(c, done)
False -> infer_file(sources, c, done, module_id)
case dict.has_key(modules, module_id) {
True -> modules
False -> infer_file(sources, modules, module_id)
}
})

// infer this file
io.println("Check " <> source)
case typed_ast.infer_module(c, module, module_id, source_text) {
Ok(c) -> #(c, done)
case
typed_ast.infer_module(
dict.map_values(modules, fn(_, p) { p.1 }),
module,
module_id,
)
{
Ok(module) ->
dict.insert(modules, module_id, #(module, typed_ast.interface(module)))
Error(err) ->
panic as error(source_text, err.location, typed_ast.inspect_error(err))
}
Expand Down
Loading