Skip to content

Commit f8098f5

Browse files
committed
Added basic sizeof operator
1 parent be232cb commit f8098f5

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

Diff for: src/asg/expr/kind.rs

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub enum ExprKind {
4242
EnumMemberLiteral(Box<EnumMemberLiteral>),
4343
ResolvedNamedExpression(Box<Expr>),
4444
Zeroed(Box<Type>),
45+
SizeOf(Box<Type>),
4546
InterpreterSyscall(InterpreterSyscallKind, Vec<Expr>),
4647
}
4748

Diff for: src/ast/expr/kind.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ use super::{
33
InterpreterSyscall, ShortCircuitingBinaryOperation, StaticMemberCall, StaticMemberValue,
44
StructLiteral, UnaryOperation, While,
55
};
6-
use crate::{ast::Privacy, name::Name, source_files::Source};
6+
use crate::{
7+
ast::{Privacy, Type},
8+
name::Name,
9+
source_files::Source,
10+
};
711
use std::ffi::CString;
812

913
#[derive(Clone, Debug)]
@@ -29,6 +33,7 @@ pub enum ExprKind {
2933
While(Box<While>),
3034
StaticMemberValue(Box<StaticMemberValue>),
3135
StaticMemberCall(Box<StaticMemberCall>),
36+
SizeOf(Box<Type>),
3237
InterpreterSyscall(Box<InterpreterSyscall>),
3338
}
3439

Diff for: src/lower/expr/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ pub fn lower_expr(
137137
Box::new(ir::Type::Void),
138138
)))),
139139
ExprKind::Call(call) => lower_expr_call(builder, ir_module, expr, function, asg, call),
140+
ExprKind::PolyCall(poly_call) => {
141+
lower_expr_poly_call(builder, ir_module, expr, function, asg, poly_call)
142+
}
140143
ExprKind::Variable(variable) => {
141144
let pointer_to_variable = lower_variable_to_value(variable.key);
142145
let variable_type = lower_type(ir_module, &builder.unpoly(&variable.ty)?, asg)?;
@@ -484,6 +487,10 @@ pub fn lower_expr(
484487
let ir_type = lower_type(ir_module, &builder.unpoly(ty)?, asg)?;
485488
Ok(ir::Value::Literal(Literal::Zeroed(ir_type)))
486489
}
490+
ExprKind::SizeOf(ty) => {
491+
let ir_type = lower_type(ir_module, &builder.unpoly(ty)?, asg)?;
492+
Ok(builder.push(ir::Instr::SizeOf(ir_type)))
493+
}
487494
ExprKind::InterpreterSyscall(syscall, args) => {
488495
let mut values = Vec::with_capacity(args.len());
489496

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

494501
Ok(builder.push(ir::Instr::InterpreterSyscall(*syscall, values)))
495502
}
496-
ExprKind::PolyCall(poly_call) => {
497-
lower_expr_poly_call(builder, ir_module, expr, function, asg, poly_call)
498-
}
499503
}
500504
}
501505

Diff for: src/parser/parse_expr/primary/mod.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ mod struct_literal;
77
use super::{super::error::ParseError, is_right_associative, is_terminating_token, Parser};
88
use crate::{
99
ast::{
10-
Block, Conditional, Expr, ExprKind, Integer, TypeKind, UnaryMathOperator, UnaryOperation,
11-
UnaryOperator, While,
10+
Block, Conditional, Expr, ExprKind, Integer, TypeArg, TypeKind, UnaryMathOperator,
11+
UnaryOperation, UnaryOperator, While,
1212
},
1313
inflow::Inflow,
1414
parser::{array_last, error::ParseErrorKind, parse_util::into_plain_name},
@@ -148,9 +148,17 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
148148
}
149149
_ => {
150150
if !generics.is_empty() {
151+
// TODO: CLEANUP: Clean up this code
151152
if let Some("sizeof") = name.as_plain_str() {
152-
todo!("sizeof operator");
153-
// Ok(Expr::new(ExprKind::SizeOf(inner), source))
153+
if let Some(type_arg) = generics.first() {
154+
if let TypeArg::Type(ty) = type_arg {
155+
if generics.len() == 1 {
156+
return Ok(
157+
ExprKind::SizeOf(Box::new(ty.clone())).at(source)
158+
);
159+
}
160+
}
161+
}
154162
}
155163

156164
return Err(ParseErrorKind::Other {

Diff for: src/resolve/expr/mod.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ use super::{
2424
Initialized, ResolveTypeCtx,
2525
};
2626
use crate::{
27-
asg::{self, Asg, CurrentConstraints, Expr, ExprKind, FuncRef, StructRef, TypeKind, TypedExpr},
27+
asg::{
28+
self, Asg, CurrentConstraints, Expr, ExprKind, FuncRef, IntegerBits, StructRef, TypeKind,
29+
TypedExpr,
30+
},
2831
ast::{
2932
self, CInteger, CIntegerAssumptions, ConformBehavior, IntegerKnown, Language, Settings,
3033
UnaryOperator,
3134
},
35+
ir::IntegerSign,
3236
resolve::{
3337
error::ResolveErrorKind,
3438
expr::{
@@ -371,6 +375,14 @@ pub fn resolve_expr(
371375
ast::ExprKind::StaticMemberCall(static_access_call) => {
372376
resolve_static_member_call(ctx, static_access_call)
373377
}
378+
ast::ExprKind::SizeOf(ast_type) => {
379+
let ty = ctx.type_ctx().resolve(ast_type)?;
380+
381+
Ok(TypedExpr::new(
382+
asg::TypeKind::Integer(IntegerBits::Bits64, IntegerSign::Unsigned).at(source),
383+
asg::ExprKind::SizeOf(Box::new(ty)).at(source),
384+
))
385+
}
374386
ast::ExprKind::InterpreterSyscall(info) => {
375387
let ast::InterpreterSyscall {
376388
kind,

0 commit comments

Comments
 (0)