Compare commits

...
83 Commits
Author SHA1 Message Date
selimaj-dev 352d0594b2 Merge pull request #120 from mist-go/patches
Patches
2026-07-14 09:20:05 +02:00
selimaj-dev 48ca58725a RA passthrough 2026-07-14 09:13:22 +02:00
selimaj-dev 708b956750 fix Analyzer file path #118 2026-07-14 08:46:33 +02:00
selimaj-dev df9e31e26d fix Analyzer file path #118 2026-07-14 07:59:36 +02:00
selimaj-dev c716b6352c Fix mapping for analyzer 2026-07-14 07:47:25 +02:00
selimaj-dev 6f675b4253 Fix mapping for compiler 2026-07-14 07:37:16 +02:00
selimaj-dev 5079b10819 Added other struct types #117 2026-07-14 07:32:42 +02:00
selimaj-dev d834ff595d Added more struct rules #117 2026-07-14 07:19:43 +02:00
selimaj-dev 70ec348f4d Fix mutability #119 2026-07-14 07:15:17 +02:00
selimaj-dev 93ba0e97da Bump to full release 2026-07-12 06:44:13 -04:00
selimaj-dev 9c0aa3d182 Merge pull request #116 from mist-go/patches
Patches
2026-07-12 12:02:00 +02:00
selimaj-dev e1b851911d Fix completion polution 2026-07-12 06:00:16 -04:00
selimaj-dev 72f7b8430d Fix publish command 2026-07-12 05:43:50 -04:00
selimaj-dev 0100abfa53 Fix analyzer bugs 2026-07-12 05:36:57 -04:00
selimaj-dev 01b7e816ee Fix include manifest path #106 2026-07-12 05:26:33 -04:00
selimaj-dev ac94882e5a Fix mapping system #115 2026-07-12 05:15:00 -04:00
selimaj-dev 9220ee3b29 Merge pull request #114 from mist-go/type-patches
Type patches
2026-07-09 12:12:54 +02:00
selimaj-dev b15f13880a Fix generics and fn types 2026-07-09 12:07:37 +02:00
selimaj-dev d538168692 Removed unnecessary type (lifetime) 2026-07-09 12:00:11 +02:00
selimaj-dev b68e4245d4 Using core for classes 2026-07-09 10:53:07 +02:00
selimaj-dev c5354ff424 Using core for c_void 2026-07-09 10:51:44 +02:00
selimaj-dev be68f93038 Updated bootstrap 2026-07-09 10:26:06 +02:00
selimaj-dev e3c9657b07 Bump version to 0.4.0-alpha.0 2026-07-09 07:39:46 +02:00
selimaj-dev 2aab50d36d Merge pull request #112 from mist-go/virtual-methods
Virtual methods
2026-07-09 07:38:25 +02:00
selimaj-dev e75b707981 Virtual and overrides in analyzer 2026-07-09 07:34:08 +02:00
selimaj-dev 18a13dd170 Override and virtual semantic checking 2026-07-09 06:59:25 +02:00
selimaj-dev aacc08b983 Updated class tests 2026-07-09 06:51:36 +02:00
selimaj-dev df8e67fd4f Implicit virtual on override 2026-07-09 06:37:37 +02:00
selimaj-dev 08771fe5be Virtual functions 2026-07-09 06:30:11 +02:00
selimaj-dev 0d2199ee3a virtual keyword parsing and ast 2026-07-09 06:23:04 +02:00
selimaj-dev 442d1e66a8 Merge pull request #111 from mist-go/include
Include
2026-07-08 11:26:29 +02:00
selimaj-dev 8198b8d5cb Local include 2026-07-08 11:23:36 +02:00
selimaj-dev 44ac3c365d Inline doc gen 2026-07-08 10:52:11 +02:00
selimaj-dev c564fc3e90 Added use include 2026-07-08 10:44:09 +02:00
selimaj-dev bb006c1ba2 Added global include 2026-07-08 10:11:26 +02:00
selimaj-dev 4b69bbaed0 Merge pull request #110 from mist-go/array-type
Array type
2026-07-05 02:47:19 +02:00
selimaj-dev f574d6e8af Fix parenthesis 2026-07-05 02:45:38 +02:00
selimaj-dev 13664413c5 Array types 2026-07-05 02:37:48 +02:00
selimaj-dev b6256283a6 Merge pull request #109 from mist-go/more-tests
Add comprehensive test coverage for grammar rules
2026-07-04 21:59:58 +02:00
selimaj-dev f30613f094 Added more tests 2026-07-04 21:55:29 +02:00
selimaj-dev a1fd169568 Added more links 2026-07-04 21:39:30 +02:00
selimaj-dev 3c5f13f3f6 Merge pull request #108 from mist-go/struct-field-style
Allman struct/enum field style, tuple var decls, cleanup
2026-07-04 19:34:57 +02:00
selimaj-dev f4f92c57ee Fix as type 2026-07-04 19:26:25 +02:00
selimaj-dev df34c72c97 Add tuple type variable decleration 2026-07-04 19:23:10 +02:00
selimaj-dev 517a2e5a2b Var decl with repeat 2026-07-04 09:55:23 +02:00
selimaj-dev eeac773c06 Removed duped module declerations 2026-07-04 09:40:04 +02:00
selimaj-dev 4f3966d8d8 Allman struct and enum field style 2026-07-04 09:38:13 +02:00
selimaj-dev 409301b3d6 Allman struct and enum field style 2026-07-04 09:16:13 +02:00
selimaj-dev 223bafaad7 Merge pull request #107 from mist-go/fix-class-initializers
Fix default initializers in classes
2026-07-04 09:05:27 +02:00
selimaj-dev 603ce1eb1e Fix default initializers in classes 2026-07-04 09:03:52 +02:00
selimaj-dev b02439edfc Bump version 2026-07-02 22:06:33 +02:00
selimaj-dev 0449789159 Merge pull request #105 from mist-go/cli-improvements
CLI improvements, module system fixes, project scaffolding, and bug fixes
2026-07-02 21:56:39 +02:00
selimaj-dev 2738f57bd9 Updated usage command 2026-07-02 21:49:33 +02:00
selimaj-dev f3046e3442 Fixed module system 2026-07-02 21:24:13 +02:00
selimaj-dev 28e9fb664c Fixed mapping 2026-07-02 21:07:57 +02:00
selimaj-dev 3a8537682e Added more cli commands #100 2026-07-02 20:57:02 +02:00
selimaj-dev eb23ff1e5a Gitignore, and mist new 2026-07-02 20:51:02 +02:00
selimaj-dev b8394bba52 Fix init 2026-07-02 20:45:00 +02:00
selimaj-dev ff861095e3 Using toml for init 2026-07-02 20:38:01 +02:00
selimaj-dev 744adeb068 Add mist toml on init 2026-07-02 20:28:25 +02:00
selimaj-dev 113ae54e6e Improved cli command options #100 2026-07-02 18:46:45 +02:00
selimaj-dev c7bf4f7131 Merge pull request #104 from mist-go/syntax-improvements
Syntax improvements
2026-07-02 07:25:26 +02:00
selimaj-dev 579d229cad Type aliases 2026-07-02 07:05:19 +02:00
selimaj-dev d62d884add Removed fmt (will add when i have close to 1:1 syntax support) 2026-07-02 06:53:47 +02:00
selimaj-dev 5346cd05be constants and static #102 2026-07-02 06:51:04 +02:00
selimaj-dev 493c668495 Working top level statement #103 2026-07-02 06:40:20 +02:00
selimaj-dev 4579c656b9 Possibly removed the need for ensure_semicolon 2026-07-02 06:33:17 +02:00
selimaj-dev 68cf239f92 Top level as statement #103 2026-07-02 06:25:44 +02:00
selimaj-dev 9021da224e Merge pull request #101 from mist-go/bug-patches
Bug patches
2026-06-30 21:04:56 +02:00
selimaj-dev d0a72b49f0 Fix module system in analyzer 2026-06-30 21:04:12 +02:00
selimaj-dev a5dad690bf Fix bug 2026-06-30 20:51:57 +02:00
selimaj-dev e9b7312362 Added include to Mist.toml 2026-06-30 19:58:47 +02:00
selimaj-dev 6e67fd852b bump to alpha 1 2026-06-30 19:48:33 +02:00
selimaj-dev 67acef8270 Merge pull request #99 from mist-go/subcrates-and-diagnostics
Subcrates and diagnostics
2026-06-30 19:46:58 +02:00
selimaj-dev a3da192b9a Analyzer Sub crate support 2026-06-30 19:41:34 +02:00
selimaj-dev 310ff6b53c Fix include non mist crates #86 2026-06-30 19:28:27 +02:00
selimaj-dev 4e1bcf36fc Include sub crates #86 2026-06-30 19:14:12 +02:00
selimaj-dev c1d7152992 Deduped config 2026-06-30 18:30:19 +02:00
selimaj-dev bd938ec3e7 Updated transpiler config 2026-06-30 18:23:15 +02:00
selimaj-dev bfaa9eeea5 Fix Diagnostics #93 2026-06-30 07:08:39 +02:00
selimaj-dev 3ff4c2ab08 Using FS for saving files #93 2026-06-30 06:24:55 +02:00
selimaj-dev 3d30e0a092 Fix fmt expr path generics 2026-06-28 17:39:35 +02:00
selimaj-dev 502bde1536 Updated boostrap 2026-06-28 17:38:16 +02:00
50 changed files with 1764 additions and 1734 deletions
Generated
+7 -5
View File
@@ -476,11 +476,12 @@ dependencies = [
[[package]] [[package]]
name = "mist-analyzer" name = "mist-analyzer"
version = "0.3.7-alpha.0" version = "0.4.0"
dependencies = [ dependencies = [
"cargo_metadata", "cargo_metadata",
"dashmap 6.1.0", "dashmap 6.1.0",
"heck", "heck",
"mist-api",
"mist-codegen", "mist-codegen",
"mist-parser", "mist-parser",
"pest", "pest",
@@ -494,7 +495,7 @@ dependencies = [
[[package]] [[package]]
name = "mist-api" name = "mist-api"
version = "0.3.7-alpha.0" version = "0.4.0"
dependencies = [ dependencies = [
"cargo_metadata", "cargo_metadata",
"mist-codegen", "mist-codegen",
@@ -506,7 +507,7 @@ dependencies = [
[[package]] [[package]]
name = "mist-codegen" name = "mist-codegen"
version = "0.3.7-alpha.0" version = "0.4.0"
dependencies = [ dependencies = [
"bytecount", "bytecount",
"mist-parser", "mist-parser",
@@ -514,15 +515,16 @@ dependencies = [
[[package]] [[package]]
name = "mist-lang" name = "mist-lang"
version = "0.3.7-alpha.0" version = "0.4.0"
dependencies = [ dependencies = [
"mist-analyzer", "mist-analyzer",
"mist-api", "mist-api",
"toml",
] ]
[[package]] [[package]]
name = "mist-parser" name = "mist-parser"
version = "0.3.7-alpha.0" version = "0.4.0"
dependencies = [ dependencies = [
"pest", "pest",
"pest_derive", "pest_derive",
+6 -5
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "mist-lang" name = "mist-lang"
version = "0.3.7-alpha.0" version = "0.4.0"
edition = "2024" edition = "2024"
description = "The Mist programming language compiler" description = "The Mist programming language compiler"
license = "Apache-2.0" license = "Apache-2.0"
@@ -18,6 +18,7 @@ include = [
[dependencies] [dependencies]
mist-analyzer = { workspace = true } mist-analyzer = { workspace = true }
mist-api = { workspace = true } mist-api = { workspace = true }
toml = "1.1.2"
[workspace] [workspace]
members = [ members = [
@@ -32,10 +33,10 @@ exclude = [
] ]
[workspace.dependencies] [workspace.dependencies]
mist-parser = { path = "crates/parser", version = "0.3.7-alpha.0" } mist-parser = { path = "crates/parser", version = "0.4.0" }
mist-analyzer = { path = "crates/analyzer", version = "0.3.7-alpha.0" } mist-analyzer = { path = "crates/analyzer", version = "0.4.0" }
mist-codegen = { path = "crates/codegen", version = "0.3.7-alpha.0" } mist-codegen = { path = "crates/codegen", version = "0.4.0" }
mist-api = { path = "crates/mist-api", version = "0.3.7-alpha.0" } mist-api = { path = "crates/mist-api", version = "0.4.0" }
[[bin]] [[bin]]
name = "mist" name = "mist"
+1
View File
@@ -1,2 +1,3 @@
package = "main.mist" package = "main.mist"
packages = [] packages = []
include = ["crates/*"]
+1 -1
View File
@@ -5,7 +5,7 @@ Originally this readme was part of the Carbon Language project, licensed with Ap
--> -->
<p align="center"> <p align="center">
<a href="https://mist.selimaj.dev">Documentation</a> <a href="https://mist.selimaj.dev/docs">Documentation</a> ∙ <a href="https://mist.selimaj.dev/blog">Blog</a> ∙ <a href="https://mist.selimaj.dev/faq">FAQ</a>
</p> </p>
+2 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "mist-analyzer" name = "mist-analyzer"
version = "0.3.7-alpha.0" version = "0.4.0"
edition = "2024" edition = "2024"
description = "The Mist programming language Analyzer LSP" description = "The Mist programming language Analyzer LSP"
license = "Apache-2.0" license = "Apache-2.0"
@@ -9,6 +9,7 @@ keywords = ["language-server", "language", "mist"]
categories = ["compilers"] categories = ["compilers"]
[dependencies] [dependencies]
mist-api = { workspace = true }
mist-parser = { workspace = true } mist-parser = { workspace = true }
mist-codegen = { workspace = true } mist-codegen = { workspace = true }
pest = "2.8.6" pest = "2.8.6"
+427 -101
View File
@@ -7,22 +7,27 @@ use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::atomic::{AtomicU64, Ordering};
use heck::ToSnakeCase; use heck::ToSnakeCase;
use mist_api::transpiler::MistConfig;
use mist_parser::MistFmtConfig; use mist_parser::MistFmtConfig;
use mist_parser::error::ParseError; use mist_parser::error::ParseError;
use mist_parser::parse_module;
use mist_parser::rev_mapper::{Mapping, MistMap, RustMap}; use mist_parser::rev_mapper::{Mapping, MistMap, RustMap};
use ropey::Rope; use ropey::Rope;
use serde::{Deserialize, Serialize}; use serde::Deserialize;
use serde_json::Value; use serde_json::Value;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tower_lsp::lsp_types::{self, *}; use tower_lsp::lsp_types::{self, *};
use tower_lsp::{Client, LanguageServer, LspService, Server}; use tower_lsp::{Client, LanguageServer, LspService, Server};
use crate::rust_analyzer::RustAnalyzer; use crate::rust_analyzer::RustAnalyzer;
use crate::transpiler::{TranspileError, format_mist, transpile_mist, transpile_mist_no_sem}; use crate::transpiler::{
TranspileError, TranspiledFile, format_mist, resolve_rust_path, transpile_mist,
transpile_mist_no_sem,
};
static MARKER_COUNTER: AtomicU64 = AtomicU64::new(0); static MARKER_COUNTER: AtomicU64 = AtomicU64::new(0);
const KEYWORDS: [&'static str; 22] = [ const KEYWORDS: [&'static str; 23] = [
"if", "if",
"else", "else",
"for", "for",
@@ -45,6 +50,7 @@ const KEYWORDS: [&'static str; 22] = [
"override", "override",
"module ", "module ",
"void ", "void ",
"virtual ",
]; ];
fn keyword_completion_items() -> impl Iterator<Item = CompletionItem> { fn keyword_completion_items() -> impl Iterator<Item = CompletionItem> {
@@ -88,7 +94,7 @@ fn clean_lsp_url(path: &std::path::Path) -> Option<Url> {
} }
fn lsp_pos_to_rust_map(pos: &Position) -> RustMap { fn lsp_pos_to_rust_map(pos: &Position) -> RustMap {
RustMap(pos.line as usize + 1, pos.character as usize) RustMap(pos.line as usize, pos.character as usize)
} }
fn mist_map_to_lsp_pos(map: &MistMap) -> Position { fn mist_map_to_lsp_pos(map: &MistMap) -> Position {
@@ -183,6 +189,10 @@ fn find_marker_position(content: &str, marker: &str) -> Option<Position> {
Some(Position { line, character }) Some(Position { line, character })
} }
fn is_mist_file(path: &Path) -> bool {
path.extension().and_then(|e| e.to_str()) == Some("mist")
}
fn mist_to_rust_path(mist_path: &Path) -> PathBuf { fn mist_to_rust_path(mist_path: &Path) -> PathBuf {
let mut path = mist_path.to_path_buf(); let mut path = mist_path.to_path_buf();
path.set_extension("rs"); path.set_extension("rs");
@@ -386,11 +396,14 @@ impl Backend {
// Fallback: the source has a syntax error that prevents transpilation. // Fallback: the source has a syntax error that prevents transpilation.
// Use the last known working Rust content + mapping to inject the // Use the last known working Rust content + mapping to inject the
// marker at the nearest valid Rust position. // marker at the nearest valid Rust position.
let mist_target = MistMap(line as usize + 1, character as usize); let mist_target = MistMap(line as usize, character as usize);
let mut rust_path = crate::from_mist_to_rust(mist_path.to_path_buf()); let source_for_path = self
if mist_path.file_name().and_then(|n| n.to_str()) == Some("package.mist") { .documents
rust_path.set_file_name("mod.rs"); .lock()
} .await
.get(mist_path)
.map(|r| r.to_string());
let rust_path = resolve_rust_path(mist_path, source_for_path.as_deref());
let (last_mapping, last_content, original_version) = { let (last_mapping, last_content, original_version) = {
let map_guard = self.mapping.lock().await; let map_guard = self.mapping.lock().await;
@@ -412,7 +425,7 @@ impl Backend {
.map(|(rust, _)| *rust); .map(|(rust, _)| *rust);
let (modified_rust, rust_pos) = if let Some(next_rust) = rust_next { let (modified_rust, rust_pos) = if let Some(next_rust) = rust_next {
let lsp_line = (next_rust.0 - 1) as u32; let lsp_line = next_rust.0 as u32;
let lsp_col = next_rust.1 as u32; let lsp_col = next_rust.1 as u32;
let modified = inject_marker_at(&last_content, lsp_line, lsp_col, &marker)?; let modified = inject_marker_at(&last_content, lsp_line, lsp_col, &marker)?;
let pos = find_marker_position(&modified, &marker)?; let pos = find_marker_position(&modified, &marker)?;
@@ -519,8 +532,9 @@ impl Backend {
Some(p) => p.clone(), Some(p) => p.clone(),
None => return String::new(), None => return String::new(),
}; };
let src_root = ws.join("src"); let crate_root = find_crate_root(mist_path).unwrap_or(ws);
let package_mist = read_mist_package(&ws); let src_root = crate_root.join("src");
let package_mist = read_mist_package(&crate_root);
let docs = self.documents.lock().await; let docs = self.documents.lock().await;
let mod_decls = compute_mod_decls(&docs, &src_root, &package_mist); let mod_decls = compute_mod_decls(&docs, &src_root, &package_mist);
mod_decls.get(mist_path).cloned().unwrap_or_default() mod_decls.get(mist_path).cloned().unwrap_or_default()
@@ -547,7 +561,7 @@ impl Backend {
mist_path: &Path, mist_path: &Path,
source: &str, source: &str,
extra_mod_decl: &str, extra_mod_decl: &str,
) { ) -> Option<TranspiledFile> {
let transpiled = match transpile_mist(mist_path, source, extra_mod_decl) { let transpiled = match transpile_mist(mist_path, source, extra_mod_decl) {
Ok(t) => t, Ok(t) => t,
Err(e) => { Err(e) => {
@@ -556,7 +570,7 @@ impl Backend {
if let Some(uri) = clean_lsp_url(mist_path) { if let Some(uri) = clean_lsp_url(mist_path) {
self.publish_diagnostics(uri, diag).await; self.publish_diagnostics(uri, diag).await;
} }
return; return None;
} }
}; };
@@ -577,28 +591,77 @@ impl Backend {
let _ = ra.did_open(rust_uri, &transpiled.rust_content).await; let _ = ra.did_open(rust_uri, &transpiled.rust_content).await;
} }
map.insert(transpiled.rust_path, transpiled.mapping); map.insert(transpiled.rust_path.clone(), transpiled.mapping.clone());
self.last_rust_contents self.last_rust_contents
.lock() .lock()
.await .await
.insert(mist_path.to_path_buf(), transpiled.rust_content); .insert(mist_path.to_path_buf(), transpiled.rust_content.clone());
} }
// Clear any previous diagnostics — transpile succeeded. // Clear any previous diagnostics — transpile succeeded.
if let Some(mist_uri) = clean_lsp_url(mist_path) { if let Some(mist_uri) = clean_lsp_url(mist_path) {
self.publish_diagnostics(mist_uri, vec![]).await; self.publish_diagnostics(mist_uri, vec![]).await;
} }
Some(transpiled)
} }
async fn rebuild_module_tree(&self) { async fn rebuild_module_tree(&self, persist: bool) {
let ws = match &*self.workspace_folder.lock().await { let ws = match &*self.workspace_folder.lock().await {
Some(p) => p.clone(), Some(p) => p.clone(),
None => return, None => return,
}; };
let src_root = ws.join("src");
let package_mist = read_mist_package(&ws);
self.ensure_implicit_packages(&src_root).await; // Prune documents whose source files no longer exist on disk.
// Without this, deleted .mist files linger in the in-memory map
// and compute_mod_decls keeps generating stale pub mod declarations.
// Also clean up stale synthetic package.mist entries and generated
// mod.rs output files for directories with no remaining source children.
{
let mut docs = self.documents.lock().await;
let stale: Vec<PathBuf> = docs.keys().filter(|p| !p.exists()).cloned().collect();
for path in &stale {
let source_for_path = docs.get(path).map(|r| r.to_string());
docs.remove(path);
if path.extension().and_then(|e| e.to_str()) == Some("mist") {
let rust_path = resolve_rust_path(path, source_for_path.as_deref());
let _ = std::fs::remove_file(&rust_path);
let map_path = rust_path.with_extension("map.json");
let _ = std::fs::remove_file(&map_path);
}
}
// Also remove synthetic package.mist entries whose directories
// no longer contain any real .mist source files.
let synthetic: Vec<PathBuf> = docs
.keys()
.filter(|p| {
p.file_name().and_then(|n| n.to_str()) == Some("package.mist") && !p.exists()
})
.cloned()
.collect();
for path in &synthetic {
docs.remove(path);
}
drop(docs);
// Clean up auxiliary state for deleted files
let mut versions = self.doc_versions.lock().await;
let mut contents = self.last_rust_contents.lock().await;
let mut diags = self.previous_diagnostics.lock().await;
let all_stale: Vec<PathBuf> = stale.into_iter().chain(synthetic).collect();
let stale_set: std::collections::HashSet<_> = all_stale.iter().collect();
versions.retain(|k, _| !stale_set.contains(k));
contents.retain(|k, _| !stale_set.contains(k));
for path in &all_stale {
if let Some(uri) = clean_lsp_url(path) {
diags.remove(&uri);
}
}
}
self.ensure_implicit_packages().await;
// Collect sources first to avoid holding locks during transpile // Collect sources first to avoid holding locks during transpile
let sources: Vec<(PathBuf, String, String)> = { let sources: Vec<(PathBuf, String, String)> = {
@@ -606,25 +669,76 @@ impl Backend {
if docs.is_empty() { if docs.is_empty() {
return; return;
} }
let mod_decls = compute_mod_decls(&docs, &src_root, &package_mist); let crate_roots = collect_crate_roots(&docs, &ws);
mod_decls let mut result = Vec::new();
.into_iter() for crate_root in &crate_roots {
.filter(|(_, decl)| !decl.is_empty()) let src_root = crate_root.join("src");
.filter_map(|(path, decl)| docs.get(&path).map(|r| (path, r.to_string(), decl))) let package_mist = read_mist_package(crate_root);
.collect() let mod_decls = compute_mod_decls(&docs, &src_root, &package_mist);
for (path, decl) in mod_decls {
// Include files that have mod declarations OR that are
// already known package/directory entries in the doc map.
// The latter case covers parents whose children were
// deleted — they need a regenerated (empty) mod.rs.
if !decl.is_empty() || docs.contains_key(&path) {
if let Some(source) = docs.get(&path) {
result.push((path.clone(), source.to_string(), decl));
}
}
}
}
result
}; };
for (mist_path, source, decl) in sources { for (mist_path, source, decl) in sources {
self.handle_transpile_and_notify(&mist_path, &source, &decl) if let Some(transpiled) = self
.await; .handle_transpile_and_notify(&mist_path, &source, &decl)
.await
{
if persist {
Self::write_transpiled_to_disk(&transpiled);
}
}
} }
} }
/// Create synthetic `package.mist` documents for subdirectories that contain /// Create synthetic `package.mist` documents for subdirectories that contain
/// .mist files but no real package.mist (implicit packages). /// .mist files but no real package.mist (implicit packages).
async fn ensure_implicit_packages(&self, src_root: &Path) { async fn ensure_implicit_packages(&self) {
let ws = match &*self.workspace_folder.lock().await {
Some(p) => p.clone(),
None => return,
};
let mut docs = self.documents.lock().await; let mut docs = self.documents.lock().await;
ensure_implicit_packages_impl(&mut *docs, src_root); let crate_roots = collect_crate_roots(&docs, &ws);
for crate_root in &crate_roots {
let src_root = crate_root.join("src");
ensure_implicit_packages_impl(&mut *docs, &src_root);
}
}
/// Write a transpiled file's `.rs` content and `.map.json` mapping to disk.
/// The output directory is created if it doesn't exist.
fn write_transpiled_to_disk(transpiled: &TranspiledFile) {
if let Some(parent) = transpiled.rust_path.parent() {
if let Err(e) = std::fs::create_dir_all(parent) {
eprintln!("Failed to create output directory {:?}: {e}", parent);
return;
}
}
if let Err(e) = std::fs::write(&transpiled.rust_path, &transpiled.rust_content) {
eprintln!(
"Failed to write transpiled file {:?}: {e}",
transpiled.rust_path
);
return;
}
let map_path = transpiled.rust_path.with_extension("map.json");
if let Ok(map_json) = serde_json::to_string(&transpiled.mapping) {
if let Err(e) = std::fs::write(&map_path, &map_json) {
eprintln!("Failed to write mapping file {:?}: {e}", map_path);
}
}
} }
async fn publish_diagnostics(&self, uri: Url, diagnostics: Vec<Diagnostic>) { async fn publish_diagnostics(&self, uri: Url, diagnostics: Vec<Diagnostic>) {
@@ -730,8 +844,6 @@ impl LanguageServer for Backend {
tokio::spawn(async move { tokio::spawn(async move {
if let Some(root) = &*ws.lock().await { if let Some(root) = &*ws.lock().await {
let src_root = root.join("src");
// Hold the lock across both initialize + initialized so the // Hold the lock across both initialize + initialized so the
// editor's didOpen cannot race in-between and send notifications // editor's didOpen cannot race in-between and send notifications
// to rust-analyzer before it has received Initialized. // to rust-analyzer before it has received Initialized.
@@ -748,7 +860,21 @@ impl LanguageServer for Backend {
} }
let mut files = Vec::new(); let mut files = Vec::new();
collect_mist_files(&src_root, &mut files); collect_mist_files(&root.join("src"), &mut files);
// Discover subcrates by scanning for Mist.toml in immediate
// subdirectories of the workspace root.
let mut subcrate_roots = Vec::new();
if let Ok(entries) = std::fs::read_dir(root) {
for entry in entries.flatten() {
let entry_path = entry.path();
if entry_path.is_dir() && entry_path.join("Mist.toml").is_file() {
let sub_src = entry_path.join("src");
collect_mist_files(&sub_src, &mut files);
subcrate_roots.push(entry_path);
}
}
}
for file in &files { for file in &files {
if let Ok(text) = std::fs::read_to_string(file) { if let Ok(text) = std::fs::read_to_string(file) {
@@ -761,15 +887,32 @@ impl LanguageServer for Backend {
eprintln!("Loaded {} mist files", files.len()); eprintln!("Loaded {} mist files", files.len());
ensure_implicit_packages_impl(&mut *documents.lock().await, &src_root); // Create implicit packages per crate root
{
let mut docs = documents.lock().await;
let all_crate_roots: Vec<PathBuf> = std::iter::once(root.clone())
.chain(subcrate_roots.iter().cloned())
.collect();
for crate_root in &all_crate_roots {
ensure_implicit_packages_impl(&mut *docs, &crate_root.join("src"));
}
}
// Compute mod_decls before any transpile so the root file gets its // Compute mod_decls per crate root before any transpile so each
// child module declarations from the very first didOpen. // crate's root file gets its child module declarations.
let package_mist = read_mist_package(root); let all_crate_roots: Vec<PathBuf> = std::iter::once(root.clone())
let src_root = root.join("src"); .chain(subcrate_roots.iter().cloned())
.collect();
let mod_decls = { let mod_decls = {
let docs = documents.lock().await; let docs = documents.lock().await;
compute_mod_decls(&docs, &src_root, &package_mist) let mut merged = HashMap::new();
for crate_root in &all_crate_roots {
let cr_src = crate_root.join("src");
let pkg = read_mist_package(crate_root);
let decls = compute_mod_decls(&docs, &cr_src, &pkg);
merged.extend(decls);
}
merged
}; };
for file in &files { for file in &files {
@@ -819,14 +962,22 @@ impl LanguageServer for Backend {
.cloned() .cloned()
.collect(); .collect();
for syn_path in &synthetic_paths { for syn_path in &synthetic_paths {
let decl = mod_decls.get(syn_path).map(String::as_str).unwrap_or(""); // Find the crate root for this synthetic file
let syn_crate_root = find_crate_root(syn_path).unwrap_or_else(|| root.clone());
let pkg = read_mist_package(&syn_crate_root);
let cr_src = syn_crate_root.join("src");
let decl = {
let docs = documents.lock().await;
let crate_decls = compute_mod_decls(&docs, &cr_src, &pkg);
crate_decls.get(syn_path).cloned().unwrap_or_default()
};
let source = documents let source = documents
.lock() .lock()
.await .await
.get(syn_path) .get(syn_path)
.map(|r| r.to_string()) .map(|r| r.to_string())
.unwrap_or_default(); .unwrap_or_default();
if let Ok(transpiled) = transpile_mist(syn_path, &source, decl) { if let Ok(transpiled) = transpile_mist(syn_path, &source, &decl) {
if let Some(rust_uri) = clean_lsp_url(&transpiled.rust_path) { if let Some(rust_uri) = clean_lsp_url(&transpiled.rust_path) {
let _ = ra let _ = ra
.lock() .lock()
@@ -942,6 +1093,18 @@ impl LanguageServer for Backend {
return; return;
} }
}; };
// Non-.mist files pass through directly to rust-analyzer.
if !is_mist_file(&mist_path) {
let _ = self
.rust_analyzer
.lock()
.await
.did_open(uri, &params.text_document.text)
.await;
return;
}
let source = params.text_document.text; let source = params.text_document.text;
self.documents self.documents
@@ -949,9 +1112,10 @@ impl LanguageServer for Backend {
.await .await
.insert(mist_path.clone(), Rope::from_str(&source)); .insert(mist_path.clone(), Rope::from_str(&source));
self.handle_transpile_and_notify(&mist_path, &source, "") let extra_decl = self.compute_mod_decl_for_file(&mist_path).await;
self.handle_transpile_and_notify(&mist_path, &source, &extra_decl)
.await; .await;
self.rebuild_module_tree().await; self.rebuild_module_tree(false).await;
} }
async fn did_change(&self, params: DidChangeTextDocumentParams) { async fn did_change(&self, params: DidChangeTextDocumentParams) {
@@ -963,6 +1127,18 @@ impl LanguageServer for Backend {
} }
}; };
if !is_mist_file(&mist_path) {
if let Some(change) = params.content_changes.into_iter().last() {
let _ = self
.rust_analyzer
.lock()
.await
.did_change(uri, &change.text, params.text_document.version)
.await;
}
return;
}
if let Some(change) = params.content_changes.into_iter().last() { if let Some(change) = params.content_changes.into_iter().last() {
let source = change.text; let source = change.text;
@@ -971,9 +1147,10 @@ impl LanguageServer for Backend {
.await .await
.insert(mist_path.clone(), Rope::from_str(&source)); .insert(mist_path.clone(), Rope::from_str(&source));
self.handle_transpile_and_notify(&mist_path, &source, "") let extra_decl = self.compute_mod_decl_for_file(&mist_path).await;
self.handle_transpile_and_notify(&mist_path, &source, &extra_decl)
.await; .await;
self.rebuild_module_tree().await; self.rebuild_module_tree(false).await;
} }
} }
@@ -986,16 +1163,40 @@ impl LanguageServer for Backend {
} }
}; };
if let Some(text) = params.text { let text = if let Some(text) = &params.text {
self.documents text.clone()
.lock() } else {
tokio::fs::read_to_string(&mist_path)
.await .await
.insert(mist_path.clone(), Rope::from_str(&text)); .expect("Failed to read file")
};
self.handle_transpile_and_notify(&mist_path, &text, "") if !is_mist_file(&mist_path) {
.await; let _ = self.rust_analyzer.lock().await.did_save(uri, &text).await;
self.rebuild_module_tree().await; return;
} }
self.documents
.lock()
.await
.insert(mist_path.clone(), Rope::from_str(&text));
let extra_decl = self.compute_mod_decl_for_file(&mist_path).await;
if let Some(transpiled) = self
.handle_transpile_and_notify(&mist_path, &text, &extra_decl)
.await
{
Self::write_transpiled_to_disk(&transpiled);
if let Some(rust_uri) = clean_lsp_url(&transpiled.rust_path) {
let _ = self
.rust_analyzer
.lock()
.await
.did_save(rust_uri, &transpiled.rust_content)
.await;
}
}
self.rebuild_module_tree(true).await;
} }
async fn did_close(&self, params: DidCloseTextDocumentParams) { async fn did_close(&self, params: DidCloseTextDocumentParams) {
@@ -1007,16 +1208,22 @@ impl LanguageServer for Backend {
} }
}; };
if !is_mist_file(&mist_path) {
let _ = self.rust_analyzer.lock().await.did_close(uri).await;
return;
}
let source_for_path = {
let docs = self.documents.lock().await;
docs.get(&mist_path).map(|r| r.to_string())
};
self.documents.lock().await.remove(&mist_path); self.documents.lock().await.remove(&mist_path);
let rust_path = mist_to_rust_path(&mist_path); let rust_path = resolve_rust_path(&mist_path, source_for_path.as_deref());
self.mapping.lock().await.remove(&rust_path);
if let Some(rust_uri) = clean_lsp_url(&rust_path) { if let Some(rust_uri) = clean_lsp_url(&rust_path) {
let _ = self.rust_analyzer.lock().await.did_close(rust_uri).await; let _ = self.rust_analyzer.lock().await.did_close(rust_uri).await;
} }
self.publish_diagnostics(uri, Vec::new()).await;
} }
async fn completion( async fn completion(
@@ -1030,6 +1237,25 @@ impl LanguageServer for Backend {
Ok(p) => p, Ok(p) => p,
Err(_) => return Ok(None), Err(_) => return Ok(None),
}; };
// Non-.mist files pass through directly to rust-analyzer.
if !is_mist_file(&mist_path) {
let result = self
.rust_analyzer
.lock()
.await
.request::<lsp_types::request::Completion>(params)
.await;
return match result {
Ok(r) => Ok(r),
Err(e) => {
eprintln!("completion error (passthrough): {e}");
Ok(None)
}
};
}
let source = match self.documents.lock().await.get(&mist_path) { let source = match self.documents.lock().await.get(&mist_path) {
Some(r) => r.to_string(), Some(r) => r.to_string(),
None => return Ok(None), None => return Ok(None),
@@ -1053,6 +1279,11 @@ impl LanguageServer for Backend {
return Ok(None); return Ok(None);
}; };
let trigger_char: Option<String> = params
.context
.as_ref()
.and_then(|c| c.trigger_character.clone());
let comp_params = CompletionParams { let comp_params = CompletionParams {
text_document_position: TextDocumentPositionParams { text_document_position: TextDocumentPositionParams {
text_document: TextDocumentIdentifier { uri: rust_uri }, text_document: TextDocumentIdentifier { uri: rust_uri },
@@ -1080,13 +1311,13 @@ impl LanguageServer for Backend {
match completion_result { match completion_result {
Ok(Some(CompletionResponse::Array(mut items))) => { Ok(Some(CompletionResponse::Array(mut items))) => {
mist_ify_completions(&source, &mist_pos, &mut items); mist_ify_completions(&source, &mist_pos, &mut items, trigger_char.as_deref());
Ok(Some(CompletionResponse::Array(items))) Ok(Some(CompletionResponse::Array(items)))
} }
Ok(Some(CompletionResponse::List(mut list))) => { Ok(Some(CompletionResponse::List(mut list))) => {
mist_ify_completions(&source, &mist_pos, &mut list.items); mist_ify_completions(&source, &mist_pos, &mut list.items, trigger_char.as_deref());
Ok(Some(CompletionResponse::List(list))) Ok(Some(CompletionResponse::List(list)))
} }
@@ -1102,7 +1333,7 @@ impl LanguageServer for Backend {
&self, &self,
params: GotoDefinitionParams, params: GotoDefinitionParams,
) -> tower_lsp::jsonrpc::Result<Option<GotoDefinitionResponse>> { ) -> tower_lsp::jsonrpc::Result<Option<GotoDefinitionResponse>> {
let mist_uri = params.text_document_position_params.text_document.uri; let mist_uri = &params.text_document_position_params.text_document.uri;
let mist_pos = params.text_document_position_params.position; let mist_pos = params.text_document_position_params.position;
eprintln!( eprintln!(
"[goto_definition] entered at mist ({}, {})", "[goto_definition] entered at mist ({}, {})",
@@ -1113,6 +1344,25 @@ impl LanguageServer for Backend {
Ok(p) => p, Ok(p) => p,
Err(_) => return Ok(None), Err(_) => return Ok(None),
}; };
// Non-.mist files pass through directly to rust-analyzer.
if !is_mist_file(&mist_path) {
let result = self
.rust_analyzer
.lock()
.await
.request::<lsp_types::request::GotoDefinition>(params)
.await;
return match result {
Ok(r) => Ok(r),
Err(e) => {
eprintln!("goto_definition error (passthrough): {e}");
Ok(None)
}
};
}
let source = match self.documents.lock().await.get(&mist_path) { let source = match self.documents.lock().await.get(&mist_path) {
Some(r) => r.to_string(), Some(r) => r.to_string(),
None => return Ok(None), None => return Ok(None),
@@ -1282,13 +1532,32 @@ impl LanguageServer for Backend {
} }
async fn hover(&self, params: HoverParams) -> tower_lsp::jsonrpc::Result<Option<Hover>> { async fn hover(&self, params: HoverParams) -> tower_lsp::jsonrpc::Result<Option<Hover>> {
let mist_uri = params.text_document_position_params.text_document.uri; let mist_uri = &params.text_document_position_params.text_document.uri;
let mist_pos = params.text_document_position_params.position; let mist_pos = params.text_document_position_params.position;
let mist_path = match mist_uri.to_file_path() { let mist_path = match mist_uri.to_file_path() {
Ok(p) => p, Ok(p) => p,
Err(_) => return Ok(None), Err(_) => return Ok(None),
}; };
// Non-.mist files pass through directly to rust-analyzer.
if !is_mist_file(&mist_path) {
let result = self
.rust_analyzer
.lock()
.await
.request::<lsp_types::request::HoverRequest>(params)
.await;
return match result {
Ok(r) => Ok(r),
Err(e) => {
eprintln!("hover error (passthrough): {e}");
Ok(None)
}
};
}
let source = match self.documents.lock().await.get(&mist_path) { let source = match self.documents.lock().await.get(&mist_path) {
Some(r) => r.to_string(), Some(r) => r.to_string(),
None => return Ok(None), None => return Ok(None),
@@ -1352,6 +1621,24 @@ impl LanguageServer for Backend {
.to_file_path() .to_file_path()
.map_err(|_| tower_lsp::jsonrpc::Error::internal_error())?; .map_err(|_| tower_lsp::jsonrpc::Error::internal_error())?;
// Non-.mist files pass through directly to rust-analyzer.
if !is_mist_file(&mist_path) {
let result = self
.rust_analyzer
.lock()
.await
.request::<lsp_types::request::Formatting>(params)
.await;
return match result {
Ok(r) => Ok(r),
Err(e) => {
eprintln!("formatting error (passthrough): {e}");
Ok(None)
}
};
}
let docs = self.documents.lock().await; let docs = self.documents.lock().await;
let old_text = docs let old_text = docs
@@ -1381,10 +1668,13 @@ impl LanguageServer for Backend {
} }
} }
fn mist_ify_completions(source: &str, pos: &Position, items: &mut Vec<CompletionItem>) { fn mist_ify_completions(
let existing: HashSet<String> = items.iter().map(|item| item.label.clone()).collect(); source: &str,
pos: &Position,
items.extend(keyword_completion_items().filter(|item| !existing.contains(&item.label))); items: &mut Vec<CompletionItem>,
trigger_char: Option<&str>,
) {
let is_scoping_trigger = matches!(trigger_char, Some("." | ":"));
for item in items.iter_mut() { for item in items.iter_mut() {
item.text_edit = None; item.text_edit = None;
@@ -1396,6 +1686,14 @@ fn mist_ify_completions(source: &str, pos: &Position, items: &mut Vec<Completion
} }
} }
if is_scoping_trigger {
return;
}
let existing: HashSet<String> = items.iter().map(|item| item.label.clone()).collect();
items.extend(keyword_completion_items().filter(|item| !existing.contains(&item.label)));
let curr_scope = current_scope(&source, pos.line, pos.character); let curr_scope = current_scope(&source, pos.line, pos.character);
// ========================= // =========================
@@ -1523,11 +1821,11 @@ fn mist_ify_completions(source: &str, pos: &Position, items: &mut Vec<Completion
// Struct/Class // Struct/Class
// ========================= // =========================
if matches!(curr_scope, Scope::Struct) { if matches!(curr_scope, Scope::Class) {
items.push(CompletionItem { items.push(CompletionItem {
label: "field".into(), label: "virtual".into(),
kind: Some(CompletionItemKind::SNIPPET), kind: Some(CompletionItemKind::SNIPPET),
insert_text: Some("$1 $2,".into()), insert_text: Some("pub virtual $1 $2()\n{\n\t$3\n}".into()),
insert_text_format: Some(InsertTextFormat::SNIPPET), insert_text_format: Some(InsertTextFormat::SNIPPET),
sort_text: Some("0000".into()), sort_text: Some("0000".into()),
preselect: Some(true), preselect: Some(true),
@@ -1535,9 +1833,19 @@ fn mist_ify_completions(source: &str, pos: &Position, items: &mut Vec<Completion
}); });
items.push(CompletionItem { items.push(CompletionItem {
label: "pub field".into(), label: "override".into(),
kind: Some(CompletionItemKind::SNIPPET), kind: Some(CompletionItemKind::SNIPPET),
insert_text: Some("pub $1 $2,".into()), insert_text: Some("$1 $2() override$3\n{\n\t$4\n}".into()),
insert_text_format: Some(InsertTextFormat::SNIPPET),
sort_text: Some("0000".into()),
preselect: Some(true),
..Default::default()
});
items.push(CompletionItem {
label: "virtual override".into(),
kind: Some(CompletionItemKind::SNIPPET),
insert_text: Some("pub virtual $1 $2() override$3\n{\n\t$4\n}".into()),
insert_text_format: Some(InsertTextFormat::SNIPPET), insert_text_format: Some(InsertTextFormat::SNIPPET),
sort_text: Some("0000".into()), sort_text: Some("0000".into()),
preselect: Some(true), preselect: Some(true),
@@ -1545,7 +1853,7 @@ fn mist_ify_completions(source: &str, pos: &Position, items: &mut Vec<Completion
}); });
} }
if matches!(curr_scope, Scope::Class) { if matches!(curr_scope, Scope::Class | Scope::Struct) {
items.push(CompletionItem { items.push(CompletionItem {
label: "field".into(), label: "field".into(),
kind: Some(CompletionItemKind::SNIPPET), kind: Some(CompletionItemKind::SNIPPET),
@@ -1575,7 +1883,7 @@ fn mist_ify_completions(source: &str, pos: &Position, items: &mut Vec<Completion
items.push(CompletionItem { items.push(CompletionItem {
label: "variant".into(), label: "variant".into(),
kind: Some(CompletionItemKind::SNIPPET), kind: Some(CompletionItemKind::SNIPPET),
insert_text: Some("$1,".into()), insert_text: Some("$1;".into()),
insert_text_format: Some(InsertTextFormat::SNIPPET), insert_text_format: Some(InsertTextFormat::SNIPPET),
sort_text: Some("0000".into()), sort_text: Some("0000".into()),
preselect: Some(true), preselect: Some(true),
@@ -1585,7 +1893,7 @@ fn mist_ify_completions(source: &str, pos: &Position, items: &mut Vec<Completion
items.push(CompletionItem { items.push(CompletionItem {
label: "tuple variant".into(), label: "tuple variant".into(),
kind: Some(CompletionItemKind::SNIPPET), kind: Some(CompletionItemKind::SNIPPET),
insert_text: Some("$1($2),".into()), insert_text: Some("$1($2);".into()),
insert_text_format: Some(InsertTextFormat::SNIPPET), insert_text_format: Some(InsertTextFormat::SNIPPET),
sort_text: Some("0000".into()), sort_text: Some("0000".into()),
preselect: Some(true), preselect: Some(true),
@@ -1595,7 +1903,7 @@ fn mist_ify_completions(source: &str, pos: &Position, items: &mut Vec<Completion
items.push(CompletionItem { items.push(CompletionItem {
label: "struct variant".into(), label: "struct variant".into(),
kind: Some(CompletionItemKind::SNIPPET), kind: Some(CompletionItemKind::SNIPPET),
insert_text: Some("$1\n{\n\t$0\n},".into()), insert_text: Some("$1\n{\n\t$0\n};".into()),
insert_text_format: Some(InsertTextFormat::SNIPPET), insert_text_format: Some(InsertTextFormat::SNIPPET),
sort_text: Some("0000".into()), sort_text: Some("0000".into()),
preselect: Some(true), preselect: Some(true),
@@ -1750,7 +2058,7 @@ async fn handle_ra_notifications(
character: if mist_end_pos.line == mist_start_pos.line { character: if mist_end_pos.line == mist_start_pos.line {
mist_end_pos.character.max(mist_start_pos.character + 1) mist_end_pos.character.max(mist_start_pos.character + 1)
} else { } else {
mist_end_pos.character.max(1) mist_end_pos.character.max(0)
}, },
}; };
@@ -1775,17 +2083,13 @@ async fn handle_ra_notifications(
}; };
if let Some(mist_uri) = mist_uri.or(Some(rust_uri.clone())) { if let Some(mist_uri) = mist_uri.or(Some(rust_uri.clone())) {
let prev = previous_diagnostics.lock().await.get(&mist_uri).cloned(); previous_diagnostics
.lock()
if prev.as_ref() != Some(&mapped_diagnostics) { .await
previous_diagnostics .insert(mist_uri.clone(), mapped_diagnostics.clone());
.lock() client
.await .publish_diagnostics(mist_uri, mapped_diagnostics, None)
.insert(mist_uri.clone(), mapped_diagnostics.clone()); .await;
client
.publish_diagnostics(mist_uri, mapped_diagnostics, None)
.await;
}
} }
} }
} }
@@ -1842,13 +2146,6 @@ fn read_mist_package(workspace_root: &Path) -> String {
.unwrap_or_else(|| "main.mist".to_string()) .unwrap_or_else(|| "main.mist".to_string())
} }
#[derive(Debug, Serialize, Deserialize)]
pub struct MistConfig {
pub package: PathBuf,
pub packages: Vec<PathBuf>,
pub fmt: Option<MistFmtConfig>,
}
fn read_mist_fmt(workspace_root: &Path) -> Option<MistFmtConfig> { fn read_mist_fmt(workspace_root: &Path) -> Option<MistFmtConfig> {
let toml_path = workspace_root.join("Mist.toml"); let toml_path = workspace_root.join("Mist.toml");
let content = match std::fs::read_to_string(&toml_path) { let content = match std::fs::read_to_string(&toml_path) {
@@ -1860,9 +2157,31 @@ fn read_mist_fmt(workspace_root: &Path) -> Option<MistFmtConfig> {
} }
}; };
toml::from_str::<MistConfig>(&content) toml::from_str::<MistConfig>(&content).ok().map(|v| v.fmt)
.ok() }
.and_then(|v| v.fmt)
fn find_crate_root(file_path: &Path) -> Option<PathBuf> {
let mut dir = file_path.parent()?;
loop {
if dir.join("Mist.toml").is_file() {
return Some(dir.to_path_buf());
}
dir = match dir.parent() {
Some(p) => p,
None => return None,
};
}
}
fn collect_crate_roots(docs: &HashMap<PathBuf, Rope>, workspace_root: &Path) -> HashSet<PathBuf> {
let mut roots = HashSet::new();
roots.insert(workspace_root.to_path_buf());
for path in docs.keys() {
if let Some(crate_root) = find_crate_root(path) {
roots.insert(crate_root);
}
}
roots
} }
fn compute_mod_decls( fn compute_mod_decls(
@@ -1889,20 +2208,27 @@ fn compute_mod_decls(
let mut sorted = files.clone(); let mut sorted = files.clone();
sorted.sort(); sorted.sort();
for file in &sorted { for file in &sorted {
let stem = match file.file_stem().and_then(|s| s.to_str()) {
Some(s) => s,
None => continue,
};
// Skip the root package entry file (e.g. main.mist) // Skip the root package entry file (e.g. main.mist)
if *file == package_path { if *file == package_path {
continue; continue;
} }
// Skip any subdirectory package.mist — it's the directory's own // Skip any subdirectory package.mist — it's the directory's own
// entry file, not a sibling submodule. // entry file, not a sibling submodule.
if stem == "package" { if file.file_name().and_then(|s| s.to_str()) == Some("package.mist") {
continue; continue;
} }
mod_decl.push_str(&format!("pub mod {};\n", stem)); // Use the module declaration from the file if present,
// otherwise fall back to file stem.
let mod_name = documents
.get(file)
.and_then(|rope| {
let source = rope.to_string();
parse_module(&source).ok().flatten().map(|(_, name)| name.0)
})
.or_else(|| file.file_stem().and_then(|s| s.to_str().map(String::from)));
if let Some(name) = mod_name {
mod_decl.push_str(&format!("pub mod {};\n", name));
}
} }
if dir == src_root { if dir == src_root {
+17
View File
@@ -374,6 +374,23 @@ impl RustAnalyzer {
.await .await
} }
pub async fn did_save(
&mut self,
uri: Url,
text: &str,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
self.notify(
"textDocument/didSave",
json!({
"textDocument": {
"uri": uri,
},
"text": text,
}),
)
.await
}
pub async fn did_close( pub async fn did_close(
&mut self, &mut self,
uri: Url, uri: Url,
+41 -22
View File
@@ -1,9 +1,8 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use mist_codegen::RustCodegen; use mist_codegen::RustCodegen;
use mist_codegen::fmt::MistCodegen;
use mist_parser::rev_mapper::Mapping; use mist_parser::rev_mapper::Mapping;
use mist_parser::{MistFmtConfig, parse}; use mist_parser::{MistFmtConfig, parse, parse_module};
pub struct TranspiledFile { pub struct TranspiledFile {
pub mist_path: PathBuf, pub mist_path: PathBuf,
@@ -18,18 +17,43 @@ pub enum TranspileError<'a> {
Semantic(Vec<mist_parser::semantics::SemanticError>), Semantic(Vec<mist_parser::semantics::SemanticError>),
} }
/// Resolve the output `.rs` path for a `.mist` source file.
///
/// Always checks for a `pub module x;` declaration via `parse_module` and
/// uses the declared module name as the filename when present. Falls back
/// to the literal file stem when parsing fails or the file has no module
/// declaration.
pub fn resolve_rust_path(mist_path: &Path, source: Option<&str>) -> PathBuf {
let mut rust_path = crate::from_mist_to_rust(mist_path.to_path_buf());
// Package files (package.mist) must output as <dir>/mod.rs so the Rust
// module hierarchy resolves correctly.
if mist_path.file_name().and_then(|n| n.to_str()) == Some("package.mist") {
rust_path.set_file_name("mod.rs");
} else if let Some(src) = source {
// Prioritize the module declaration from the source over the file stem.
// Every caller re-parses so we always pick up the current declaration.
if let Ok(Some((_, ref name))) = parse_module(src) {
if rust_path.file_name().map(|v| v.to_str()).unwrap_or_default() != Some("mod.rs") {
if let Some(ext) = rust_path.extension().map(|e| e.to_owned()) {
let mut new_name = std::ffi::OsString::from(&name.0);
new_name.push(".");
new_name.push(ext);
rust_path.set_file_name(new_name);
} else {
rust_path.set_file_name(&name.0);
}
}
}
}
rust_path
}
pub fn transpile_mist<'a>( pub fn transpile_mist<'a>(
mist_path: &Path, mist_path: &Path,
source: &'a str, source: &'a str,
extra_mod_decl: &str, extra_mod_decl: &str,
) -> Result<TranspiledFile, TranspileError<'a>> { ) -> Result<TranspiledFile, TranspileError<'a>> {
let mut rust_path = crate::from_mist_to_rust(mist_path.to_path_buf()); let rust_path = resolve_rust_path(mist_path, Some(source));
// Package files (package.mist) must output as <dir>/mod.rs so the Rust module
// hierarchy resolves correctly (pub mod <child>; declarations look for sibling
// .rs files, and the parent module declaration looks for <dir>/mod.rs).
if mist_path.file_name().and_then(|n| n.to_str()) == Some("package.mist") {
rust_path.set_file_name("mod.rs");
}
let parsed = parse(source).map_err(TranspileError::Parse)?; let parsed = parse(source).map_err(TranspileError::Parse)?;
@@ -59,13 +83,7 @@ pub fn transpile_mist_no_sem(
source: &str, source: &str,
extra_mod_decl: &str, extra_mod_decl: &str,
) -> Result<TranspiledFile, String> { ) -> Result<TranspiledFile, String> {
let mut rust_path = crate::from_mist_to_rust(mist_path.to_path_buf()); let rust_path = resolve_rust_path(mist_path, Some(source));
// Package files (package.mist) must output as <dir>/mod.rs so the Rust module
// hierarchy resolves correctly (pub mod <child>; declarations look for sibling
// .rs files, and the parent module declaration looks for <dir>/mod.rs).
if mist_path.file_name().and_then(|n| n.to_str()) == Some("package.mist") {
rust_path.set_file_name("mod.rs");
}
let parsed = parse(source).map_err(|e| format!("parse error: {e:?}"))?; let parsed = parse(source).map_err(|e| format!("parse error: {e:?}"))?;
@@ -85,12 +103,13 @@ pub fn transpile_mist_no_sem(
}) })
} }
pub fn format_mist(source: &str, config: MistFmtConfig) -> Result<String, String> { pub fn format_mist(source: &str, _config: MistFmtConfig) -> Result<String, String> {
let parsed = parse(source).map_err(|e| format!("parse error: {e:?}"))?; // let parsed = parse(source).map_err(|e| format!("parse error: {e:?}"))?;
let mut codegen = MistCodegen::new(config); // let mut codegen = MistCodegen::new(config);
codegen.generate(parsed.mod_attributes); // codegen.generate(parsed.mod_attributes);
let output = codegen.generate(parsed.items); // let output = codegen.generate(parsed.items);
Ok(output) // Ok(output)
Ok(source.to_string())
} }
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "mist-codegen" name = "mist-codegen"
version = "0.3.7-alpha.0" version = "0.4.0"
edition = "2024" edition = "2024"
description = "The Mist programming language Rust code generator" description = "The Mist programming language Rust code generator"
license = "Apache-2.0" license = "Apache-2.0"
+32 -24
View File
@@ -16,7 +16,7 @@ pub struct ClassProcessedData {
items: Vec<ClassItem>, items: Vec<ClassItem>,
methods: Vec<Spanned<FunctionDecl>>, methods: Vec<Spanned<FunctionDecl>>,
v_table: Vec<Identifier>, v_table: Vec<Identifier>,
override_v_table: HashMap<Override, Spanned<Vec<Identifier>>>, override_v_table: HashMap<Override, Spanned<Vec<(Identifier, bool)>>>,
} }
impl ClassProcessedData { impl ClassProcessedData {
@@ -45,13 +45,13 @@ impl ClassProcessedData {
.collect::<Vec<Spanned<FunctionDecl>>>(); .collect::<Vec<Spanned<FunctionDecl>>>();
let mut v_table = Vec::new(); let mut v_table = Vec::new();
let mut override_v_table = std::collections::HashMap::new(); let mut override_v_table = HashMap::new();
for method in &methods { for method in &methods {
if matches!(method.item.visibility, Visibility::Public) { if matches!(method.item.visibility, Visibility::Public) {
match &method.item.is_override { match &method.item.is_override {
None => { None => {
if method.item.self_param.is_some() { if method.item.is_virtual && method.item.self_param.is_some() {
v_table.push(method.item.name.clone()); v_table.push(method.item.name.clone());
} }
} }
@@ -64,7 +64,7 @@ impl ClassProcessedData {
item: Vec::new(), item: Vec::new(),
}) })
.item .item
.push(method.item.name.clone()); .push((method.item.name.clone(), method.item.is_virtual));
} }
} }
} }
@@ -113,7 +113,7 @@ impl ClassProcessedData {
cg.add(&get_type_from_path(inherits).get_rust()); cg.add(&get_type_from_path(inherits).get_rust());
cg.addln(","); cg.addln(",");
} else { } else {
cg.add_indentedln("pub _vptr: &'static [*const std::ffi::c_void],"); cg.add_indentedln("pub _vptr: &'static [*const core::ffi::c_void],");
} }
for field in &self.fields { for field in &self.fields {
@@ -174,11 +174,11 @@ impl ClassProcessedData {
self.v_table.len() self.v_table.len()
)); ));
cg.add_indentedln("pub const __V_TABLE: &'static [*const std::ffi::c_void] = &{"); cg.add_indentedln("pub const __V_TABLE: &'static [*const core::ffi::c_void] = &{");
cg.indent += 1; cg.indent += 1;
if has_parent { if has_parent {
cg.add_indentedln("let mut table = [std::ptr::null(); Self::__V_COUNT];"); cg.add_indentedln("let mut table = [core::ptr::null(); Self::__V_COUNT];");
cg.add_indentedln(&format!("let parent_table = {}::__V_TABLE;", parent_path)); cg.add_indentedln(&format!("let parent_table = {}::__V_TABLE;", parent_path));
cg.add_indentedln( cg.add_indentedln(
@@ -191,20 +191,20 @@ impl ClassProcessedData {
.as_ref() .as_ref()
.unwrap_or(self.inherits.as_ref().unwrap()) .unwrap_or(self.inherits.as_ref().unwrap())
.get_rust(); .get_rust();
for method_ident in &overriden_method_idents.item { for (method_ident, is_virtual) in &overriden_method_idents.item {
cg.add_indentedln(&format!( cg.add_indentedln(&format!(
"table[{}::__FN_{}] = {}::__m_{} as *const std::ffi::c_void;", "table[{}::__FN_{}] = {}::{} as *const core::ffi::c_void;",
base_class_path, base_class_path,
method_ident.0.to_uppercase(), method_ident.0.to_uppercase(),
self.self_path.get_rust(), self.self_path.get_rust(),
method_ident.get_rust() get_virtual_name(method_ident, *is_virtual)
)); ));
} }
} }
for method_name in &self.v_table { for method_name in &self.v_table {
cg.add_indentedln(&format!( cg.add_indentedln(&format!(
"table[Self::__FN_{}] = Self::__m_{} as *const std::ffi::c_void;", "table[Self::__FN_{}] = Self::__m_{} as *const core::ffi::c_void;",
method_name.0.to_uppercase(), method_name.0.to_uppercase(),
method_name.get_rust() method_name.get_rust()
)); ));
@@ -216,7 +216,7 @@ impl ClassProcessedData {
cg.indent += 1; cg.indent += 1;
for method_name in &self.v_table { for method_name in &self.v_table {
cg.add_indentedln(&format!( cg.add_indentedln(&format!(
"Self::__m_{} as *const std::ffi::c_void,", "Self::__m_{} as *const core::ffi::c_void,",
method_name.get_rust() method_name.get_rust()
)); ));
} }
@@ -293,7 +293,7 @@ impl ClassProcessedData {
// Deref tests for override targets // Deref tests for override targets
if self.inherits.is_some() && !self.override_v_table.is_empty() { if self.inherits.is_some() && !self.override_v_table.is_empty() {
cg.add_indentedln("let this: &Self = &unsafe { std::mem::MaybeUninit::<Self>::zeroed().assume_init() };"); cg.add_indentedln("let this: &Self = &unsafe { core::mem::MaybeUninit::<Self>::zeroed().assume_init() };");
for (override_tier, v) in &self.override_v_table { for (override_tier, v) in &self.override_v_table {
if let Some(path) = &override_tier.0 { if let Some(path) = &override_tier.0 {
@@ -346,14 +346,15 @@ impl ClassProcessedData {
cg.addln(") -> Self {"); cg.addln(") -> Self {");
cg.indent += 1; cg.indent += 1;
cg.add_indentedln("let mut this: Self = unsafe { std::mem::MaybeUninit::<Self>::zeroed().assume_init() };"); cg.add_indentedln("let mut this: Self = unsafe { core::mem::MaybeUninit::<Self>::zeroed().assume_init() };");
cg.add_indentedln("this._vptr = &Self::__V_TABLE;"); cg.add_indentedln("this._vptr = &Self::__V_TABLE;");
for field in &self.fields { for field in &self.fields {
if let Some(init) = &field.item.init { if let Some(init) = &field.item.init {
field.gen_span(cg); field.gen_span(cg);
cg.add_indentedln(&format!("this.{} = ", field.item.decl.name.get_rust())); cg.add_indented(&format!("this.{} = ", field.item.decl.name.get_rust()));
init.gen_rust(ctx, cg); init.gen_rust(ctx, cg);
cg.addln(";");
} }
} }
@@ -362,7 +363,6 @@ impl ClassProcessedData {
if *i > 0 { if *i > 0 {
cg.add(", "); cg.add(", ");
} }
ctx.expr_ensure_semicolon = false;
param.name.gen_rust(ctx, cg); param.name.gen_rust(ctx, cg);
} }
cg.addln(");"); cg.addln(");");
@@ -380,6 +380,7 @@ impl ClassProcessedData {
column: constructor.column, column: constructor.column,
item: FunctionDecl { item: FunctionDecl {
visibility: constructor.item.visibility.clone(), visibility: constructor.item.visibility.clone(),
is_virtual: false,
is_override: None, is_override: None,
name: Identifier(String::from("constructor")), name: Identifier(String::from("constructor")),
generics: constructor.item.generics.clone(), generics: constructor.item.generics.clone(),
@@ -396,7 +397,7 @@ impl ClassProcessedData {
for method in &self.methods { for method in &self.methods {
match method.item.visibility { match method.item.visibility {
Visibility::Public => { Visibility::Public => {
if method.item.self_param.is_some() { if method.item.is_virtual && method.item.self_param.is_some() {
if method.item.is_override.is_none() { if method.item.is_override.is_none() {
gen_method_point(&method.item, ctx, cg); gen_method_point(&method.item, ctx, cg);
} }
@@ -432,7 +433,7 @@ impl ClassProcessedData {
let generics_expr_str = Generics::from(self.generics.clone()).get_rust(); let generics_expr_str = Generics::from(self.generics.clone()).get_rust();
cg.add(&format!( cg.add(&format!(
"impl{} std::ops::Deref for {}{}", "impl{} core::ops::Deref for {}{}",
generics_str, generics_str,
self.name.get_rust(), self.name.get_rust(),
generics_expr_str generics_expr_str
@@ -451,7 +452,7 @@ impl ClassProcessedData {
cg.addln("}"); cg.addln("}");
cg.add(&format!( cg.add(&format!(
"impl{} std::ops::DerefMut for {}{}", "impl{} core::ops::DerefMut for {}{}",
generics_str, generics_str,
self.name.get_rust(), self.name.get_rust(),
generics_expr_str generics_expr_str
@@ -579,7 +580,7 @@ pub fn gen_method_point(method: &FunctionDecl, ctx: &mut Context, cg: &mut RustC
mutable: mutable_self, mutable: mutable_self,
ty: Box::new(TypeExpr::Path( ty: Box::new(TypeExpr::Path(
Path(vec![ Path(vec![
Identifier(String::from("std")), Identifier(String::from("core")),
Identifier(String::from("ffi")), Identifier(String::from("ffi")),
Identifier(String::from("c_void")), Identifier(String::from("c_void")),
]), ]),
@@ -589,17 +590,16 @@ pub fn gen_method_point(method: &FunctionDecl, ctx: &mut Context, cg: &mut RustC
); );
cg.add(&TypeExpr::StaticFn(param_types, method.return_type.clone().map(Box::new)).get_rust()); cg.add(&TypeExpr::StaticFn(param_types, method.return_type.clone().map(Box::new)).get_rust());
cg.addln(" = std::mem::transmute(func_ptr);"); cg.addln(" = core::mem::transmute(func_ptr);");
if mutable_self { if mutable_self {
cg.add_indented("func(self as *mut Self as *const std::ffi::c_void"); cg.add_indented("func(self as *mut Self as *mut core::ffi::c_void");
} else { } else {
cg.add_indented("func(self as *const Self as *const std::ffi::c_void"); cg.add_indented("func(self as *const Self as *const core::ffi::c_void");
} }
for (_, param) in &params { for (_, param) in &params {
cg.add(", "); cg.add(", ");
ctx.expr_ensure_semicolon = false;
param.name.gen_rust(ctx, cg); param.name.gen_rust(ctx, cg);
} }
@@ -618,3 +618,11 @@ pub fn get_type_from_path(path: &ExprPath) -> TypeExpr {
path.0.last().unwrap().generics.clone(), path.0.last().unwrap().generics.clone(),
) )
} }
pub fn get_virtual_name(name: &Identifier, is_virtual: bool) -> String {
if is_virtual {
format!("__m_{}", name.get_rust())
} else {
name.get_rust()
}
}
-16
View File
@@ -68,14 +68,6 @@ impl GenRust for Literal {
impl GenRust for Expression { impl GenRust for Expression {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
let ensure_semicolon = if ctx.expr_ensure_semicolon {
ctx.expr_ensure_semicolon = false;
true
} else {
false
};
match self { match self {
Expression::Path(path) => cg.add(&path.get_rust_ctx(ctx)), Expression::Path(path) => cg.add(&path.get_rust_ctx(ctx)),
Expression::Literal(literal) => literal.gen_rust(ctx, cg), Expression::Literal(literal) => literal.gen_rust(ctx, cg),
@@ -152,14 +144,6 @@ impl GenRust for Expression {
} }
} }
} }
if ensure_semicolon {
ctx.expr_ensure_semicolon = true;
if !self.is_block() {
cg.add(";");
}
}
} }
} }
-171
View File
@@ -1,171 +0,0 @@
use mist_parser::ast::*;
use crate::fmt::{Context, GenMist, GetMist, MistCodegen};
impl GenMist for Expression {
fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) {
let ensure_semicolon = if ctx.expr_ensure_semicolon {
ctx.expr_ensure_semicolon = false;
true
} else {
false
};
match self {
Expression::Path(path) => cg.add(&path.get_mist()),
Expression::Literal(literal) => literal.gen_mist(ctx, cg),
Expression::Statement(stmt) => stmt.gen_mist(ctx, cg),
Expression::Array(values) => {
cg.add("[");
for (i, val) in values.iter().enumerate() {
if i > 0 {
cg.add(", ");
}
val.gen_mist(ctx, cg);
}
cg.add("]");
}
Expression::ArrayRepeat(value, repeat) => {
cg.add("[");
value.gen_mist(ctx, cg);
cg.add("; ");
repeat.gen_mist(ctx, cg);
cg.add("]");
}
Expression::Fix {
initial,
prefixes,
postfixes,
} => {
for prefix in prefixes {
prefix.gen_mist(ctx, cg);
}
initial.gen_mist(ctx, cg);
for postfix in postfixes {
postfix.gen_mist(ctx, cg);
}
}
Expression::Binary { lhs, op, rhs } => {
lhs.gen_mist(ctx, cg);
cg.add(" ");
cg.add(op);
cg.add(" ");
rhs.gen_mist(ctx, cg);
}
Expression::Closure {
return_type,
params,
body,
} => {
cg.add("(");
for (i, arg) in params.iter().enumerate() {
if i > 0 {
cg.add(", ");
}
arg.gen_mist(ctx, cg);
}
cg.add(") => ");
if let Some(ty) = return_type {
cg.add(&ty.get_mist());
cg.add(" ");
cg.ensure_brackets_expr(ctx, body);
} else {
body.gen_mist(ctx, cg);
}
}
}
if ensure_semicolon {
ctx.expr_ensure_semicolon = true;
if !self.is_block() {
cg.add(";");
}
}
}
}
impl GenMist for Prefix {
fn gen_mist(&self, _ctx: &mut Context, cg: &mut MistCodegen) {
match self {
Self::Deref => cg.add("*"),
Self::Ref => cg.add("&"),
Self::RefMut => cg.add("&mut "),
Self::Not => cg.add("!"),
Self::Neg => cg.add("-"),
}
}
}
impl GenMist for Postfix {
fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) {
match self {
Postfix::TupleFieldAccess(field, generics) => {
cg.add(&format!(
".{}{}",
field,
generics
.iter()
.map(|v| format!("::{}", v.get_mist()))
.collect::<String>()
));
}
Postfix::FieldAccess(field, generics) => {
cg.add(&format!(
".{}{}",
field.get_mist(),
generics
.iter()
.map(|v| format!("::{}", v.get_mist()))
.collect::<String>()
));
}
Postfix::Call(args) => {
cg.add("(");
for (i, arg) in args.iter().enumerate() {
if i > 0 {
cg.add(", ");
}
arg.gen_mist(ctx, cg);
}
cg.add(")");
}
Postfix::MacroCall { inner, delimiter } => {
let (open, close) = match delimiter {
MacroDelimiter::Paren => ("!(", ")"),
MacroDelimiter::Bracket => ("![", "]"),
MacroDelimiter::Brace => ("!{", "}"),
};
cg.add(open);
cg.add(inner);
cg.add(close);
}
Postfix::StructCall(fields) => {
cg.addln(" {");
cg.indent += 1;
for (name, expr) in fields {
cg.add_indented(&name.get_mist());
if let Some(expr) = expr {
cg.add(": ");
expr.gen_mist(ctx, cg);
}
cg.addln(",");
}
cg.indent -= 1;
cg.add_indented("}");
}
Postfix::Index(idx) => {
cg.add("[");
idx.gen_mist(ctx, cg);
cg.add("]");
}
Postfix::As(ty) => {
cg.add(" as ");
cg.add(&ty.get_mist());
}
Postfix::Try => cg.add("?"),
Postfix::Increment => cg.add("++"),
Postfix::Decrement => cg.add("--"),
}
}
}
-450
View File
@@ -1,450 +0,0 @@
pub mod expr;
pub mod statement;
pub mod top_level;
use mist_parser::{MistFmtConfig, ast::*};
pub struct Context {
pub expr_ensure_semicolon: bool,
}
pub struct MistCodegen {
config: MistFmtConfig,
indent_amount: u8,
output: String,
indent: usize,
}
impl MistCodegen {
pub fn new(config: MistFmtConfig) -> Self {
Self {
config,
output: String::new(),
indent: 0,
indent_amount: 4,
}
}
fn indent_str(&self) -> String {
" ".repeat(self.indent_amount as usize).repeat(self.indent)
}
pub fn add(&mut self, s: &str) {
self.output.push_str(s);
}
pub fn addln(&mut self, s: &str) {
self.add(s);
self.add("\n");
}
pub fn add_indented(&mut self, s: &str) {
let line = format!("{}{}", self.indent_str(), s);
self.add(&line);
}
pub fn add_indentedln(&mut self, s: &str) {
let line = format!("{}{}\n", self.indent_str(), s);
self.add(&line);
}
pub fn start_indent(&mut self) {
if self.config.allman_bracket_style {
self.addln("");
self.add_indented("");
} else {
self.add(" ");
}
}
pub fn start_bracket(&mut self) {
if self.config.allman_bracket_style {
self.addln("");
self.add_indentedln("{");
} else {
self.addln(" {");
}
self.indent += 1;
}
pub fn into_output(self) -> String {
self.output
}
pub fn generate(&mut self, toplevels: Vec<TopLevel>) -> String {
let mut ctx = Context {
expr_ensure_semicolon: true,
};
for (i, tl) in toplevels.iter().enumerate() {
if i > 0 {
self.add("\n");
}
tl.gen_mist(&mut ctx, self);
}
self.output.clone()
}
pub fn ensure_brackets(&mut self, ctx: &mut Context, stmt: &Box<Statement>) {
match &**stmt {
Statement::Block(_) => stmt.gen_mist(ctx, self),
_ => {
self.add("{");
self.indent += 1;
stmt.gen_mist(ctx, self);
self.indent -= 1;
self.add_indented("}");
}
}
}
pub fn ensure_brackets_expr(&mut self, ctx: &mut Context, expr: &Expression) {
match expr {
Expression::Statement(stmt) => self.ensure_brackets(ctx, stmt),
_ => {
self.add("{");
self.indent += 1;
expr.gen_mist(ctx, self);
self.indent -= 1;
self.add_indented("}");
}
}
}
}
pub trait GenMist {
fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen);
}
pub trait GetMist {
fn get_mist(&self) -> String;
}
impl GetMist for Path {
fn get_mist(&self) -> String {
self.0
.iter()
.map(Identifier::get_mist)
.collect::<Vec<String>>()
.join("::")
}
}
impl GetMist for Identifier {
fn get_mist(&self) -> String {
self.0.clone()
}
}
impl GetMist for Visibility {
fn get_mist(&self) -> String {
match self {
Visibility::Public => "pub ".to_string(),
Visibility::PublicTarget(path) => format!("pub({}) ", path.get_mist()),
Visibility::Private => "".to_string(),
}
}
}
impl GetMist for TypeExpr {
fn get_mist(&self) -> String {
match self {
Self::Path(path, generics) => {
if let Some(generics) = generics {
format!("{}{}", path.get_mist(), generics.get_mist())
} else {
path.get_mist()
}
}
Self::Lifetime(name) => format!("'{}", name.get_mist()),
Self::Tuple(types) => format!(
"({})",
types
.iter()
.map(|t| t.get_mist())
.collect::<Vec<_>>()
.join(", ")
),
Self::StaticFn(types, return_type) => {
if let Some(return_type) = return_type {
format!(
"fn({}) -> {}",
types
.iter()
.map(|t| t.get_mist())
.collect::<Vec<_>>()
.join(", "),
return_type.get_mist()
)
} else {
format!(
"fn({})",
types
.iter()
.map(|t| t.get_mist())
.collect::<Vec<_>>()
.join(", "),
)
}
}
Self::UnsafePtr { mutable, ty } => {
let mutable = if *mutable { "mut " } else { "const " };
format!("{} {mutable} unsafe&", ty.get_mist())
}
Self::Ref {
lifetime,
mutable,
ty,
} => {
let base = ty.get_mist();
if let Some(lifetime) = lifetime {
format!(
"{} {} '{}&",
base,
if *mutable { "mut" } else { "" },
lifetime.get_mist()
)
} else if *mutable {
format!("{} mut&", base)
} else {
format!("{}&", base)
}
}
Self::Dyn(ty) => {
format!("dyn {}", ty.get_mist())
}
Self::Void => "void".to_string(),
Self::Fn {
kind,
return_type,
params,
} => {
format!(
"{} {}({})",
return_type.get_mist(),
kind.get_mist(),
params
.iter()
.map(TypeExpr::get_mist)
.collect::<Vec<_>>()
.join(", ")
)
}
}
}
}
impl GetMist for FnKind {
fn get_mist(&self) -> String {
match self {
Self::Fn => "fn",
Self::UnsafeFn => "unsafe fn",
Self::FnClosure => "Fn",
Self::FnMut => "FnMut",
Self::FnOnce => "FnOnce",
}
.to_string()
}
}
impl GetMist for ExprPath {
fn get_mist(&self) -> String {
self.0
.iter()
.map(ExprPathSegment::get_mist)
.collect::<Vec<_>>()
.join("::")
}
}
impl GetMist for ExprPathSegment {
fn get_mist(&self) -> String {
format!(
"{}{}",
self.ident.get_mist(),
self.generics
.as_ref()
.map(|v| format!("::{}", v.get_mist()))
.unwrap_or_default()
)
}
}
impl GetMist for Generics {
fn get_mist(&self) -> String {
if self.0.is_empty() {
String::new()
} else {
format!(
"<{}>",
self.0
.iter()
.map(Generic::get_mist)
.collect::<Vec<_>>()
.join(", ")
)
}
}
}
impl GetMist for Generic {
fn get_mist(&self) -> String {
match self {
Self::Lifetime(name) => format!("'{}", name.get_mist()),
Self::Type(ty) => ty.get_mist(),
}
}
}
impl GetMist for GenericsDecl {
fn get_mist(&self) -> String {
if self.0.is_empty() {
String::new()
} else {
format!(
"<{}>",
self.0
.iter()
.map(|v| v.get_mist())
.collect::<Vec<_>>()
.join(", ")
)
}
}
}
impl GetMist for GenericDecl {
fn get_mist(&self) -> String {
match self {
GenericDecl::Lifetime(name) => format!("'{}", name.get_mist()),
GenericDecl::Type(name, requirements) => {
name.get_mist()
+ &(if !requirements.is_empty() {
format!(
": {}",
requirements
.iter()
.map(TypeExpr::get_mist)
.collect::<Vec<_>>()
.join(" + ")
)
} else {
String::new()
})
}
}
}
}
impl<T: GetMist> GenMist for T {
fn gen_mist(&self, _: &mut Context, cg: &mut MistCodegen) {
cg.add(&self.get_mist());
}
}
impl<T: GenMist> GenMist for Spanned<T> {
fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) {
self.item.gen_mist(ctx, cg);
}
}
impl GenMist for Attribute {
fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) {
match self {
Self::Path(path) => cg.add(&path.get_mist()),
Self::NameValue { path, value } => {
cg.add(&format!("{} = ", path.get_mist()));
value.gen_mist(ctx, cg);
}
Self::List { path, items } => {
cg.add(&path.get_mist());
cg.add("(");
for (i, item) in items.iter().enumerate() {
if i > 0 {
cg.add(", ");
}
item.gen_mist(ctx, cg);
}
cg.add(")");
}
}
}
}
impl GenMist for Pattern {
fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) {
match self {
Self::Etc => cg.add(".."),
Self::Literal(lit) => lit.gen_mist(ctx, cg),
Self::Path(mutable, path) => {
if *mutable {
cg.add("mut ");
}
cg.add(&path.get_mist())
}
Self::Struct(path, inner) => {
cg.add(&path.get_mist());
cg.add(" {");
for (idx, i) in inner.iter().enumerate() {
if idx > 0 {
cg.add(", ");
}
if let Some((name, pat)) = i {
cg.add(&name.get_mist());
if let Some(pat) = pat {
cg.add(": ");
pat.gen_mist(ctx, cg);
}
} else {
cg.add("..");
}
}
cg.add("}");
}
Self::NamedTuple(path, inner) => {
cg.add(&path.get_mist());
cg.add("(");
for (i, pat) in inner.iter().enumerate() {
if i > 0 {
cg.add(", ");
}
pat.gen_mist(ctx, cg);
}
cg.add(")");
}
Self::Tuple(inner) => {
cg.add("(");
for (i, pat) in inner.iter().enumerate() {
if i > 0 {
cg.add(", ");
}
pat.gen_mist(ctx, cg);
}
cg.add(")");
}
}
}
}
impl GenMist for Literal {
fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) {
match self {
Self::Int(n) => cg.add(&n.to_string()),
Self::Float(n) => cg.add(&format!("{n:?}")),
Self::Bool(b) => cg.add(&b.to_string()),
Self::String(s) => cg.add(&format!("\"{s}\"")),
Self::Tuple(values) => {
cg.add("(");
for (i, val) in values.iter().enumerate() {
if i > 0 {
cg.add(", ");
}
val.gen_mist(ctx, cg);
}
cg.add(")");
}
}
}
}
-157
View File
@@ -1,157 +0,0 @@
use mist_parser::ast::*;
use crate::fmt::{Context, GenMist, GetMist, MistCodegen};
impl GenMist for Block {
fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) {
cg.addln("{");
cg.indent += 1;
for stmt in &self.statements {
ctx.expr_ensure_semicolon = true;
cg.add_indented("");
stmt.gen_mist(ctx, cg);
cg.addln("");
}
if let Some(soft_return) = &self.soft_return {
ctx.expr_ensure_semicolon = false;
cg.add_indented("");
soft_return.gen_mist(ctx, cg);
cg.addln("");
}
cg.indent -= 1;
cg.add_indented("}");
}
}
impl GenMist for Statement {
fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) {
match self {
Statement::UnsafeBlock(block) => {
cg.add("unsafe ");
block.gen_mist(ctx, cg);
}
Statement::Block(block) => block.gen_mist(ctx, cg),
Statement::VarDecl(VarDeclStmt { decl, init }) => {
if decl.type_.is_some() {
cg.add(&decl.type_.as_ref().unwrap().get_mist());
cg.add(" ");
decl.name.gen_mist(ctx, cg);
} else {
cg.add("let ");
decl.name.gen_mist(ctx, cg);
}
if let Some(init) = init {
cg.add(" = ");
init.gen_mist(ctx, cg);
}
}
Statement::Match(expr, match_items) => {
cg.add("match ");
expr.gen_mist(ctx, cg);
cg.add(" {");
if !match_items.is_empty() {
cg.addln("");
cg.indent += 1;
for match_item in match_items {
let MatchItem(pat, body) = &match_item.item;
cg.add_indented("");
for (i, p) in pat.iter().enumerate() {
if i > 0 {
cg.add(" | ");
}
p.gen_mist(ctx, cg);
}
cg.add(" => ");
if body.is_block() {
body.gen_mist(ctx, cg);
cg.addln("");
} else {
ctx.expr_ensure_semicolon = false;
body.gen_mist(ctx, cg);
cg.addln(",");
}
}
cg.indent -= 1;
cg.add_indented("}");
} else {
cg.add("}");
}
}
Statement::If {
initial,
else_if,
else_branch,
} => {
cg.add("if ");
ctx.expr_ensure_semicolon = false;
initial.condition.gen_mist(ctx, cg);
cg.add(" ");
initial.body.gen_mist(ctx, cg);
for else_if_branch in else_if {
cg.add(" else if ");
ctx.expr_ensure_semicolon = false;
else_if_branch.condition.gen_mist(ctx, cg);
cg.add(" ");
else_if_branch.body.gen_mist(ctx, cg);
}
if let Some(else_br) = else_branch {
cg.add(" else ");
else_br.gen_mist(ctx, cg);
}
}
Statement::While(StatementBranch { condition, body }) => {
cg.add("while ");
condition.gen_mist(ctx, cg);
cg.add(" ");
body.gen_mist(ctx, cg);
}
Statement::Loop(body) => {
cg.add("loop ");
body.gen_mist(ctx, cg);
}
Statement::CStyleFor {
init,
condition,
update,
body,
} => {
cg.add("for (");
ctx.expr_ensure_semicolon = true;
init.gen_mist(ctx, cg);
cg.add(" ");
ctx.expr_ensure_semicolon = false;
condition.gen_mist(ctx, cg);
cg.add(" ");
update.gen_mist(ctx, cg);
cg.add(") ");
body.gen_mist(ctx, cg);
}
Statement::For {
pattern,
iterator,
body,
} => {
cg.add("for ");
pattern.gen_mist(ctx, cg);
cg.add(" in ");
iterator.gen_mist(ctx, cg);
cg.add(" ");
body.gen_mist(ctx, cg);
}
Statement::Return(expr) => {
cg.add("return");
if let Some(expr) = expr {
cg.add(" ");
expr.gen_mist(ctx, cg);
}
}
Statement::Break => cg.add("break"),
Statement::Continue => cg.add("continue"),
}
}
}
-336
View File
@@ -1,336 +0,0 @@
use mist_parser::ast::*;
use crate::fmt::{Context, GenMist, GetMist, MistCodegen};
impl GenMist for ImplDecl {
fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) {
cg.add_indented("impl ");
let generics = self.generics.get_mist();
if !generics.is_empty() {
cg.add(&generics);
cg.add(" ");
}
if let Some(trait_) = &self.trait_ {
cg.add(&trait_.get_mist());
cg.add(" for ");
}
cg.add(&self.target.get_mist());
cg.start_bracket();
for method in &self.methods {
method.gen_mist(ctx, cg);
}
cg.indent -= 1;
cg.add_indentedln("}");
}
}
impl GenMist for FunctionDecl {
fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) {
cg.add_indented(&self.visibility.get_mist());
if self.return_type.is_none() {
cg.add("void ");
} else {
cg.add(&self.return_type.as_ref().unwrap().get_mist());
cg.add(" ");
}
cg.add(&self.name.get_mist());
cg.add(&self.generics.get_mist());
cg.add("(");
if let Some((is_ref, lifetime, is_mut)) = &self.self_param {
if *is_ref {
cg.add("&");
}
if let Some(lifetime) = lifetime {
cg.add(&format!("'{} ", lifetime.0));
}
if *is_mut {
cg.add("mut ");
}
cg.add("self");
if !self.params.0.is_empty() {
cg.add(", ");
}
}
for (i, param) in self.params.0.iter().enumerate() {
if i > 0 {
cg.add(", ");
}
param.gen_mist(ctx, cg);
}
cg.add(")");
if let Some(override_spec) = &self.is_override {
cg.add(" override");
if let Some(path) = &override_spec.0 {
cg.add("(");
cg.add(&path.get_mist());
cg.add(")");
}
}
if let Some(body) = &self.body {
cg.start_indent();
body.gen_mist(ctx, cg);
cg.addln("");
} else {
cg.addln(";");
}
}
}
impl GenMist for ParamList {
fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) {
for (i, param) in self.0.iter().enumerate() {
if i > 0 {
cg.add(", ");
}
param.gen_mist(ctx, cg);
}
}
}
impl GenMist for VarDecl {
fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) {
if let Some(type_) = &self.type_ {
cg.add(&type_.get_mist());
cg.add(" ");
}
self.name.gen_mist(ctx, cg);
}
}
impl GetMist for FieldDecl {
fn get_mist(&self) -> String {
format!(
"{}{} {}",
self.visibility.get_mist(),
self.type_.get_mist(),
self.name.get_mist(),
)
}
}
impl GetMist for EnumItem {
fn get_mist(&self) -> String {
match self {
Self::Named(id) => id.get_mist(),
Self::Struct(id, s) => format!(
"{} {{{}}}",
id.get_mist(),
s.iter()
.map(FieldDecl::get_mist)
.collect::<Vec<_>>()
.join(", ")
),
Self::Tuple(id, t) => format!(
"{}({})",
id.get_mist(),
t.iter()
.map(TypeExpr::get_mist)
.collect::<Vec<_>>()
.join(", ")
),
}
}
}
impl GenMist for TopLevel {
fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) {
if let TopLevelKind::ModAttribute = self.0.item {
for attr in &self.1 {
cg.add("#![");
attr.gen_mist(ctx, cg);
cg.addln("]");
}
} else {
for attr in &self.1 {
cg.add("#[");
attr.gen_mist(ctx, cg);
cg.addln("]");
}
}
self.0.gen_mist(ctx, cg);
}
}
impl GenMist for TopLevelKind {
fn gen_mist(&self, ctx: &mut Context, cg: &mut MistCodegen) {
match self {
Self::ModAttribute => {}
Self::Import(vis, path) => {
cg.addln(&format!("{}use {};", vis.get_mist(), path.get_mist()))
}
Self::DeclareModule(vis, name) => {
cg.addln(&format!("{}module {};", vis.get_mist(), name.get_mist()))
}
Self::FunctionDecl(decl) => decl.gen_mist(ctx, cg),
Self::ImplDecl(impl_) => impl_.gen_mist(ctx, cg),
Self::StructDecl {
visibility,
name,
generics,
fields,
} => {
cg.add(&format!(
"{}struct {}{}",
visibility.get_mist(),
name.get_mist(),
generics.get_mist()
));
if fields.is_empty() {
cg.add(" {}");
} else {
cg.start_bracket();
for field in fields {
cg.add_indentedln(&format!("{},", field.item.get_mist()));
}
cg.indent -= 1;
cg.add_indented("}");
}
cg.addln("");
}
Self::EnumDecl {
visibility,
name,
generics,
fields,
} => {
cg.add(&format!(
"{}enum {}{}",
visibility.get_mist(),
name.get_mist(),
generics.get_mist()
));
if fields.is_empty() {
cg.add(" {}");
} else {
cg.start_bracket();
for field in fields {
cg.add_indentedln(&format!("{},", field.item.get_mist()));
}
cg.indent -= 1;
cg.add_indented("}");
}
cg.addln("");
}
Self::TraitDecl {
visibility,
name,
generics,
requirements,
items,
} => {
cg.add(&format!(
"{}trait {}{}",
visibility.get_mist(),
name.get_mist(),
generics.get_mist()
));
if !requirements.is_empty() {
cg.add(" : ");
cg.add(
&requirements
.iter()
.map(TypeExpr::get_mist)
.collect::<Vec<_>>()
.join(" + "),
);
}
cg.start_bracket();
for item in items {
item.gen_mist(ctx, cg);
}
cg.indent -= 1;
cg.add_indentedln("}");
}
Self::ClassDecl {
visibility,
name,
generics,
inherits,
fields,
constructor,
items,
} => {
cg.add(&format!(
"{}class {}{}",
visibility.get_mist(),
name.get_mist(),
generics.get_mist()
));
if let Some(inherits) = inherits {
cg.add(" : ");
cg.add(&inherits.get_mist());
}
cg.start_bracket();
for field in fields {
cg.add_indented(&field.item.decl.get_mist());
if let Some(init) = &field.item.init {
cg.add(" = ");
init.gen_mist(ctx, cg);
}
cg.addln(";");
}
if let Some(constructor) = constructor {
if fields.len() > 0 {
cg.addln("");
}
cg.add_indented(&constructor.item.visibility.get_mist());
cg.add("constructor");
cg.add(&constructor.item.generics.get_mist());
cg.add("(");
for (i, param) in constructor.item.params.0.iter().enumerate() {
if i > 0 {
cg.add(", ");
}
param.gen_mist(ctx, cg);
}
cg.add(") ");
cg.start_indent();
constructor.item.body.gen_mist(ctx, cg);
cg.addln("");
if items.len() > 0 {
cg.addln("");
}
}
for (i, item) in items.iter().enumerate() {
if i > 0 {
cg.addln("");
}
match item {
ClassItem::Method(method) => method.gen_mist(ctx, cg),
ClassItem::ImplDecl(impl_) => impl_.gen_mist(ctx, cg),
}
}
cg.indent -= 1;
cg.add_indentedln("}");
}
}
}
}
+51 -10
View File
@@ -1,10 +1,9 @@
pub mod class_decl; pub mod class_decl;
pub mod expr; pub mod expr;
pub mod fmt;
pub mod statement; pub mod statement;
pub mod top_level; pub mod top_level;
use std::path::PathBuf; use std::{collections::HashMap, path::PathBuf};
use mist_parser::{ use mist_parser::{
ast::*, ast::*,
@@ -12,7 +11,6 @@ use mist_parser::{
}; };
pub struct Context { pub struct Context {
pub expr_ensure_semicolon: bool,
pub expr_super: Option<ExprPath>, pub expr_super: Option<ExprPath>,
} }
@@ -28,9 +26,15 @@ pub trait GetRust {
} }
} }
pub enum Include {
Glob(Path),
Use(Visibility, Path),
}
pub struct RustCodegen { pub struct RustCodegen {
output: String, output: String,
indent: usize, indent: usize,
pub crates: HashMap<Identifier, Vec<Include>>,
pub mapping: Mapping, pub mapping: Mapping,
position: RustMap, position: RustMap,
} }
@@ -40,8 +44,9 @@ impl RustCodegen {
Self { Self {
output: String::new(), output: String::new(),
indent: 0, indent: 0,
crates: HashMap::new(),
mapping: Mapping::new(mist_path), mapping: Mapping::new(mist_path),
position: RustMap(1, 0), position: RustMap(0, 0),
} }
} }
@@ -80,15 +85,45 @@ impl RustCodegen {
} }
pub fn generate(&mut self, toplevels: Vec<TopLevel>) -> String { pub fn generate(&mut self, toplevels: Vec<TopLevel>) -> String {
let mut ctx = Context { let mut ctx = Context { expr_super: None };
expr_ensure_semicolon: true,
expr_super: None,
};
for tl in toplevels { for tl in toplevels {
tl.gen_rust(&mut ctx, self); tl.gen_rust(&mut ctx, self);
} }
let mut crates = HashMap::new();
std::mem::swap(&mut self.crates, &mut crates);
for (c, items) in crates {
self.add("mod ");
c.gen_rust(&mut ctx, self);
self.addln(" {");
self.indent += 1;
self.add_indented("extern crate ");
c.gen_rust(&mut ctx, self);
self.addln(";");
for item in items {
self.add_indentedln("#[doc(inline)]");
self.add_indented("pub use ");
match item {
Include::Glob(item) => {
item.gen_rust(&mut ctx, self);
self.add("::*");
}
Include::Use(vis, item) => {
vis.gen_rust(&mut ctx, self);
item.gen_rust(&mut ctx, self);
}
}
self.addln(";");
}
self.indent -= 1;
self.addln("}");
}
self.output.clone() self.output.clone()
} }
@@ -151,7 +186,7 @@ impl<T> GenSpanTranslation for Spanned<T> {
fn gen_span(&self, cg: &mut RustCodegen) { fn gen_span(&self, cg: &mut RustCodegen) {
cg.mapping cg.mapping
.map .map
.insert((cg.position, MistMap(self.line, self.column))); .insert((cg.position, MistMap(self.line, self.column - 1)));
} }
} }
@@ -204,7 +239,6 @@ impl GetRust for TypeExpr {
path.get_rust() path.get_rust()
} }
} }
Self::Lifetime(name) => format!("'{}", name.get_rust()),
Self::Tuple(types) => format!( Self::Tuple(types) => format!(
"({})", "({})",
types types
@@ -278,6 +312,13 @@ impl GetRust for TypeExpr {
return_type.get_rust(), return_type.get_rust(),
) )
} }
Self::Array(ty, count) => {
if let Some(count) = count {
format!("[{}; {count}]", ty.get_rust())
} else {
format!("[{}]", ty.get_rust())
}
}
} }
} }
} }
+49 -19
View File
@@ -10,14 +10,15 @@ impl GenRust for Block {
cg.indent += 1; cg.indent += 1;
for stmt in &self.statements { for stmt in &self.statements {
ctx.expr_ensure_semicolon = true;
cg.add_indented(""); cg.add_indented("");
stmt.gen_rust(ctx, cg); stmt.gen_rust(ctx, cg);
if stmt.item.is_semicolon_required() {
cg.add(";");
}
cg.addln(""); cg.addln("");
} }
if let Some(soft_return) = &self.soft_return { if let Some(soft_return) = &self.soft_return {
ctx.expr_ensure_semicolon = false;
cg.add_indented(""); cg.add_indented("");
soft_return.gen_rust(ctx, cg); soft_return.gen_rust(ctx, cg);
cg.addln(""); cg.addln("");
@@ -31,13 +32,14 @@ impl GenRust for Block {
impl GenRust for Statement { impl GenRust for Statement {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
match self { match self {
Statement::TopLevel(tl) => tl.gen_rust(ctx, cg),
Statement::Block(block) => block.gen_rust(ctx, cg),
Statement::UnsafeBlock(block) => { Statement::UnsafeBlock(block) => {
cg.add("unsafe "); cg.add("unsafe ");
block.gen_rust(ctx, cg); block.gen_rust(ctx, cg);
} }
Statement::Block(block) => block.gen_rust(ctx, cg),
Statement::VarDecl(VarDeclStmt { decl, init }) => { Statement::VarDecl(VarDeclStmt { decl, init }) => {
cg.add("let "); cg.add("let ");
decl.gen_rust(ctx, cg); decl.gen_rust(ctx, cg);
@@ -85,14 +87,12 @@ impl GenRust for Statement {
else_branch, else_branch,
} => { } => {
cg.add("if "); cg.add("if ");
ctx.expr_ensure_semicolon = false;
initial.condition.gen_rust(ctx, cg); initial.condition.gen_rust(ctx, cg);
cg.add(" "); cg.add(" ");
initial.body.gen_rust(ctx, cg); initial.body.gen_rust(ctx, cg);
for else_if_branch in else_if { for else_if_branch in else_if {
cg.add(" else if "); cg.add(" else if ");
ctx.expr_ensure_semicolon = false;
else_if_branch.condition.gen_rust(ctx, cg); else_if_branch.condition.gen_rust(ctx, cg);
cg.add(" "); cg.add(" ");
else_if_branch.body.gen_rust(ctx, cg); else_if_branch.body.gen_rust(ctx, cg);
@@ -125,17 +125,18 @@ impl GenRust for Statement {
cg.addln("{"); cg.addln("{");
cg.indent += 1; cg.indent += 1;
ctx.expr_ensure_semicolon = true;
cg.add_indented(""); cg.add_indented("");
init.gen_rust(ctx, cg); init.gen_rust(ctx, cg);
if init.is_semicolon_required() {
cg.add(";");
}
cg.addln(""); cg.addln("");
cg.add_indented("while "); cg.add_indented("while ");
ctx.expr_ensure_semicolon = false;
condition.gen_rust(ctx, cg); condition.gen_rust(ctx, cg);
cg.add(" "); cg.add(" ");
@@ -143,12 +144,14 @@ impl GenRust for Statement {
cg.add("{"); cg.add("{");
cg.indent += 1; cg.indent += 1;
ctx.expr_ensure_semicolon = true;
body.gen_rust(ctx, cg); body.gen_rust(ctx, cg);
update.gen_rust(ctx, cg); update.gen_rust(ctx, cg);
if update.is_semicolon_required() {
cg.add(";");
}
cg.addln(""); cg.addln("");
cg.indent -= 1; cg.indent -= 1;
@@ -185,14 +188,41 @@ impl GenRust for Statement {
impl GenRust for VarDecl { impl GenRust for VarDecl {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
self.name.gen_rust(ctx, cg); if self.tuple_names.is_empty() {
self.name.gen_rust(ctx, cg);
} else {
cg.add("(");
self.name.gen_rust(ctx, cg);
for name in &self.tuple_names {
cg.add(", ");
name.gen_rust(ctx, cg);
}
cg.add(")");
}
cg.add( if self.true_type {
&self cg.add(
.type_ &self
.as_ref() .type_
.map(|t| format!(": {}", t.get_rust())) .as_ref()
.unwrap_or_default(), .map(|t| format!(": {}", t.get_rust()))
); .unwrap_or_default(),
);
} else {
cg.add(
&self
.type_
.as_ref()
.map(|t| {
let t = vec![t.get_rust(); self.tuple_names.len() + 1];
if self.tuple_names.is_empty() {
format!(": {}", t.join(", "))
} else {
format!(": ({})", t.join(", "))
}
})
.unwrap_or_default(),
);
}
} }
} }
+92 -2
View File
@@ -24,7 +24,21 @@ impl GetRust for GenericsDecl {
impl GetRust for GenericDecl { impl GetRust for GenericDecl {
fn get_rust(&self) -> String { fn get_rust(&self) -> String {
match self { match self {
GenericDecl::Lifetime(name) => format!("'{}", name.get_rust()), GenericDecl::Lifetime(name, requirements) => {
format!("'{}", name.get_rust())
+ &(if requirements.len() != 0 {
format!(
": {}",
requirements
.into_iter()
.map(GetRust::get_rust)
.collect::<Vec<_>>()
.join("+")
)
} else {
String::new()
})
}
GenericDecl::Type(name, requirements) => { GenericDecl::Type(name, requirements) => {
name.get_rust() name.get_rust()
+ &(if requirements.len() != 0 { + &(if requirements.len() != 0 {
@@ -32,7 +46,7 @@ impl GetRust for GenericDecl {
": {}", ": {}",
requirements requirements
.into_iter() .into_iter()
.map(TypeExpr::get_rust) .map(GetRust::get_rust)
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join("+") .join("+")
) )
@@ -191,6 +205,38 @@ impl GenRust for TopLevelKind {
fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) {
match self { match self {
Self::ModAttribute => {} Self::ModAttribute => {}
Self::TypeAlias { name, generics, ty } => {
cg.add("type ");
name.gen_rust(ctx, cg);
if let Some(g) = generics {
g.gen_rust(ctx, cg);
}
cg.add(" = ");
ty.gen_rust(ctx, cg);
cg.addln(";");
}
Self::ConstDecl(decl) => {
cg.add("const ");
decl.decl.gen_rust(ctx, cg);
if let Some(init) = &decl.init {
cg.add(" = ");
init.gen_rust(ctx, cg);
}
cg.addln(";");
}
Self::StaticDecl(decl) => {
cg.add("static ");
decl.decl.gen_rust(ctx, cg);
if let Some(init) = &decl.init {
cg.add(" = ");
init.gen_rust(ctx, cg);
}
cg.addln(";");
}
Self::Import(vis, path) => { Self::Import(vis, path) => {
cg.addln(&format!("{}use {};", vis.get_rust(), path.get_rust())) cg.addln(&format!("{}use {};", vis.get_rust(), path.get_rust()))
} }
@@ -220,6 +266,33 @@ impl GenRust for TopLevelKind {
cg.indent -= 1; cg.indent -= 1;
cg.addln("}\n"); cg.addln("}\n");
} }
Self::StructDeclTupleUnit {
visibility,
name,
generics,
unit,
} => {
cg.addln(&format!(
"{}struct {}{}",
visibility.get_rust(),
name.get_rust(),
generics.get_rust()
));
cg.indent += 1;
if let Some(unit) = unit {
cg.add(" (");
for unit in unit {
cg.add_indented("");
unit.gen_rust(ctx, cg);
cg.add(", ");
}
cg.add(")");
}
cg.indent -= 1;
cg.addln(";\n");
}
Self::EnumDecl { Self::EnumDecl {
visibility, visibility,
name, name,
@@ -294,6 +367,23 @@ impl GenRust for TopLevelKind {
constructor, constructor,
items, items,
), ),
Self::IncludeGlobal(incl) => {
cg.crates
.entry(incl.0[0].clone())
.or_insert(Vec::new())
.push(crate::Include::Glob(incl.clone()));
}
Self::IncludeUse(vis, incl) => {
cg.crates
.entry(incl.0[0].clone())
.or_insert(Vec::new())
.push(crate::Include::Use(vis.clone(), incl.clone()));
}
Self::IncludeLocal(path) => {
cg.add("include!(\"");
cg.add(&path.replace(".mist", ".rs"));
cg.add("\");");
}
} }
} }
} }
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "mist-api" name = "mist-api"
version = "0.3.7-alpha.0" version = "0.4.0"
edition = "2024" edition = "2024"
description = "The Mist programming language module system" description = "The Mist programming language module system"
license = "Apache-2.0" license = "Apache-2.0"
+66 -97
View File
@@ -1,178 +1,147 @@
pub module builder; pub module builder;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Command; use std::process::Command;
use std::process::Stdio; use std::process::Stdio;
use cargo_metadata::CompilerMessage; use cargo_metadata::CompilerMessage;
use cargo_metadata::Message; use cargo_metadata::Message;
use mist_parser::rev_mapper; use mist_parser::rev_mapper;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct MistDiagnosticMessage { pub struct MistDiagnosticMessage
pub String message, {
pub PathBuf file_path, pub String message;
pub String file_name, pub PathBuf file_path;
pub usize line, pub String file_name;
pub usize column, pub usize line;
pub usize column;
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum MistDiagnostic { pub enum MistDiagnostic
Error(MistDiagnosticMessage), {
Warning(MistDiagnosticMessage), Error(MistDiagnosticMessage);
Rust(CompilerMessage), Warning(MistDiagnosticMessage);
Rust(CompilerMessage);
} }
pub bool build(Vec<String> mut args, PathBuf root) { pub bool build(Vec<String> mut args, PathBuf root)
{
args.remove(0); args.remove(0);
args.insert(1, "--message-format=json".to_string()); args.insert(1, "--message-format=json".to_string());
let mut command = Command::new("cargo").args(args).stdout(Stdio::piped()).stderr(Stdio::inherit()).stdin(Stdio::inherit()).spawn().expect("Failed to run cargo");
let mut command = Command::new("cargo")
.args(args)
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.stdin(Stdio::inherit())
.spawn()
.expect("Failed to run cargo");
let mut reader = std::io::BufReader::new(command.stdout.take().expect("Failed to get reader")); let mut reader = std::io::BufReader::new(command.stdout.take().expect("Failed to get reader"));
let mut diagnostics = Vec::new(); let mut diagnostics = Vec::new();
let mut mapping = HashMap::new(); let mut mapping = HashMap::new();
for message in cargo_metadata::Message::parse_stream(&mut reader) { for message in cargo_metadata::Message::parse_stream(&mut reader) {
match message { match message {
Ok(Message::CompilerMessage(msg)) => { Ok(Message::CompilerMessage(msg)) => {
for span in &msg.message.spans { for span in &msg.message.spans {
if span.is_primary { if span.is_primary {
let rust_path = root.join(&span.file_name); let rust_path = root.join(&span.file_name);
let mapping_path = rust_path.with_extension("map.json"); let mapping_path = rust_path.with_extension("map.json");
if !fs::exists(&mapping_path).expect("Unable to check if map file exists") { if !fs::exists(&mapping_path).expect("Unable to check if map file exists") {
diagnostics.push(MistDiagnostic::Rust(msg)); diagnostics.push(MistDiagnostic::Rust(msg));
break; break;
} }
let map = mapping.entry(mapping_path.clone()).or_insert_with(() => { let map = mapping.entry(mapping_path.clone()).or_insert_with(() => {
let map_str = fs::read_to_string(mapping_path).expect("Failed to read map file"); let map_str = fs::read_to_string(mapping_path).expect("Failed to read map file");
serde_json::from_str<rev_mapper::Mapping>(&map_str).expect("Failed to parse map file") serde_json::from_str<rev_mapper::Mapping>(&map_str).expect("Failed to parse map file")
}); });
let mist_file = map.mist_path.strip_prefix(&root); let mist_file = map.mist_path.strip_prefix(&root);
let rust_lookup = rev_mapper::RustMap(span.line_end - 1, span.column_start - 1);
let (_, rev_mapper::MistMap(line, column)) = match map.find(&rust_lookup) {
map.find(&rev_mapper::RustMap(span.line_end, span.column_start)) Some((_, rev_mapper::MistMap(line, column))) => {
.expect("Unable to find mapping"); let mist_msg = MistDiagnosticMessage {
message: format!("{}: {}",
let mist_msg = MistDiagnosticMessage { msg.message.message,
message: format!( span.label.clone().unwrap_or_default()),
"{}: {}", file_name: if let Ok(mist_file) = mist_file {
msg.message.message, mist_file.to_string_lossy().to_string()
span.label.clone().unwrap_or_default() } else {
), map.mist_path.to_string_lossy().to_string()
file_name: if let Ok(mist_file) = mist_file { },
mist_file.to_string_lossy().to_string() file_path: map.mist_path.clone(),
} else { line: line,
map.mist_path.to_string_lossy().to_string() column: column,
}, };
file_path: map.mist_path.clone(), match msg.message.level {
line: line, cargo_metadata::diagnostic::DiagnosticLevel::Error => {
column: column diagnostics.push(MistDiagnostic::Error(mist_msg))
}; }
cargo_metadata::diagnostic::DiagnosticLevel::Warning => {
match msg.message.level { diagnostics.push(MistDiagnostic::Warning(mist_msg))
cargo_metadata::diagnostic::DiagnosticLevel::Error => { }
diagnostics.push(MistDiagnostic::Error(mist_msg)) _ => {
}
}
} }
None => {
cargo_metadata::diagnostic::DiagnosticLevel::Warning => { diagnostics.push(MistDiagnostic::Rust(msg));
diagnostics.push(MistDiagnostic::Warning(mist_msg)) break;
} }
_ => {}
} }
} }
} }
} }
Ok(Message::BuildFinished(finish)) => { Ok(Message::BuildFinished(finish)) => {
print_diagnostics(&diagnostics); print_diagnostics(&diagnostics);
let mut raw_reader = reader.into_inner(); let mut raw_reader = reader.into_inner();
let mut stdout = std::io::stdout(); let mut stdout = std::io::stdout();
std::io::copy(&mut raw_reader, &mut stdout).map_err((v) => v.to_string()).expect("(Mist) Failed to copy IO");
std::io::copy(&mut raw_reader, &mut stdout)
.map_err((v) => v.to_string())
.expect("(Mist) Failed to copy IO");
command.wait().unwrap(); command.wait().unwrap();
return finish.success; return finish.success;
} }
Ok(Message::TextLine(text)) => println!("{text}"),
Ok(Message::TextLine(text)) => println!("{text}"); _ => {
_ => {} }
} }
} }
false false
} }
pub void print_diagnostics(Vec<MistDiagnostic>& diagnostics) { pub void print_diagnostics(Vec<MistDiagnostic>& diagnostics)
{
let mut files = HashMap::new(); let mut files = HashMap::new();
for diag in diagnostics { for diag in diagnostics {
match diag { match diag {
MistDiagnostic::Error(msg) => { MistDiagnostic::Error(msg) => {
let line = get_line(&mut files, &msg); let line = get_line(&mut files, &msg);
println!("\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}",
println!(
"\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}",
msg.file_name, msg.file_name,
msg.line, msg.line,
msg.column, msg.column,
msg.message, msg.message,
line.unwrap_or_default(), line.unwrap_or_default(),)
)
} }
MistDiagnostic::Warning(msg) => { MistDiagnostic::Warning(msg) => {
let line = get_line(&mut files, &msg); let line = get_line(&mut files, &msg);
println!("\n{}:{}:{}\n \x1b[33mWarning\x1b[0m: {}\n\t{}",
println!(
"\n{}:{}:{}\n \x1b[33mWarning\x1b[0m: {}\n\t{}",
msg.file_name, msg.file_name,
msg.line, msg.line,
msg.column, msg.column,
msg.message, msg.message,
line.unwrap_or_default(), line.unwrap_or_default(),)
)
} }
MistDiagnostic::Rust(rs) => println!("{rs}"),
MistDiagnostic::Rust(rs) => println!("{rs}");
} }
} }
} }
pub Option<String> get_line( pub Option<String> get_line(HashMap<PathBuf, Vec<String>> mut& files, MistDiagnosticMessage& msg)
HashMap<PathBuf, Vec<String>> mut& files, {
MistDiagnosticMessage& msg,
) {
let src_path = msg.file_path.clone(); let src_path = msg.file_path.clone();
let lines = files.entry(src_path.clone()).or_insert_with(() => { let lines = files.entry(src_path.clone()).or_insert_with(() => {
fs::read_to_string(src_path) fs::read_to_string(src_path).expect("Unable to read mist file").lines().into_iter().map(String::from).collect()
.expect("Unable to read mist file")
.lines()
.into_iter()
.map(String::from)
.collect()
}); });
lines.get(msg.line.saturating_sub(1)).map((v) => v.trim().to_string())
lines.get(msg.line - 1).map((v) => v.trim().to_string())
} }
+21 -28
View File
@@ -1,75 +1,72 @@
pub module modules; pub module modules;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::fs; use std::fs;
use std::process; use std::process;
use crate::transpiler::MistConfig; use crate::transpiler::MistConfig;
#[derive(Debug)] #[derive(Debug)]
pub class Module { pub class Module
{
pub String name; pub String name;
pub PathBuf path; pub PathBuf path;
pub Vec<Module> children; pub Vec<Module> children;
pub constructor(PathBuf mist_path, MistConfig& config) { pub constructor(PathBuf mist_path, MistConfig& config)
{
self.children = Vec::new(); self.children = Vec::new();
self.name = mist_path.file_stem() self.name = mist_path.file_stem().and_then((s) => s.to_str()).expect("Failed to get module name").to_string();
.and_then((s) => s.to_str())
.expect("Failed to get module name")
.to_string();
self.path = mist_path; self.path = mist_path;
if self.path.is_dir() { if self.path.is_dir() {
let dir = self.path.clone(); let dir = self.path.clone();
let pkg_gate = self.path.join("package.mist"); let pkg_gate = self.path.join("package.mist");
if pkg_gate.exists() { if pkg_gate.exists() {
self.path = pkg_gate; self.path = pkg_gate;
} }
self.scan_directory(&dir, config); self.scan_directory(&dir, config);
} else if config.should_ignore(&self.path) { } else if config.should_ignore(&self.path) {
let dir = self.path.parent().expect("Failed to get parent directory to scan").to_path_buf(); let dir = self.path.parent().expect("Failed to get parent directory to scan").to_path_buf();
self.scan_directory(&dir, config); self.scan_directory(&dir, config);
} }
} }
void scan_directory(&mut self, Path& search_dir, MistConfig& config) { void scan_directory(&mut self, Path& search_dir, MistConfig& config)
{
let entries = match fs::read_dir(search_dir) { let entries = match fs::read_dir(search_dir) {
Ok(entries) => entries; Ok(entries) => entries,
Err(e) => { Err(e) => {
eprintln!("error: failed to read directory {}\n {}", search_dir.display(), e); eprintln!("error: failed to read directory {}\n {}", search_dir.display(), e);
process::exit(1); process::exit(1);
} }
}; };
for entry in entries { for entry in entries {
let entry = match entry { let entry = match entry {
Ok(entry) => entry; Ok(entry) => entry,
Err(e) => { Err(e) => {
eprintln!("error: failed to read directory entry\n {}", e); eprintln!("error: failed to read directory entry\n {}", e);
process::exit(1); process::exit(1);
} }
}; };
let child_path = entry.path(); let child_path = entry.path();
if child_path == self.path || child_path == config.package { if child_path == self.path || child_path == config.package {
continue; continue;
} }
self.children.push(Module::new(child_path, config)); self.children.push(Module::new(child_path, config));
} }
} }
pub bool is_package(&self) { pub bool is_package(&self)
{
self.path.file_name().map((v) => v.to_str()).unwrap_or_default() == Some("package.mist") self.path.file_name().map((v) => v.to_str()).unwrap_or_default() == Some("package.mist")
} }
pub PathBuf output_dir(&self, PathBuf& parent_dir) { pub PathBuf output_dir(&self, PathBuf& parent_dir)
{
if self.path.is_dir() || self.is_package() { if self.path.is_dir() || self.is_package() {
parent_dir.join(&self.name) parent_dir.join(&self.name)
} else { } else {
@@ -77,19 +74,15 @@ pub class Module {
} }
} }
pub PathBuf output_path(&self, pub PathBuf output_path(&self, PathBuf& parent_dir, Option<(mist_parser::ast::Visibility, mist_parser::ast::Identifier)>& curr_mod_decl)
PathBuf& parent_dir, {
Option<( let name = if self.path.is_dir() || self.is_package() {
mist_parser::ast::Visibility, &self.name
mist_parser::ast::Identifier } else if let Some((_, name)) = curr_mod_decl {
)>& curr_mod_decl
) {
let name = if let Some((_, name)) = curr_mod_decl {
&name.0 &name.0
} else { } else {
&self.name &self.name
}; };
if self.path.is_dir() || self.is_package() { if self.path.is_dir() || self.is_package() {
parent_dir.join(name).join("mod.rs") parent_dir.join(name).join("mod.rs")
} else { } else {
+159 -172
View File
@@ -1,170 +1,194 @@
pub module transpiler; pub module transpiler;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::process; use std::process;
use std::time; use std::time;
use std::env;
use mist_parser::error; use mist_parser::error;
use crate::modules; use crate::modules;
use mist_codegen::GetRust; use mist_codegen::GetRust;
use serde::Deserialize; use serde::Deserialize;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(default)] #[serde(default)]
pub struct MistConfig { pub struct MistConfig
pub PathBuf package, {
pub Vec<PathBuf> packages, pub PathBuf package;
pub String main_mods pub Vec<PathBuf> packages;
pub Vec<PathBuf> include;
pub mist_parser::MistFmtConfig fmt;
} }
impl MistConfig { impl MistConfig
pub bool should_ignore(&self, Path& path) { {
pub bool should_ignore(&self, Path& path)
{
if path == self.package { if path == self.package {
return true; return true;
} }
for pkg in &self.packages { for pkg in &self.packages {
if path.ends_with(pkg) { if path.ends_with(pkg) {
return true; return true;
} }
} }
false false
} }
} }
impl Default for MistConfig {
Self default() { impl Default for MistConfig
{
Self default()
{
Self { Self {
package: PathBuf::from("main.mist"), package: PathBuf::from("main.mist"),
packages: Vec::new(), packages: Vec::new(),
main_mods: String::new(), include: Vec::new(),
fmt: mist_parser::MistFmtConfig {
allman_bracket_style: true,
},
} }
} }
} }
pub PathBuf transpile() { pub void transpile(Path& root)
{
println!("\x1b[32m\nTranspiling\x1b[0m \x1b[94m{:?}\x1b[0m", root.file_name().unwrap_or_default());
let start = time::Instant::now(); let start = time::Instant::now();
let root = env::current_dir().expect("Unable to find project root");
let toml_path = root.join("Mist.toml"); let toml_path = root.join("Mist.toml");
let src = root.join("src"); let src = root.join("src");
let out = root.join(".mist/src"); let out = root.join(".mist/src");
let toml_content = fs::read_to_string(&toml_path).unwrap_or_else((_) => panic!("Could not read file at {:?}", toml_path));
let toml_content = fs::read_to_string(&toml_path) MistConfig mut config = toml::from_str(&toml_content).unwrap_or_else((_) => panic!("Failed to parse Mist.toml from {:?}", toml_path));
.unwrap_or_else((_) => panic!("Could not read file at {:?}", toml_path));
MistConfig mut config = toml::from_str(&toml_content)
.unwrap_or_else((_) => panic!("Failed to parse Mist.toml from {:?}", toml_path));
for pkg in &mut config.packages { for pkg in &mut config.packages {
*pkg = src.join(&*pkg); *pkg = src.join(&*pkg);
} }
config.package = src.join(&*config.package); config.package = src.join(&*config.package);
let master = modules::Module::new(config.package.clone(), &config);
let master = modules::Module::new( master.transpile(&out, &mut config, &mut String::new());
config.package.clone(),
&config
);
master.transpile(&out, &mut config);
let elapsed = start.elapsed(); let elapsed = start.elapsed();
println!("\x1b[32m\nTranspile successful\x1b[0m in \x1b[34m{:.2?}\x1b[0m", elapsed);
println!( for incl in &config.include {
"\x1b[32m\nTranspile successful\x1b[0m in \x1b[34m{:.2?}\x1b[0m", let incl = if incl.is_relative() { root.join(incl) } else { incl.clone() };
elapsed if ends_with_star(&incl) {
); let sub_crates = incl.parent().expect("Failed to get parent");
let entries = match fs::read_dir(sub_crates) {
root Ok(entries) => entries,
}
impl modules::Module {
(bool, String) transpile(&self, PathBuf& parent_dir, MistConfig mut& config) {
if !self.path.is_dir() && self.path.extension().and_then((v) => v.to_str()) != Some("mist") {
let file_name = self.path.file_name()
.and_then((s) => s.to_str())
.expect("Failed to get file name");
fs::copy(&self.path, parent_dir.join(&file_name)).expect("Failed to copy SideFile");
return (true, String::new());
}
let dir = self.output_dir(parent_dir);
let _ = fs::create_dir_all(&dir);
let mut output = String::new();
let mut child_changed = false;
for child in &self.children {
if !config.should_ignore(&child.path) {
let out = child.transpile(&dir, config);
if out.0 {
child_changed = true;
}
output.push_str(&out.1);
}
}
if self.path == config.package {
config.main_mods = output.clone();
} else if config.should_ignore(&self.path) {
output.push_str(&config.main_mods)
}
self.write_transpile(output, parent_dir, child_changed)
}
(bool, String) write_transpile(&self, String output, PathBuf& parent_dir, bool child_changed) {
if self.path.is_dir() {
let output_file = self.output_path(parent_dir, &None);
if output.len() == 0 {
return (true, String::new());
}
let res = fs::write(&output_file, output);
if res.is_err() {
eprintln!(
"error: failed to write output {}\n {}",
output_file.display(),
res.unwrap_err(),
);
process::exit(1);
}
(false, format!("pub mod {};\n", self.name))
} else {
let source = match fs::read_to_string(&self.path) {
Ok(s) => s;
Err(e) => { Err(e) => {
eprintln!("error: failed to read file {}\n {}", self.path.display(), e); eprintln!("error: failed to read directory {}\n {}", sub_crates.display(), e);
process::exit(1); process::exit(1);
} }
}; };
for sub_crate in entries {
if let Ok(sub_crate) = sub_crate {
let sub_crate = sub_crate.path();
if fs::exists(sub_crate.join("Mist.toml")).unwrap_or_default() {
transpile(&sub_crate);
}
}
}
} else {
transpile(&incl);
}
}
}
bool ends_with_star(Path& path)
{
path.file_name().and_then((name) => name.to_str()).is_some_and((name) => name.ends_with("*"))
}
impl modules::Module
{
(bool, String) transpile(&self, PathBuf& parent_dir, MistConfig mut& config, String mut& main_mods)
{
if !self.path.is_dir() && self.path.extension().and_then((v) => v.to_str()) != Some("mist") {
let file_name = self.path.file_name().and_then((s) => s.to_str()).expect("Failed to get file name");
fs::copy(&self.path, parent_dir.join(&file_name)).expect("Failed to copy SideFile");
return (true, String::new());
}
let dir = self.output_dir(parent_dir);
let _ = fs::create_dir_all(&dir);
let mut output = String::new();
let mut child_changed = false;
for child in &self.children {
if !config.should_ignore(&child.path) {
let out = child.transpile(&dir, config, main_mods);
if out.0 {
child_changed = true;
}
output.push_str(&out.1);
}
}
if self.path == config.package {
*main_mods = output.clone();
} else if config.should_ignore(&self.path) {
output.push_str(main_mods)
}
self.write_transpile(output, parent_dir, child_changed)
}
(bool, String) write_transpile(&self, String output, PathBuf& parent_dir, bool child_changed)
{
if self.path.is_dir() || self.is_package() {
let output_file = self.output_path(parent_dir, &None);
let final_output = if self.is_package() {
let source = match fs::read_to_string(&self.path) {
Ok(s) => s,
Err(e) => {
eprintln!("error: failed to read file {}\n {}", self.path.display(), e);
process::exit(1);
}
};
let curr_mod_decl = mist_parser::parse_module(&source).expect("Failed parsing module");
let (result_path, gc, rust_code) = match transpile_code(&self.path, &source, output_file.clone(), &output, &curr_mod_decl) {
Ok(v) => v,
Err(e) => {
decode_err(&e, &self.path);
process::exit(1);
}
};
let map_path = result_path.with_extension("map.json");
if let Some(parent) = map_path.parent() {
let _ = fs::create_dir_all(parent);
}
fs::write(&map_path, serde_json::to_string(&gc.mapping).unwrap()).expect("Failed to write map file");
rust_code
} else {
if output.len() == 0 {
return (true, String::new());
}
output
};
let res = fs::write(&output_file, final_output);
if res.is_err() {
eprintln!("error: failed to write output {}\n {}",
output_file.display(),
res.unwrap_err(),);
process::exit(1);
}
(false, format!("pub mod {};\n", self.name))
} else {
let source = match fs::read_to_string(&self.path) {
Ok(s) => s,
Err(e) => {
eprintln!("error: failed to read file {}\n {}", self.path.display(), e);
process::exit(1);
}
};
let curr_mod_decl = mist_parser::parse_module(&source).expect("Failed parsing module"); let curr_mod_decl = mist_parser::parse_module(&source).expect("Failed parsing module");
let output_file = self.output_path(parent_dir, &curr_mod_decl); let output_file = self.output_path(parent_dir, &curr_mod_decl);
let should_change = is_source_newer(&self.path, &output_file).expect("Failed to cache"); let should_change = is_source_newer(&self.path, &output_file).expect("Failed to cache");
if should_change || child_changed { if should_change || child_changed {
transpile_file(&self.path, &source, output_file, &output, &curr_mod_decl); transpile_file(&self.path, &source, output_file, &output, &curr_mod_decl);
} }
(should_change, if let Some((vis, name)) = &curr_mod_decl { (should_change, if let Some((vis, name)) = &curr_mod_decl {
format!("{}mod {};\n", vis.get_rust(), name.get_rust()) format!("{}mod {};\n", vis.get_rust(), name.get_rust())
} else { } else {
@@ -174,141 +198,104 @@ impl modules::Module {
} }
} }
std::io::Result<bool> is_source_newer(Path& source, Path& output) { std::io::Result<bool> is_source_newer(Path& source, Path& output)
{
if !output.exists() { if !output.exists() {
return Ok(true); return Ok(true);
} }
let source_time = fs::metadata(source)?.modified()?; let source_time = fs::metadata(source)?.modified()?;
let output_time = fs::metadata(output)?.modified()?; let output_time = fs::metadata(output)?.modified()?;
Ok(source_time > output_time) Ok(source_time > output_time)
} }
pub enum TranspileError<'a> { pub enum TranspileError<'a>
Parse(error::ParseError<'a>), {
Semantic(Vec<mist_parser::semantics::SemanticError>) Parse(error::ParseError<'a>);
Semantic(Vec<mist_parser::semantics::SemanticError>);
} }
pub Result<(PathBuf, mist_codegen::RustCodegen, String), TranspileError<'a>> pub Result<(PathBuf, mist_codegen::RustCodegen, String), TranspileError<'a>> transpile_code<'a>(PathBuf& path, str 'a& source, PathBuf mut output_file, str& mod_decl, Option<(mist_parser::ast::Visibility, mist_parser::ast::Identifier)>& curr_mod_decl)
transpile_code<'a>( {
PathBuf& path,
str 'a& source,
PathBuf mut output_file,
str& mod_decl,
Option<(mist_parser::ast::Visibility, mist_parser::ast::Identifier)>& curr_mod_decl
) {
if let Some((_, name)) = &curr_mod_decl { if let Some((_, name)) = &curr_mod_decl {
if output_file.file_name().map((v) => v.to_str()).unwrap_or_default() != Some("mod.rs") { if output_file.file_name().map((v) => v.to_str()).unwrap_or_default() != Some("mod.rs") {
if let Some(ext) = output_file.extension().map((e) => e.to_owned()) { if let Some(ext) = output_file.extension().map((e) => e.to_owned()) {
let mut new_name = std::ffi::OsString::from(&name.0); let mut new_name = std::ffi::OsString::from(&name.0);
new_name.push("."); new_name.push(".");
new_name.push(ext); new_name.push(ext);
output_file.set_file_name(new_name); output_file.set_file_name(new_name);
} else { } else {
output_file.set_file_name(&name.0); output_file.set_file_name(&name.0);
} }
} }
} }
let mut gc = mist_codegen::RustCodegen::new(path.clone()); let mut gc = mist_codegen::RustCodegen::new(path.clone());
let parsed = mist_parser::parse(source).map_err(TranspileError::Parse)?; let parsed = mist_parser::parse(source).map_err(TranspileError::Parse)?;
for i in &parsed.items { for i in &parsed.items {
let _ = mist_parser::semantics::check_class_semantics(i).map_err(TranspileError::Semantic)?; let _ = mist_parser::semantics::check_class_semantics(i).map_err(TranspileError::Semantic)?;
} }
gc.generate(parsed.mod_attributes); gc.generate(parsed.mod_attributes);
gc.add(mod_decl); gc.add(mod_decl);
let output = gc.generate(parsed.items); let output = gc.generate(parsed.items);
Ok((output_file, gc, output)) Ok((output_file, gc, output))
} }
pub void transpile_file( pub void transpile_file(PathBuf& path, str& source, PathBuf output_file, str& mod_decl, Option<(mist_parser::ast::Visibility, mist_parser::ast::Identifier)>& curr_mod_decl)
PathBuf& path, {
str& source,
PathBuf output_file,
str& mod_decl,
Option<(mist_parser::ast::Visibility, mist_parser::ast::Identifier)>& curr_mod_decl
) {
let (output_file, gc, output) = match transpile_code(path, source, output_file, mod_decl, curr_mod_decl) { let (output_file, gc, output) = match transpile_code(path, source, output_file, mod_decl, curr_mod_decl) {
Ok(output) => output; Ok(output) => output,
Err(e) => { Err(e) => {
decode_err(&e, path); decode_err(&e, path);
process::exit(1); process::exit(1);
} }
}; };
if let Some(parent) = output_file.parent() {
let _ = fs::create_dir_all(parent);
}
let res = fs::write(&output_file, output); let res = fs::write(&output_file, output);
let output_file_map = output_file.with_extension("map.json"); let output_file_map = output_file.with_extension("map.json");
let res_map = fs::write(&output_file_map, serde_json::to_string(&gc.mapping).unwrap()); let res_map = fs::write(&output_file_map, serde_json::to_string(&gc.mapping).unwrap());
if res_map.is_err() { if res_map.is_err() {
eprintln!( eprintln!("error: failed to write mapping output {}\n {}",
"error: failed to write mapping output {}\n {}",
output_file_map.display(), output_file_map.display(),
res.as_ref().unwrap_err(), res_map.as_ref().unwrap_err(),);
);
} }
if res.is_err() { if res.is_err() {
eprintln!( eprintln!("error: failed to write output {}\n {}",
"error: failed to write output {}\n {}",
output_file.display(), output_file.display(),
res.as_ref().unwrap_err(), res.as_ref().unwrap_err(),);
);
} }
if res_map.is_err() || res.is_err() { if res_map.is_err() || res.is_err() {
process::exit(1); process::exit(1);
} }
} }
pub void decode_err<'a>(TranspileError<'a>& err, PathBuf& path) { pub void decode_err<'a>(TranspileError<'a>& err, PathBuf& path)
{
let curr_dir = std::env::current_dir().unwrap(); let curr_dir = std::env::current_dir().unwrap();
match err { match err {
TranspileError::Semantic(e_list) => { TranspileError::Semantic(e_list) => {
for e in e_list { for e in e_list {
eprintln!( eprintln!("\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}",
"\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}",
path.strip_prefix(&curr_dir).unwrap_or_else(|_| path).as_os_str().display(), path.strip_prefix(&curr_dir).unwrap_or_else(|_| path).as_os_str().display(),
e.line, e.line,
e.column, e.column,
e.error_message, e.error_message,);
);
} }
} }
TranspileError::Parse(e) => { TranspileError::Parse(e) => {
match e { match e {
error::ParseError::Ast(e) => { error::ParseError::Ast(e) => {
let start_pos = e.span.start_pos().line_col(); let start_pos = e.span.start_pos().line_col();
let span = e.span.as_str(); let span = e.span.as_str();
eprintln!("\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}{}\t{}",
eprintln!(
"\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}{}\t{}",
path.strip_prefix(curr_dir).unwrap_or_else(|_| path).as_os_str().display(), path.strip_prefix(curr_dir).unwrap_or_else(|_| path).as_os_str().display(),
start_pos.0, start_pos.0,
start_pos.1, start_pos.1,
e.error_message, e.error_message,
span, span,
if span.ends_with("\n") { "" } else { "\n" }, if span.ends_with("\n") { "" } else { "\n" },
"^".repeat(span.trim().len()), "^".repeat(span.trim().len()),);
);
} }
error::ParseError::PreAst(e) => { error::ParseError::PreAst(e) => {
eprintln!("error: parse failed in {}\n{}", path.strip_prefix(curr_dir).unwrap_or_else(|_| path).display(), e); eprintln!("error: parse failed in {}\n{}", path.strip_prefix(curr_dir).unwrap_or_else(|_| path).display(), e);
} }
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "mist-parser" name = "mist-parser"
version = "0.3.7-alpha.0" version = "0.4.0"
edition = "2024" edition = "2024"
description = "The Mist programming language parser" description = "The Mist programming language parser"
license = "Apache-2.0" license = "Apache-2.0"
+20 -10
View File
@@ -96,16 +96,6 @@ pub enum Generic {
Type(TypeExpr), Type(TypeExpr),
} }
impl Expression {
pub fn is_block(&self) -> bool {
if let Expression::Statement(stmt) = self {
stmt.is_block()
} else {
false
}
}
}
impl From<Path> for ExprPath { impl From<Path> for ExprPath {
fn from(path: Path) -> Self { fn from(path: Path) -> Self {
Self( Self(
@@ -119,3 +109,23 @@ impl From<Path> for ExprPath {
) )
} }
} }
impl Expression {
pub fn is_semicolon_required(&self) -> bool {
match self {
Self::Statement(v) => match &**v {
Statement::TopLevel(_) => false,
stmt => !stmt.is_block(),
},
_ => true,
}
}
pub fn is_block(&self) -> bool {
if let Expression::Statement(stmt) = self {
stmt.is_block()
} else {
false
}
}
}
+2 -2
View File
@@ -41,13 +41,13 @@ pub enum TypeExpr {
Path(Path, Option<Generics>), Path(Path, Option<Generics>),
StaticFn(Vec<TypeExpr>, Option<Box<TypeExpr>>), StaticFn(Vec<TypeExpr>, Option<Box<TypeExpr>>),
Tuple(Vec<TypeExpr>), Tuple(Vec<TypeExpr>),
Lifetime(Identifier),
Void, Void,
Fn { Fn {
kind: FnKind, kind: FnKind,
return_type: Box<TypeExpr>, return_type: Box<TypeExpr>,
params: Vec<TypeExpr>, params: Vec<TypeExpr>,
}, },
Array(Box<TypeExpr>, Option<isize>)
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
@@ -60,7 +60,7 @@ pub struct Spanned<T> {
impl From<GenericDecl> for Generic { impl From<GenericDecl> for Generic {
fn from(value: GenericDecl) -> Self { fn from(value: GenericDecl) -> Self {
match value { match value {
GenericDecl::Lifetime(life) => Generic::Lifetime(life), GenericDecl::Lifetime(life, _) => Generic::Lifetime(life),
GenericDecl::Type(ty, _) => Generic::Type(TypeExpr::Path(Path(vec![ty]), None)), GenericDecl::Type(ty, _) => Generic::Type(TypeExpr::Path(Path(vec![ty]), None)),
} }
} }
+4 -1
View File
@@ -12,6 +12,7 @@ pub struct Block {
pub enum Statement { pub enum Statement {
UnsafeBlock(Block), UnsafeBlock(Block),
Block(Block), Block(Block),
TopLevel(Box<TopLevel>),
If { If {
initial: StatementBranch, initial: StatementBranch,
else_if: Vec<StatementBranch>, else_if: Vec<StatementBranch>,
@@ -43,8 +44,10 @@ pub struct MatchItem(pub Vec<Pattern>, pub Expression);
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct VarDecl { pub struct VarDecl {
pub name: Pattern,
pub type_: Option<TypeExpr>, pub type_: Option<TypeExpr>,
pub name: Pattern,
pub true_type: bool,
pub tuple_names: Vec<Pattern>,
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
+21 -4
View File
@@ -28,8 +28,18 @@ pub struct TopLevel(pub Spanned<TopLevelKind>, pub Vec<Attribute>);
pub enum TopLevelKind { pub enum TopLevelKind {
ModAttribute, ModAttribute,
Import(Visibility, Path), Import(Visibility, Path),
IncludeGlobal(Path),
IncludeUse(Visibility, Path),
IncludeLocal(String),
DeclareModule(Visibility, Identifier), DeclareModule(Visibility, Identifier),
ImplDecl(ImplDecl), ImplDecl(ImplDecl),
StaticDecl(VarDeclStmt),
ConstDecl(VarDeclStmt),
TypeAlias {
name: Identifier,
generics: Option<GenericsDecl>,
ty: TypeExpr,
},
TraitDecl { TraitDecl {
visibility: Visibility, visibility: Visibility,
name: Identifier, name: Identifier,
@@ -49,6 +59,12 @@ pub enum TopLevelKind {
generics: GenericsDecl, generics: GenericsDecl,
fields: Vec<Spanned<FieldDecl>>, fields: Vec<Spanned<FieldDecl>>,
}, },
StructDeclTupleUnit {
visibility: Visibility,
name: Identifier,
generics: GenericsDecl,
unit: Option<Vec<Spanned<TypeExpr>>>,
},
FunctionDecl(FunctionDecl), FunctionDecl(FunctionDecl),
ClassDecl { ClassDecl {
visibility: Visibility, visibility: Visibility,
@@ -72,8 +88,8 @@ pub struct GenericsDecl(pub Vec<GenericDecl>);
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub enum GenericDecl { pub enum GenericDecl {
Lifetime(Identifier), Lifetime(Identifier, Vec<Generic>),
Type(Identifier, Vec<TypeExpr>), Type(Identifier, Vec<Generic>),
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
@@ -97,12 +113,13 @@ pub struct Override(pub Option<ExprPath>);
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct FunctionDecl { pub struct FunctionDecl {
pub visibility: Visibility, pub visibility: Visibility,
pub is_override: Option<Override>, pub is_virtual: bool,
pub return_type: Option<TypeExpr>,
pub name: Identifier, pub name: Identifier,
pub generics: GenericsDecl, pub generics: GenericsDecl,
pub self_param: Option<(bool, Option<Identifier>, bool)>, pub self_param: Option<(bool, Option<Identifier>, bool)>,
pub params: ParamList, pub params: ParamList,
pub return_type: Option<TypeExpr>, pub is_override: Option<Override>,
pub body: Option<Block>, pub body: Option<Block>,
} }
+1
View File
@@ -24,6 +24,7 @@ pub enum ErrorCode {
InvalidStatement, InvalidStatement,
AstGenBug, AstGenBug,
Module, Module,
NumberParse,
} }
impl From<pest::error::Error<Rule>> for ParseError<'_> { impl From<pest::error::Error<Rule>> for ParseError<'_> {
+51 -14
View File
@@ -24,6 +24,7 @@ keyword = {
| "class" | "class"
| "trait" | "trait"
| "impl" | "impl"
| "use"
| "pub" | "pub"
| "mut" | "mut"
| "let" | "let"
@@ -33,6 +34,9 @@ keyword = {
| "loop" | "loop"
| "unsafe" | "unsafe"
| "override" | "override"
| "const"
| "type"
| "virtual"
} }
identifier = @{ identifier = @{
@@ -86,7 +90,7 @@ mutable = { "mut" }
// ====================================================== // ======================================================
// GENERICS // GENERICS
// ====================================================== // ======================================================
generic_decl = { lifetime | (identifier ~ (":" ~ generic ~ ("+" ~ generic)*)?) } generic_decl = { (lifetime | identifier) ~ (":" ~ generic ~ ("+" ~ generic)*)? }
generics_decl = { "<" ~ (generic_decl ~ ("," ~ generic_decl)*)? ~ ">" } generics_decl = { "<" ~ (generic_decl ~ ("," ~ generic_decl)*)? ~ ">" }
generic = { lifetime | type_expr } generic = { lifetime | type_expr }
@@ -121,9 +125,9 @@ fn_kind_mut = { "FnMut" }
fn_kind = _{ fn_kind = _{
fn_kind_fn fn_kind_fn
| fn_kind_unsafe | fn_kind_unsafe
| fn_kind_closure
| fn_kind_once | fn_kind_once
| fn_kind_mut | fn_kind_mut
| fn_kind_closure
} }
fn_type = { fn_type = {
@@ -138,8 +142,12 @@ void_type = {
"void" "void"
} }
array_type = {
"[" ~ integer? ~ "]"
}
type_expr = { type_expr = {
(void_type | path_type | tuple_type | dyn_type) ~ (unsafe_ref_type | ref_type | fn_type)* (void_type | path_type | tuple_type | dyn_type) ~ (unsafe_ref_type | ref_type | fn_type | array_type)*
} }
lifetime = { lifetime = {
@@ -222,9 +230,12 @@ attributes = {
// ====================================================== // ======================================================
// DECLARATION HELPERS // DECLARATION HELPERS
// ====================================================== // ======================================================
as_kw = { "as" }
var_decl = { var_decl = {
("let" ~ pattern) ("let" ~ pattern ~ ("," ~ pattern)*)
| type_expr ~ (!tuple_pattern ~ pattern) | (type_expr ~ as_kw ~ (!tuple_pattern ~ pattern ~ ("," ~ pattern)+))
| (type_expr ~ (!tuple_pattern ~ pattern ~ ("," ~ pattern)*))
} }
field = { field = {
@@ -237,7 +248,7 @@ param = {
} }
field_list = { field_list = {
(field ~ ("," ~ field)* ~ ","?)? (field ~ ";")*
} }
param_list = { param_list = {
@@ -248,6 +259,18 @@ param_list = {
// IMPORTS / MODULES // IMPORTS / MODULES
// ====================================================== // ======================================================
include_global = {
"#" ~ "include" ~ "<" ~ identifier ~ ("/" ~ identifier)* ~ ">"
}
include_use_global = {
visibility? ~ "#" ~ "use" ~ static_path ~ semicolon
}
include_local = {
"#" ~ "include" ~ string_lit
}
import = { import = {
visibility? ~ "use" ~ static_path ~ semicolon visibility? ~ "use" ~ static_path ~ semicolon
} }
@@ -261,7 +284,11 @@ declare_module = {
// ====================================================== // ======================================================
struct_decl = { struct_decl = {
visibility? ~ "struct" ~ identifier ~ generics_decl? ~ "{" ~ field_list? ~ "}" visibility? ~ "struct" ~ identifier ~ generics_decl? ~ "{" ~ field_list ~ "}"
}
tuple_struct_decl = {
visibility? ~ "struct" ~ identifier ~ generics_decl? ~ tuple_type? ~ ";"
} }
// ====================================================== // ======================================================
@@ -286,12 +313,8 @@ enum_field = _{
| enum_named | enum_named
} }
enum_fields = _{
enum_field ~ ("," ~ enum_field)* ~ ","?
}
enum_decl = { enum_decl = {
visibility? ~ "enum" ~ identifier ~ generics_decl? ~ "{" ~ enum_fields? ~ "}" visibility? ~ "enum" ~ identifier ~ generics_decl? ~ "{" ~ (enum_field ~ ";")* ~ "}"
} }
// ====================================================== // ======================================================
@@ -355,19 +378,32 @@ trait_decl = {
// FUNCTIONS // FUNCTIONS
// ====================================================== // ======================================================
virtual_kw = { "virtual" }
override_kw = { "override" ~ ("(" ~ expr_path ~ ")")? } override_kw = { "override" ~ ("(" ~ expr_path ~ ")")? }
function_decl = { function_decl = {
visibility? ~ ("void" | type_expr) ~ identifier ~ generics_decl? ~ "(" ~ method_args ~ ")" ~ override_kw? ~ (block | semicolon) visibility? ~ virtual_kw? ~ ("void" | type_expr) ~ identifier ~ generics_decl? ~ "(" ~ method_args ~ ")" ~ override_kw? ~ (block | semicolon)
} }
// ======================================================
// CONSTANTS / STATIC
// ======================================================
const_decl = { "const" ~ var_decl_statement ~ ";" }
static_decl = { "static" ~ var_decl_statement ~ ";" }
// ======================================================
// Type aliases
// ======================================================
type_alias = { "type" ~ identifier ~ generics_decl? ~ "=" ~ type_expr ~ ";" }
// ====================================================== // ======================================================
// TOP LEVEL // TOP LEVEL
// ====================================================== // ======================================================
top_level = { top_level = {
mod_attribute mod_attribute
| (attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl)) | (attributes ~ (include_global | include_local | include_use_global | import | impl_for_decl | impl_decl | trait_decl | struct_decl | tuple_struct_decl | enum_decl | class_decl | declare_module | function_decl | const_decl | static_decl | type_alias))
} }
// ====================================================== // ======================================================
@@ -388,6 +424,7 @@ statement_list = {
statement = _{ statement = _{
control_flow control_flow
| top_level
| (expr ~ semicolon) | (expr ~ semicolon)
} }
+3 -1
View File
@@ -2,7 +2,7 @@ use crate::{
Rule, Rule,
ast::*, ast::*,
error::{AstError, collect_recovered}, error::{AstError, collect_recovered},
parser::consume_rule, parser::{consume_rule, listen_rule},
}; };
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDeclStmt {
@@ -55,7 +55,9 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for VarDecl {
type_: consume_rule(&mut inner, Rule::type_expr) type_: consume_rule(&mut inner, Rule::type_expr)
.map(TypeExpr::try_from) .map(TypeExpr::try_from)
.transpose()?, .transpose()?,
true_type: listen_rule(&mut inner, Rule::as_kw),
name: Pattern::try_from(inner.next().unwrap())?, name: Pattern::try_from(inner.next().unwrap())?,
tuple_names: collect_recovered(inner)?,
}) })
} }
+2 -1
View File
@@ -94,7 +94,8 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
| Rule::basic_stmt | Rule::basic_stmt
| Rule::control_flow | Rule::control_flow
| Rule::block | Rule::block
| Rule::unsafe_block => Ok(Expression::Statement(Box::new(pair.try_into()?))), | Rule::unsafe_block
| Rule::top_level => Ok(Expression::Statement(Box::new(pair.try_into()?))),
_ => AstError::bug_unimplemented(pair), _ => AstError::bug_unimplemented(pair),
} }
@@ -51,6 +51,8 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Statement {
Rule::block => Ok(Statement::Block(pair.try_into()?)), Rule::block => Ok(Statement::Block(pair.try_into()?)),
Rule::top_level => Ok(Statement::TopLevel(Box::new(pair.try_into()?))),
Rule::var_decl_statement => Ok(Statement::VarDecl(pair.try_into()?)), Rule::var_decl_statement => Ok(Statement::VarDecl(pair.try_into()?)),
Rule::return_stmt => Ok(Statement::Return( Rule::return_stmt => Ok(Statement::Return(
+20 -1
View File
@@ -50,13 +50,31 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TypeExpr {
}; };
} }
Rule::array_type => {
ty = TypeExpr::Array(
Box::new(ty),
ref_inner
.next()
.map(|v| {
v.as_str().parse().map_err(|_| AstError {
span: v.as_span(),
error_code: crate::error::ErrorCode::NumberParse,
error_message: format!(
"Number exceeds isize limits: {}",
v.as_str()
),
})
})
.transpose()?,
)
}
_ => AstError::bug_unimplemented(ref_pair)?, _ => AstError::bug_unimplemented(ref_pair)?,
} }
} }
Ok(ty) Ok(ty)
} }
Rule::lifetime => Ok(TypeExpr::Lifetime(inner.next().unwrap().try_into()?)),
Rule::void_type => Ok(TypeExpr::Void), Rule::void_type => Ok(TypeExpr::Void),
Rule::tuple_type => Ok(TypeExpr::Tuple(collect_recovered(inner)?)), Rule::tuple_type => Ok(TypeExpr::Tuple(collect_recovered(inner)?)),
@@ -113,6 +131,7 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for GenericDecl {
if let Some(pair) = consume_rule(&mut inner, Rule::lifetime) { if let Some(pair) = consume_rule(&mut inner, Rule::lifetime) {
Ok(GenericDecl::Lifetime( Ok(GenericDecl::Lifetime(
pair.into_inner().next().unwrap().try_into()?, pair.into_inner().next().unwrap().try_into()?,
collect_recovered(inner)?,
)) ))
} else { } else {
Ok(GenericDecl::Type( Ok(GenericDecl::Type(
+4 -1
View File
@@ -14,6 +14,8 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
let visibility = Visibility::try_from(&mut inner)?; let visibility = Visibility::try_from(&mut inner)?;
let is_virtual = listen_rule(&mut inner, Rule::virtual_kw);
let return_type = consume_rule(&mut inner, Rule::type_expr) let return_type = consume_rule(&mut inner, Rule::type_expr)
.map(TypeExpr::try_from) .map(TypeExpr::try_from)
.transpose()?; .transpose()?;
@@ -45,12 +47,13 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for FunctionDecl {
Ok(Self { Ok(Self {
visibility: visibility, visibility: visibility,
is_override: is_override, is_virtual: is_virtual,
return_type: return_type, return_type: return_type,
name: name, name: name,
generics: generics, generics: generics,
self_param: self_param, self_param: self_param,
params: params, params: params,
is_override: is_override,
body: body, body: body,
}) })
}) })
+43 -3
View File
@@ -54,11 +54,22 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
.transpose() .transpose()
.map(|v| v.unwrap_or_default())?, .map(|v| v.unwrap_or_default())?,
fields: inner fields: collect_recovered(inner.next().unwrap().into_inner())?,
.next() }),
.map(|pair| collect_recovered(pair.into_inner()))
Rule::tuple_struct_decl => Ok(TopLevelKind::StructDeclTupleUnit {
visibility: Visibility::try_from(&mut inner)?,
name: inner.next().unwrap().try_into()?,
generics: consume_rule(&mut inner, Rule::generics_decl)
.map(GenericsDecl::try_from)
.transpose() .transpose()
.map(|v| v.unwrap_or_default())?, .map(|v| v.unwrap_or_default())?,
unit: consume_rule(&mut inner, Rule::tuple_type)
.map(|v| collect_recovered(v.into_inner()))
.transpose()?,
}), }),
Rule::class_decl => Ok(TopLevelKind::ClassDecl { Rule::class_decl => Ok(TopLevelKind::ClassDecl {
@@ -122,6 +133,35 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
items: collect_recovered(&mut inner)?, items: collect_recovered(&mut inner)?,
}), }),
Rule::const_decl => Ok(TopLevelKind::ConstDecl(inner.next().unwrap().try_into()?)),
Rule::static_decl => Ok(TopLevelKind::StaticDecl(inner.next().unwrap().try_into()?)),
Rule::type_alias => Ok(TopLevelKind::TypeAlias {
name: inner.next().unwrap().try_into()?,
generics: consume_rule(&mut inner, Rule::generics_decl)
.map(GenericsDecl::try_from)
.transpose()?,
ty: inner.next().unwrap().try_into()?,
}),
Rule::include_global => Ok(TopLevelKind::IncludeGlobal(Path(collect_recovered(
&mut inner,
)?))),
Rule::include_use_global => Ok(TopLevelKind::IncludeUse(
Visibility::try_from(&mut inner)?,
Path::try_from(inner.next().unwrap())?,
)),
Rule::include_local => Ok(TopLevelKind::IncludeLocal(
inner
.next()
.unwrap()
.into_inner()
.next()
.unwrap()
.to_string(),
)),
_ => AstError::bug_unimplemented(pair), _ => AstError::bug_unimplemented(pair),
} }
} }
+2 -2
View File
@@ -3,12 +3,12 @@ use std::{collections::HashSet, path::PathBuf};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive( #[derive(
Debug, Default, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize,
)] )]
pub struct MistMap(pub usize, pub usize); pub struct MistMap(pub usize, pub usize);
#[derive( #[derive(
Debug, Default, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize,
)] )]
pub struct RustMap(pub usize, pub usize); pub struct RustMap(pub usize, pub usize);
+30 -4
View File
@@ -46,10 +46,13 @@ pub fn check_class_semantics<'a>(top_level: &TopLevel) -> Result<(), Vec<Semanti
if let Some(constructor) = constructor { if let Some(constructor) = constructor {
let mut fields = fields let mut fields = fields
.iter() .iter()
.map(|field| Spanned { .filter_map(|field| match field.item.init {
item: field.item.decl.name.clone(), Some(_) => None,
line: field.line, None => Some(Spanned {
column: field.column, item: field.item.decl.name.clone(),
line: field.line,
column: field.column,
}),
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@@ -99,6 +102,28 @@ pub fn check_class_semantics<'a>(top_level: &TopLevel) -> Result<(), Vec<Semanti
} }
Ok(()) Ok(())
} else if let TopLevelKind::FunctionDecl(decl) = &top_level.0.item {
if decl.is_virtual && decl.is_override.is_some() {
Err(vec![SemanticError {
line: top_level.0.line,
column: top_level.0.column,
error_message: "virtual and override are only used in classes".to_string(),
}])
} else if decl.is_virtual {
Err(vec![SemanticError {
line: top_level.0.line,
column: top_level.0.column,
error_message: "virtual is only used in classes".to_string(),
}])
} else if decl.is_override.is_some() {
Err(vec![SemanticError {
line: top_level.0.line,
column: top_level.0.column,
error_message: "override is only used in classes".to_string(),
}])
} else {
Ok(())
}
} else { } else {
Ok(()) Ok(())
} }
@@ -129,6 +154,7 @@ impl GetMutability for Block {
impl GetMutability for Statement { impl GetMutability for Statement {
fn get_mutability(&self) -> Vec<Identifier> { fn get_mutability(&self) -> Vec<Identifier> {
match self { match self {
Statement::TopLevel(_) => Vec::new(),
Statement::Block(body) | Statement::UnsafeBlock(body) => body.get_mutability(), Statement::Block(body) | Statement::UnsafeBlock(body) => body.get_mutability(),
Statement::If { Statement::If {
initial, initial,
+56
View File
@@ -6,6 +6,13 @@ pub void test_let_basic()
assert_eq!(x, 42); assert_eq!(x, 42);
} }
pub void test_let_repeat()
{
let x, y = (42, 67);
assert_eq!(x, 42);
assert_eq!(y, 67);
}
pub void test_let_typed() pub void test_let_typed()
{ {
i32 x = 42; i32 x = 42;
@@ -18,6 +25,29 @@ pub void test_let_typed()
assert_eq!(f, 3.14); assert_eq!(f, 3.14);
} }
pub void test_let_repeat_typed()
{
i32 x, x1 = (42, 67);
assert_eq!(x, 42);
assert_eq!(x1, 67);
str& s, s1 = ("hello", "world");
assert_eq!(s, "hello");
assert_eq!(s1, "world");
bool b, b1 = (true, false);
assert_eq!(b, true);
assert_eq!(b1, false);
f64 f, f1 = (3.14, 5.5);
assert_eq!(f, 3.14);
assert_eq!(f1, 5.5);
(i32, f64) as a, b = (70, 1.4);
assert_eq!(a, 70);
assert_eq!(b, 1.4);
}
pub void test_let_mut() pub void test_let_mut()
{ {
let mut _x = 42; let mut _x = 42;
@@ -76,10 +106,33 @@ pub void test_let_bool()
assert_eq!(t, !f); assert_eq!(t, !f);
} }
pub void test_let_decl_only()
{
let x;
x = 42;
assert_eq!(x, 42);
}
pub void test_let_typed_decl_only()
{
i32 x;
x = 100;
assert_eq!(x, 100);
}
pub void test_let_decl_mut_no_init()
{
let mut x;
x = 5;
assert_eq!(x, 5);
}
pub void run_tests() pub void run_tests()
{ {
test_let_basic(); test_let_basic();
test_let_repeat();
test_let_typed(); test_let_typed();
test_let_repeat_typed();
test_let_mut(); test_let_mut();
test_let_tuple_destructure(); test_let_tuple_destructure();
test_let_nested_tuple(); test_let_nested_tuple();
@@ -88,4 +141,7 @@ pub void run_tests()
test_let_with_type_and_init(); test_let_with_type_and_init();
test_string_literal(); test_string_literal();
test_let_bool(); test_let_bool();
test_let_decl_only();
test_let_typed_decl_only();
test_let_decl_mut_no_init();
} }
+64
View File
@@ -155,6 +155,63 @@ pub void test_binary_assign()
assert_eq!(x, 2); assert_eq!(x, 2);
} }
pub void test_binary_assign_bit()
{
let mut x = 10;
x &= 12;
assert_eq!(x, 8);
x |= 1;
assert_eq!(x, 9);
x ^= 3;
assert_eq!(x, 10);
let mut y = 8;
y <<= 2;
assert_eq!(y, 32);
y >>= 1;
assert_eq!(y, 16);
}
pub void test_empty_tuple()
{
let t = ();
assert_eq!(t, ());
}
pub void test_empty_array()
{
let arr = [0; 0];
assert_eq!(arr.len(), 0);
}
pub void test_range_inclusive()
{
let mut sum = 0;
for i in 1 ..= 3 {
sum += i;
}
assert_eq!(sum, 6);
}
pub void test_closure_no_args()
{
let f = () => 42;
assert_eq!(f(), 42);
}
pub void test_closure_with_typed_params()
{
let add = (i32 a, i32 b) => a + b;
assert_eq!(add(3, 4), 7);
}
pub void test_mut_ref()
{
let mut x = 42;
let r = &mut x;
*r = 100;
assert_eq!(x, 100);
}
pub void run_tests() pub void run_tests()
{ {
test_literals(); test_literals();
@@ -174,4 +231,11 @@ pub void run_tests()
test_if_expr(); test_if_expr();
test_range_expr(); test_range_expr();
test_binary_assign(); test_binary_assign();
test_binary_assign_bit();
test_empty_tuple();
test_empty_array();
test_range_inclusive();
test_closure_no_args();
test_closure_with_typed_params();
test_mut_ref();
} }
+68
View File
@@ -160,6 +160,68 @@ pub void test_for_with_pattern_destructure()
assert_eq!(keys, 3); assert_eq!(keys, 3);
} }
pub void test_c_for_loop()
{
let mut sum = 0;
for (let mut i = 0; i < 4; i++) {
sum += i;
}
assert_eq!(sum, 6);
}
pub void test_unsafe_block()
{
let mut x = 42;
unsafe {
let r = &mut x;
*r = 100;
}
assert_eq!(x, 100);
}
pub void test_match_parenthesized()
{
let result;
match (2) {
1 => result = 10,
2 => result = 20,
_ => panic!(),
}
assert_eq!(result, 20);
}
pub void test_if_parenthesized()
{
let mut x = 0;
if (true) {
x = 1;
}
assert_eq!(x, 1);
}
pub void test_while_parenthesized()
{
let mut count = 0;
while (count < 3) {
count++;
}
assert_eq!(count, 3);
}
pub void test_loop_with_break_value()
{
let mut x = 0;
let result;
loop {
if x >= 3 {
result = x;
break;
}
x++;
}
assert_eq!(result, 3);
}
pub void run_tests() pub void run_tests()
{ {
test_var_decl(); test_var_decl();
@@ -176,4 +238,10 @@ pub void run_tests()
test_match_or_pattern(); test_match_or_pattern();
test_match_default(); test_match_default();
test_for_with_pattern_destructure(); test_for_with_pattern_destructure();
test_c_for_loop();
test_unsafe_block();
test_match_parenthesized();
test_if_parenthesized();
test_while_parenthesized();
test_loop_with_break_value();
} }
+28
View File
@@ -52,6 +52,30 @@ pub void test_fn_return_type() {
assert_eq!(add(3, 4), 7); assert_eq!(add(3, 4), 7);
} }
pub void test_generic_type() {
Option<i32> x = Option::Some(42);
assert_eq!(x.is_some(), true);
}
type MyInt = i32;
pub void test_type_alias() {
MyInt x = 42;
assert_eq!(x, 42);
}
const i32 MY_CONST = 100;
pub void test_const_decl() {
assert_eq!(MY_CONST, 100);
}
static i32 my_static = 200;
pub void test_static_decl() {
assert_eq!(my_static, 200);
}
pub void run_tests() { pub void run_tests() {
test_integer_type(); test_integer_type();
test_float_type(); test_float_type();
@@ -62,4 +86,8 @@ pub void run_tests() {
test_mut_ref_type(); test_mut_ref_type();
test_void_type(); test_void_type();
test_fn_return_type(); test_fn_return_type();
test_generic_type();
test_type_alias();
test_const_decl();
test_static_decl();
} }
+14 -1
View File
@@ -3,7 +3,7 @@ pub module attribute_test;
#[derive(Debug)] #[derive(Debug)]
pub struct Debuggable pub struct Debuggable
{ {
i32 value, i32 value;
} }
pub void test_derive_attribute() pub void test_derive_attribute()
@@ -22,8 +22,21 @@ pub void test_allow_attribute()
assert_eq!(x, 42); assert_eq!(x, 42);
} }
#[allow(dead_code)]
pub struct Tagged
{
i32 id;
}
pub void test_attribute_with_args()
{
Tagged t = Tagged { id: 1 };
assert_eq!(t.id, 1);
}
pub void run_tests() pub void run_tests()
{ {
test_derive_attribute(); test_derive_attribute();
test_allow_attribute(); test_allow_attribute();
test_attribute_with_args();
} }
+80 -1
View File
@@ -1,5 +1,9 @@
pub module class_test; pub module class_test;
pub enum AnimalType {
Mammal;
}
pub class Animal pub class Animal
{ {
pub String name; pub String name;
@@ -9,11 +13,17 @@ pub class Animal
self.name = "Rex".to_string(); self.name = "Rex".to_string();
} }
pub String speak(&self) pub virtual AnimalType get_animal_type(&self) {
panic!()
}
pub virtual String speak(&self)
{ {
"Unknown".to_string() "Unknown".to_string()
} }
pub virtual void speak_mut(&mut self){}
pub void test_speak<'a>(&'a self) pub void test_speak<'a>(&'a self)
{ {
assert_eq!(self.speak(), "Woof!"); assert_eq!(self.speak(), "Woof!");
@@ -31,6 +41,11 @@ pub class Mammal : Animal
super = Super::new(); super = Super::new();
} }
// override without a target assumes current super
pub AnimalType get_animal_type(&self) override {
AnimalType::Mammal
}
pub void args_test(&self, i32 x, str& y) pub void args_test(&self, i32 x, str& y)
{ {
assert_eq!(x, 10); assert_eq!(x, 10);
@@ -59,10 +74,74 @@ pub class Dog : Mammal
} }
} }
pub class Counter
{
pub i32 count;
constructor()
{
self.count = 42;
}
i32 get_count(&self)
{
self.count
}
pub i32 static_fn()
{
99
}
}
pub void test_class_no_inheritance()
{
Counter c = Counter::new();
assert_eq!(c.get_count(), 42);
}
pub class WithDefault
{
pub String name = "default".to_string();
pub i32 value = 42;
constructor()
{
}
pub String get_name(&self)
{
self.name.clone()
}
}
pub void test_class_field_default()
{
WithDefault w = WithDefault::new();
assert_eq!(w.get_name(), "default");
assert_eq!(w.value, 42);
}
pub class EmptyClass
{
constructor()
{
}
}
pub void test_class_empty()
{
EmptyClass e = EmptyClass::new();
}
pub void run_tests() pub void run_tests()
{ {
let dawg = Dog::new(); let dawg = Dog::new();
dawg.args_test(10, "hello"); dawg.args_test(10, "hello");
dawg.test_speak(); dawg.test_speak();
dawg.get_animal_type();
assert_eq!(format!("{dawg}"), "🐾 Rex"); assert_eq!(format!("{dawg}"), "🐾 Rex");
test_class_no_inheritance();
test_class_field_default();
test_class_empty();
} }
+42 -7
View File
@@ -2,8 +2,8 @@ pub module enum_test;
enum OptionInt enum OptionInt
{ {
None, None;
Some(i32), Some(i32);
} }
pub void test_enum_named() pub void test_enum_named()
@@ -34,8 +34,13 @@ pub void test_enum_tuple()
enum Shape enum Shape
{ {
Circle {i32 radius}, Circle {
Rect {i32 w, i32 h}, i32 radius;
};
Rect {
i32 w;
i32 h;
};
} }
pub void test_enum_struct() pub void test_enum_struct()
@@ -67,9 +72,9 @@ pub void test_enum_struct()
enum Color enum Color
{ {
Red, Red;
Green, Green;
Blue, Blue;
} }
pub void test_enum_eq() pub void test_enum_eq()
@@ -85,10 +90,40 @@ pub void test_enum_eq()
} }
} }
enum Option<T>
{
None;
Some(T);
}
pub void test_generic_enum()
{
Option<i32> x = Option::Some(42);
match x {
Option::Some(v) => {
assert_eq!(v, 42);
}
_ => {
assert_eq!(0, 1);
}
}
Option<bool> y = Option::None;
match y {
Option::None => {
assert_eq!(1, 1);
}
_ => {
assert_eq!(0, 1);
}
}
}
pub void run_tests() pub void run_tests()
{ {
test_enum_named(); test_enum_named();
test_enum_tuple(); test_enum_tuple();
test_enum_struct(); test_enum_struct();
test_enum_eq(); test_enum_eq();
test_generic_enum();
} }
+12
View File
@@ -69,6 +69,17 @@ pub void test_recursive_fn()
assert_eq!(factorial(5), 120); assert_eq!(factorial(5), 120);
} }
T identity<T>(T x)
{
x
}
pub void test_fn_generic()
{
assert_eq!(identity(42), 42);
assert_eq!(identity("hello"), "hello");
}
pub void run_tests() pub void run_tests()
{ {
test_basic_function(); test_basic_function();
@@ -79,4 +90,5 @@ pub void run_tests()
test_method_call(); test_method_call();
test_nested_fn_calls(); test_nested_fn_calls();
test_recursive_fn(); test_recursive_fn();
test_fn_generic();
} }
+22 -2
View File
@@ -2,8 +2,8 @@ pub module impl_test;
struct Vector2 struct Vector2
{ {
i32 x, i32 x;
i32 y, i32 y;
} }
impl Vector2 impl Vector2
@@ -72,9 +72,29 @@ pub void test_multiple_impls()
assert_eq!(c.y, 6); assert_eq!(c.y, 6);
} }
struct Wrapper<T>
{
T value;
}
impl<T: Copy> Wrapper<T>
{
pub T get(&self)
{
self.value
}
}
pub void test_impl_generic()
{
Wrapper<i32> w = Wrapper { value: 42 };
assert_eq!(w.get(), 42);
}
pub void run_tests() pub void run_tests()
{ {
test_inherent_impl(); test_inherent_impl();
test_trait_impl_block(); test_trait_impl_block();
test_multiple_impls(); test_multiple_impls();
test_impl_generic();
} }
+52 -4
View File
@@ -2,8 +2,8 @@ pub module struct_test;
struct Point struct Point
{ {
i32 x, i32 x;
i32 y, i32 y;
} }
pub void test_struct_create_access() pub void test_struct_create_access()
@@ -39,8 +39,8 @@ pub void test_struct_destructure()
struct Line struct Line
{ {
Point start, Point start;
Point end, Point end;
} }
pub void test_struct_nested() pub void test_struct_nested()
@@ -59,10 +59,58 @@ pub void test_struct_nested()
assert_eq!(l.end.y, 10); assert_eq!(l.end.y, 10);
} }
struct Empty {}
pub void test_empty_struct()
{
let e = Empty {};
}
struct Pair<T>
{
T first;
T second;
}
pub void test_generic_struct()
{
Pair<i32> p = Pair<i32> { first: 1, second: 2 };
assert_eq!(p.first, 1);
assert_eq!(p.second, 2);
}
pub void test_struct_partial_destructure()
{
let p = Point {
x: 10,
y: 20,
};
let Point {x, ..} = p;
assert_eq!(x, 10);
}
pub void test_struct_destructure_rename()
{
let p = Point {
x: 5,
y: 15,
};
let Point {x: a, y: b} = p;
assert_eq!(a, 5);
assert_eq!(b, 15);
}
struct TuplePoint(i32, i32);
struct Unit;
pub void run_tests() pub void run_tests()
{ {
test_struct_create_access(); test_struct_create_access();
test_struct_mut_field(); test_struct_mut_field();
test_struct_destructure(); test_struct_destructure();
test_struct_nested(); test_struct_nested();
test_empty_struct();
test_generic_struct();
test_struct_partial_destructure();
test_struct_destructure_rename();
} }
+1 -1
View File
@@ -12,7 +12,7 @@ trait Greet : Speak
struct Dog struct Dog
{ {
String name, String name;
} }
impl Speak for Dog impl Speak for Dog
+2 -1
View File
@@ -1,3 +1,4 @@
void main() { void main()
{
mist_analyzer::start(); mist_analyzer::start();
} }
+141 -51
View File
@@ -1,35 +1,64 @@
use std::process; use std::process;
use std::env; use std::env;
use std::fs; use std::fs;
use mist_api::builder; use mist_api::builder;
use mist_api::transpiler; use mist_api::transpiler;
void main() { void main()
Vec<String> args = env::args().collect(); {
Vec<String> mut args = env::args().collect();
if args.len() < 2 { if args.len() < 2 {
print_usage(); print_usage();
process::exit(1); process::exit(1);
} }
match args[1].as_str() { match args[1].as_str() {
"run" | "build" | "check" | "r" | "b" | "c" => { "run" | "build" | "check" | "test" | "bench" | "doc" | "fix" | "clippy" | "r" | "b" | "c" => {
let root = transpiler::transpile(); let mut root = env::current_dir().expect("Unable to find project root");
modify_args(&mut args, &mut root);
transpiler::transpile(&root);
println!(""); println!("");
if builder::build(args, root) { if builder::build(args, root) {
println!("\x1b[32m\nBuild successful\x1b[0m"); println!("\x1b[32m\nBuild successful\x1b[0m");
} else { } else {
println!("\x1b[31m\nBuild failed\x1b[0m"); println!("\x1b[31m\nBuild failed\x1b[0m");
} }
} }
"publish" => {
let mut root = env::current_dir().expect("Unable to find project root");
modify_args(&mut args, &mut root);
transpiler::transpile(&root);
let status = std::process::Command::new("cargo")
.args(args)
.status()
.expect("Failed to run cargo");
if status.success() {
println!("\x1b[32m\nPublished successfully\x1b[0m");
} else {
println!("\x1b[31m\nFailed to publish\x1b[0m");
}
}
"transpile" | "t" => { "transpile" | "t" => {
transpiler::transpile(); let mut root = env::current_dir().expect("Unable to find project root");
modify_args(&mut args, &mut root);
transpiler::transpile(&root);
}
"clean" => {
args.remove(0);
process::Command::new("cargo").args(args).stdout(process::Stdio::inherit()).stderr(process::Stdio::inherit()).stdin(process::Stdio::inherit()).spawn().expect("Failed to run cargo").wait().expect("Unable to wait");
} }
"init" => { "init" => {
init_project(args); init_project(args);
} }
"new" => {
new_project(args);
}
"version" | "--version" | "-v" => { "version" | "--version" | "-v" => {
println!("mist {}", env!("CARGO_PKG_VERSION")); println!("mist {}", env!("CARGO_PKG_VERSION"));
} }
@@ -44,62 +73,123 @@ void main() {
} }
} }
void print_usage() { void modify_args(Vec<String> mut& args, std::path::PathBuf mut& root)
println!("Mist - the mist compiler\n"); {
println!("󰖟 View our documentation! \x1b[35mhttps://mist.selimaj.dev\x1b[0m"); if let Some(path_arg) = args.get(2) {
println!(" Follow us on github! \x1b[35mhttps://github.com/mist-go\x1b[0m\n"); if path_arg == "--path" {
println!("usage:"); if args.len() > 3 {
println!(" \x1b[36m mist run\x1b[0m,\x1b[36m r\x1b[0m run the project in the current directory\n"); let path_arg = args[3].clone();
println!( *root = root.join(&path_arg);
" \x1b[36m mist build\x1b[0m,\x1b[36m b\x1b[0m build the project in the current directory\n" args.remove(2);
); args.remove(2);
println!( args.push(format!("--manifest-path={}/Cargo.toml", root.display()));
" \x1b[36m mist transpile\x1b[0m,\x1b[36m t\x1b[0m transpile the project in the current directory\n" }
); }
println!( }
" \x1b[36m mist check\x1b[0m,\x1b[36m c\x1b[0m check the project in the current directory\n"
);
println!(
" \x1b[36m mist init\x1b[0m initialize a project in the current folder\n"
);
println!(" \x1b[36m mist version \x1b[0m print the compiler version\n");
println!(" \x1b[36m mist help\x1b[0m print this message\n");
} }
void init_project(Vec<String> mut args) { void print_usage()
{
println!("Mist - the mist compiler\n");
println!("󰖟 View our documentation! \x1b[95mhttps://mist.selimaj.dev\x1b[0m");
println!(" Follow us on github! \x1b[95mhttps://github.com/mist-go\x1b[0m\n");
println!("usage:");
println!(" \x1b[96m mist run\x1b[0m,\x1b[96m r\x1b[0m run the project\n");
println!(" \x1b[96m mist build\x1b[0m,\x1b[96m b\x1b[0m build the project\n");
println!(" \x1b[96m mist test\x1b[0m test the project\n");
println!(" \x1b[96m mist publish\x1b[0m publish the project\n");
println!(" \x1b[96m mist bench\x1b[0m benchmark the project\n");
println!(" \x1b[96m mist doc\x1b[0m build documentation\n");
println!(" \x1b[96m mist fix\x1b[0m automatically fix warnings\n");
println!(" \x1b[96m mist clippy\x1b[0m lint the project\n");
println!(" \x1b[96m mist check\x1b[0m,\x1b[96m c\x1b[0m check the project\n");
println!(" \x1b[96m mist clean\x1b[0m clean build artifacts\n");
println!(" \x1b[96m mist transpile\x1b[0m,\x1b[96m t\x1b[0m transpile the project\n");
println!(" \x1b[96m mist init\x1b[0m initialize a project in the current folder\n");
println!(" \x1b[96m mist new\x1b[0m create a new project\n");
println!(" \x1b[96m mist version \x1b[0m print the compiler version\n");
println!(" \x1b[96m mist help\x1b[0m print this message\n");
}
void init_project(Vec<String> mut args)
{
args.remove(0); args.remove(0);
let status = process::Command::new("cargo").args(args).stdout(process::Stdio::inherit()).stderr(process::Stdio::inherit()).stdin(process::Stdio::inherit()).spawn().expect("Failed to run cargo").wait().expect("Unable to wait");
process::Command::new("cargo") if !status.success() {
.args(args) return;
.stdout(process::Stdio::inherit()) }
.stderr(process::Stdio::inherit())
.stdin(process::Stdio::inherit())
.spawn()
.expect("Failed to run cargo").wait().expect("Unable to wait");
bool is_lib = fs::metadata("src/lib.rs").is_ok(); bool is_lib = fs::metadata("src/lib.rs").is_ok();
if is_lib { if is_lib {
fs::remove_file("src/lib.rs").expect("Failed to remove src/lib.rs"); fs::remove_file("src/lib.rs").expect("Failed to remove src/lib.rs");
fs::write("src/lib.mist", "fn test() {\n}").expect("Failed to create src/lib.mist"); fs::write("src/lib.mist", "void test() {\n}").expect("Failed to create src/lib.mist");
} else { } else {
fs::remove_file("src/main.rs").expect("Failed to remove src/main.rs"); fs::remove_file("src/main.rs").expect("Failed to remove src/main.rs");
fs::write("src/main.mist", "fn main() {\n println!(\"Hello, World!\");\n}\n").expect("Failed to create src/main.mist"); fs::write("src/main.mist", "void main() {\n println!(\"Hello, World!\");\n}\n").expect("Failed to create src/main.mist");
} }
inject_mist_target(is_lib); inject_mist_target(is_lib);
add_mist_toml(is_lib);
println!("\n\x1b[34minfo\x1b[0m: Please open Cargo.toml and adjust `\x1b[35mname\x1b[0m = ` as needed, Thank you for using Mist!\n"); add_gitignore();
println!("Thank you for using Mist!\n");
} }
void inject_mist_target(bool is_lib) { void new_project(Vec<String> mut args)
String toml_content = fs::read_to_string("Cargo.toml").expect("Failed to read Cargo.toml"); {
args.remove(0);
str& inject = if is_lib { if args.len() < 2 {
"\n[lib]\nname = \"my_lib\"\npath = \".mist/src/lib.rs\"\n" eprintln!("error: missing project name\n");
print_usage();
process::exit(1);
}
String project_name = args[1].clone();
let status = process::Command::new("cargo").args(args).stdout(process::Stdio::inherit()).stderr(process::Stdio::inherit()).stdin(process::Stdio::inherit()).spawn().expect("Failed to run cargo").wait().expect("Unable to wait");
if !status.success() {
return;
}
std::path::PathBuf project_path = env::current_dir().expect("Unable to find project root").join(&project_name);
env::set_current_dir(&project_path).expect("Failed to change to project directory");
bool is_lib = fs::metadata("src/lib.rs").is_ok();
if is_lib {
fs::remove_file("src/lib.rs").expect("Failed to remove src/lib.rs");
fs::write("src/lib.mist", "void test() {\n}").expect("Failed to create src/lib.mist");
} else { } else {
"\n[[bin]]\nname = \"my_bin\"\npath = \".mist/src/main.rs\"\n" fs::remove_file("src/main.rs").expect("Failed to remove src/main.rs");
fs::write("src/main.mist", "void main() {\n println!(\"Hello, World!\");\n}\n").expect("Failed to create src/main.mist");
}
inject_mist_target(is_lib);
add_mist_toml(is_lib);
add_gitignore();
println!("Thank you for using Mist!\n");
}
void inject_mist_target(bool is_lib)
{
String toml_content = fs::read_to_string("Cargo.toml").expect("Failed to read Cargo.toml");
toml::Value parsed = toml::from_str(&toml_content).expect("Failed to parse Cargo.toml");
String name = parsed["package"]["name"].as_str().expect("Missing package.name in Cargo.toml").to_string();
String inject = if is_lib {
format!("\n[lib]\nname = \"{name}\"\npath = \".mist/src/lib.rs\"\n")
} else {
format!("\n[[bin]]\nname = \"{name}\"\npath = \".mist/src/main.rs\"\n")
};
fs::write("Cargo.toml", toml_content + &inject).expect("Failed to inject to Cargo.toml");
}
void add_mist_toml(bool is_lib)
{
str& content = if is_lib {
"package = \"lib.mist\"\npackages = []\ninclude = []\n"
} else {
"package = \"main.mist\"\npackages = []\ninclude = []\n"
}; };
fs::write("Mist.toml", content).expect("Failed to add Mist.toml");
}
fs::write("Cargo.toml", toml_content+inject).expect("Failed to inject to Cargo.toml"); void add_gitignore()
{
if fs::metadata(".gitignore").is_ok() {
String content = fs::read_to_string(".gitignore").expect("Failed to read .gitignore");
if !content.contains(".mist") {
fs::write(".gitignore", content + "\n.mist\n").expect("Failed to write .gitignore");
}
}
} }