Skip to content

Commit

Permalink
fixed aaron's parsing issue and made bools a little smidgeon beter
Browse files Browse the repository at this point in the history
  • Loading branch information
CreggEgg committed Jul 23, 2024
1 parent 42da55a commit afc7b20
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 23 deletions.
16 changes: 16 additions & 0 deletions aaron.ct
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
let not = fn(a : bool): bool {
if a {
false
} else {
true
}
};

let equals = fn(a: int, ba: int): bool {
not(a < ba)
};

let is_mult = fn(a: int,b: int) {
let div_mult = a/b * b;
div_mult = a
};
1 change: 1 addition & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub enum Literal {
body: Vec<UntypedExpr>,
ret_type: Option<TypeName>,
},
Bool(bool),
}

#[derive(Debug, Clone)]
Expand Down
41 changes: 23 additions & 18 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,20 +511,14 @@ fn compile_global(
crate::ast::BinaryOperator::Divide => builder.ins().sdiv(lhs, rhs),
crate::ast::BinaryOperator::Power => todo!(),
// crate::ast::BinaryOperator::Semicolon => todo!(),
crate::ast::BinaryOperator::Gt => {
builder.ins().icmp(IntCC::SignedGreaterThan, lhs, rhs)
}
crate::ast::BinaryOperator::Gt => cmp(IntCC::SignedGreaterThan, lhs, rhs, builder),

crate::ast::BinaryOperator::Lt => {
builder.ins().icmp(IntCC::SignedLessThan, lhs, rhs)
}
crate::ast::BinaryOperator::Lt => cmp(IntCC::SignedLessThan, lhs, rhs, builder),
crate::ast::BinaryOperator::Gte => {
builder
.ins()
.icmp(IntCC::SignedGreaterThanOrEqual, lhs, rhs)
cmp(IntCC::SignedGreaterThanOrEqual, lhs, rhs, builder)
}
crate::ast::BinaryOperator::Lte => {
builder.ins().icmp(IntCC::SignedLessThanOrEqual, lhs, rhs)
cmp(IntCC::SignedLessThanOrEqual, lhs, rhs, builder)
}
};
Ok(CitrusValue::Value {
Expand All @@ -539,6 +533,12 @@ fn compile_global(
r#type,
// storage_location: StorageLocation::Heap,
}),

TypedLiteral::Bool(x) => Ok(CitrusValue::Value {
cranelift_value: builder.ins().iconst(I64, if x { 1 } else { 0 }),
r#type,
// storage_location: StorageLocation::Heap,
}),
TypedLiteral::String(_) => todo!(),
TypedLiteral::Function {
args,
Expand Down Expand Up @@ -600,29 +600,25 @@ fn compile_expr(
},
crate::ast::BinaryOperator::Power => todo!(),
crate::ast::BinaryOperator::Gt => CitrusValue::Value {
cranelift_value: builder.ins().icmp(IntCC::SignedGreaterThan, lhs, rhs),
cranelift_value: cmp(IntCC::SignedGreaterThan, lhs, rhs, builder),
r#type,
// storage_location: StorageLocation::Stack,
},
crate::ast::BinaryOperator::Lt => CitrusValue::Value {
cranelift_value: builder.ins().icmp(IntCC::SignedLessThan, lhs, rhs),
cranelift_value: cmp(IntCC::SignedLessThan, lhs, rhs, builder),

r#type,
// storage_location: StorageLocation::Stack,
},
crate::ast::BinaryOperator::Gte => CitrusValue::Value {
cranelift_value: builder.ins().icmp(
IntCC::SignedGreaterThanOrEqual,
lhs,
rhs,
),
cranelift_value: cmp(IntCC::SignedGreaterThanOrEqual, lhs, rhs, builder),

r#type,
// storage_location: StorageLocation::Stack,
},

crate::ast::BinaryOperator::Lte => CitrusValue::Value {
cranelift_value: builder.ins().icmp(IntCC::SignedLessThanOrEqual, lhs, rhs),
cranelift_value: cmp(IntCC::SignedLessThanOrEqual, lhs, rhs, builder),

r#type,
// storage_location: StorageLocation::Stack,
Expand Down Expand Up @@ -663,6 +659,10 @@ fn compile_expr(
body,
ret_type,
} => todo!(),
TypedLiteral::Bool(value) => CitrusValue::Value {
cranelift_value: builder.ins().iconst(I64, if value { 1 } else { 0 }),
r#type: Type::Bool,
},
},
scope,
)),
Expand Down Expand Up @@ -790,3 +790,8 @@ fn get_size_bytes(r#type: &Type) -> i64 {
//cranelift ir functions and not values by this point
}
}

