diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,198 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "anstream" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "anstyle-parse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + +[[package]] +name = "clap" +version = "4.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2ce8604710f6733aa641a2b3731eaa1e8b3d9973d5e3565da11800813f997a9" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" + +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + +[[package]] +name = "compiler_cli" +version = "0.1.0" +dependencies = [ + "clap", + "compiler_core", + "owo-colors", +] + +[[package]] +name = "compiler_core" +version = "0.1.0" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "owo-colors" +version = "4.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d211803b9b6b570f68772237e415a029d5a50c65d382910b879fb19d3271f94d" + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] diff --git a/Cargo.toml b/Cargo.toml --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,38 @@ [workspace] resolver = "2" members = ["crates/cli", "crates/core"] + +[workspace.lints.clippy] +bool_comparison = "deny" +duplicate_mod = "deny" +inconsistent_struct_constructor = "deny" +invalid_regex = "deny" +mem_forget = "deny" +mixed_case_hex_literals = "deny" +suspicious_arithmetic_impl = "deny" +uninit_assumed_init = "deny" +suspicious_else_formatting = "deny" +suspicious_op_assign_impl = "deny" +suspicious_to_owned = "deny" +cmp_owned = "deny" +cmp_null = "deny" +manual_map = "deny" + +too_many_arguments = "warn" +cognitive_complexity = "warn" +large_enum_variant = "warn" +needless_borrow = "warn" +needless_pass_by_value = "warn" +redundant_clone = "warn" +unnecessary_cast = "warn" +inefficient_to_string = "warn" +or_fun_call = "warn" +unnecessary_to_owned = "warn" +map_clone = "warn" +flat_map_identity = "warn" +needless_collect = "warn" +vec_init_then_push = "warn" + +len_zero = "allow" +range_plus_one = "allow" +manual_range_contains = "allow" diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,6 @@ +max_width = 120 +fn_params_layout = "Compressed" +single_line_if_else_max_width = 100 +single_line_let_else_max_width = 100 +use_field_init_shorthand = true +struct_lit_width = 100 diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -4,3 +4,6 @@ edition = "2024" [dependencies] +clap = { version = "4.5.40", features = ["derive"] } +compiler_core = { path = "../core" } +owo-colors = "4.2.1" diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -4,6 +4,7 @@ # Internal planning +- [Supported subset](internal/supported_subset.md) - [Specs]() - [Pipeline overview](internal/specs/00_pipeline_overview.md) - [Tree-sitter to AST](internal/specs/01_tree_sitter_ast.md) diff --git a/crates/cli/src/args.rs b/crates/cli/src/args.rs new file mode 100644 --- /dev/null +++ b/crates/cli/src/args.rs @@ -0,0 +1,20 @@ +use std::path::PathBuf; + +use clap::{Parser, Subcommand}; + +#[derive(Debug, Parser)] +#[command(name = "gleam-wasm")] +#[command(about = "Compile Gleam source to WebAssembly")] +pub struct Args { + #[command(subcommand)] + pub command: Command, +} + +#[derive(Debug, Subcommand)] +pub enum Command { + /// Run the compiler pipeline for a source file. + Compile { + /// Gleam source file to compile. + input: PathBuf, + }, +} diff --git a/crates/cli/src/commands.rs b/crates/cli/src/commands.rs new file mode 100644 --- /dev/null +++ b/crates/cli/src/commands.rs @@ -0,0 +1,39 @@ +use std::{fs, process::ExitCode}; + +use compiler_core::{self, source::SourceFile, source::SourceFileId}; + +use crate::{args::Command, echo}; + +pub fn run(command: Command) -> ExitCode { + match command { + Command::Compile { input } => compile(input), + } +} + +fn compile(input: std::path::PathBuf) -> ExitCode { + let source = match fs::read_to_string(&input) { + Ok(source) => source, + Err(error) => { + echo::error(format!("could not read {}: {error}", input.display())); + return ExitCode::FAILURE; + } + }; + + let source = SourceFile::with_path(SourceFileId(0), input.clone(), source); + match compiler_core::compile_source(source) { + Ok(output) => { + echo::status( + "compiled", + format!("{} ({} bytes)", input.display(), output.wasm.bytes.len()), + ); + ExitCode::SUCCESS + } + Err(diagnostics) => { + echo::error(format!("could not compile {}", input.display())); + for diagnostic in diagnostics { + echo::diagnostic(diagnostic.message); + } + ExitCode::FAILURE + } + } +} diff --git a/crates/cli/src/echo.rs b/crates/cli/src/echo.rs new file mode 100644 --- /dev/null +++ b/crates/cli/src/echo.rs @@ -0,0 +1,13 @@ +use owo_colors::OwoColorize; + +pub fn status(label: &str, message: impl AsRef) { + eprintln!("{} {}", label.bright_magenta().bold(), message.as_ref()); +} + +pub fn error(message: impl AsRef) { + eprintln!("{} {}", "error".bright_red().bold(), message.as_ref()); +} + +pub fn diagnostic(message: impl AsRef) { + eprintln!("{} {}", "diagnostic".bright_yellow().bold(), message.as_ref()); +} diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -1,3 +1,10 @@ -fn main() { - println!("Hello, world!"); +mod args; +mod commands; +mod echo; + +use clap::Parser; + +fn main() -> std::process::ExitCode { + let args = args::Args::parse(); + commands::run(args.command) } diff --git a/crates/core/src/ast.rs b/crates/core/src/ast.rs new file mode 100644 --- /dev/null +++ b/crates/core/src/ast.rs @@ -0,0 +1,11 @@ +use crate::{diagnostic::Diagnostics, parse::ConcreteSyntaxTree, source::Span}; + +/// Compiler-owned AST module. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Module { + pub span: Span, +} + +pub fn build(cst: ConcreteSyntaxTree) -> Result { + Ok(Module { span: cst.source.whole_span() }) +} diff --git a/crates/core/src/diagnostic.rs b/crates/core/src/diagnostic.rs new file mode 100644 --- /dev/null +++ b/crates/core/src/diagnostic.rs @@ -0,0 +1,61 @@ +use crate::source::Span; + +/// A stable diagnostic code. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum DiagnosticCode { + Unsupported, + ParseError, + AstError, + ResolveError, + TypeError, + LoweringError, + WasmError, +} + +/// A source location attached to a diagnostic message. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Label { + pub span: Span, + pub message: Option, +} + +impl Label { + pub fn primary(span: Span, message: impl Into) -> Self { + Self { span, message: Some(message.into()) } + } +} + +/// A compiler diagnostic that can be rendered by the CLI or tests. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Diagnostic { + pub code: DiagnosticCode, + pub message: String, + pub labels: Vec