Skip to content

Commit

Permalink
Fix booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
Victorious3 committed Nov 8, 2021
1 parent 2cc3191 commit 3968003
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/compiler.pr
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,9 @@ export def convert_to(node: *parser::Node, value: Value, tpe: *typechecking::Typ
return value
}
} else if typechecking::is_integer(tpe) {
if typechecking::is_integer(value.tpe) {
if value.tpe.kind == typechecking::TypeKind::BOOL {
kind = InsnKind::ZEXT
} else if typechecking::is_integer(value.tpe) {
if (@value.tpe).size == (@tpe).size {
// Types only differ in sign, llvm doesn't treat them any different
return value
Expand Down Expand Up @@ -2415,6 +2417,11 @@ def compare(node: *parser::Node, left: Value, right: Value, state: *State) -> Va
}
if typechecking::is_arithmetic(left.tpe) and typechecking::is_arithmetic(right.tpe) {
tpe = typechecking::common_type(left.tpe, right.tpe)
if not tpe {
typechecking::errorn(node, "Can't compare ")
error(debug::type_to_str(left.tpe), " and ", debug::type_to_str(right.tpe), "\n")
return NO_VALUE
}

left = convert_to(node, left, tpe, state)
right = convert_to(node, right, tpe, state)
Expand Down
2 changes: 2 additions & 0 deletions src/eval.pr
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,8 @@ def type_to_ffi(tpe: *typechecking::Type) -> *cstd::ffi_type {
return *cstd::ffi_type_float
} else if typechecking::equals(tpe, builtins::double_) {
return *cstd::ffi_type_double
} else if typechecking::equals(tpe, builtins::bool_) {
return *cstd::ffi_type_sint8
} else {
error(debug::type_to_str(tpe), "\n")
assert(false)
Expand Down
1 change: 1 addition & 0 deletions src/repl.pr
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ def execute(source: string) {
preprocess::preprocess(module)
typechecking::typecheck(module)

// TODO Do it lua style with a = to emit the value
assert(node.kind == parser::NodeKind::PROGRAM)
node = vector::peek(node.value.body) !*parser::Node
if not node { return }
Expand Down
11 changes: 11 additions & 0 deletions src/test/test_ctfe.pr
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,22 @@ def test_strings {
tassert(hello_world == "Hello World!")
}

def test_bug_1_ -> bool {
return true == (true == true)
}

def test_bug_1 {
const ac = test_bug_1_()
let al = test_bug_1_()
tassert(ac == al == true)
}

export def run_tests {
print("Running tests on compile time functions... \n")
run_test("test_constants", *test_constants)
run_test("test_func_call", *test_func_call)
run_test("test_static_if", *test_static_if)
run_test("test_c_functions", *test_c_functions)
run_test("test_strings", *test_strings)
run_test("test_bug_1", *test_bug_1)
}
2 changes: 1 addition & 1 deletion src/toolchain.pr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def create_version_string -> string {
return buffer::to_string(*buf)
}

export const version = create_version_string()
export let version = create_version_string()

// Debug stuff
export var print_ast = false
Expand Down
5 changes: 5 additions & 0 deletions src/typechecking.pr
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,12 @@ export def common_type(a: *Type, b: *Type) -> *Type {
return a
} else if (@a).kind == TypeKind::WORD and (@b).kind == TypeKind::FLOAT {
return b
} else if a.kind == TypeKind::BOOL and is_arithmetic(b) {
return a
} else if b.kind == TypeKind::BOOL and is_arithmetic(a) {
return b
}

if (@a).kind == (@b).kind {
if (@a).size == (@b).size {
if (@b).unsig {
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION=0.1.9
VERSION=0.1.10

0 comments on commit 3968003

Please sign in to comment.