Skip to content

Commit a6aaa12

Browse files
committed
Added sizeof example and cleaned up internal sizeof code
1 parent f8098f5 commit a6aaa12

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

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

+24-9
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,32 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
148148
}
149149
_ => {
150150
if !generics.is_empty() {
151-
// TODO: CLEANUP: Clean up this code
151+
let mut generics = generics;
152+
let mut generics = generics.drain(..);
153+
152154
if let Some("sizeof") = name.as_plain_str() {
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-
}
155+
let Some(arg) = generics.next() else {
156+
return Err(ParseErrorKind::Other {
157+
message: "Expected type argument to sizeof macro".into(),
160158
}
161-
}
159+
.at(source));
160+
};
161+
162+
let TypeArg::Type(ty) = arg else {
163+
return Err(ParseErrorKind::Other {
164+
message: "Cannot get size of non-type value".into(),
165+
}
166+
.at(source));
167+
};
168+
169+
if generics.next().is_some() {
170+
return Err(ParseErrorKind::Other {
171+
message: "Too many arguments to sizeof macro".into(),
172+
}
173+
.at(source));
174+
};
175+
176+
return Ok(ExprKind::SizeOf(Box::new(ty.clone())).at(source));
162177
}
163178

164179
return Err(ParseErrorKind::Other {

Diff for: tests/success/sizeof/main.adept

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
pragma => adept("3.0")
3+
4+
#[foreign]
5+
func printf(format ptr-char, ...) int
6+
7+
func main {
8+
printf(c"sizeof-char = %d\n", sizeof-char.int())
9+
printf(c"sizeof-short = %d\n", sizeof-char.int())
10+
printf(c"sizeof-int = %d\n", sizeof-int.int())
11+
printf(c"sizeof-long = %d\n", sizeof-long.int())
12+
printf(c"sizeof-longlong = %d\n", sizeof-longlong.int())
13+
printf(c"sizeof-ptr-char = %d\n", sizeof-ptr-char.int())
14+
}
15+

0 commit comments

Comments
 (0)