fn cmp(cmp: IntCC, lhs: Value, rhs: Value, builder: &mut FunctionBuilder) -> Value {
let as_i8 = builder.ins().icmp(cmp, lhs, rhs);
builder.ins().sextend(I64, as_i8)
}
10 changes: 6 additions & 4 deletions src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ Expr: UntypedExpr = {
#[precedence(level="2")] #[assoc(side="left")]
<l: Expr> "+" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Add, rhs: Box::new(r)},
<l: Expr> "-" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Subtract, rhs: Box::new(r)},
<l: Expr> ">" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Gt, rhs: Box::new(r)},
<l: Expr> "<" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Lt, rhs: Box::new(r)},
<l: Expr> ">=" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Gte, rhs: Box::new(r)},
<l: Expr> "<=" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Lte, rhs: Box::new(r)},
#[precedence(level="3")] #[assoc(side="left")]
<l: Expr> "*" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Multiply, rhs: Box::new(r)},
<l: Expr> "/" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Divide, rhs: Box::new(r)},
#[precedence(level="5")]
<i: Ident> <a: FunctionArgs> => UntypedExpr::FunctionCall(i, a),
#[precedence(level="1")] #[assoc(side="left")]
<i: Ident> => UntypedExpr::Ident(i),
<l: Expr> ">" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Gt, rhs: Box::new(r)},
<l: Expr> "<" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Lt, rhs: Box::new(r)},
<l: Expr> ">=" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Gte, rhs: Box::new(r)},
<l: Expr> "<=" <r: Expr> => UntypedExpr::BinaryOp {lhs: Box::new(l), op: BinaryOperator::Lte, rhs: Box::new(r)},
"if" <c: Expr> "{" <t: Block> "}" "else" "{" <e: Block> "}" => UntypedExpr::IfElse { condition: Box::new(c), then: t, r#else: e},
#[precedence(level="4")]
"@" <e: Expr> => UntypedExpr::UnaryOp {op: UnaryOperator::Exclave, target: Box::new(e)},
Expand All @@ -59,6 +59,8 @@ FunctionArgs: Vec<UntypedExpr> = "(" <a: Comma<Expr>> ")" => a;
Ident: String = r"[a-zA-Z_]+" => <>.to_owned();
Literal: Literal = {
r"[0-9]+" => Literal::Int(<>.parse::<i32>().unwrap()),
"false" => Literal::Bool(false),
"true" => Literal::Bool(true),
<s: r#"".+""#> => Literal::String(s[1..(s.len() - 1)].to_owned()),
"fn(" <a: Comma<AnnotatedIdent>> ")" <r: Annotation?> "{"<b: Block>"}" => Literal::Function {args: a, body: b,ret_type: r}
}
Expand Down
15 changes: 14 additions & 1 deletion src/types/inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,20 @@ fn type_expr(
crate::ast::UntypedExpr::BinaryOp { lhs, op, rhs } => {
let lhs = type_expr(*lhs, types, scope)?;
let rhs = type_expr(*rhs, types, scope)?;
let shared_type = require_identical(get_type(lhs.0.clone()), get_type(rhs.0.clone()))?;
Ok((
TypedExpr::BinaryOp {
r#type: require_identical(get_type(lhs.0.clone()), get_type(rhs.0.clone()))?,
r#type: match op {
crate::ast::BinaryOperator::Add
| crate::ast::BinaryOperator::Subtract
| crate::ast::BinaryOperator::Multiply
| crate::ast::BinaryOperator::Divide
| crate::ast::BinaryOperator::Power => shared_type,
crate::ast::BinaryOperator::Gt
| crate::ast::BinaryOperator::Lt
| crate::ast::BinaryOperator::Gte
| crate::ast::BinaryOperator::Lte => Type::Bool,
},
lhs: Box::new(lhs.0),
op,
rhs: Box::new(rhs.0),
Expand Down Expand Up @@ -337,6 +348,7 @@ fn type_literal(
) -> Result<TypedLiteral, TypeError> {
Ok(match literal {
crate::ast::Literal::Int(x) => TypedLiteral::Int(*x),
crate::ast::Literal::Bool(x) => TypedLiteral::Bool(*x),
crate::ast::Literal::String(x) => TypedLiteral::String(x.clone()),
crate::ast::Literal::Function {
args,
Expand Down Expand Up @@ -397,6 +409,7 @@ fn get_literal_type(
) -> Result<Type, TypeError> {
match literal {
crate::ast::Literal::Int(_) => Ok(Type::Int),
crate::ast::Literal::Bool(_) => Ok(Type::Bool),
crate::ast::Literal::String(val) => Ok(Type::Array(Box::new(Type::Int))),
crate::ast::Literal::Function {
args,
Expand Down
1 change: 1 addition & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub enum TypedLiteral {
body: Vec<TypedExpr>,
ret_type: Option<Type>,
},
Bool(bool),
}

#[derive(Debug, PartialEq, Clone)]
Expand Down

0 comments on commit afc7b20

Please sign in to comment.