From d263f1a41f085c5285b6dc8af1143f7be3ba71c0 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Mon, 4 May 2026 17:36:25 +0200 Subject: [PATCH] Type postix gen --- parser/src/ast.rs | 11 ++++++++--- parser/src/grammar.pest | 3 ++- src/codegen.rs | 28 ++++++++++++++++++++++------ test/src/main.mist | 4 +--- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 826ac95..90f4375 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -11,11 +11,16 @@ pub struct ParamList(pub Vec); #[derive(Debug, Clone, Serialize)] pub struct Block(pub Vec); +#[derive(Debug, Clone, Serialize)] +pub enum TypePostfix { + Ref, +} + #[derive(Debug, Clone, Serialize)] pub enum TypeExpr { - Path(StaticPath), - PathParams(StaticPath, Vec), - Tuple(Vec), + Path(StaticPath, Vec), + PathParams(StaticPath, Vec, Vec), + Tuple(Vec, Vec), } #[derive(Debug, Clone, Serialize)] diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index 2b1384b..035791b 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -75,7 +75,8 @@ while_stmt = { // ================= EXPRESSIONS ================= tuple_type = { "(" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ")" } path_type = { static_path ~ ("<" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ">")? } -type_expr = { path_type | tuple_type } +ref_type = { "*" } +type_expr = { (path_type | tuple_type) ~ ref_type* } primary = { integer diff --git a/src/codegen.rs b/src/codegen.rs index 1ede7e7..4e37482 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -1,6 +1,6 @@ use parser::ast::{ BinaryOp, Block, Expression, IfStmt, Postfix, Statement, StaticPath, TopLevel, TypeExpr, - VarAssignStmt, VarDecl, VarDeclStmt, WhileStmt, + TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, WhileStmt, }; // --------------------------------------------------------------------------- @@ -75,10 +75,13 @@ impl Default for RustCodegen { impl GetRust for TypeExpr { fn get_rust(&self) -> String { match self { - TypeExpr::Path(path) => get_static_type_path(path), - TypeExpr::PathParams(path, params) => { + TypeExpr::Path(path, postix) => { + get_type_postfixes(postix) + &get_static_type_path(path) + } + TypeExpr::PathParams(path, params, postfix) => { format!( - "{}<{}>", + "{}{}<{}>", + get_type_postfixes(postix), get_static_type_path(path), params .iter() @@ -87,8 +90,9 @@ impl GetRust for TypeExpr { .join(", ") ) } - TypeExpr::Tuple(types) => format!( - "({})", + TypeExpr::Tuple(types, postfix) => format!( + "{}({})", + get_type_postfixes(postix), types .iter() .map(|t| t.get_rust()) @@ -335,6 +339,14 @@ impl GetRust for StaticPath { } } +impl GetRust for TypePostfix { + fn get_rust(&self) -> String { + match self { + TypePostfix::Ref => format!("&"), + } + } +} + pub fn get_static_type_path(path: &StaticPath) -> String { let rust_path = path.get_rust(); @@ -344,3 +356,7 @@ pub fn get_static_type_path(path: &StaticPath) -> String { rust_path } } + +pub fn get_type_postfixes(postfixes: &[TypePostfix]) -> String { + postfixes.iter().map(TypePostfix::get_rust).collect() +} diff --git a/test/src/main.mist b/test/src/main.mist index b81268b..9f2489e 100644 --- a/test/src/main.mist +++ b/test/src/main.mist @@ -1,3 +1 @@ -Result main() { - -} \ No newline at end of file +i32 add(i32* a, i32* b) {} \ No newline at end of file