Skip to content

Commit

Permalink
Added basic sizeof operator
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacShelton committed Jan 29, 2025
1 parent be232cb commit f8098f5
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/asg/expr/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub enum ExprKind {
EnumMemberLiteral(Box<EnumMemberLiteral>),
ResolvedNamedExpression(Box<Expr>),
Zeroed(Box<Type>),
SizeOf(Box<Type>),
InterpreterSyscall(InterpreterSyscallKind, Vec<Expr>),
}

Expand Down
7 changes: 6 additions & 1 deletion src/ast/expr/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use super::{
InterpreterSyscall, ShortCircuitingBinaryOperation, StaticMemberCall, StaticMemberValue,
StructLiteral, UnaryOperation, While,
};
use crate::{ast::Privacy, name::Name, source_files::Source};
use crate::{
ast::{Privacy, Type},
name::Name,
source_files::Source,
};
use std::ffi::CString;

#[derive(Clone, Debug)]
Expand All @@ -29,6 +33,7 @@ pub enum ExprKind {
While(Box<While>),
StaticMemberValue(Box<StaticMemberValue>),
StaticMemberCall(Box<StaticMemberCall>),
SizeOf(Box<Type>),
InterpreterSyscall(Box<InterpreterSyscall>),
}

Expand Down
10 changes: 7 additions & 3 deletions src/lower/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ pub fn lower_expr(
Box::new(ir::Type::Void),
)))),
ExprKind::Call(call) => lower_expr_call(builder, ir_module, expr, function, asg, call),
ExprKind::PolyCall(poly_call) => {
lower_expr_poly_call(builder, ir_module, expr, function, asg, poly_call)
}
ExprKind::Variable(variable) => {
let pointer_to_variable = lower_variable_to_value(variable.key);
let variable_type = lower_type(ir_module, &builder.unpoly(&variable.ty)?, asg)?;
Expand Down Expand Up @@ -484,6 +487,10 @@ pub fn lower_expr(
let ir_type = lower_type(ir_module, &builder.unpoly(ty)?, asg)?;
Ok(ir::Value::Literal(Literal::Zeroed(ir_type)))
}
ExprKind::SizeOf(ty) => {
let ir_type = lower_type(ir_module, &builder.unpoly(ty)?, asg)?;
Ok(builder.push(ir::Instr::SizeOf(ir_type)))
}
ExprKind::InterpreterSyscall(syscall, args) => {
let mut values = Vec::with_capacity(args.len());

Expand All @@ -493,9 +500,6 @@ pub fn lower_expr(

Ok(builder.push(ir::Instr::InterpreterSyscall(*syscall, values)))
}
ExprKind::PolyCall(poly_call) => {
lower_expr_poly_call(builder, ir_module, expr, function, asg, poly_call)
}
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/parser/parse_expr/primary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ mod struct_literal;
use super::{super::error::ParseError, is_right_associative, is_terminating_token, Parser};
use crate::{
ast::{
Block, Conditional, Expr, ExprKind, Integer, TypeKind, UnaryMathOperator, UnaryOperation,
UnaryOperator, While,
Block, Conditional, Expr, ExprKind, Integer, TypeArg, TypeKind, UnaryMathOperator,
UnaryOperation, UnaryOperator, While,
},
inflow::Inflow,
parser::{array_last, error::ParseErrorKind, parse_util::into_plain_name},
Expand Down Expand Up @@ -148,9 +148,17 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
}
_ => {
if !generics.is_empty() {
// TODO: CLEANUP: Clean up this code
if let Some("sizeof") = name.as_plain_str() {
todo!("sizeof operator");
// Ok(Expr::new(ExprKind::SizeOf(inner), source))
if let Some(type_arg) = generics.first() {
if let TypeArg::Type(ty) = type_arg {
if generics.len() == 1 {
return Ok(
ExprKind::SizeOf(Box::new(ty.clone())).at(source)
);
}
}
}
}

return Err(ParseErrorKind::Other {
Expand Down
14 changes: 13 additions & 1 deletion src/resolve/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ use super::{
Initialized, ResolveTypeCtx,
};
use crate::{
asg::{self, Asg, CurrentConstraints, Expr, ExprKind, FuncRef, StructRef, TypeKind, TypedExpr},
asg::{
self, Asg, CurrentConstraints, Expr, ExprKind, FuncRef, IntegerBits, StructRef, TypeKind,
TypedExpr,
},
ast::{
self, CInteger, CIntegerAssumptions, ConformBehavior, IntegerKnown, Language, Settings,
UnaryOperator,
},
ir::IntegerSign,
resolve::{
error::ResolveErrorKind,
expr::{
Expand Down Expand Up @@ -371,6 +375,14 @@ pub fn resolve_expr(
ast::ExprKind::StaticMemberCall(static_access_call) => {
resolve_static_member_call(ctx, static_access_call)
}
ast::ExprKind::SizeOf(ast_type) => {
let ty = ctx.type_ctx().resolve(ast_type)?;

Ok(TypedExpr::new(
asg::TypeKind::Integer(IntegerBits::Bits64, IntegerSign::Unsigned).at(source),
asg::ExprKind::SizeOf(Box::new(ty)).at(source),
))
}
ast::ExprKind::InterpreterSyscall(info) => {
let ast::InterpreterSyscall {
kind,
Expand Down

0 comments on commit f8098f5

Please sign in to comment.