Type postix gen
This commit is contained in:
+8
-3
@@ -11,11 +11,16 @@ pub struct ParamList(pub Vec<VarDecl>);
|
|||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub struct Block(pub Vec<Statement>);
|
pub struct Block(pub Vec<Statement>);
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
pub enum TypePostfix {
|
||||||
|
Ref,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub enum TypeExpr {
|
pub enum TypeExpr {
|
||||||
Path(StaticPath),
|
Path(StaticPath, Vec<TypePostfix>),
|
||||||
PathParams(StaticPath, Vec<TypeExpr>),
|
PathParams(StaticPath, Vec<TypeExpr>, Vec<TypePostfix>),
|
||||||
Tuple(Vec<TypeExpr>),
|
Tuple(Vec<TypeExpr>, Vec<TypePostfix>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
|||||||
@@ -75,7 +75,8 @@ while_stmt = {
|
|||||||
// ================= EXPRESSIONS =================
|
// ================= EXPRESSIONS =================
|
||||||
tuple_type = { "(" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ")" }
|
tuple_type = { "(" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ")" }
|
||||||
path_type = { static_path ~ ("<" ~ (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 = {
|
primary = {
|
||||||
integer
|
integer
|
||||||
|
|||||||
+22
-6
@@ -1,6 +1,6 @@
|
|||||||
use parser::ast::{
|
use parser::ast::{
|
||||||
BinaryOp, Block, Expression, IfStmt, Postfix, Statement, StaticPath, TopLevel, TypeExpr,
|
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 {
|
impl GetRust for TypeExpr {
|
||||||
fn get_rust(&self) -> String {
|
fn get_rust(&self) -> String {
|
||||||
match self {
|
match self {
|
||||||
TypeExpr::Path(path) => get_static_type_path(path),
|
TypeExpr::Path(path, postix) => {
|
||||||
TypeExpr::PathParams(path, params) => {
|
get_type_postfixes(postix) + &get_static_type_path(path)
|
||||||
|
}
|
||||||
|
TypeExpr::PathParams(path, params, postfix) => {
|
||||||
format!(
|
format!(
|
||||||
"{}<{}>",
|
"{}{}<{}>",
|
||||||
|
get_type_postfixes(postix),
|
||||||
get_static_type_path(path),
|
get_static_type_path(path),
|
||||||
params
|
params
|
||||||
.iter()
|
.iter()
|
||||||
@@ -87,8 +90,9 @@ impl GetRust for TypeExpr {
|
|||||||
.join(", ")
|
.join(", ")
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
TypeExpr::Tuple(types) => format!(
|
TypeExpr::Tuple(types, postfix) => format!(
|
||||||
"({})",
|
"{}({})",
|
||||||
|
get_type_postfixes(postix),
|
||||||
types
|
types
|
||||||
.iter()
|
.iter()
|
||||||
.map(|t| t.get_rust())
|
.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 {
|
pub fn get_static_type_path(path: &StaticPath) -> String {
|
||||||
let rust_path = path.get_rust();
|
let rust_path = path.get_rust();
|
||||||
|
|
||||||
@@ -344,3 +356,7 @@ pub fn get_static_type_path(path: &StaticPath) -> String {
|
|||||||
rust_path
|
rust_path
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_type_postfixes(postfixes: &[TypePostfix]) -> String {
|
||||||
|
postfixes.iter().map(TypePostfix::get_rust).collect()
|
||||||
|
}
|
||||||
|
|||||||
+1
-3
@@ -1,3 +1 @@
|
|||||||
Result<void, String> main() {
|
i32 add(i32* a, i32* b) {}
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user