Skip to content

Commit

Permalink
CORE: Allow implicitly casting from a smaller integer to a bigger float
Browse files Browse the repository at this point in the history
  • Loading branch information
MineGame159 committed Feb 5, 2024
1 parent 96d2714 commit 56e58a9
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (c *checker) expectPrimitiveValue(expr ast.Expr, kind ast.PrimitiveKind) {
}

func (c *checker) checkRequired(required ast.Type, expr ast.Expr) {
if required == nil || expr == nil || expr.Result().Kind == ast.InvalidResultKind {
if required == nil || expr == nil || expr.Result().Kind == ast.InvalidResultKind || expr.Result().Type == nil {
return
}

Expand Down
5 changes: 2 additions & 3 deletions core/common/casts.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,8 @@ func GetImplicitCast(from, to ast.Type) (CastKind, bool) {
return Extend, true
}

// Primitive (same integer) -> Primitive (same floating)
// TODO: Allow converting a smaller integer to the next bigger floating (eg. i16 -> f32)
if ast.IsInteger(from.Kind) && ast.IsFloating(to.Kind) && toSize == fromSize {
// Primitive (smaller or equal integer) -> Primitive (bigger or equal floating)
if ast.IsInteger(from.Kind) && ast.IsFloating(to.Kind) && toSize >= fromSize {
return Int2Float, true
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/llvm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (w *textWriter) writeInstruction(inst ir.Inst) {
w.writeValueValue(inst.Right)

case *ir.AndInst:
w.writeString("add ")
w.writeString("and ")
w.writeValue(inst.Left)
w.writeString(", ")
w.writeValueValue(inst.Right)
Expand Down
8 changes: 4 additions & 4 deletions gen/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ var expressions = Group{
"Paren",
field("expr", type_("Expr")),
),
node(
"Literal",
field("token", type_("scanner.Token")),
),
node(
"Unary",
field("prefix", type_("bool")),
Expand Down Expand Up @@ -235,10 +239,6 @@ var expressions = Group{
"Identifier",
field("name", type_("scanner.Token")),
),
node(
"Literal",
field("token", type_("scanner.Token")),
),
},
}

Expand Down
8 changes: 8 additions & 0 deletions tests/src/implicit_casts.fb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ func int2float() bool {
return b == 5f;
}

#[Test]
func int2float_differentSize() bool {
var a u8 = 6 as u8;
var b f64 = a;

return b == 6.0;
}

#[Test]
func long2double() bool {
var a i64 = 5;
Expand Down
2 changes: 1 addition & 1 deletion vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "vscode-languageclient/node";
import {ChildProcess, exec} from "child_process";

const DEV = true;
const DEV = false;

let process: ChildProcess;
let client: LanguageClient;
Expand Down

0 comments on commit 56e58a9

Please sign in to comment.