Skip to content

Commit

Permalink
Add ++ and -- for float/double
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Jan 26, 2025
1 parent 3fadbda commit 15eb274
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions compiler/builders/ast_to_builder.jou
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ class AstToBuilder:
match t->kind:
case TypeKind.SignedInteger | TypeKind.UnsignedInteger:
new_value = self->builder->add(old_value, self->builder->integer(t, diff))
case TypeKind.FloatingPoint:
new_value = self->builder->add(old_value, self->builder->cast(self->builder->integer(intType, diff), t))
case TypeKind.Pointer:
idx = self->builder->integer(longType, diff)
new_value = self->builder->indexed_pointer(old_value, idx)
Expand Down
2 changes: 1 addition & 1 deletion compiler/typecheck/step3_function_and_method_bodies.jou
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ def check_increment_or_decrement(state: State*, expr: AstExpression*) -> Type*:
assert False

t = typecheck_expression_not_void(state, &expr->operands[0])
if not t->is_integer_type() and t->kind != TypeKind.Pointer:
if not t->is_number_type() and t->kind != TypeKind.Pointer:
msg: byte[500]
snprintf(msg, sizeof(msg), bad_type_fmt, t->name)
fail(expr->location, msg)
Expand Down
14 changes: 14 additions & 0 deletions tests/should_succeed/plusplus_minusminus.jou
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,18 @@ def main() -> int:
n = 0
printf("%d %d\n", n, ++n) # Output: 0 1

# float
f = 1.23f
printf("%.2f f=%.2f\n", f++, f) # Output: 1.23 f=2.23
printf("%.2f f=%.2f\n", f--, f) # Output: 2.23 f=1.23
printf("%.2f f=%.2f\n", ++f, f) # Output: 2.23 f=2.23
printf("%.2f f=%.2f\n", --f, f) # Output: 1.23 f=1.23

# double
d = 1.23
printf("%.2f d=%.2f\n", d++, d) # Output: 1.23 d=2.23
printf("%.2f d=%.2f\n", d--, d) # Output: 2.23 d=1.23
printf("%.2f d=%.2f\n", ++d, d) # Output: 2.23 d=2.23
printf("%.2f d=%.2f\n", --d, d) # Output: 1.23 d=1.23

return 0

0 comments on commit 15eb274

Please sign in to comment.