- renamed assembler_runner to just assembler
- implemented type parsing including custom types and generics (useless for now as we do no semantic analysis) - implemented struct literal parsing - implemented struct definition parsing (no generics yet) - implemented tuple parsing - registers are now allocated starting from zero - updated to-dos
This commit is contained in:
+86
-12
@@ -40,6 +40,10 @@ pub enum Declaration {
|
||||
is_const: bool,
|
||||
},
|
||||
Dependency(Dependency),
|
||||
Struct {
|
||||
name: Name,
|
||||
fields: Vec<Variable>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -62,8 +66,20 @@ pub enum TypeId {
|
||||
Void,
|
||||
Ptr(Box<TypeId>),
|
||||
Ref(Box<TypeId>),
|
||||
Array(Box<TypeId>, usize),
|
||||
Struct { name: Name, fields: Vec<TypeId> },
|
||||
Tuple(Vec<TypeId>),
|
||||
Array {
|
||||
r#type: Box<TypeId>,
|
||||
size: usize,
|
||||
},
|
||||
UnknownCustom {
|
||||
name: Name,
|
||||
generics: Vec<TypeId>,
|
||||
},
|
||||
Struct {
|
||||
name: Name,
|
||||
fields: Vec<TypeId>,
|
||||
generics: Vec<TypeId>,
|
||||
},
|
||||
}
|
||||
|
||||
impl TypeId {
|
||||
@@ -80,7 +96,10 @@ impl TypeId {
|
||||
Self::Void => 0,
|
||||
Self::Ptr(t) => t.size(),
|
||||
Self::Ref(t) => t.size(),
|
||||
Self::Array(t, size) => t.size() * size,
|
||||
Self::Tuple(types) => types.iter().map(|t| t.size()).sum(),
|
||||
Self::Array { r#type, size } => r#type.size() * size,
|
||||
Self::UnknownCustom { .. } => 1, /* TODO: calculate type size during */
|
||||
// semantic analysis
|
||||
Self::Struct { fields, .. } => fields.iter().map(|t| t.size()).sum(),
|
||||
}
|
||||
}
|
||||
@@ -100,14 +119,47 @@ impl fmt::Display for TypeId {
|
||||
Self::Void => write!(f, "void"),
|
||||
Self::Ptr(t) => write!(f, "*{}", t),
|
||||
Self::Ref(t) => write!(f, "&{}", t),
|
||||
Self::Array(t, len) => write!(f, "[{}; {}]", t, len),
|
||||
Self::Struct { name, fields } => {
|
||||
write!(f, "struct {} {{", name)?;
|
||||
for (i, field) in fields.iter().enumerate() {
|
||||
write!(f, "{}: {}", i, field)?;
|
||||
}
|
||||
write!(f, "}}")
|
||||
Self::Tuple(elems) => write!(
|
||||
f,
|
||||
"({})",
|
||||
elems
|
||||
.iter()
|
||||
.map(|t| t.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
),
|
||||
Self::Array { r#type, size } => write!(f, "[{}; {}]", r#type, size),
|
||||
Self::UnknownCustom { name, generics } => {
|
||||
write!(
|
||||
f,
|
||||
"{}<{}>",
|
||||
name,
|
||||
generics
|
||||
.iter()
|
||||
.map(|t| t.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
)
|
||||
}
|
||||
Self::Struct {
|
||||
name,
|
||||
fields,
|
||||
generics,
|
||||
} => write!(
|
||||
f,
|
||||
"struct<{}> {} {{{}}}",
|
||||
generics
|
||||
.iter()
|
||||
.map(|t| t.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(", "),
|
||||
name,
|
||||
fields
|
||||
.iter()
|
||||
.map(|t| t.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -241,6 +293,11 @@ pub enum Expression {
|
||||
elements: Vec<Expression>,
|
||||
type_id: Option<TypeId>,
|
||||
},
|
||||
StructLiteral {
|
||||
name: Name,
|
||||
fields: Vec<(Name, Expression)>,
|
||||
type_id: Option<TypeId>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -266,9 +323,12 @@ impl Expression {
|
||||
expr.is_pure() && index.is_pure()
|
||||
}
|
||||
Expression::MemberAccess { expr, .. } => expr.is_pure(),
|
||||
Expression::ArrayLiteral { elements, type_id } => {
|
||||
Expression::ArrayLiteral { elements, .. } => {
|
||||
elements.iter().all(|element| element.is_pure())
|
||||
}
|
||||
Expression::StructLiteral { fields, .. } => {
|
||||
fields.iter().all(|(_, expr)| expr.is_pure())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -304,7 +364,21 @@ impl Expression {
|
||||
let element_type = elements
|
||||
.first()
|
||||
.map_or(TypeId::Void, |e| e.type_id().unwrap_or(TypeId::Void));
|
||||
Ok(TypeId::Array(Box::new(element_type), elements.len()))
|
||||
Ok(TypeId::Array {
|
||||
r#type: Box::new(element_type),
|
||||
size: elements.len(),
|
||||
})
|
||||
}
|
||||
Expression::StructLiteral { name, fields, .. } => {
|
||||
let fields = fields
|
||||
.iter()
|
||||
.map(|(_, expr)| expr.type_id())
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
Ok(TypeId::Struct {
|
||||
name: name.clone(),
|
||||
fields,
|
||||
generics: Vec::new(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user