Compare commits
28
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e3c9657b07 | ||
|
|
2aab50d36d | ||
|
|
e75b707981 | ||
|
|
18a13dd170 | ||
|
|
aacc08b983 | ||
|
|
df8e67fd4f | ||
|
|
08771fe5be | ||
|
|
0d2199ee3a | ||
|
|
442d1e66a8 | ||
|
|
8198b8d5cb | ||
|
|
44ac3c365d | ||
|
|
c564fc3e90 | ||
|
|
bb006c1ba2 | ||
|
|
4b69bbaed0 | ||
|
|
f574d6e8af | ||
|
|
13664413c5 | ||
|
|
b6256283a6 | ||
|
|
f30613f094 | ||
|
|
a1fd169568 | ||
|
|
3c5f13f3f6 | ||
|
|
f4f92c57ee | ||
|
|
df34c72c97 | ||
|
|
517a2e5a2b | ||
|
|
eeac773c06 | ||
|
|
4f3966d8d8 | ||
|
|
409301b3d6 | ||
|
|
223bafaad7 | ||
|
|
603ce1eb1e |
Generated
+5
-5
@@ -476,7 +476,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mist-analyzer"
|
name = "mist-analyzer"
|
||||||
version = "0.3.8-alpha.0"
|
version = "0.4.0-alpha.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cargo_metadata",
|
"cargo_metadata",
|
||||||
"dashmap 6.1.0",
|
"dashmap 6.1.0",
|
||||||
@@ -495,7 +495,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mist-api"
|
name = "mist-api"
|
||||||
version = "0.3.8-alpha.0"
|
version = "0.4.0-alpha.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cargo_metadata",
|
"cargo_metadata",
|
||||||
"mist-codegen",
|
"mist-codegen",
|
||||||
@@ -507,7 +507,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mist-codegen"
|
name = "mist-codegen"
|
||||||
version = "0.3.8-alpha.0"
|
version = "0.4.0-alpha.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytecount",
|
"bytecount",
|
||||||
"mist-parser",
|
"mist-parser",
|
||||||
@@ -515,7 +515,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mist-lang"
|
name = "mist-lang"
|
||||||
version = "0.3.8-alpha.0"
|
version = "0.4.0-alpha.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"mist-analyzer",
|
"mist-analyzer",
|
||||||
"mist-api",
|
"mist-api",
|
||||||
@@ -524,7 +524,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mist-parser"
|
name = "mist-parser"
|
||||||
version = "0.3.8-alpha.0"
|
version = "0.4.0-alpha.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"pest",
|
"pest",
|
||||||
"pest_derive",
|
"pest_derive",
|
||||||
|
|||||||
+5
-5
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mist-lang"
|
name = "mist-lang"
|
||||||
version = "0.3.8-alpha.0"
|
version = "0.4.0-alpha.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"
|
||||||
@@ -33,10 +33,10 @@ exclude = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
mist-parser = { path = "crates/parser", version = "0.3.8-alpha.0" }
|
mist-parser = { path = "crates/parser", version = "0.4.0-alpha.0" }
|
||||||
mist-analyzer = { path = "crates/analyzer", version = "0.3.8-alpha.0" }
|
mist-analyzer = { path = "crates/analyzer", version = "0.4.0-alpha.0" }
|
||||||
mist-codegen = { path = "crates/codegen", version = "0.3.8-alpha.0" }
|
mist-codegen = { path = "crates/codegen", version = "0.4.0-alpha.0" }
|
||||||
mist-api = { path = "crates/mist-api", version = "0.3.8-alpha.0" }
|
mist-api = { path = "crates/mist-api", version = "0.4.0-alpha.0" }
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "mist"
|
name = "mist"
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mist-analyzer"
|
name = "mist-analyzer"
|
||||||
version = "0.3.8-alpha.0"
|
version = "0.4.0-alpha.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"
|
||||||
|
|||||||
+23
-14
@@ -26,7 +26,7 @@ use crate::transpiler::{
|
|||||||
|
|
||||||
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",
|
||||||
@@ -49,6 +49,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> {
|
||||||
@@ -1630,11 +1631,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),
|
||||||
@@ -1642,9 +1643,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),
|
||||||
@@ -1652,7 +1663,8 @@ 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),
|
||||||
@@ -1682,7 +1694,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),
|
||||||
@@ -1692,7 +1704,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),
|
||||||
@@ -1702,7 +1714,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),
|
||||||
@@ -2024,10 +2036,7 @@ fn compute_mod_decls(
|
|||||||
let source = rope.to_string();
|
let source = rope.to_string();
|
||||||
parse_module(&source).ok().flatten().map(|(_, name)| name.0)
|
parse_module(&source).ok().flatten().map(|(_, name)| name.0)
|
||||||
})
|
})
|
||||||
.or_else(|| {
|
.or_else(|| file.file_stem().and_then(|s| s.to_str().map(String::from)));
|
||||||
file.file_stem()
|
|
||||||
.and_then(|s| s.to_str().map(String::from))
|
|
||||||
});
|
|
||||||
if let Some(name) = mod_name {
|
if let Some(name) = mod_name {
|
||||||
mod_decl.push_str(&format!("pub mod {};\n", name));
|
mod_decl.push_str(&format!("pub mod {};\n", name));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mist-codegen"
|
name = "mist-codegen"
|
||||||
version = "0.3.8-alpha.0"
|
version = "0.4.0-alpha.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"
|
||||||
|
|||||||
@@ -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 {
|
||||||
@@ -51,7 +51,7 @@ impl ClassProcessedData {
|
|||||||
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -191,13 +191,13 @@ 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 std::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)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -352,8 +352,9 @@ impl ClassProcessedData {
|
|||||||
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(";");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -379,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(),
|
||||||
@@ -395,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);
|
||||||
}
|
}
|
||||||
@@ -616,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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ pub mod expr;
|
|||||||
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::*,
|
||||||
@@ -26,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,
|
||||||
}
|
}
|
||||||
@@ -38,6 +44,7 @@ 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(1, 0),
|
||||||
}
|
}
|
||||||
@@ -78,14 +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_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()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -275,6 +313,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())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -188,8 +188,19 @@ 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) {
|
||||||
|
if self.tuple_names.is_empty() {
|
||||||
self.name.gen_rust(ctx, cg);
|
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(")");
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.true_type {
|
||||||
cg.add(
|
cg.add(
|
||||||
&self
|
&self
|
||||||
.type_
|
.type_
|
||||||
@@ -197,5 +208,21 @@ impl GenRust for VarDecl {
|
|||||||
.map(|t| format!(": {}", t.get_rust()))
|
.map(|t| format!(": {}", t.get_rust()))
|
||||||
.unwrap_or_default(),
|
.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(),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -326,6 +326,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,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mist-api"
|
name = "mist-api"
|
||||||
version = "0.3.8-alpha.0"
|
version = "0.4.0-alpha.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"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mist-parser"
|
name = "mist-parser"
|
||||||
version = "0.3.8-alpha.0"
|
version = "0.4.0-alpha.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"
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ pub enum TypeExpr {
|
|||||||
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)]
|
||||||
|
|||||||
@@ -44,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)]
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ 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),
|
StaticDecl(VarDeclStmt),
|
||||||
@@ -104,12 +107,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>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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<'_> {
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ keyword = {
|
|||||||
| "override"
|
| "override"
|
||||||
| "const"
|
| "const"
|
||||||
| "type"
|
| "type"
|
||||||
|
| "virtual"
|
||||||
}
|
}
|
||||||
|
|
||||||
identifier = @{
|
identifier = @{
|
||||||
@@ -141,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 = {
|
||||||
@@ -225,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 = {
|
||||||
@@ -240,7 +248,7 @@ param = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
field_list = {
|
field_list = {
|
||||||
(field ~ ("," ~ field)* ~ ","?)?
|
(field ~ ";")*
|
||||||
}
|
}
|
||||||
|
|
||||||
param_list = {
|
param_list = {
|
||||||
@@ -251,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
|
||||||
}
|
}
|
||||||
@@ -264,7 +284,7 @@ declare_module = {
|
|||||||
// ======================================================
|
// ======================================================
|
||||||
|
|
||||||
struct_decl = {
|
struct_decl = {
|
||||||
visibility? ~ "struct" ~ identifier ~ generics_decl? ~ "{" ~ field_list? ~ "}"
|
visibility? ~ "struct" ~ identifier ~ generics_decl? ~ "{" ~ field_list ~ "}"
|
||||||
}
|
}
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
@@ -289,12 +309,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 ~ ";")* ~ "}"
|
||||||
}
|
}
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
@@ -358,10 +374,12 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
@@ -381,7 +399,7 @@ type_alias = { "type" ~ identifier ~ generics_decl? ~ "=" ~ type_expr ~ ";" }
|
|||||||
|
|
||||||
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 | const_decl | static_decl | type_alias))
|
| (attributes ~ (include_global | include_local | include_use_global | import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | declare_module | function_decl | const_decl | static_decl | type_alias))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
|
|||||||
@@ -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)?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,25 @@ 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)?,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -54,11 +54,7 @@ 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()))
|
|
||||||
.transpose()
|
|
||||||
.map(|v| v.unwrap_or_default())?,
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
Rule::class_decl => Ok(TopLevelKind::ClassDecl {
|
Rule::class_decl => Ok(TopLevelKind::ClassDecl {
|
||||||
@@ -133,6 +129,25 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for TopLevelKind {
|
|||||||
ty: inner.next().unwrap().try_into()?,
|
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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
Some(_) => None,
|
||||||
|
None => Some(Spanned {
|
||||||
item: field.item.decl.name.clone(),
|
item: field.item.decl.name.clone(),
|
||||||
line: field.line,
|
line: field.line,
|
||||||
column: field.column,
|
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(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
pub module decl_test;
|
pub module decl_test;
|
||||||
pub module decl_test;
|
|
||||||
|
|
||||||
pub void test_let_basic()
|
pub void test_let_basic()
|
||||||
{
|
{
|
||||||
@@ -7,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;
|
||||||
@@ -19,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;
|
||||||
@@ -77,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();
|
||||||
@@ -89,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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
pub module expr_test;
|
pub module expr_test;
|
||||||
pub module expr_test;
|
|
||||||
|
|
||||||
pub void test_literals()
|
pub void test_literals()
|
||||||
{
|
{
|
||||||
@@ -156,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();
|
||||||
@@ -175,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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
pub module package;
|
|
||||||
pub module common;
|
pub module common;
|
||||||
|
|
||||||
pub void test_package() {}
|
pub void test_package() {}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
pub module statement_test;
|
pub module statement_test;
|
||||||
pub module statement_test;
|
|
||||||
|
|
||||||
pub void test_var_decl()
|
pub void test_var_decl()
|
||||||
{
|
{
|
||||||
@@ -161,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();
|
||||||
@@ -177,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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
pub module types_test;
|
pub module types_test;
|
||||||
pub module types_test;
|
|
||||||
|
|
||||||
pub void test_integer_type() {
|
pub void test_integer_type() {
|
||||||
i32 x = 42;
|
i32 x = 42;
|
||||||
@@ -53,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();
|
||||||
@@ -63,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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
pub module attribute_test;
|
pub module attribute_test;
|
||||||
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()
|
||||||
@@ -23,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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
pub module class_test;
|
pub module class_test;
|
||||||
pub module class_test;
|
|
||||||
|
pub enum AnimalType {
|
||||||
|
Mammal;
|
||||||
|
}
|
||||||
|
|
||||||
pub class Animal
|
pub class Animal
|
||||||
{
|
{
|
||||||
@@ -10,7 +13,11 @@ 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()
|
||||||
}
|
}
|
||||||
@@ -32,6 +39,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);
|
||||||
@@ -60,10 +72,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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
pub module enum_test;
|
pub module enum_test;
|
||||||
pub module enum_test;
|
|
||||||
|
|
||||||
enum OptionInt
|
enum OptionInt
|
||||||
{
|
{
|
||||||
None,
|
None;
|
||||||
Some(i32),
|
Some(i32);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub void test_enum_named()
|
pub void test_enum_named()
|
||||||
@@ -35,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()
|
||||||
@@ -68,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()
|
||||||
@@ -86,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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
pub module function_test;
|
pub module function_test;
|
||||||
pub module function_test;
|
|
||||||
|
|
||||||
void greet()
|
void greet()
|
||||||
{
|
{
|
||||||
@@ -70,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();
|
||||||
@@ -80,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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
pub module impl_test;
|
pub module impl_test;
|
||||||
pub module impl_test;
|
|
||||||
|
|
||||||
struct Vector2
|
struct Vector2
|
||||||
{
|
{
|
||||||
i32 x,
|
i32 x;
|
||||||
i32 y,
|
i32 y;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Vector2
|
impl Vector2
|
||||||
@@ -73,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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
pub module struct_test;
|
pub module struct_test;
|
||||||
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()
|
||||||
@@ -40,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()
|
||||||
@@ -60,10 +59,55 @@ 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);
|
||||||
|
}
|
||||||
|
|
||||||
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,5 +1,4 @@
|
|||||||
pub module trait_test;
|
pub module trait_test;
|
||||||
pub module trait_test;
|
|
||||||
|
|
||||||
trait Speak
|
trait Speak
|
||||||
{
|
{
|
||||||
@@ -13,7 +12,7 @@ trait Greet : Speak
|
|||||||
|
|
||||||
struct Dog
|
struct Dog
|
||||||
{
|
{
|
||||||
String name,
|
String name;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Speak for Dog
|
impl Speak for Dog
|
||||||
|
|||||||
Reference in New Issue
Block a user