navid/haze / commit
cb1bac46fbd9
Navid M <navidmomtahen@gmail.com> · 2026-12-02 · [master]
parents: ea08ceb46539
Browse files at this commit · Download source
--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -182,6 +182,8 @@ "${CMAKE_CURRENT_SOURCE_DIR}/samples/baseline_2.7/*.hz") file(GLOB baseline_28_samples CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/samples/baseline_2.8/*.hz")+file(GLOB baseline_29_samples CONFIGURE_DEPENDS+ "${CMAKE_CURRENT_SOURCE_DIR}/samples/baseline_2.9/*.hz") list(SORT baseline_samples) list(SORT baseline_21_samples) list(SORT baseline_22_samples)@@ -191,10 +193,11 @@ list(SORT baseline_26_samples) list(SORT baseline_27_samples) list(SORT baseline_28_samples)+list(SORT baseline_29_samples) foreach(source IN LISTS baseline_samples baseline_21_samples baseline_22_samples baseline_23_samples baseline_24_samples baseline_25_samples baseline_26_samples- baseline_27_samples baseline_28_samples)+ baseline_27_samples baseline_28_samples baseline_29_samples) add_haze_sample_test("${source}") endforeach() add_haze_sample_test("${CMAKE_CURRENT_SOURCE_DIR}/samples/baseline_2/main.hz")--- a/TODO.md+++ b/TODO.md@@ -1,5 +1,5 @@ - [x] General user-defined generic type declarations (type Map[K, V] ...)-- [ ] u8/u16/u32/u64, bitwise operators, f64, bytes, UTF-8 string operations+- [x] u8/u16/u32/u64, bitwise operators, f64, bytes, UTF-8 string operations - [ ] @cfg conditional compilation - [ ] extern (C) functions and C-layout extern type - [ ] String interpolation and format specs--- a/runtime/hazert.cpp+++ b/runtime/hazert.cpp@@ -1498,6 +1498,127 @@ void haze_print_i64(int64_t value) { std::printf("%lld\n", static_cast<long long>(value));+}++void haze_print_u32(uint32_t value) {+ std::printf("%u\n", value);+}++void haze_print_u64(uint64_t value) {+ std::printf("%llu\n", static_cast<unsigned long long>(value));+}++void haze_print_f64(double value) {+ std::printf("%g\n", value);+}++uint64_t haze_str_len(const char* value) {+ return value ? static_cast<uint64_t>(std::strlen(value)) : 0;+}++const char* haze_str_concat(const char* a, const char* b) {+ size_t an = a ? std::strlen(a) : 0;+ size_t bn = b ? std::strlen(b) : 0;+ char* out = static_cast<char*>(std::malloc(an + bn + 1));+ if (!out)+ std::abort();+ if (an)+ std::memcpy(out, a, an);+ if (bn)+ std::memcpy(out + an, b, bn);+ out[an + bn] = '\0';+ return out;+}++const char* haze_str_slice(const char* value, uint64_t start, uint64_t end) {+ uint64_t len = haze_str_len(value);+ if (start > len)+ start = len;+ if (end > len)+ end = len;+ if (end < start)+ end = start;+ uint64_t n = end - start;+ char* out = static_cast<char*>(std::malloc(static_cast<size_t>(n) + 1));+ if (!out)+ std::abort();+ if (n)+ std::memcpy(out, value + start, static_cast<size_t>(n));+ out[n] = '\0';+ return out;+}++static uint8_t* haze_bytes_data(void* object) {+ return static_cast<uint8_t*>(object) + 24;+}++void* haze_bytes_new(uint64_t len) {+ if (len > 0xffffffffu)+ std::abort();+ uint8_t* object = static_cast<uint8_t*>(+ haze_alloc_slow(static_cast<uint32_t>(16 + len), 0));+ *reinterpret_cast<uint64_t*>(object + 8) = len;+ *reinterpret_cast<uint64_t*>(object + 16) = len;+ return object;+}++void* haze_bytes_from_str(const char* value) {+ uint64_t len = haze_str_len(value);+ uint8_t* object = static_cast<uint8_t*>(haze_bytes_new(len));+ if (len)+ std::memcpy(haze_bytes_data(object), value, static_cast<size_t>(len));+ return object;+}++const char* haze_bytes_to_str(void* object) {+ uint64_t len = *reinterpret_cast<uint64_t*>(static_cast<uint8_t*>(object) + 8);+ char* out = static_cast<char*>(std::malloc(static_cast<size_t>(len) + 1));+ if (!out)+ std::abort();+ if (len)+ std::memcpy(out, haze_bytes_data(object), static_cast<size_t>(len));+ out[len] = '\0';+ return out;+}++void* haze_bytes_append(void* object, uint32_t byte) {+ uint8_t* bytes = static_cast<uint8_t*>(object);+ uint64_t len = *reinterpret_cast<uint64_t*>(bytes + 8);+ uint64_t cap = *reinterpret_cast<uint64_t*>(bytes + 16);+ if (len < cap) {+ haze_bytes_data(object)[len] = static_cast<uint8_t>(byte);+ *reinterpret_cast<uint64_t*>(bytes + 8) = len + 1;+ return object;+ }+ uint64_t next_cap = cap ? cap * 2 : 8;+ uint8_t* grown = static_cast<uint8_t*>(haze_bytes_new(next_cap));+ if (len)+ std::memcpy(haze_bytes_data(grown), haze_bytes_data(object),+ static_cast<size_t>(len));+ haze_bytes_data(grown)[len] = static_cast<uint8_t>(byte);+ *reinterpret_cast<uint64_t*>(grown + 8) = len + 1;+ return grown;+}++void* haze_bytes_slice(void* object, uint64_t start, uint64_t end) {+ uint8_t* bytes = static_cast<uint8_t*>(object);+ uint64_t len = *reinterpret_cast<uint64_t*>(bytes + 8);+ if (start > len)+ start = len;+ if (end > len)+ end = len;+ if (end < start)+ end = start;+ uint64_t n = end - start;+ uint8_t* out = static_cast<uint8_t*>(haze_bytes_new(n));+ if (n)+ std::memcpy(haze_bytes_data(out), haze_bytes_data(object) + start,+ static_cast<size_t>(n));+ return out;+}++void* haze_bytes_ptr(void* object) {+ return haze_bytes_data(object); } void haze_print_bool(int32_t value) {--- /dev/null+++ b/samples/baseline_2.9/bytes_strings.hz@@ -0,0 +1,31 @@+// Managed bytes buffers and UTF-8 byte-oriented string operations.++fn main() {+ var buffer = bytes_new(4)+ buffer[0] = 65+ buffer[1] = 66+ buffer[2] = 67+ buffer[3] = 68+ print(buffer.len)+ print(buffer[0])+ print(buffer[3])++ buffer = bytes_append(buffer, 69)+ print(buffer.len)+ print(bytes_to_str(buffer))++ var copy = bytes_from_str("hello")+ print(copy.len)+ copy[0] = 72+ print(bytes_to_str(copy))++ var slice = bytes_slice(copy, 1, 4)+ print(bytes_to_str(slice))++ var text = "hello"+ print(text.len)+ print(text[1])+ print(text + " world")+ print(str_slice(text, 1, 4))+ print(text == "hello")+}--- /dev/null+++ b/samples/baseline_2.9/float_bitwise_error.hz@@ -0,0 +1,5 @@+// Bitwise operators require integers.++fn main() {+ print(1.5 & 1.0)+}--- /dev/null+++ b/samples/baseline_2.9/float_mod_error.hz@@ -0,0 +1,5 @@+// Remainder is not defined for f64.++fn main() {+ print(1.5 % 1.0)+}--- /dev/null+++ b/samples/baseline_2.9/floats.hz@@ -0,0 +1,30 @@+// f64 literals, arithmetic, comparisons, records, lists, and numeric casts.++type Vec {+ x f64+ y f64+}++fn add(a f64, b f64) f64 => a + b++fn main() {+ var v = Vec{ x = 1.5, y = 2.5 }+ print(v.x)+ print(v.y)+ print(add(v.x, v.y))++ var values = [1.0, 2.5, 3.75]+ print(values[2])++ print(1.5 + 2.25)+ print(3.0 * 1.5)+ print(1.0 / 4.0)+ print(-1.5)+ print(1.5 < 2.5)+ print(1.5 == 1.5)++ var count u64 = 3+ print(cast[f64](count))+ print(cast[u64](3.9))+ print(cast[i32](3.9))+}--- /dev/null+++ b/samples/baseline_2.9/str_immutable_error.hz@@ -0,0 +1,7 @@+// Strings are immutable; mutate their bytes instead.++fn main() {+ var text = "hello"+ text[0] = 72+ print(text)+}--- /dev/null+++ b/samples/baseline_2.9/unsigned.hz@@ -0,0 +1,29 @@+// Unsigned integers, bitwise operators, shifts, and mixed-width casts.++fn main() {+ var a u8 = 255+ var b u16 = 65535+ var c u32 = 4294967295+ var d u64 = 18446744073709551615++ print(a)+ print(b)+ print(c)+ print(d)++ print(cast[u32](-1))+ print(trunc[u8](300))+ print(c / 3)+ print(d >> 32)++ print(a & 15)+ print(a | 1)+ print(a ^ 3)+ print(a << 2)+ print(a >> 2)+ print(~a)++ print(a < b)+ print(c > b)+ print(d == 18446744073709551615)+}--- /dev/null+++ b/samples/baseline_2.9/unsigned_negative_error.hz@@ -0,0 +1,6 @@+// Negative constants do not fit in unsigned types.++fn main() {+ var value u16 = -1+ print(value)+}--- /dev/null+++ b/samples/baseline_2.9/unsigned_overflow_error.hz@@ -0,0 +1,6 @@+// 256 does not fit in u8.++fn main() {+ var value u8 = 256+ print(value)+}--- /dev/null+++ b/samples/baseline_2.9/unsigned_unary_minus_error.hz@@ -0,0 +1,6 @@+// Unsigned values cannot use unary minus.++fn main() {+ var value u32 = 1+ print(-value)+}--- a/src/codegen.cpp+++ b/src/codegen.cpp@@ -2,6 +2,7 @@ #include <cassert> #include <cctype>+#include <iomanip> #include <sstream> #include <utility> @@ -12,7 +13,7 @@ constexpr size_t kObjectHeader = 8; size_t slot_size(const TypePtr& t) {- return qbe_type(t) == "l" ? 8 : 4;+ return (qbe_type(t) == "l" || qbe_type(t) == "d") ? 8 : 4; } size_t align_up(size_t n, size_t a) {@@ -19,6 +20,16 @@ return (n + a - 1) / a * a; } +std::string double_literal(double value) {+ std::ostringstream ss;+ ss << std::setprecision(17) << value;+ std::string s = ss.str();+ if (s.find('.') == std::string::npos && s.find('e') == std::string::npos &&+ s.find('E') == std::string::npos)+ s += ".0";+ return "d_" + s;+}+ struct FieldLayout { size_t size = 0; std::vector<size_t> offsets;@@ -174,8 +185,10 @@ Value global_value(const Global& g) { const Expr& e = *g.expr; TypePtr ty = g.type ? g.type : e.ty;- if (e.kind == Expr::Kind::Int)- return Value{ty, std::to_string(e.ival), true};+ if (e.kind == Expr::Kind::Int) {+ std::string value = e.unsigned_literal ? std::to_string(e.uval) : std::to_string(e.ival);+ return Value{ty, value, true};+ } if (e.kind == Expr::Kind::Bool) return Value{ty, e.bval ? "1" : "0", true}; if (e.kind == Expr::Kind::String)@@ -404,7 +417,7 @@ std::string alloc_slot(TypePtr ty) { std::string s = "%s" + std::to_string(next_slot_++);- bool wide = qbe_type(ty) == "l";+ bool wide = qbe_type(ty) == "l" || qbe_type(ty) == "d"; allocs_.push_back(s + " =l alloc" + (wide ? "8" : "4") + " " + (wide ? "8" : "4")); return s; }@@ -411,7 +424,7 @@ Value load_slot(const std::string& slot, TypePtr ty) { std::string t = new_temp();- emit(t + " =" + qbe_type(ty) + " load" + (qbe_type(ty) == "l" ? "l" : "w") + " " + slot);+ emit(t + " =" + qbe_type(ty) + " load" + load_suffix(ty) + " " + slot); return Value{ty, t, true}; } @@ -432,7 +445,7 @@ std::string addr = new_temp(); emit(addr + " =l add " + base.s + ", " + std::to_string(offset)); std::string t = new_temp();- emit(t + " =" + qbe_type(ty) + " load" + (qbe_type(ty) == "l" ? "l" : "w") + " " + addr);+ emit(t + " =" + qbe_type(ty) + " load" + load_suffix(ty) + " " + addr); return Value{ty, t, true}; } @@ -440,9 +453,10 @@ if (!v.valid || !to || same_type(v.ty, to)) return v; if (is_int(v.ty) && is_int(to)) {- if (to->kind == Type::Kind::I64 && v.ty->kind != Type::Kind::I64) {+ if ((to->kind == Type::Kind::I64 || to->kind == Type::Kind::U64) &&+ int_rank(v.ty) < 4) { std::string t = new_temp();- emit(t + " =l extsw " + v.s);+ emit(t + " =l " + (is_signed_int(v.ty) ? "extsw " : "extuw ") + v.s); return Value{to, t, true}; } return Value{to, v.s, true};@@ -453,9 +467,14 @@ } std::string store_suffix(TypePtr ty) {- return qbe_type(ty) == "l" ? "l" : "w";+ std::string q = qbe_type(ty);+ return q == "l" ? "l" : (q == "d" ? "d" : "w"); } + std::string load_suffix(TypePtr ty) {+ return store_suffix(ty);+ }+ void store_local(const Local& l, Value v) { v = coerce(v, l.ty); emit("store" + store_suffix(l.ty) + " " + v.s + ", " + l.slot);@@ -710,8 +729,7 @@ std::string addr = new_temp(); emit(addr + " =l add %env, " + std::to_string(8 + i * 8)); std::string v = new_temp();- emit(v + " =" + qbe_type(c.type) + " load" +- (qbe_type(c.type) == "l" ? "l" : "w") + " " + addr);+ emit(v + " =" + qbe_type(c.type) + " load" + load_suffix(c.type) + " " + addr); declare_local(c.name, c.type, Value{c.type, v, true}); } }@@ -863,8 +881,12 @@ Value gen_expr(Expr& e) { switch (e.kind) {- case Expr::Kind::Int:- return Value{e.ty, std::to_string(e.ival), true};+ case Expr::Kind::Int: {+ std::string text = e.unsigned_literal ? std::to_string(e.uval) : std::to_string(e.ival);+ return Value{e.ty, text, true};+ }+ case Expr::Kind::Float:+ return Value{e.ty, double_literal(e.fval), true}; case Expr::Kind::Bool: return Value{e.ty, e.bval ? "1" : "0", true}; case Expr::Kind::String: {@@ -1025,11 +1047,18 @@ } if (e.kind == Expr::Kind::Index) { Value base = gen_expr(*e.lhs);- if (!base.ty || base.ty->kind != Type::Kind::List)- throw Error(e.pos, "index assignment requires a list"); Value idx = gen_expr(*e.rhs);+ Value wide = index_to_long(idx);+ if (base.ty && base.ty->kind == Type::Kind::Bytes) {+ std::string addr = new_temp();+ emit(addr + " =l add " + base.s + ", " + std::to_string(kObjectHeader + 16));+ std::string final_addr = new_temp();+ emit(final_addr + " =l add " + addr + ", " + wide.s);+ return Value{t_u8(), final_addr, true};+ }+ if (!base.ty || base.ty->kind != Type::Kind::List)+ throw Error(e.pos, "index assignment requires a list or bytes"); size_t stride = slot_size(base.ty->elem);- Value wide = coerce(idx, t_i64()); std::string off = new_temp(); emit(off + " =l mul " + wide.s + ", " + std::to_string(stride)); std::string addr = new_temp();@@ -1191,6 +1220,13 @@ Value base = gen_expr(*e.lhs); if (base.ty->kind == Type::Kind::List && e.text == "len") return load_at(base, kObjectHeader, t_i32());+ if (base.ty->kind == Type::Kind::Bytes && e.text == "len")+ return load_at(base, kObjectHeader, t_u64());+ if (base.ty->kind == Type::Kind::Str && e.text == "len") {+ std::string t = new_temp();+ emit(t + " =l call $haze_str_len(l " + base.s + ")");+ return Value{t_u64(), t, true};+ } if (base.ty->kind != Type::Kind::Record) throw Error(e.pos, "field access requires a record"); const TypePtr& rt = base.ty;@@ -1201,21 +1237,38 @@ throw Error(e.pos, "record '" + type_name(rt) + "' has no field '" + e.text + "'"); } + Value index_to_long(Value v) {+ if (qbe_type(v.ty) == "l")+ return Value{t_i64(), v.s, true};+ std::string t = new_temp();+ emit(t + " =l " + (is_unsigned_int(v.ty) ? "extuw " : "extsw ") + v.s);+ return Value{t_i64(), t, true};+ }+ Value gen_index(Expr& e) { Value base = gen_expr(*e.lhs); Value idx = gen_expr(*e.rhs);+ Value wide = index_to_long(idx);+ if (base.ty->kind == Type::Kind::Str || base.ty->kind == Type::Kind::Bytes) {+ std::string addr = new_temp();+ size_t data_offset = base.ty->kind == Type::Kind::Bytes ? kObjectHeader + 16 : 0;+ emit(addr + " =l add " + base.s + ", " + std::to_string(data_offset));+ std::string final_addr = new_temp();+ emit(final_addr + " =l add " + addr + ", " + wide.s);+ std::string t = new_temp();+ emit(t + " =w loadub " + final_addr);+ return Value{t_u8(), t, true};+ } TypePtr elem = base.ty->elem; size_t stride = slot_size(elem);- Value off_idx = coerce(idx, t_i64()); std::string off = new_temp();- emit(off + " =l mul " + off_idx.s + ", " + std::to_string(stride));+ emit(off + " =l mul " + wide.s + ", " + std::to_string(stride)); std::string addr = new_temp(); emit(addr + " =l add " + base.s + ", " + std::to_string(kObjectHeader + 8)); std::string final_addr = new_temp(); emit(final_addr + " =l add " + addr + ", " + off); std::string t = new_temp();- emit(t + " =" + qbe_type(elem) + " load" + (qbe_type(elem) == "l" ? "l" : "w") + " " +- final_addr);+ emit(t + " =" + qbe_type(elem) + " load" + load_suffix(elem) + " " + final_addr); return Value{elem, t, true}; } @@ -1302,36 +1355,103 @@ TypePtr to = e.ty; if (!v.valid || !to || same_type(v.ty, to)) return Value{to, v.s, v.valid};++ if (to->kind == Type::Kind::F64) {+ if (v.ty && v.ty->kind == Type::Kind::F64)+ return Value{to, v.s, true};+ if (is_int(v.ty)) {+ bool wide = qbe_type(v.ty) == "l";+ std::string op = is_unsigned_int(v.ty)+ ? (wide ? "ultof" : "uwtof")+ : (wide ? "sltof" : "swtof");+ std::string t = new_temp();+ emit(t + " =d " + op + " " + v.s);+ return Value{to, t, true};+ }+ return v;+ }++ if (v.ty && v.ty->kind == Type::Kind::F64 && is_int(to)) {+ std::string op = is_unsigned_int(to) ? "dtoui" : "dtosi";+ std::string t = new_temp();+ emit(t + " =" + qbe_type(to) + " " + op + " " + v.s);+ return gen_int_normalize(Value{to, t, true}, to);+ }++ if (!is_int(v.ty) || !is_int(to))+ return v;+ std::string src = v.s;- if (qbe_type(v.ty) == "l") {+ int from_rank = int_rank(v.ty);+ int to_rank = int_rank(to);++ if (from_rank > to_rank && qbe_type(v.ty) == "l") { std::string w = new_temp(); emit(w + " =w add " + src + ", 0"); src = w; }- std::string t = new_temp();- if (to->kind == Type::Kind::I64) {- emit(t + " =l extsw " + src);- return Value{to, t, true};++ if (to_rank > from_rank) {+ if (to_rank == 4) {+ std::string t = new_temp();+ emit(t + " =l " + (is_signed_int(v.ty) ? "extsw " : "extuw ") + src);+ return Value{to, t, true};+ }+ return Value{to, src, true}; }- if (to->kind == Type::Kind::I8) {- emit(t + " =w extsb " + src);- return Value{to, t, true};- }- if (to->kind == Type::Kind::I16) {- emit(t + " =w extsh " + src);- return Value{to, t, true};- }++ Value normalized{to, src, true};+ if (to->kind == Type::Kind::I8)+ return gen_int_normalize(normalized, to);+ if (to->kind == Type::Kind::I16)+ return gen_int_normalize(normalized, to);+ if (to->kind == Type::Kind::U8)+ return gen_int_normalize(normalized, to);+ if (to->kind == Type::Kind::U16)+ return gen_int_normalize(normalized, to); return Value{to, src, true}; } + Value gen_int_normalize(Value v, const TypePtr& to) {+ std::string src = v.s;+ if (qbe_type(to) == "w" && v.ty && qbe_type(v.ty) == "l") {+ std::string w = new_temp();+ emit(w + " =w add " + src + ", 0");+ src = w;+ }+ std::string op;+ if (to->kind == Type::Kind::I8)+ op = "extsb";+ else if (to->kind == Type::Kind::I16)+ op = "extsh";+ else if (to->kind == Type::Kind::U8)+ op = "extub";+ else if (to->kind == Type::Kind::U16)+ op = "extuh";+ else+ return Value{to, src, true};+ std::string t = new_temp();+ emit(t + " =w " + op + " " + src);+ return Value{to, t, true};+ }+ Value gen_unary(Expr& e) { Value v = gen_expr(*e.lhs); if (e.op == "-") { v = coerce(v, e.ty); std::string t = new_temp();- emit(t + " =" + qbe_type(e.ty) + " sub 0, " + v.s);+ if (e.ty && e.ty->kind == Type::Kind::F64)+ emit(t + " =d neg " + v.s);+ else+ emit(t + " =" + qbe_type(e.ty) + " sub 0, " + v.s); return Value{e.ty, t, true}; }+ if (e.op == "~") {+ v = coerce(v, e.ty);+ std::string t = new_temp();+ emit(t + " =" + qbe_type(e.ty) + " xor " + v.s + ", -1");+ return gen_int_normalize(Value{e.ty, t, true}, e.ty);+ } std::string t = new_temp(); emit(t + " =w ceqw " + v.s + ", 0"); return Value{t_bool(), t, true};@@ -1373,8 +1493,7 @@ std::string saddr2 = new_temp(); emit(saddr2 + " =l add " + saddr + ", " + off); std::string ev = new_temp();- emit(ev + " =" + qbe_type(elem) + " load" +- (qbe_type(elem) == "l" ? "l" : "w") + " " + saddr2);+ emit(ev + " =" + qbe_type(elem) + " load" + load_suffix(elem) + " " + saddr2); std::string dest_index = i.s; if (append) {@@ -1388,8 +1507,7 @@ emit(daddr + " =l add " + obj.s + ", " + std::to_string(kObjectHeader + 8)); std::string daddr2 = new_temp(); emit(daddr2 + " =l add " + daddr + ", " + doff);- emit(std::string("store") + (qbe_type(elem) == "l" ? "l" : "w") + " " + ev +- ", " + daddr2);+ emit(std::string("store") + store_suffix(elem) + " " + ev + ", " + daddr2); std::string next = new_temp(); emit(next + " =w add " + i.s + ", 1");@@ -1415,58 +1533,99 @@ b.ty->kind == Type::Kind::List) return gen_list_concat(a, b, e.ty->elem); + if (e.op == "+" && a.ty && b.ty && a.ty->kind == Type::Kind::Str &&+ b.ty->kind == Type::Kind::Str) {+ std::string t = new_temp();+ emit(t + " =l call $haze_str_concat(l " + a.s + ", l " + b.s + ")");+ return Value{t_str(), t, true};+ }+ if (e.op == "+" || e.op == "-" || e.op == "*" || e.op == "/" || e.op == "%") {- TypePtr ty = e.ty;+ TypePtr ty = e.operand_type ? e.operand_type : e.ty; a = coerce(a, ty); b = coerce(b, ty); std::string ins = e.op == "+" ? "add" : e.op == "-" ? "sub" : e.op == "*" ? "mul"- : e.op == "/" ? "div"- : "rem";+ : e.op == "/" ? (is_unsigned_int(ty) ? "udiv" : "div")+ : (is_unsigned_int(ty) ? "urem" : "rem"); std::string t = new_temp(); emit(t + " =" + qbe_type(ty) + " " + ins + " " + a.s + ", " + b.s); return Value{ty, t, true}; } + if (e.op == "&" || e.op == "|" || e.op == "^") {+ TypePtr ty = e.operand_type ? e.operand_type : e.ty;+ a = coerce(a, ty);+ b = coerce(b, ty);+ std::string ins = e.op == "&" ? "and" : (e.op == "|" ? "or" : "xor");+ std::string t = new_temp();+ emit(t + " =" + qbe_type(ty) + " " + ins + " " + a.s + ", " + b.s);+ return Value{ty, t, true};+ }++ if (e.op == "<<" || e.op == ">>") {+ TypePtr ty = e.operand_type ? e.operand_type : e.ty;+ a = coerce(a, ty);+ std::string ins = e.op == "<<" ? "shl" : (is_unsigned_int(ty) ? "shr" : "sar");+ std::string t = new_temp();+ emit(t + " =" + qbe_type(ty) + " " + ins + " " + a.s + ", " + b.s);+ return Value{ty, t, true};+ }+ if (e.op == "==" || e.op == "!=") {- TypePtr ty = a.ty;- if (ty && ty->kind != Type::Kind::Str) {- if (a.ty->kind == Type::Kind::I64 || b.ty->kind == Type::Kind::I64) {- a = coerce(a, t_i64());- b = coerce(b, t_i64());- ty = t_i64();- }+ TypePtr ty = e.operand_type;+ if (ty && ty->kind == Type::Kind::F64) {+ a = coerce(a, t_f64());+ b = coerce(b, t_f64());+ std::string t = new_temp();+ emit(t + " =w " + (e.op == "==" ? "ceqd " : "cned ") + a.s + ", " + b.s);+ return Value{t_bool(), t, true}; }- if (ty && ty->kind == Type::Kind::Str) {+ if (ty && is_int(ty)) {+ a = coerce(a, ty);+ b = coerce(b, ty); std::string t = new_temp();+ emit(t + " =w " + (e.op == "==" ? "ceq" : "cne") + qbe_type(ty) + " " ++ a.s + ", " + b.s);+ return Value{t_bool(), t, true};+ }+ if (a.ty && b.ty && a.ty->kind == Type::Kind::Str &&+ b.ty->kind == Type::Kind::Str) {+ std::string t = new_temp(); emit(t + " =w call $strcmp(l " + a.s + ", l " + b.s + ")"); std::string q = new_temp(); emit(q + " =w " + (e.op == "==" ? "ceqw " : "cnew ") + t + ", 0"); return Value{t_bool(), q, true}; }- std::string base = qbe_type(ty);- std::string ins = e.op == "==" ? "ceq" : "cne"; std::string t = new_temp();- emit(t + " =w " + ins + base + " " + a.s + ", " + b.s);+ emit(t + " =w " + (e.op == "==" ? "ceqw " : "cnew ") + a.s + ", " + b.s); return Value{t_bool(), t, true}; } - std::string ins;- if (e.op == "<")- ins = "cslt";- else if (e.op == "<=")- ins = "csle";- else if (e.op == ">")- ins = "csgt";- else- ins = "csge";- TypePtr ty = a.ty;- if (a.ty->kind == Type::Kind::I64 || b.ty->kind == Type::Kind::I64) {- a = coerce(a, t_i64());- b = coerce(b, t_i64());- ty = t_i64();+ TypePtr ty = e.operand_type;+ if (ty && ty->kind == Type::Kind::F64) {+ a = coerce(a, t_f64());+ b = coerce(b, t_f64());+ std::string ins = e.op == "<" ? "cltd"+ : e.op == "<=" ? "cled"+ : e.op == ">" ? "cgtd"+ : "cged";+ std::string t = new_temp();+ emit(t + " =w " + ins + " " + a.s + ", " + b.s);+ return Value{t_bool(), t, true}; }+ if (!ty)+ ty = a.ty;+ if (!ty)+ ty = t_i32();+ a = coerce(a, ty);+ b = coerce(b, ty);+ std::string prefix = is_unsigned_int(ty) ? "cu" : "cs";+ std::string ins = e.op == "<" ? prefix + "lt"+ : e.op == "<=" ? prefix + "le"+ : e.op == ">" ? prefix + "gt"+ : prefix + "ge"; std::string t = new_temp(); emit(t + " =w " + ins + qbe_type(ty) + " " + a.s + ", " + b.s); return Value{t_bool(), t, true};@@ -1718,13 +1877,35 @@ return Value{}; }; + if (name == "bytes_new")+ return call("haze_bytes_new", {t_u64()}, t_bytes());+ if (name == "bytes_from_str")+ return call("haze_bytes_from_str", {t_str()}, t_bytes());+ if (name == "bytes_to_str")+ return call("haze_bytes_to_str", {t_bytes()}, t_str());+ if (name == "bytes_append")+ return call("haze_bytes_append", {t_bytes(), t_u8()}, t_bytes());+ if (name == "bytes_slice")+ return call("haze_bytes_slice", {t_bytes(), t_u64(), t_u64()}, t_bytes());+ if (name == "str_slice")+ return call("haze_str_slice", {t_str(), t_u64(), t_u64()}, t_str());+ if (name == "bytes_ptr")+ return call("haze_bytes_ptr", {t_bytes()}, t_ptr());+ if (name == "print") { TypePtr t = args[0].ty; if (t->kind == Type::Kind::I8 || t->kind == Type::Kind::I16 || t->kind == Type::Kind::I32) return call("haze_print_i32", {t_i32()}, t_void());+ if (t->kind == Type::Kind::U8 || t->kind == Type::Kind::U16 ||+ t->kind == Type::Kind::U32)+ return call("haze_print_u32", {t_u32()}, t_void()); if (t->kind == Type::Kind::I64) return call("haze_print_i64", {t_i64()}, t_void());+ if (t->kind == Type::Kind::U64)+ return call("haze_print_u64", {t_u64()}, t_void());+ if (t->kind == Type::Kind::F64)+ return call("haze_print_f64", {t_f64()}, t_void()); if (t->kind == Type::Kind::Bool) return call("haze_print_bool", {t_bool()}, t_void()); return call("haze_print_str", {t_str()}, t_void());@@ -1809,8 +1990,10 @@ Value addr = gen_lvalue(*s.expr); Value v = gen_expr(*s.rhs); v = coerce(v, s.expr->ty);- emit(std::string("store") + store_suffix(s.expr->ty) + " " + v.s + ", " +- addr.s);+ bool byte_store = s.expr->kind == Expr::Kind::Index && s.expr->lhs->ty &&+ s.expr->lhs->ty->kind == Type::Kind::Bytes;+ emit(std::string("store") + (byte_store ? "b" : store_suffix(s.expr->ty)) ++ " " + v.s + ", " + addr.s); break; } Local* l = lookup_local(s.expr->text);@@ -1893,9 +2076,18 @@ void gen_for_range(Stmt& s) { Value lo = gen_expr(*s.lo); Value hi = gen_expr(*s.hi);- TypePtr it = lo.ty->kind == Type::Kind::I64 || (hi.ty && hi.ty->kind == Type::Kind::I64)- ? t_i64()- : t_i32();+ TypePtr it;+ if ((lo.ty && lo.ty->kind == Type::Kind::U64) ||+ (hi.ty && hi.ty->kind == Type::Kind::U64))+ it = t_u64();+ else if ((lo.ty && lo.ty->kind == Type::Kind::I64) ||+ (hi.ty && hi.ty->kind == Type::Kind::I64))+ it = t_i64();+ else if ((lo.ty && lo.ty->kind == Type::Kind::U32) ||+ (hi.ty && hi.ty->kind == Type::Kind::U32))+ it = t_u32();+ else+ it = t_i32(); lo = coerce(lo, it); hi = coerce(hi, it); @@ -1921,7 +2113,8 @@ Value iv = load_slot(i_slot, it); Value hv = load_slot(hi_slot, it); std::string c = new_temp();- emit(c + " =w " + (it->kind == Type::Kind::I64 ? "csltl " : "csltw ") + iv.s + ", " + hv.s);+ emit(c + " =w " + (is_unsigned_int(it) ? "cult" : "cslt") ++ (qbe_type(it) == "l" ? "l " : "w ") + iv.s + ", " + hv.s); branch(Value{t_bool(), c, true}, body, end); start_block(body);@@ -1933,7 +2126,7 @@ start_block(incr); iv = load_slot(i_slot, it);- std::string one = it->kind == Type::Kind::I64 ? "1" : "1";+ std::string one = "1"; std::string n = new_temp(); emit(n + " =" + qbe_type(it) + " add " + iv.s + ", " + one); emit("store" + store_suffix(it) + " " + n + ", " + i_slot);@@ -1986,8 +2179,7 @@ std::string final_addr = new_temp(); emit(final_addr + " =l add " + addr + ", " + off); std::string ev = new_temp();- emit(ev + " =" + qbe_type(elem) + " load" + (qbe_type(elem) == "l" ? "l" : "w") + " " +- final_addr);+ emit(ev + " =" + qbe_type(elem) + " load" + load_suffix(elem) + " " + final_addr); store_local(eleml, Value{elem, ev, true}); loops_.push_back(LoopCtx{end, incr});--- a/src/eval.cpp+++ b/src/eval.cpp@@ -10,9 +10,11 @@ namespace { struct ConstValue {- enum class Kind { Int, Bool, String };+ enum class Kind { Int, UInt, Float, Bool, String }; Kind kind = Kind::Int; long long integer = 0;+ unsigned long long uinteger = 0;+ double floating = 0.0; bool boolean = false; std::string string; @@ -20,6 +22,18 @@ ConstValue c; c.kind = Kind::Int; c.integer = v;+ return c;+ }+ static ConstValue from_uint(unsigned long long v) {+ ConstValue c;+ c.kind = Kind::UInt;+ c.uinteger = v;+ return c;+ }+ static ConstValue from_float(double v) {+ ConstValue c;+ c.kind = Kind::Float;+ c.floating = v; return c; } static ConstValue from_bool(bool v) {@@ -93,7 +107,7 @@ Env globals_; static bool is_int(const ConstValue& v) {- return v.kind == ConstValue::Kind::Int;+ return v.kind == ConstValue::Kind::Int || v.kind == ConstValue::Kind::UInt; } static bool is_bool(const ConstValue& v) { return v.kind == ConstValue::Kind::Bool;@@ -115,6 +129,18 @@ case ConstValue::Kind::Int: e.kind = Expr::Kind::Int; e.ival = v.integer;+ e.uval = static_cast<unsigned long long>(v.integer);+ e.unsigned_literal = false;+ break;+ case ConstValue::Kind::UInt:+ e.kind = Expr::Kind::Int;+ e.uval = v.uinteger;+ e.ival = static_cast<long long>(v.uinteger);+ e.unsigned_literal = v.uinteger > static_cast<unsigned long long>(LLONG_MAX);+ break;+ case ConstValue::Kind::Float:+ e.kind = Expr::Kind::Float;+ e.fval = v.floating; break; case ConstValue::Kind::Bool: e.kind = Expr::Kind::Bool;@@ -132,7 +158,11 @@ return std::nullopt; switch (e.kind) { case Expr::Kind::Int:+ if (e.unsigned_literal)+ return ConstValue::from_uint(e.uval); return ConstValue::from_int(e.ival);+ case Expr::Kind::Float:+ return ConstValue::from_float(e.fval); case Expr::Kind::Bool: return ConstValue::from_bool(e.bval); case Expr::Kind::String:@@ -148,9 +178,11 @@ if (!a) return std::nullopt; if (e.op == "-") {- if (!is_int(*a))- return std::nullopt;- return ConstValue::from_int(-a->integer);+ if (a->kind == ConstValue::Kind::Int)+ return ConstValue::from_int(-a->integer);+ if (a->kind == ConstValue::Kind::Float)+ return ConstValue::from_float(-a->floating);+ return std::nullopt; } if (!is_bool(*a)) return std::nullopt;@@ -158,9 +190,44 @@ } case Expr::Kind::Cast: { auto a = eval_expr(*e.lhs, env, depth + 1);- if (!a || !is_int(*a) || !e.ty)+ if (!a || !e.ty) return std::nullopt;- return ConstValue::from_int(truncate_int(a->integer, e.ty));+ if (e.ty->kind == Type::Kind::F64) {+ if (a->kind == ConstValue::Kind::Float)+ return *a;+ if (is_int(*a)) {+ double v = a->kind == ConstValue::Kind::UInt+ ? static_cast<double>(a->uinteger)+ : static_cast<double>(a->integer);+ return ConstValue::from_float(v);+ }+ return std::nullopt;+ }+ if (a->kind == ConstValue::Kind::Float) {+ if (!haze::is_int(e.ty))+ return std::nullopt;+ long long v = static_cast<long long>(a->floating);+ return ConstValue::from_int(truncate_int(v, e.ty));+ }+ if (!is_int(*a) || !haze::is_int(e.ty))+ return std::nullopt;+ bool target_unsigned = e.ty->kind == Type::Kind::U8 ||+ e.ty->kind == Type::Kind::U16 ||+ e.ty->kind == Type::Kind::U32 ||+ e.ty->kind == Type::Kind::U64;+ unsigned long long raw = a->kind == ConstValue::Kind::UInt+ ? a->uinteger+ : static_cast<unsigned long long>(a->integer);+ if (target_unsigned) {+ if (e.ty->kind == Type::Kind::U8)+ raw = static_cast<unsigned char>(raw);+ else if (e.ty->kind == Type::Kind::U16)+ raw = static_cast<unsigned short>(raw);+ else if (e.ty->kind == Type::Kind::U32)+ raw = static_cast<unsigned int>(raw);+ return ConstValue::from_uint(raw);+ }+ return ConstValue::from_int(truncate_int(static_cast<long long>(raw), e.ty)); } case Expr::Kind::Binary: return eval_binary(e, env, depth + 1);@@ -256,9 +323,32 @@ if (e.op == "==" || e.op == "!=") { bool eq = false;- if (is_int(*a) && is_int(*b))- eq = a->integer == b->integer;- else if (is_bool(*a) && is_bool(*b))+ if (is_int(*a) && is_int(*b)) {+ if (a->kind == ConstValue::Kind::UInt || b->kind == ConstValue::Kind::UInt) {+ unsigned long long x = a->kind == ConstValue::Kind::UInt+ ? a->uinteger+ : static_cast<unsigned long long>(a->integer);+ unsigned long long y = b->kind == ConstValue::Kind::UInt+ ? b->uinteger+ : static_cast<unsigned long long>(b->integer);+ eq = x == y;+ } else {+ eq = a->integer == b->integer;+ }+ } else if ((is_int(*a) || a->kind == ConstValue::Kind::Float) &&+ (is_int(*b) || b->kind == ConstValue::Kind::Float)) {+ double x = a->kind == ConstValue::Kind::Float+ ? a->floating+ : (a->kind == ConstValue::Kind::UInt+ ? static_cast<double>(a->uinteger)+ : static_cast<double>(a->integer));+ double y = b->kind == ConstValue::Kind::Float+ ? b->floating+ : (b->kind == ConstValue::Kind::UInt+ ? static_cast<double>(b->uinteger)+ : static_cast<double>(b->integer));+ eq = x == y;+ } else if (is_bool(*a) && is_bool(*b)) eq = a->boolean == b->boolean; else if (a->kind == ConstValue::Kind::String && b->kind == ConstValue::Kind::String)@@ -266,10 +356,77 @@ else return std::nullopt; return ConstValue::from_bool(e.op == "==" ? eq : !eq);+ }++ if ((a->kind == ConstValue::Kind::Float || b->kind == ConstValue::Kind::Float) &&+ (is_int(*a) || a->kind == ConstValue::Kind::Float) &&+ (is_int(*b) || b->kind == ConstValue::Kind::Float)) {+ double x = a->kind == ConstValue::Kind::Float+ ? a->floating+ : (a->kind == ConstValue::Kind::UInt+ ? static_cast<double>(a->uinteger)+ : static_cast<double>(a->integer));+ double y = b->kind == ConstValue::Kind::Float+ ? b->floating+ : (b->kind == ConstValue::Kind::UInt+ ? static_cast<double>(b->uinteger)+ : static_cast<double>(b->integer));+ if (e.op == "+")+ return ConstValue::from_float(x + y);+ if (e.op == "-")+ return ConstValue::from_float(x - y);+ if (e.op == "*")+ return ConstValue::from_float(x * y);+ if (e.op == "/")+ return ConstValue::from_float(x / y);+ if (e.op == "<")+ return ConstValue::from_bool(x < y);+ if (e.op == "<=")+ return ConstValue::from_bool(x <= y);+ if (e.op == ">")+ return ConstValue::from_bool(x > y);+ if (e.op == ">=")+ return ConstValue::from_bool(x >= y);+ return std::nullopt; } if (!is_int(*a) || !is_int(*b))+ return std::nullopt;++ if (a->kind == ConstValue::Kind::UInt || b->kind == ConstValue::Kind::UInt) {+ unsigned long long x = a->kind == ConstValue::Kind::UInt+ ? a->uinteger+ : static_cast<unsigned long long>(a->integer);+ unsigned long long y = b->kind == ConstValue::Kind::UInt+ ? b->uinteger+ : static_cast<unsigned long long>(b->integer);+ if (e.op == "+")+ return ConstValue::from_uint(x + y);+ if (e.op == "-")+ return ConstValue::from_uint(x - y);+ if (e.op == "*")+ return ConstValue::from_uint(x * y);+ if (e.op == "/") {+ if (y == 0)+ throw Error(e.pos, "division by zero");+ return ConstValue::from_uint(x / y);+ }+ if (e.op == "%") {+ if (y == 0)+ throw Error(e.pos, "remainder by zero");+ return ConstValue::from_uint(x % y);+ }+ if (e.op == "<")+ return ConstValue::from_bool(x < y);+ if (e.op == "<=")+ return ConstValue::from_bool(x <= y);+ if (e.op == ">")+ return ConstValue::from_bool(x > y);+ if (e.op == ">=")+ return ConstValue::from_bool(x >= y); return std::nullopt;+ }+ long long x = a->integer; long long y = b->integer; if (e.op == "+")--- a/src/formatter.cpp+++ b/src/formatter.cpp@@ -28,8 +28,9 @@ bool is_operator(const std::string& s) { static const std::vector<std::string> ops = {- "=>", "==", "!=", "<=", ">=", "&&", "||", "=", "+=", "-=", "*=", "/=",- "%=", "+", "-", "*", "/", "%", "<", ">", "..", "++", "--"};+ "=>", "==", "!=", "<=", ">=", "&&", "||", "<<", ">>", "=", "+=", "-=", "*=",+ "/=", "%=", "+", "-", "*", "/", "%", "<", ">", "&", "|", "^", "~",+ "..", "++", "--"}; for (auto& op : ops) if (s == op) return true;@@ -128,16 +129,40 @@ continue; } if (std::isdigit(static_cast<unsigned char>(c))) {- size_t start = i++;- while (i < line.size() &&- (std::isalnum(static_cast<unsigned char>(line[i])) || line[i] == '_'))- i++;+ size_t start = i;+ if (c == '0' && i + 1 < line.size() &&+ (line[i + 1] == 'x' || line[i + 1] == 'X')) {+ i += 2;+ while (i < line.size() &&+ (std::isxdigit(static_cast<unsigned char>(line[i])) || line[i] == '_'))+ i++;+ } else {+ while (i < line.size() &&+ (std::isdigit(static_cast<unsigned char>(line[i])) || line[i] == '_'))+ i++;+ if (i < line.size() && line[i] == '.' &&+ (i + 1 >= line.size() || line[i + 1] != '.')) {+ i++;+ while (i < line.size() &&+ (std::isdigit(static_cast<unsigned char>(line[i])) || line[i] == '_'))+ i++;+ }+ if (i < line.size() && (line[i] == 'e' || line[i] == 'E')) {+ i++;+ if (i < line.size() && (line[i] == '+' || line[i] == '-'))+ i++;+ while (i < line.size() &&+ (std::isdigit(static_cast<unsigned char>(line[i])) ||+ line[i] == '_'))+ i++;+ }+ } out.push_back(FmtToken{FmtToken::Kind::Number, line.substr(start, i - start)}); continue; } static const char* multi[] = {"=>", "==", "!=", "<=", ">=", "&&", "||", "+=", "-=", "*=", "/=", "%=", "++", "--",- "..", "::"};+ "<<", ">>", "..", "::"}; bool matched = false; for (auto op : multi) { size_t n = std::char_traits<char>::length(op);@@ -314,7 +339,9 @@ } bool token_is_unary(const std::vector<FmtToken>& tokens, size_t i) {- if (!tokens[i].text.empty() && tokens[i].text == "!")+ if (tokens[i].text == "!")+ return true;+ if (tokens[i].text == "~") return true; if (tokens[i].text != "-") return false;--- a/src/haze.h+++ b/src/haze.h@@ -43,9 +43,15 @@ I16, I32, I64,+ U8,+ U16,+ U32,+ U64,+ F64, Bool, Str, Ptr,+ Bytes, List, Record, Sum,@@ -76,9 +82,15 @@ TypePtr t_i16(); TypePtr t_i32(); TypePtr t_i64();+TypePtr t_u8();+TypePtr t_u16();+TypePtr t_u32();+TypePtr t_u64();+TypePtr t_f64(); TypePtr t_bool(); TypePtr t_str(); TypePtr t_ptr();+TypePtr t_bytes(); TypePtr t_list(TypePtr elem); TypePtr t_record(const std::string& name); TypePtr t_sum(const std::string& name);@@ -93,9 +105,13 @@ TypePtr t_unknown(const std::string& name); bool same_type(const TypePtr& a, const TypePtr& b); bool is_int(const TypePtr& t);+bool is_signed_int(const TypePtr& t);+bool is_unsigned_int(const TypePtr& t);+bool is_float(const TypePtr& t); bool is_narrow_int(const TypePtr& t); int int_rank(const TypePtr& t); long long truncate_int(long long v, const TypePtr& t);+bool int_constant_fits(long long v, const TypePtr& t); bool is_managed(const TypePtr& t); std::string type_name(const TypePtr& t); std::string qbe_type(const TypePtr& t);@@ -105,6 +121,7 @@ End, Ident, Int,+ Float, Str, Arrow, EqEq,@@ -118,6 +135,12 @@ Star, Slash, Percent,+ Amp,+ Pipe,+ Caret,+ Tilde,+ Shl,+ Shr, Assign, PlusEq, MinusEq,@@ -137,7 +160,6 @@ RBracket, Colon, ColonColon,- Pipe, Bang, Lt, Gt,@@ -149,6 +171,8 @@ Tok kind = Tok::End; std::string text; long long ival = 0;+ unsigned long long uval = 0;+ double fval = 0.0; Pos pos; }; @@ -182,6 +206,7 @@ struct Expr { enum class Kind { Int,+ Float, Bool, String, Var,@@ -205,6 +230,9 @@ Pos pos; TypePtr ty; long long ival = 0;+ unsigned long long uval = 0;+ double fval = 0.0;+ bool unsigned_literal = false; bool bval = false; std::string text; std::string op;@@ -223,6 +251,7 @@ Pattern pattern; TypePtr lit_type; TypePtr ctor_type;+ TypePtr operand_type; const Variant* ctor_variant = nullptr; int ctor_index = -1; };--- a/src/lexer.cpp+++ b/src/lexer.cpp@@ -145,6 +145,7 @@ if (std::isdigit(static_cast<unsigned char>(c))) { std::string s; int base = 10;+ bool is_float = false; if (c == '0' && (peek(1) == 'x' || peek(1) == 'X')) { s.push_back(take()); s.push_back(take());@@ -153,12 +154,38 @@ s.push_back(take()); } else { while (std::isdigit(static_cast<unsigned char>(peek())) || peek() == '_')+ s.push_back(take());+ if (peek() == '.' && peek(1) != '.') {+ is_float = true;+ s.push_back(take());+ while (std::isdigit(static_cast<unsigned char>(peek())) || peek() == '_')+ s.push_back(take());+ }+ if (peek() == 'e' || peek() == 'E') {+ is_float = true; s.push_back(take());+ if (peek() == '+' || peek() == '-')+ s.push_back(take());+ if (!std::isdigit(static_cast<unsigned char>(peek())))+ throw Error(p, "invalid floating-point literal");+ while (std::isdigit(static_cast<unsigned char>(peek())) || peek() == '_')+ s.push_back(take());+ } } std::string clean; for (char ch : s) if (ch != '_') clean.push_back(ch);+ if (is_float) {+ errno = 0;+ char* end = nullptr;+ double v = std::strtod(clean.c_str(), &end);+ if (errno || !end || *end)+ throw Error(p, "invalid floating-point literal");+ Token t = tok(Tok::Float, p);+ t.fval = v;+ return t;+ } errno = 0; char* end = nullptr; unsigned long long v = std::strtoull(clean.c_str(), &end, base);@@ -166,6 +193,7 @@ throw Error(p, "invalid integer literal"); Token t = tok(Tok::Int, p); t.ival = static_cast<long long>(v);+ t.uval = v; return t; } @@ -241,6 +269,10 @@ return tok(Tok::AndAnd, p); if (match("||")) return tok(Tok::OrOr, p);+ if (match("<<"))+ return tok(Tok::Shl, p);+ if (match(">>"))+ return tok(Tok::Shr, p); if (match("+=")) return tok(Tok::PlusEq, p); if (match("-="))@@ -272,6 +304,12 @@ return tok(Tok::Slash, p); case '%': return tok(Tok::Percent, p);+ case '&':+ return tok(Tok::Amp, p);+ case '^':+ return tok(Tok::Caret, p);+ case '~':+ return tok(Tok::Tilde, p); case '=': return tok(Tok::Assign, p); case '.':--- a/src/parser.cpp+++ b/src/parser.cpp@@ -1,6 +1,7 @@ #include "haze.h" #include <cctype>+#include <climits> #include <utility> namespace haze {@@ -130,6 +131,16 @@ return t_i32(); if (name == "i64") return t_i64();+ if (name == "u8")+ return t_u8();+ if (name == "u16")+ return t_u16();+ if (name == "u32")+ return t_u32();+ if (name == "u64")+ return t_u64();+ if (name == "f64")+ return t_f64(); if (name == "bool") return t_bool(); if (name == "str")@@ -136,6 +147,8 @@ return t_str(); if (name == "ptr") return t_ptr();+ if (name == "bytes")+ return t_bytes(); if (name == "void") return t_void(); if (name == "list") {@@ -961,6 +974,8 @@ e->kind = Expr::Kind::Int; e->pos = p; e->ival = cur().ival;+ e->uval = cur().uval;+ e->unsigned_literal = cur().uval > static_cast<unsigned long long>(LLONG_MAX); advance(); return e; }@@ -969,6 +984,14 @@ e->kind = Expr::Kind::String; e->pos = p; e->text = cur().text;+ advance();+ return e;+ }+ if (at(Tok::Float)) {+ auto e = std::make_unique<Expr>();+ e->kind = Expr::Kind::Float;+ e->pos = p;+ e->fval = cur().fval; advance(); return e; }@@ -1091,9 +1114,9 @@ } ExprPtr parse_unary() {- if (at(Tok::Minus) || at(Tok::Bang)) {+ if (at(Tok::Minus) || at(Tok::Bang) || at(Tok::Tilde)) { Pos p = cur().pos;- std::string op = at(Tok::Minus) ? "-" : "!";+ std::string op = at(Tok::Minus) ? "-" : (at(Tok::Bang) ? "!" : "~"); advance(); auto e = std::make_unique<Expr>(); e->kind = Expr::Kind::Unary;@@ -1137,8 +1160,19 @@ return l; } - ExprPtr parse_comparison() {+ ExprPtr parse_shift() { ExprPtr l = parse_additive();+ while (at(Tok::Shl) || at(Tok::Shr)) {+ Pos p = cur().pos;+ std::string op = at(Tok::Shl) ? "<<" : ">>";+ advance();+ l = make_binary(op, std::move(l), parse_additive(), p);+ }+ return l;+ }++ ExprPtr parse_comparison() {+ ExprPtr l = parse_shift(); while (at(Tok::Lt) || at(Tok::Le) || at(Tok::Gt) || at(Tok::Ge)) { Pos p = cur().pos; std::string op;@@ -1157,7 +1191,7 @@ break; } advance();- l = make_binary(op, std::move(l), parse_additive(), p);+ l = make_binary(op, std::move(l), parse_shift(), p); } return l; }@@ -1186,12 +1220,42 @@ return l; } - ExprPtr parse_and() {+ ExprPtr parse_bitand() { ExprPtr l = parse_equality();+ while (at(Tok::Amp)) {+ Pos p = cur().pos;+ advance();+ l = make_binary("&", std::move(l), parse_equality(), p);+ }+ return l;+ }++ ExprPtr parse_bitxor() {+ ExprPtr l = parse_bitand();+ while (at(Tok::Caret)) {+ Pos p = cur().pos;+ advance();+ l = make_binary("^", std::move(l), parse_bitand(), p);+ }+ return l;+ }++ ExprPtr parse_bitor() {+ ExprPtr l = parse_bitxor();+ while (at(Tok::Pipe)) {+ Pos p = cur().pos;+ advance();+ l = make_binary("|", std::move(l), parse_bitxor(), p);+ }+ return l;+ }++ ExprPtr parse_and() {+ ExprPtr l = parse_bitor(); while (at(Tok::AndAnd)) { Pos p = cur().pos; advance();- l = make_binary("&&", std::move(l), parse_equality(), p);+ l = make_binary("&&", std::move(l), parse_bitor(), p); } return l; }--- a/src/sema.cpp+++ b/src/sema.cpp@@ -99,6 +99,7 @@ e->pattern = src.pattern; e->lit_type = src.lit_type; e->ctor_type = src.ctor_type;+ e->operand_type = src.operand_type; e->ctor_variant = src.ctor_variant; e->ctor_index = src.ctor_index; if (src.block)@@ -194,6 +195,7 @@ bt = substitute_type(bt, subst); e.lit_type = substitute_type(e.lit_type, subst); e.ctor_type = substitute_type(e.ctor_type, subst);+ e.operand_type = substitute_type(e.operand_type, subst); if (e.ctor_type && e.ctor_variant) { std::string ctor_name = e.ctor_variant->name; for (size_t i = 0; i < e.ctor_type->variants.size(); i++)@@ -528,22 +530,58 @@ } static bool printable(const TypePtr& t) {- return t && (is_int(t) || t->kind == Type::Kind::Bool || t->kind == Type::Kind::Str);+ return t && (is_int(t) || is_float(t) || t->kind == Type::Kind::Bool ||+ t->kind == Type::Kind::Str); } TypePtr unify_int(const TypePtr& a, const TypePtr& b, Pos p) { if (!is_int(a) || !is_int(b)) throw Error(p, "integer operands required");- if (a->kind == Type::Kind::I64 || b->kind == Type::Kind::I64)- return t_i64();- if (is_narrow_int(a) || is_narrow_int(b))- return t_i32();- return t_i32();+ bool as = is_signed_int(a);+ bool bs = is_signed_int(b);+ int ar = int_rank(a);+ int br = int_rank(b);+ if (as == bs) {+ int rank = std::max(ar, br);+ if (rank < 3)+ rank = 3;+ if (as)+ return rank == 4 ? t_i64() : t_i32();+ return rank == 4 ? t_u64() : t_u32();+ }+ int srank = as ? ar : br;+ int urank = as ? br : ar;+ if (srank > urank) {+ if (srank < 3)+ srank = 3;+ return srank == 4 ? t_i64() : t_i32();+ }+ int needed = urank + 1;+ if (needed < 3)+ needed = 3;+ if (needed > 4)+ return t_u64();+ return needed == 4 ? t_i64() : t_i32();+ }++ static void numeric_pair(const TypePtr& a, const TypePtr& b, TypePtr& out, Pos p) {+ if (is_float(a) || is_float(b)) {+ if ((is_float(a) || is_int(a)) && (is_float(b) || is_int(b))) {+ out = t_f64();+ return;+ }+ throw Error(p, "numeric operands required");+ }+ if (!is_int(a) || !is_int(b))+ throw Error(p, "numeric operands required");+ out = nullptr; } static bool constant_int(const Expr& e, long long& out) { switch (e.kind) { case Expr::Kind::Int:+ if (e.unsigned_literal)+ return false; out = e.ival; return true; case Expr::Kind::Unary: {@@ -587,20 +625,36 @@ default: return false; }+ }++ static bool constant_unsigned(const Expr& e, unsigned long long& out) {+ if (e.kind != Expr::Kind::Int)+ return false;+ out = e.uval;+ return true; } static bool value_assignable(const TypePtr& to, const TypePtr& from, const Expr& value) { if (assignable(to, from)) return true;- if (is_int(to) && is_int(from) && int_rank(to) < int_rank(from)) {+ if (is_int(to) && is_int(from)) {+ if (to->kind == Type::Kind::U64) {+ unsigned long long u = 0;+ if (constant_unsigned(value, u))+ return true;+ } long long v = 0;- return constant_int(value, v) && truncate_int(v, to) == v;+ return constant_int(value, v) && int_constant_fits(v, to); } return false; } static bool constant_out_of_range(const TypePtr& to, const Expr& value, long long& v) {- return is_int(to) && constant_int(value, v) && truncate_int(v, to) != v;+ if (!is_int(to))+ return false;+ if (to->kind == Type::Kind::U64)+ return false;+ return constant_int(value, v) && !int_constant_fits(v, to); } void check_function(Function& fn) {@@ -759,6 +813,9 @@ throw Error(s.pos, "cannot assign through const '" + root->text + "'"); } TypePtr target = check_expr(*s.expr);+ if (s.expr->kind == Expr::Kind::Index && s.expr->lhs->ty &&+ s.expr->lhs->ty->kind == Type::Kind::Str)+ throw Error(s.pos, "str values are immutable"); TypePtr rhs = check_expr(*s.rhs); if (!value_assignable(target, rhs, *s.rhs)) throw Error(s.pos,@@ -1143,6 +1200,60 @@ } } return inst;+ }++ if (name == "bytes_new" || name == "bytes_from_str" || name == "bytes_to_str" ||+ name == "bytes_append" || name == "bytes_slice" || name == "str_slice" ||+ name == "bytes_ptr") {+ for (auto& a : e.args)+ check_expr(*a);+ auto require = [&](size_t count) {+ if (e.args.size() != count)+ throw Error(e.pos, name + " expects " + std::to_string(count) ++ " arguments");+ };+ if (name == "bytes_new") {+ require(1);+ if (!is_int(e.args[0]->ty))+ throw Error(e.pos, "bytes_new expects an integer length");+ return t_bytes();+ }+ if (name == "bytes_from_str") {+ require(1);+ if (e.args[0]->ty->kind != Type::Kind::Str)+ throw Error(e.pos, "bytes_from_str expects str");+ return t_bytes();+ }+ if (name == "bytes_to_str") {+ require(1);+ if (e.args[0]->ty->kind != Type::Kind::Bytes)+ throw Error(e.pos, "bytes_to_str expects bytes");+ return t_str();+ }+ if (name == "bytes_append") {+ require(2);+ if (e.args[0]->ty->kind != Type::Kind::Bytes || !is_int(e.args[1]->ty))+ throw Error(e.pos, "bytes_append expects (bytes, u8)");+ return t_bytes();+ }+ if (name == "bytes_slice") {+ require(3);+ if (e.args[0]->ty->kind != Type::Kind::Bytes || !is_int(e.args[1]->ty) ||+ !is_int(e.args[2]->ty))+ throw Error(e.pos, "bytes_slice expects (bytes, u64, u64)");+ return t_bytes();+ }+ if (name == "str_slice") {+ require(3);+ if (e.args[0]->ty->kind != Type::Kind::Str || !is_int(e.args[1]->ty) ||+ !is_int(e.args[2]->ty))+ throw Error(e.pos, "str_slice expects (str, u64, u64)");+ return t_str();+ }+ require(1);+ if (e.args[0]->ty->kind != Type::Kind::Bytes)+ throw Error(e.pos, "bytes_ptr expects bytes");+ return t_ptr(); } if (name == "print" || name == "__gc_alloc" || name == "__gc_store" ||@@ -1313,7 +1424,9 @@ TypePtr t; switch (e.kind) { case Expr::Kind::Int:- if (e.ival >= INT32_MIN && e.ival <= INT32_MAX)+ if (e.unsigned_literal)+ t = t_u64();+ else if (e.ival >= INT32_MIN && e.ival <= INT32_MAX) t = t_i32(); else t = t_i64();@@ -1323,6 +1436,9 @@ break; case Expr::Kind::String: t = t_str();+ break;+ case Expr::Kind::Float:+ t = t_f64(); break; case Expr::Kind::Var: { if (e.text == "None" && !ctors_.count("None") && !generic_ctors_.count("None")) {@@ -1382,9 +1498,19 @@ case Expr::Kind::Unary: { TypePtr a = check_expr(*e.lhs); if (e.op == "-") {+ if (is_float(a)) {+ t = t_f64();+ } else if (is_signed_int(a)) {+ t = is_narrow_int(a) ? t_i32() : a;+ } else if (is_unsigned_int(a)) {+ throw Error(e.pos, "unary '-' requires a signed integer or f64");+ } else {+ throw Error(e.pos, "unary '-' requires a signed integer or f64");+ }+ } else if (e.op == "~") { if (!is_int(a))- throw Error(e.pos, "unary '-' requires an integer");- t = is_narrow_int(a) ? t_i32() : a;+ throw Error(e.pos, "unary '~' requires an integer");+ t = a; } else { if (a->kind != Type::Kind::Bool) throw Error(e.pos, "unary '!' requires bool");@@ -1400,18 +1526,51 @@ throw Error(e.pos, "cannot concatenate " + type_name(a) + " and " + type_name(b)); t = a;+ } else if (e.op == "+" && a->kind == Type::Kind::Str &&+ b->kind == Type::Kind::Str) {+ t = t_str(); } else if (e.op == "+" || e.op == "-" || e.op == "*" || e.op == "/" || e.op == "%") {- t = unify_int(a, b, e.pos);+ TypePtr common;+ numeric_pair(a, b, common, e.pos);+ if (common) {+ if (e.op == "%")+ throw Error(e.pos, "remainder requires integers");+ t = common;+ } else {+ t = unify_int(a, b, e.pos);+ }+ e.operand_type = t; } else if (e.op == "<" || e.op == "<=" || e.op == ">" || e.op == ">=") {- unify_int(a, b, e.pos);+ TypePtr common;+ numeric_pair(a, b, common, e.pos);+ if (!common)+ common = unify_int(a, b, e.pos);+ e.operand_type = common; t = t_bool(); } else if (e.op == "==" || e.op == "!=") {- if (!(is_int(a) && is_int(b)) &&- !(a->kind == Type::Kind::Bool && b->kind == Type::Kind::Bool) &&- !(a->kind == Type::Kind::Str && b->kind == Type::Kind::Str))+ if (is_int(a) && is_int(b)) {+ e.operand_type = unify_int(a, b, e.pos);+ } else if ((is_float(a) || is_int(a)) && (is_float(b) || is_int(b))) {+ TypePtr common;+ numeric_pair(a, b, common, e.pos);+ e.operand_type = common;+ } else if (!(a->kind == Type::Kind::Bool && b->kind == Type::Kind::Bool) &&+ !(a->kind == Type::Kind::Str && b->kind == Type::Kind::Str)) { throw Error(e.pos, "operands are not comparable");+ } t = t_bool();+ } else if (e.op == "&" || e.op == "|" || e.op == "^") {+ t = unify_int(a, b, e.pos);+ e.operand_type = t;+ } else if (e.op == "<<" || e.op == ">>") {+ if (!is_int(a) || !is_int(b))+ throw Error(e.pos, "shift operands must be integers");+ if (is_narrow_int(a))+ t = is_signed_int(a) ? t_i32() : t_u32();+ else+ t = a;+ e.operand_type = t; } else if (e.op == "&&" || e.op == "||") { if (a->kind != Type::Kind::Bool || b->kind != Type::Kind::Bool) throw Error(e.pos, "logical operands must be bool");@@ -1459,17 +1618,18 @@ throw Error(e.pos, "cannot cast between str and " + type_name(src->kind == Type::Kind::Str ? e.ty : src) + ": strings are managed values");- if (!is_int(src) || !is_int(e.ty))- throw Error(e.pos, "cast requires integer source and target, got " +- type_name(src) + " and " + type_name(e.ty));+ if (!is_int(src) && !is_float(src))+ throw Error(e.pos, "cast requires numeric source, got " + type_name(src));+ if (!is_int(e.ty) && !is_float(e.ty))+ throw Error(e.pos, "cast requires numeric target, got " + type_name(e.ty)); bool trunc = e.op == "trunc";- int from_rank = int_rank(src);- int to_rank = int_rank(e.ty);- if (trunc && to_rank >= from_rank)+ if (trunc && (!is_int(src) || !is_int(e.ty)))+ throw Error(e.pos, "trunc requires integer source and target");+ if (trunc && int_rank(e.ty) >= int_rank(src)) throw Error(e.pos, "trunc target " + type_name(e.ty) + " must be narrower than " + type_name(src) + "; use cast for widening");- if (!trunc && to_rank < from_rank)+ if (!trunc && is_int(src) && is_int(e.ty) && int_rank(e.ty) < int_rank(src)) throw Error(e.pos, "narrowing cast from " + type_name(src) + " to " + type_name(e.ty) + " requires trunc"); t = e.ty;@@ -1649,8 +1809,15 @@ TypePtr base = check_expr(*e.lhs); if (base->kind == Type::Kind::List && e.text == "len") { t = t_i32();+ break;+ }+ if ((base->kind == Type::Kind::Str || base->kind == Type::Kind::Bytes) &&+ e.text == "len") {+ t = t_u64(); break; }+ if (base->kind == Type::Kind::Str || base->kind == Type::Kind::Bytes)+ throw Error(e.pos, "field access requires a record"); if (base->kind != Type::Kind::Record) throw Error(e.pos, "field access requires a record"); const Field* field = nullptr;@@ -1668,9 +1835,13 @@ TypePtr idx = check_expr(*e.rhs); if (!is_int(idx)) throw Error(e.rhs->pos, "list index must be an integer");- if (base->kind != Type::Kind::List)- throw Error(e.pos, "indexing requires a list");- t = base->elem;+ if (base->kind == Type::Kind::List) {+ t = base->elem;+ } else if (base->kind == Type::Kind::Str || base->kind == Type::Kind::Bytes) {+ t = t_u8();+ } else {+ throw Error(e.pos, "indexing requires a list, str, or bytes");+ } break; } case Expr::Kind::Assign:--- a/src/types.cpp+++ b/src/types.cpp@@ -1,5 +1,6 @@ #include "haze.h" +#include <climits> #include <unordered_map> namespace haze {@@ -26,6 +27,21 @@ } TypePtr t_i64() { return make(Type::Kind::I64);+}+TypePtr t_u8() {+ return make(Type::Kind::U8);+}+TypePtr t_u16() {+ return make(Type::Kind::U16);+}+TypePtr t_u32() {+ return make(Type::Kind::U32);+}+TypePtr t_u64() {+ return make(Type::Kind::U64);+}+TypePtr t_f64() {+ return make(Type::Kind::F64); } TypePtr t_bool() { return make(Type::Kind::Bool);@@ -35,6 +51,9 @@ } TypePtr t_ptr() { return make(Type::Kind::Ptr);+}+TypePtr t_bytes() {+ return make(Type::Kind::Bytes); } TypePtr t_list(TypePtr elem) {@@ -265,12 +284,29 @@ } bool is_int(const TypePtr& t) {+ return t && (t->kind == Type::Kind::I8 || t->kind == Type::Kind::I16 ||+ t->kind == Type::Kind::I32 || t->kind == Type::Kind::I64 ||+ t->kind == Type::Kind::U8 || t->kind == Type::Kind::U16 ||+ t->kind == Type::Kind::U32 || t->kind == Type::Kind::U64);+}++bool is_signed_int(const TypePtr& t) { return t && (t->kind == Type::Kind::I8 || t->kind == Type::Kind::I16 || t->kind == Type::Kind::I32 || t->kind == Type::Kind::I64);+}++bool is_unsigned_int(const TypePtr& t) {+ return t && (t->kind == Type::Kind::U8 || t->kind == Type::Kind::U16 ||+ t->kind == Type::Kind::U32 || t->kind == Type::Kind::U64);+}++bool is_float(const TypePtr& t) {+ return t && t->kind == Type::Kind::F64; } bool is_narrow_int(const TypePtr& t) {- return t && (t->kind == Type::Kind::I8 || t->kind == Type::Kind::I16);+ return t && (t->kind == Type::Kind::I8 || t->kind == Type::Kind::I16 ||+ t->kind == Type::Kind::U8 || t->kind == Type::Kind::U16); } int int_rank(const TypePtr& t) {@@ -278,12 +314,16 @@ return 0; switch (t->kind) { case Type::Kind::I8:+ case Type::Kind::U8: return 1; case Type::Kind::I16:+ case Type::Kind::U16: return 2; case Type::Kind::I32:+ case Type::Kind::U32: return 3; case Type::Kind::I64:+ case Type::Kind::U64: return 4; default: return 0;@@ -300,16 +340,47 @@ return static_cast<int16_t>(static_cast<unsigned short>(v)); case Type::Kind::I32: return static_cast<int32_t>(static_cast<unsigned int>(v));+ case Type::Kind::U8:+ return static_cast<unsigned char>(v);+ case Type::Kind::U16:+ return static_cast<unsigned short>(v);+ case Type::Kind::U32:+ return static_cast<unsigned int>(v); default: return v;+ }+}++bool int_constant_fits(long long v, const TypePtr& t) {+ if (!t)+ return false;+ switch (t->kind) {+ case Type::Kind::I8:+ return v >= -128 && v <= 127;+ case Type::Kind::I16:+ return v >= -32768 && v <= 32767;+ case Type::Kind::I32:+ return v >= INT32_MIN && v <= INT32_MAX;+ case Type::Kind::I64:+ return true;+ case Type::Kind::U8:+ return v >= 0 && v <= 255;+ case Type::Kind::U16:+ return v >= 0 && v <= 65535;+ case Type::Kind::U32:+ return v >= 0 && v <= static_cast<long long>(UINT32_MAX);+ case Type::Kind::U64:+ return v >= 0;+ default:+ return false; } } bool is_managed(const TypePtr& t) { return t && (t->kind == Type::Kind::Ptr || t->kind == Type::Kind::List ||- t->kind == Type::Kind::Record || t->kind == Type::Kind::Sum ||- t->kind == Type::Kind::Option || t->kind == Type::Kind::Result ||- t->kind == Type::Kind::Function);+ t->kind == Type::Kind::Bytes || t->kind == Type::Kind::Record ||+ t->kind == Type::Kind::Sum || t->kind == Type::Kind::Option ||+ t->kind == Type::Kind::Result || t->kind == Type::Kind::Function); } std::string type_name(const TypePtr& t) {@@ -326,6 +397,16 @@ return "i32"; case Type::Kind::I64: return "i64";+ case Type::Kind::U8:+ return "u8";+ case Type::Kind::U16:+ return "u16";+ case Type::Kind::U32:+ return "u32";+ case Type::Kind::U64:+ return "u64";+ case Type::Kind::F64:+ return "f64"; case Type::Kind::Bool: return "bool"; case Type::Kind::Str:@@ -332,6 +413,8 @@ return "str"; case Type::Kind::Ptr: return "ptr";+ case Type::Kind::Bytes:+ return "bytes"; case Type::Kind::List: return "list[" + type_name(t->elem) + "]"; case Type::Kind::Option:@@ -374,8 +457,10 @@ return "w"; switch (t->kind) { case Type::Kind::I64:+ case Type::Kind::U64: case Type::Kind::Str: case Type::Kind::Ptr:+ case Type::Kind::Bytes: case Type::Kind::List: case Type::Kind::Record: case Type::Kind::Sum:@@ -384,6 +469,8 @@ case Type::Kind::Function: case Type::Kind::Param: return "l";+ case Type::Kind::F64:+ return "d"; default: return "w"; }@@ -395,6 +482,8 @@ if (same_type(to, from)) return true; if (is_int(to) && is_int(from) && int_rank(to) > int_rank(from))+ return is_signed_int(to) == is_signed_int(from);+ if (is_float(to) && is_float(from)) return true; if (to->kind == Type::Kind::Option && from->kind == Type::Kind::Option) return same_type(to->elem, from->elem) || from->elem->kind == Type::Kind::Unknown ||