Compare commits

..

No commits in common. "257496aae2233769a2031f809c71478aa603048e" and "b84672ef8c40dc3cf3487ed70d650750e1c088a8" have entirely different histories.

10 changed files with 28 additions and 52 deletions

View File

@ -1,6 +1,6 @@
// Main // Main
fn main() -> bool { fn main() -> bool {
return (5 == fibonacci(5)) && false; return (5 == fibonacci(5)) && (2 == fibonacci(3));
} }
// Fibonacci // Fibonacci

View File

@ -17,7 +17,7 @@ fn main() {
// If N < 3 // If N < 3
Box::new(Expression( Box::new(Expression(
ExprKind::BinOp( ExprKind::BinOp(
BinaryOperator::Cmp(CmpOperator::GT), BinaryOperator::Logic(LogicOperator::GT),
Box::new(Expression( Box::new(Expression(
ExprKind::Variable(VariableReference( ExprKind::Variable(VariableReference(
TypeKind::I32, TypeKind::I32,

View File

@ -24,7 +24,6 @@ pub enum TypeKind {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Literal { pub enum Literal {
Number(u64), Number(u64),
Bool(bool),
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View File

@ -71,14 +71,6 @@ impl Parse for PrimaryExpression {
Kind::Literal(Literal::Number(v.parse().unwrap())), Kind::Literal(Literal::Number(v.parse().unwrap())),
stream.get_range().unwrap(), stream.get_range().unwrap(),
), ),
Token::True => Expression(
Kind::Literal(Literal::Bool(true)),
stream.get_range().unwrap(),
),
Token::False => Expression(
Kind::Literal(Literal::Bool(false)),
stream.get_range().unwrap(),
),
Token::ParenOpen => { Token::ParenOpen => {
let exp = stream.parse()?; let exp = stream.parse()?;
stream.expect(Token::ParenClose)?; stream.expect(Token::ParenClose)?;

View File

@ -150,12 +150,12 @@ impl ast::BinaryOperator {
ast::BinaryOperator::Minus => mir::BinaryOperator::Minus, ast::BinaryOperator::Minus => mir::BinaryOperator::Minus,
ast::BinaryOperator::Mult => mir::BinaryOperator::Mult, ast::BinaryOperator::Mult => mir::BinaryOperator::Mult,
ast::BinaryOperator::And => mir::BinaryOperator::And, ast::BinaryOperator::And => mir::BinaryOperator::And,
ast::BinaryOperator::LT => mir::BinaryOperator::Cmp(mir::CmpOperator::LT), ast::BinaryOperator::LT => mir::BinaryOperator::Logic(mir::LogicOperator::LT),
ast::BinaryOperator::LE => mir::BinaryOperator::Cmp(mir::CmpOperator::LE), ast::BinaryOperator::LE => mir::BinaryOperator::Logic(mir::LogicOperator::LE),
ast::BinaryOperator::GT => mir::BinaryOperator::Cmp(mir::CmpOperator::GT), ast::BinaryOperator::GT => mir::BinaryOperator::Logic(mir::LogicOperator::GT),
ast::BinaryOperator::GE => mir::BinaryOperator::Cmp(mir::CmpOperator::GE), ast::BinaryOperator::GE => mir::BinaryOperator::Logic(mir::LogicOperator::GE),
ast::BinaryOperator::EQ => mir::BinaryOperator::Cmp(mir::CmpOperator::EQ), ast::BinaryOperator::EQ => mir::BinaryOperator::Logic(mir::LogicOperator::EQ),
ast::BinaryOperator::NE => mir::BinaryOperator::Cmp(mir::CmpOperator::NE), ast::BinaryOperator::NE => mir::BinaryOperator::Logic(mir::LogicOperator::NE),
} }
} }
} }
@ -164,7 +164,6 @@ impl ast::Literal {
fn mir(&self) -> mir::Literal { fn mir(&self) -> mir::Literal {
match *self { match *self {
ast::Literal::Number(v) => mir::Literal::Vague(mir::VagueLiteral::Number(v)), ast::Literal::Number(v) => mir::Literal::Vague(mir::VagueLiteral::Number(v)),
ast::Literal::Bool(v) => mir::Literal::Bool(v),
} }
} }
} }

View File

@ -246,7 +246,7 @@ impl mir::Expression {
mir::BinaryOperator::And => { mir::BinaryOperator::And => {
scope.block.build(InstructionKind::And(lhs, rhs)).unwrap() scope.block.build(InstructionKind::And(lhs, rhs)).unwrap()
} }
mir::BinaryOperator::Cmp(l) => scope mir::BinaryOperator::Logic(l) => scope
.block .block
.build(InstructionKind::ICmp(l.int_predicate(), lhs, rhs)) .build(InstructionKind::ICmp(l.int_predicate(), lhs, rhs))
.unwrap(), .unwrap(),
@ -290,15 +290,15 @@ impl mir::Expression {
} }
} }
impl mir::CmpOperator { impl mir::LogicOperator {
fn int_predicate(&self) -> CmpPredicate { fn int_predicate(&self) -> CmpPredicate {
match self { match self {
mir::CmpOperator::LT => CmpPredicate::LT, mir::LogicOperator::LT => CmpPredicate::LT,
mir::CmpOperator::GT => CmpPredicate::GT, mir::LogicOperator::GT => CmpPredicate::GT,
mir::CmpOperator::LE => CmpPredicate::LE, mir::LogicOperator::LE => CmpPredicate::LE,
mir::CmpOperator::GE => CmpPredicate::GE, mir::LogicOperator::GE => CmpPredicate::GE,
mir::CmpOperator::EQ => CmpPredicate::EQ, mir::LogicOperator::EQ => CmpPredicate::EQ,
mir::CmpOperator::NE => CmpPredicate::NE, mir::LogicOperator::NE => CmpPredicate::NE,
} }
} }
} }
@ -341,7 +341,6 @@ impl mir::Literal {
mir::Literal::U32(val) => ConstValue::U32(val), mir::Literal::U32(val) => ConstValue::U32(val),
mir::Literal::U64(val) => ConstValue::U64(val), mir::Literal::U64(val) => ConstValue::U64(val),
mir::Literal::U128(val) => ConstValue::U128(val), mir::Literal::U128(val) => ConstValue::U128(val),
mir::Literal::Bool(val) => ConstValue::Bool(val),
mir::Literal::Vague(_) => panic!("Got vague literal!"), mir::Literal::Vague(_) => panic!("Got vague literal!"),
}) })
} }

View File

@ -22,10 +22,6 @@ pub enum Token {
Arrow, Arrow,
/// `if` /// `if`
If, If,
/// `true`
True,
/// `false`
False,
// Symbols // Symbols
/// `;` /// `;`
@ -169,8 +165,6 @@ pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
"return" => Token::ReturnKeyword, "return" => Token::ReturnKeyword,
"fn" => Token::FnKeyword, "fn" => Token::FnKeyword,
"if" => Token::If, "if" => Token::If,
"true" => Token::True,
"false" => Token::False,
_ => Token::Identifier(value), _ => Token::Identifier(value),
}; };
variant variant

View File

@ -166,7 +166,6 @@ impl Display for Literal {
Self::U32(val) => write!(f, "{}u32", val), Self::U32(val) => write!(f, "{}u32", val),
Self::U64(val) => write!(f, "{}u64", val), Self::U64(val) => write!(f, "{}u64", val),
Self::U128(val) => write!(f, "{}u128", val), Self::U128(val) => write!(f, "{}u128", val),
Self::Bool(val) => write!(f, "{}", val),
Self::Vague(val) => val.fmt(f), Self::Vague(val) => val.fmt(f),
} }
} }
@ -179,20 +178,20 @@ impl Display for BinaryOperator {
BinaryOperator::Minus => write!(f, "-"), BinaryOperator::Minus => write!(f, "-"),
BinaryOperator::Mult => write!(f, "*"), BinaryOperator::Mult => write!(f, "*"),
BinaryOperator::And => write!(f, "&&"), BinaryOperator::And => write!(f, "&&"),
BinaryOperator::Cmp(op) => Display::fmt(op, f), BinaryOperator::Logic(op) => Display::fmt(op, f),
} }
} }
} }
impl Display for CmpOperator { impl Display for LogicOperator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
CmpOperator::LT => write!(f, "<"), LogicOperator::LT => write!(f, "<"),
CmpOperator::LE => write!(f, "<="), LogicOperator::LE => write!(f, "<="),
CmpOperator::GT => write!(f, ">"), LogicOperator::GT => write!(f, ">"),
CmpOperator::GE => write!(f, ">="), LogicOperator::GE => write!(f, ">="),
CmpOperator::EQ => write!(f, "=="), LogicOperator::EQ => write!(f, "=="),
CmpOperator::NE => write!(f, "!="), LogicOperator::NE => write!(f, "!="),
} }
} }
} }

