- C frontend broken for now
- If statements work properly now (hopefully) - still issues with while loops pushing vars to the stack. need scoping implemented to fix this! - refactored registers.rs and fixed faulty logic. - made register allocation optimisations
This commit is contained in:
+80
-8
@@ -9,9 +9,11 @@ pub enum CompilerError {
|
||||
Undefined(Name),
|
||||
InvalidSyntax(String),
|
||||
Generic(String),
|
||||
UnknownType,
|
||||
TypeMismatch(TypeId, TypeId),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
pub struct Name {
|
||||
pub name: String,
|
||||
pub namespace: Option<String>,
|
||||
@@ -46,7 +48,7 @@ pub struct Dependency {
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum TypeId {
|
||||
U8,
|
||||
U16,
|
||||
@@ -54,18 +56,45 @@ pub enum TypeId {
|
||||
I8,
|
||||
I16,
|
||||
I32,
|
||||
Bool,
|
||||
Char,
|
||||
Void,
|
||||
Ptr(Box<TypeId>),
|
||||
Ref(Box<TypeId>),
|
||||
Array(Box<TypeId>, usize),
|
||||
Struct { name: Name, fields: Vec<Variable> },
|
||||
Struct { name: Name, fields: Vec<TypeId> },
|
||||
}
|
||||
|
||||
impl fmt::Display for TypeId {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::U8 => write!(f, "u8"),
|
||||
Self::U16 => write!(f, "u16"),
|
||||
Self::U32 => write!(f, "u32"),
|
||||
Self::I8 => write!(f, "i8"),
|
||||
Self::I16 => write!(f, "i16"),
|
||||
Self::I32 => write!(f, "i32"),
|
||||
Self::Bool => write!(f, "bool"),
|
||||
Self::Char => write!(f, "char"),
|
||||
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, "}}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type Block = Vec<Statement>;
|
||||
|
||||
#[allow(unused)]
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Variable {
|
||||
pub name: String,
|
||||
pub type_id: TypeId,
|
||||
@@ -100,6 +129,7 @@ pub enum Statement {
|
||||
body: Vec<Statement>,
|
||||
},
|
||||
Loop(Block),
|
||||
Defer(Call),
|
||||
Break,
|
||||
Continue,
|
||||
Return(Option<Expression>),
|
||||
@@ -128,28 +158,47 @@ pub enum Expression {
|
||||
op: BinaryOperator,
|
||||
left: Box<Expression>,
|
||||
right: Box<Expression>,
|
||||
|
||||
// Post-Semantic Analysis
|
||||
type_id: Option<TypeId>,
|
||||
},
|
||||
Unary {
|
||||
op: UnaryOperator,
|
||||
operand: Box<Expression>,
|
||||
|
||||
// Post-Semantic Analysis
|
||||
type_id: Option<TypeId>,
|
||||
},
|
||||
Variable {
|
||||
name: Name,
|
||||
expr_type: Option<TypeId>,
|
||||
},
|
||||
Call {
|
||||
name: Name,
|
||||
args: Vec<Expression>,
|
||||
func: Call,
|
||||
|
||||
// Post-Semantic Analysis
|
||||
type_id: Option<TypeId>,
|
||||
},
|
||||
Number {
|
||||
value: isize,
|
||||
|
||||
// Post-Semantic Analysis
|
||||
type_id: Option<TypeId>,
|
||||
},
|
||||
Number(isize),
|
||||
StringLiteral(String),
|
||||
CharLiteral(char),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Call {
|
||||
pub name: Name,
|
||||
pub args: Vec<Expression>,
|
||||
}
|
||||
|
||||
impl Expression {
|
||||
pub fn is_pure(&self) -> bool {
|
||||
match self {
|
||||
Expression::Number(_) => true,
|
||||
Expression::Number { .. } => true,
|
||||
Expression::StringLiteral(_) => true,
|
||||
Expression::CharLiteral(_) => true,
|
||||
Expression::Call { .. } => false,
|
||||
@@ -159,6 +208,29 @@ impl Expression {
|
||||
Expression::Variable { .. } => true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn type_id(&self) -> Result<TypeId, CompilerError> {
|
||||
match self {
|
||||
Expression::Number { type_id, .. } => {
|
||||
type_id.clone().ok_or(CompilerError::UnknownType)
|
||||
}
|
||||
Expression::StringLiteral(_) => Ok(TypeId::Ptr(Box::new(TypeId::Char))),
|
||||
Expression::CharLiteral(_) => Ok(TypeId::Char),
|
||||
Expression::Call { type_id, .. } => {
|
||||
type_id.clone().ok_or(CompilerError::UnknownType)
|
||||
}
|
||||
Expression::Binary { type_id, .. } => {
|
||||
type_id.clone().ok_or(CompilerError::UnknownType)
|
||||
}
|
||||
Expression::Unary { type_id, .. } => {
|
||||
type_id.clone().ok_or(CompilerError::UnknownType)
|
||||
}
|
||||
Expression::Empty => Ok(TypeId::Void),
|
||||
Expression::Variable { expr_type, .. } => {
|
||||
expr_type.clone().ok_or(CompilerError::UnknownType)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
|
||||
Reference in New Issue
Block a user