Merge pull request #22 from mist-go/classes-and-ast-refactor

Classes and ast refactor
This commit is contained in:
2026-05-05 22:53:52 +02:00
committed by GitHub
5 changed files with 346 additions and 114 deletions
+31 -7
View File
@@ -1,7 +1,7 @@
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct FieldList(pub Vec<(String, bool, TypeExpr)>); pub struct FieldList(pub Vec<(String, Visibility, TypeExpr)>);
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct ParamList(pub Vec<VarDecl>); pub struct ParamList(pub Vec<VarDecl>);
@@ -15,6 +15,12 @@ pub enum TypePostfix {
RefMut, RefMut,
} }
#[derive(Debug, Clone, Serialize)]
pub enum Visibility {
Public,
Private,
}
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub enum Attribute { pub enum Attribute {
/// #[test] /// #[test]
@@ -63,19 +69,36 @@ pub enum TopLevelKind {
ModAttribute, ModAttribute,
Include(Path), Include(Path),
StructDecl { StructDecl {
export: bool, visibility: Visibility,
name: String, name: String,
fields: FieldList, fields: FieldList,
}, },
FunctionDecl { FunctionDecl(FunctionDecl),
export: bool, ClassDecl {
visibility: Visibility,
name: String, name: String,
params: ParamList, fields: Vec<VarDeclStmt>,
return_type: TypeExpr, constructor: ClassConstructor,
body: Block, methods: Vec<FunctionDecl>,
}, },
} }
#[derive(Debug, Clone, Serialize)]
pub struct ClassConstructor {
pub visibility: Visibility,
pub params: ParamList,
pub body: Block,
}
#[derive(Debug, Clone, Serialize)]
pub struct FunctionDecl {
pub visibility: Visibility,
pub name: String,
pub params: ParamList,
pub return_type: TypeExpr,
pub body: Block,
}
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub enum Postfix { pub enum Postfix {
FieldAccess(String), FieldAccess(String),
@@ -91,6 +114,7 @@ pub enum Prefix {
Ref, Ref,
RefMut, RefMut,
Deref, Deref,
New,
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
+24 -3
View File
@@ -19,8 +19,27 @@ var = { "var" }
var_decl = { (var | type_expr) ~ mutable? ~ identifier } var_decl = { (var | type_expr) ~ mutable? ~ identifier }
semicolon = _{ ";" } semicolon = _{ ";" }
// ================= CLASS =================
class_constructor = {
export? ~ "constructor" ~ "(" ~ param_list? ~ ")" ~ block
}
class_fields = { var_decl_statement* }
self_param = { "self" ~ mutable? ~ deref_px? }
method = {
export? ~ type_expr ~ identifier ~ "(" ~ self_param? ~ param_list? ~ ")" ~ block
}
class_decl = {
"class" ~ identifier ~ "{" ~ class_fields ~ class_constructor ~ method* ~ "}"
}
// ================= TOP LEVEL ================= // ================= TOP LEVEL =================
top_level = { (attributes ~ (import | function_decl | struct_decl | class_decl)) | mod_attribute }
attribute = { attribute = {
"#" ~ "[" ~ meta ~ "]" "#" ~ "[" ~ meta ~ "]"
} }
@@ -39,8 +58,6 @@ meta_list = {
attributes = { attribute* } attributes = { attribute* }
top_level = { (attributes ~ (import | function_decl | struct_decl)) | mod_attribute }
import = { sharp ~ "include" ~ "<" ~ static_path ~ ">" } import = { sharp ~ "include" ~ "<" ~ static_path ~ ">" }
field_list = { field ~ ("," ~ field)* ~ ","? } field_list = { field ~ ("," ~ field)* ~ ","? }
@@ -154,12 +171,16 @@ prefix = {
deref_px deref_px
| mut_ref_px | mut_ref_px
| ref_px | ref_px
| new_px
} }
deref_px = { "*" } deref_px = { "*" }
mut_ref_px = { "&" ~ mutable } mut_ref_px = { "&" ~ mutable }
ref_px = { "&" } ref_px = { "&" }
new_px = { "new" }
prefix_list = { prefix* }
expr = { expr = {
prefix* ~ primary ~ postfix* prefix_list ~ primary ~ postfix*
} }
+142 -77
View File
@@ -96,15 +96,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for FieldList {
.into_inner() .into_inner()
.map(|p| { .map(|p| {
let mut param_inner = p.into_inner(); let mut param_inner = p.into_inner();
let export = if param_inner.peek().unwrap().as_rule() == Rule::export { let visibility = Visibility::from(&mut param_inner);
param_inner.next().unwrap();
true
} else {
false
};
let param_type = TypeExpr::from(param_inner.next().unwrap()); let param_type = TypeExpr::from(param_inner.next().unwrap());
let param_name = param_inner.next().unwrap().as_str().to_string(); let param_name = param_inner.next().unwrap().as_str().to_string();
(param_name, export, param_type) (param_name, visibility, param_type)
}) })
.collect(); .collect();
@@ -210,67 +205,60 @@ impl From<pest::iterators::Pair<'_, Rule>> for StatementBranch {
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for ClassConstructor {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
let mut inner = pair.into_inner();
let visibility = Visibility::from(&mut inner);
let params = consume_rule(&mut inner, Rule::param_list)
.map(ParamList::from)
.unwrap_or_else(|| ParamList(Vec::new()));
Self {
visibility,
params,
body: Block::from(inner.next().unwrap()),
}
}
}
impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind { impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
let rule = pair.as_rule(); let rule = pair.as_rule();
let mut inner = pair.into_inner(); let mut inner = pair.clone().into_inner();
match rule { match rule {
Rule::import => TopLevelKind::Include(Path::from(inner.next().unwrap())), Rule::import => TopLevelKind::Include(Path::from(inner.next().unwrap())),
Rule::function_decl => { Rule::function_decl => TopLevelKind::FunctionDecl(FunctionDecl::from(pair)),
let export = if let Some(first) = inner.peek() {
if first.as_rule() == Rule::export {
inner.next();
true
} else {
false
}
} else {
false
};
let return_type = TypeExpr::from(inner.next().unwrap());
let name = inner.next().unwrap().as_str().to_string();
let params = if inner.peek().unwrap().as_rule() == Rule::param_list {
ParamList::from(inner.next().unwrap())
} else {
ParamList(Vec::new())
};
let body = Block::from(inner.next().unwrap());
TopLevelKind::FunctionDecl {
export,
name,
params,
return_type,
body,
}
}
Rule::struct_decl => { Rule::struct_decl => {
let export = if let Some(first) = inner.peek() { let visibility = Visibility::from(&mut inner);
if first.as_rule() == Rule::export {
inner.next();
true
} else {
false
}
} else {
false
};
let name = inner.next().unwrap().as_str().to_string(); let name = inner.next().unwrap().as_str().to_string();
let fields_pair = inner.next().unwrap(); let fields_pair = inner.next().unwrap();
let fields = FieldList::from(fields_pair); let fields = FieldList::from(fields_pair);
TopLevelKind::StructDecl { TopLevelKind::StructDecl {
export, visibility,
name, name,
fields, fields,
} }
} }
Rule::class_decl => TopLevelKind::ClassDecl {
visibility: Visibility::from(&mut inner),
name: inner.next().unwrap().as_str().to_string(),
fields: inner
.next()
.unwrap()
.into_inner()
.map(VarDeclStmt::from)
.collect(),
constructor: ClassConstructor::from(inner.next().unwrap()),
methods: inner.into_iter().map(FunctionDecl::from).collect(),
},
_ => unimplemented!("{rule:#?}"), _ => unimplemented!("{rule:#?}"),
} }
} }
@@ -380,15 +368,10 @@ impl From<pest::iterators::Pair<'_, Rule>> for Expression {
match rule { match rule {
Rule::expr => { Rule::expr => {
let mut prefixes = Vec::new(); let prefixes: Vec<Prefix> = inner
.next()
while inner .map(|p| p.into_inner().into_iter().map(Prefix::from).collect())
.peek() .unwrap_or_default();
.map(|v| v.as_rule() == Rule::prefix)
.unwrap_or_default()
{
prefixes.push(Prefix::from(inner.next().unwrap()));
}
let exp = Expression::from(inner.next().unwrap()); let exp = Expression::from(inner.next().unwrap());
@@ -404,18 +387,8 @@ impl From<pest::iterators::Pair<'_, Rule>> for Expression {
} }
Rule::primary => Expression::from(inner.next().unwrap()), Rule::primary => Expression::from(inner.next().unwrap()),
Rule::static_path => Expression::Path(Path::from(pair)), Rule::static_path => Expression::Path(Path::from(pair)),
Rule::integer => { Rule::integer | Rule::float | Rule::boolean | Rule::string_lit | Rule::tuple => {
Expression::Literal(Literal::Int(pair.as_str().parse::<i64>().unwrap())) Expression::Literal(Literal::from(pair))
}
Rule::float => {
Expression::Literal(Literal::Float(pair.as_str().parse::<f64>().unwrap()))
}
Rule::boolean => {
Expression::Literal(Literal::Bool(pair.as_str().parse::<bool>().unwrap()))
}
Rule::string_lit => Expression::Literal(Literal::String(inner.as_str().to_string())),
Rule::tuple => {
Expression::Literal(Literal::Tuple(inner.map(Expression::from).collect()))
} }
_ => unimplemented!("{rule:#?}"), _ => unimplemented!("{rule:#?}"),
} }
@@ -429,6 +402,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for Prefix {
Rule::deref_px => Self::Deref, Rule::deref_px => Self::Deref,
Rule::mut_ref_px => Self::RefMut, Rule::mut_ref_px => Self::RefMut,
Rule::ref_px => Self::Ref, Rule::ref_px => Self::Ref,
Rule::new_px => Self::New,
_ => unimplemented!("{pair:#?}"), _ => unimplemented!("{pair:#?}"),
} }
} }
@@ -523,12 +497,8 @@ impl From<pest::iterators::Pair<'_, Rule>> for VarDecl {
Some(TypeExpr::from(pair)) Some(TypeExpr::from(pair))
} }
}); });
let mutable = if inner.peek().unwrap().as_rule() == Rule::mutable { let mutable = listen_rule(&mut inner, Rule::mutable);
inner.next();
true
} else {
false
};
let name = inner.next().unwrap().as_str().to_string(); let name = inner.next().unwrap().as_str().to_string();
VarDecl { VarDecl {
@@ -542,3 +512,98 @@ impl From<pest::iterators::Pair<'_, Rule>> for VarDecl {
} }
} }
} }
impl From<pest::iterators::Pair<'_, Rule>> for FunctionDecl {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
let mut inner = pair.into_inner();
let visibility = Visibility::from(&mut inner);
let return_type = TypeExpr::from(inner.next().unwrap());
let name = inner.next().unwrap().as_str().to_string();
let self_param = consume_rule(&mut inner, Rule::self_param).map(|param| {
let mut param_inner = param.into_inner();
let name = format!("self");
let mutable = listen_rule(&mut param_inner, Rule::mutable);
let is_ref = listen_rule(&mut param_inner, Rule::deref_px);
VarDecl {
mutable: mutable && !is_ref,
name,
type_: Some(TypeExpr(
TypeExprKind::Path(Path(vec![format!("Self")])),
if is_ref {
vec![if mutable {
TypePostfix::RefMut
} else {
TypePostfix::Ref
}]
} else {
Vec::new()
},
)),
}
});
let params = consume_rule(&mut inner, Rule::param_list)
.map({
let self_param = self_param.clone();
|params_pair| {
let mut params = ParamList::from(params_pair);
if let Some(x) = self_param {
params.0.insert(0, x);
}
params
}
})
.unwrap_or_else(|| ParamList(self_param.into_iter().collect()));
let body = Block::from(inner.next().unwrap());
Self {
visibility,
name,
params,
return_type,
body,
}
}
}
impl From<&mut pest::iterators::Pairs<'_, Rule>> for Visibility {
fn from(pairs: &mut pest::iterators::Pairs<'_, Rule>) -> Self {
if listen_rule(pairs, Rule::export) {
Visibility::Public
} else {
Visibility::Private
}
}
}
pub fn listen_rule(pairs: &mut pest::iterators::Pairs<'_, Rule>, rule: Rule) -> bool {
let consumed = pairs
.peek()
.map(|p| p.as_rule() == rule)
.unwrap_or_default();
if consumed {
pairs.next();
}
consumed
}
pub fn consume_rule<'a>(
pairs: &mut pest::iterators::Pairs<'a, Rule>,
rule: Rule,
) -> Option<pest::iterators::Pair<'a, Rule>> {
let consumed = pairs
.peek()
.map(|p| p.as_rule() == rule)
.unwrap_or_default();
if consumed { pairs.next() } else { None }
}
+136 -23
View File
@@ -1,7 +1,7 @@
use parser::ast::{ use parser::ast::{
Attribute, BinaryOp, Block, Expression, Literal, Path, Postfix, Prefix, Statement, Attribute, BinaryOp, Block, Expression, FunctionDecl, Literal, Path, Postfix, Prefix,
StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix,
VarDecl, VarDeclStmt, VarAssignStmt, VarDecl, VarDeclStmt, Visibility,
}; };
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -148,7 +148,12 @@ impl GetRust for Expression {
initial, initial,
prefixes, prefixes,
postfixes, postfixes,
} => prefixes.get_rust() + &initial.get_rust() + &postfixes.get_rust(), } => {
prefixes.get_rust()
+ &initial.get_rust()
+ &Some(prefixes).get_rust()
+ &postfixes.get_rust()
}
} }
} }
} }
@@ -159,6 +164,7 @@ impl GetRust for Prefix {
Self::Deref => "*", Self::Deref => "*",
Self::Ref => "&", Self::Ref => "&",
Self::RefMut => "&mut ", Self::RefMut => "&mut ",
Self::New => "",
} }
.to_string() .to_string()
} }
@@ -170,6 +176,23 @@ impl GetRust for [Prefix] {
} }
} }
impl GetRust for Option<&Vec<Prefix>> {
fn get_rust(&self) -> String {
self.map(|prefixes| {
prefixes
.iter()
.last()
.map(|p| match p {
Prefix::New => "::new",
_ => "",
})
.unwrap_or_default()
.to_string()
})
.unwrap_or_default()
}
}
impl GetRust for Postfix { impl GetRust for Postfix {
fn get_rust(&self) -> String { fn get_rust(&self) -> String {
match self { match self {
@@ -287,14 +310,13 @@ impl ToRust for TopLevelKind {
Self::Include(path) => { Self::Include(path) => {
cg.addln(&format!("use {};", path.get_rust())); cg.addln(&format!("use {};", path.get_rust()));
} }
Self::FunctionDecl(decl) => decl.to_rust(cg),
Self::StructDecl { Self::StructDecl {
export, visibility,
name, name,
fields, fields,
} => { } => {
let vis = if *export { "pub " } else { "" }; cg.addln(&format!("{}struct {} {{", visibility.get_rust(), name));
cg.addln(&format!("{}struct {} {{", vis, name));
cg.indent += 1; cg.indent += 1;
for (field_name, _, ty) in &fields.0 { for (field_name, _, ty) in &fields.0 {
@@ -305,32 +327,89 @@ impl ToRust for TopLevelKind {
cg.indent -= 1; cg.indent -= 1;
cg.addln("}\n"); cg.addln("}\n");
} }
Self::ClassDecl {
Self::FunctionDecl { visibility,
export,
name, name,
params, fields,
return_type, constructor,
body, methods,
} => { } => {
let vis = if *export { "pub " } else { "" }; // Struct decl
cg.addln(&format!("{}struct {} {{", visibility.get_rust(), name));
cg.indent += 1;
let params_str = params for field in fields {
let ty = field.decl.type_.clone().unwrap().get_rust();
cg.add_indentedln(&format!("pub {}: {},", field.decl.name, ty));
}
cg.indent -= 1;
cg.addln("}\n");
// Constructor
cg.addln(&format!("impl {} {{", name));
cg.indent += 1;
let params_str = constructor
.params
.0 .0
.iter() .iter()
.map(VarDecl::get_rust) .map(VarDecl::get_rust)
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", "); .join(", ");
cg.addln(&format!( cg.add_indentedln(&format!(
"{}fn {}({}) -> {} {{", "{}fn new({}) -> Self {{",
vis, constructor.visibility.get_rust(),
name, params_str
params_str,
return_type.get_rust()
)); ));
cg.indent += 1; cg.indent += 1;
body.to_rust(cg);
cg.add_indentedln("let mut this: Self = unsafe { std::mem::MaybeUninit::<Self>::zeroed().assume_init() };");
for field in fields {
if let Some(init) = &field.init {
cg.add_indentedln(&format!(
"this.{} = {};",
field.decl.name,
init.get_rust()
));
}
}
cg.add_indentedln(&format!(
"this.construct_class({});",
constructor
.params
.0
.iter()
.map(|e| e.name.to_string())
.collect::<Vec<_>>()
.join(", ")
));
cg.add_indentedln("this");
cg.indent -= 1;
cg.add_indentedln("}\n");
// Constructor function
cg.add_indentedln(&format!(
"{}fn construct_class(&mut self, {}) {{",
constructor.visibility.get_rust(),
params_str
));
cg.indent += 1;
constructor.body.to_rust(cg);
cg.indent -= 1;
cg.add_indentedln("}\n");
for method in methods {
method.to_rust(cg);
}
cg.indent -= 1; cg.indent -= 1;
cg.addln("}\n"); cg.addln("}\n");
} }
@@ -437,6 +516,30 @@ impl ToRust for Statement {
} }
} }
impl ToRust for FunctionDecl {
fn to_rust(&self, cg: &mut RustCodegen) {
let params_str = self
.params
.0
.iter()
.map(VarDecl::get_rust)
.collect::<Vec<_>>()
.join(", ");
cg.add_indentedln(&format!(
"{}fn {}({}) -> {} {{",
self.visibility.get_rust(),
self.name,
params_str,
self.return_type.get_rust()
));
cg.indent += 1;
self.body.to_rust(cg);
cg.indent -= 1;
cg.add_indentedln("}\n");
}
}
impl GetRust for VarDecl { impl GetRust for VarDecl {
fn get_rust(&self) -> String { fn get_rust(&self) -> String {
let mutability = if self.mutable { "mut " } else { "" }; let mutability = if self.mutable { "mut " } else { "" };
@@ -466,6 +569,16 @@ impl GetRust for TypePostfix {
} }
} }
impl GetRust for Visibility {
fn get_rust(&self) -> String {
match self {
Visibility::Public => "pub ",
Visibility::Private => "",
}
.to_string()
}
}
pub fn get_static_type_path(path: &Path) -> String { pub fn get_static_type_path(path: &Path) -> String {
let rust_path = path.get_rust(); let rust_path = path.get_rust();
+13 -4
View File
@@ -1,7 +1,16 @@
void main() { class MyClass {
var iter = vec!(1, 2, 3, 4, 5); String message;
for (i in iter) { public constructor(str* name) {
println!("{i}"); self.message = format!("Hello {}!", name);
}
public void greet(self*) {
println!("{}", self.message);
} }
} }
void main() {
var my_class = new MyClass("World");
my_class.greet();
}