diff --git a/parser/src/ast.rs b/parser/src/ast.rs index fb72ea9..5a75b81 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -1,7 +1,7 @@ use serde::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)] pub struct ParamList(pub Vec); @@ -15,6 +15,12 @@ pub enum TypePostfix { RefMut, } +#[derive(Debug, Clone, Serialize)] +pub enum Visibility { + Public, + Private, +} + #[derive(Debug, Clone, Serialize)] pub enum Attribute { /// #[test] @@ -63,19 +69,36 @@ pub enum TopLevelKind { ModAttribute, Include(Path), StructDecl { - export: bool, + visibility: Visibility, name: String, fields: FieldList, }, - FunctionDecl { - export: bool, + FunctionDecl(FunctionDecl), + ClassDecl { + visibility: Visibility, name: String, - params: ParamList, - return_type: TypeExpr, - body: Block, + fields: Vec, + constructor: ClassConstructor, + methods: Vec, }, } +#[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)] pub enum Postfix { FieldAccess(String), @@ -91,6 +114,7 @@ pub enum Prefix { Ref, RefMut, Deref, + New, } #[derive(Debug, Clone, Serialize)] diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index 56e45ad..9bc04cc 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -19,8 +19,27 @@ var = { "var" } var_decl = { (var | type_expr) ~ mutable? ~ identifier } 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 = { (attributes ~ (import | function_decl | struct_decl | class_decl)) | mod_attribute } + attribute = { "#" ~ "[" ~ meta ~ "]" } @@ -39,8 +58,6 @@ meta_list = { attributes = { attribute* } -top_level = { (attributes ~ (import | function_decl | struct_decl)) | mod_attribute } - import = { sharp ~ "include" ~ "<" ~ static_path ~ ">" } field_list = { field ~ ("," ~ field)* ~ ","? } @@ -154,12 +171,16 @@ prefix = { deref_px | mut_ref_px | ref_px + | new_px } deref_px = { "*" } mut_ref_px = { "&" ~ mutable } ref_px = { "&" } +new_px = { "new" } + +prefix_list = { prefix* } expr = { - prefix* ~ primary ~ postfix* + prefix_list ~ primary ~ postfix* } diff --git a/parser/src/lib.rs b/parser/src/lib.rs index ce1dd56..d4e2c27 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -96,15 +96,10 @@ impl From> for FieldList { .into_inner() .map(|p| { let mut param_inner = p.into_inner(); - let export = if param_inner.peek().unwrap().as_rule() == Rule::export { - param_inner.next().unwrap(); - true - } else { - false - }; + let visibility = Visibility::from(&mut param_inner); let param_type = TypeExpr::from(param_inner.next().unwrap()); let param_name = param_inner.next().unwrap().as_str().to_string(); - (param_name, export, param_type) + (param_name, visibility, param_type) }) .collect(); @@ -210,67 +205,60 @@ impl From> for StatementBranch { } } +impl From> 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> for TopLevelKind { fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { let rule = pair.as_rule(); - let mut inner = pair.into_inner(); + let mut inner = pair.clone().into_inner(); match rule { Rule::import => TopLevelKind::Include(Path::from(inner.next().unwrap())), - Rule::function_decl => { - 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::function_decl => TopLevelKind::FunctionDecl(FunctionDecl::from(pair)), Rule::struct_decl => { - let export = if let Some(first) = inner.peek() { - if first.as_rule() == Rule::export { - inner.next(); - true - } else { - false - } - } else { - false - }; + let visibility = Visibility::from(&mut inner); let name = inner.next().unwrap().as_str().to_string(); let fields_pair = inner.next().unwrap(); let fields = FieldList::from(fields_pair); TopLevelKind::StructDecl { - export, + visibility, name, 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:#?}"), } } @@ -380,15 +368,10 @@ impl From> for Expression { match rule { Rule::expr => { - let mut prefixes = Vec::new(); - - while inner - .peek() - .map(|v| v.as_rule() == Rule::prefix) - .unwrap_or_default() - { - prefixes.push(Prefix::from(inner.next().unwrap())); - } + let prefixes: Vec = inner + .next() + .map(|p| p.into_inner().into_iter().map(Prefix::from).collect()) + .unwrap_or_default(); let exp = Expression::from(inner.next().unwrap()); @@ -404,18 +387,8 @@ impl From> for Expression { } Rule::primary => Expression::from(inner.next().unwrap()), Rule::static_path => Expression::Path(Path::from(pair)), - Rule::integer => { - Expression::Literal(Literal::Int(pair.as_str().parse::().unwrap())) - } - Rule::float => { - Expression::Literal(Literal::Float(pair.as_str().parse::().unwrap())) - } - Rule::boolean => { - Expression::Literal(Literal::Bool(pair.as_str().parse::().unwrap())) - } - Rule::string_lit => Expression::Literal(Literal::String(inner.as_str().to_string())), - Rule::tuple => { - Expression::Literal(Literal::Tuple(inner.map(Expression::from).collect())) + Rule::integer | Rule::float | Rule::boolean | Rule::string_lit | Rule::tuple => { + Expression::Literal(Literal::from(pair)) } _ => unimplemented!("{rule:#?}"), } @@ -429,6 +402,7 @@ impl From> for Prefix { Rule::deref_px => Self::Deref, Rule::mut_ref_px => Self::RefMut, Rule::ref_px => Self::Ref, + Rule::new_px => Self::New, _ => unimplemented!("{pair:#?}"), } } @@ -523,12 +497,8 @@ impl From> for VarDecl { Some(TypeExpr::from(pair)) } }); - let mutable = if inner.peek().unwrap().as_rule() == Rule::mutable { - inner.next(); - true - } else { - false - }; + let mutable = listen_rule(&mut inner, Rule::mutable); + let name = inner.next().unwrap().as_str().to_string(); VarDecl { @@ -542,3 +512,98 @@ impl From> for VarDecl { } } } + +impl From> 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> { + let consumed = pairs + .peek() + .map(|p| p.as_rule() == rule) + .unwrap_or_default(); + + if consumed { pairs.next() } else { None } +} diff --git a/src/codegen.rs b/src/codegen.rs index 3440a77..463e448 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -1,7 +1,7 @@ use parser::ast::{ - Attribute, BinaryOp, Block, Expression, Literal, Path, Postfix, Prefix, Statement, - StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, - VarDecl, VarDeclStmt, + Attribute, BinaryOp, Block, Expression, FunctionDecl, Literal, Path, Postfix, Prefix, + Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, + VarAssignStmt, VarDecl, VarDeclStmt, Visibility, }; // --------------------------------------------------------------------------- @@ -148,7 +148,12 @@ impl GetRust for Expression { initial, prefixes, 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::Ref => "&", Self::RefMut => "&mut ", + Self::New => "", } .to_string() } @@ -170,6 +176,23 @@ impl GetRust for [Prefix] { } } +impl GetRust for Option<&Vec> { + 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 { fn get_rust(&self) -> String { match self { @@ -287,14 +310,13 @@ impl ToRust for TopLevelKind { Self::Include(path) => { cg.addln(&format!("use {};", path.get_rust())); } - + Self::FunctionDecl(decl) => decl.to_rust(cg), Self::StructDecl { - export, + visibility, name, fields, } => { - let vis = if *export { "pub " } else { "" }; - cg.addln(&format!("{}struct {} {{", vis, name)); + cg.addln(&format!("{}struct {} {{", visibility.get_rust(), name)); cg.indent += 1; for (field_name, _, ty) in &fields.0 { @@ -305,32 +327,89 @@ impl ToRust for TopLevelKind { cg.indent -= 1; cg.addln("}\n"); } - - Self::FunctionDecl { - export, + Self::ClassDecl { + visibility, name, - params, - return_type, - body, + fields, + constructor, + 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 .iter() .map(VarDecl::get_rust) .collect::>() .join(", "); - cg.addln(&format!( - "{}fn {}({}) -> {} {{", - vis, - name, - params_str, - return_type.get_rust() + cg.add_indentedln(&format!( + "{}fn new({}) -> Self {{", + constructor.visibility.get_rust(), + params_str )); cg.indent += 1; - body.to_rust(cg); + + cg.add_indentedln("let mut this: Self = unsafe { std::mem::MaybeUninit::::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::>() + .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.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::>() + .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 { fn get_rust(&self) -> String { 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 { let rust_path = path.get_rust(); diff --git a/test/src/main.mist b/test/src/main.mist index fe1d604..28009dc 100644 --- a/test/src/main.mist +++ b/test/src/main.mist @@ -1,7 +1,16 @@ -void main() { - var iter = vec!(1, 2, 3, 4, 5); +class MyClass { + String message; - for (i in iter) { - println!("{i}"); + public constructor(str* name) { + self.message = format!("Hello {}!", name); + } + + public void greet(self*) { + println!("{}", self.message); } } + +void main() { + var my_class = new MyClass("World"); + my_class.greet(); +}