View File

@ -128,7 +128,6 @@ pub enum Literal {
U32(u32), U32(u32),
U64(u64), U64(u64),
U128(u128), U128(u128),
Bool(bool),
Vague(VagueLiteral), Vague(VagueLiteral),
} }
@ -150,7 +149,6 @@ impl Literal {
Literal::U32(_) => TypeKind::U32, Literal::U32(_) => TypeKind::U32,
Literal::U64(_) => TypeKind::U64, Literal::U64(_) => TypeKind::U64,
Literal::U128(_) => TypeKind::U128, Literal::U128(_) => TypeKind::U128,
Literal::Bool(_) => TypeKind::Bool,
Literal::Vague(VagueLiteral::Number(_)) => TypeKind::Vague(VagueType::Number), Literal::Vague(VagueLiteral::Number(_)) => TypeKind::Vague(VagueType::Number),
} }
} }
@ -162,12 +160,11 @@ pub enum BinaryOperator {
Minus, Minus,
Mult, Mult,
And, And,
Cmp(CmpOperator), Logic(LogicOperator),
} }
/// Specifically the operators that LLVM likes to take in as "icmp" parameters
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum CmpOperator { pub enum LogicOperator {
LT, LT,
LE, LE,
GT, GT,

View File

@ -15,8 +15,6 @@ pub enum ErrorKind {
TypeIsVague(VagueType), TypeIsVague(VagueType),
#[error("Can not coerce {0} to vague type {1}")] #[error("Can not coerce {0} to vague type {1}")]
HintIsVague(TypeKind, VagueType), HintIsVague(TypeKind, VagueType),
#[error("Literal {0} can not be coerced to type {1}")]
LiteralIncompatible(Literal, TypeKind),
#[error("Types {0} and {1} are incompatible")] #[error("Types {0} and {1} are incompatible")]
TypesIncompatible(TypeKind, TypeKind), TypesIncompatible(TypeKind, TypeKind),
#[error("Variable not defined: {0}")] #[error("Variable not defined: {0}")]
@ -254,7 +252,6 @@ impl Literal {
(L::U32(_), U32) => self, (L::U32(_), U32) => self,
(L::U64(_), U64) => self, (L::U64(_), U64) => self,
(L::U128(_), U128) => self, (L::U128(_), U128) => self,
(L::Bool(_), Bool) => self,
(L::Vague(VagueL::Number(v)), I8) => L::I8(v as i8), (L::Vague(VagueL::Number(v)), I8) => L::I8(v as i8),
(L::Vague(VagueL::Number(v)), I16) => L::I16(v as i16), (L::Vague(VagueL::Number(v)), I16) => L::I16(v as i16),
(L::Vague(VagueL::Number(v)), I32) => L::I32(v as i32), (L::Vague(VagueL::Number(v)), I32) => L::I32(v as i32),
@ -268,7 +265,7 @@ impl Literal {
// Default type for number literal if unable to find true type. // Default type for number literal if unable to find true type.
(L::Vague(VagueL::Number(v)), Vague(Number)) => L::I32(v as i32), (L::Vague(VagueL::Number(v)), Vague(Number)) => L::I32(v as i32),
(_, Vague(_)) => self, (_, Vague(_)) => self,
_ => Err(ErrorKind::LiteralIncompatible(self, hint))?, _ => Err(ErrorKind::TypesIncompatible(self.as_type(), hint))?,
}) })
} else { } else {
Ok(self) Ok(self)
@ -302,7 +299,7 @@ impl TypeKind {
BinaryOperator::Minus => res, BinaryOperator::Minus => res,
BinaryOperator::Mult => res, BinaryOperator::Mult => res,
BinaryOperator::And => res, BinaryOperator::And => res,
BinaryOperator::Cmp(_) => Bool, BinaryOperator::Logic(_) => Bool,
}) })
} }
} }