hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

qtype.ha (2032B)


      1 // SPDX-License-Identifier: GPL-3.0-only
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use fmt;
      5 use hare::types;
      6 use hare::types::{builtin};
      7 
      8 type stype = enum rune {
      9 	BYTE = 'b',
     10 	HALF = 'h',
     11 	WORD = 'w',
     12 	LONG = 'l',
     13 	SINGLE = 's',
     14 	DOUBLE = 'd',
     15 };
     16 
     17 type qcompound = struct {
     18 	name: str,
     19 	fields: []qtype_field,
     20 	base: const *types::_type,
     21 };
     22 
     23 type qtype = (stype | qcompound | void);
     24 
     25 type qtype_field = struct {
     26 	qtype: const *qtype,
     27 	count: size,
     28 };
     29 
     30 // Singletons
     31 const qbyte: qtype = stype::BYTE;
     32 const qhalf: qtype = stype::HALF;
     33 const qword: qtype = stype::WORD;
     34 const qlong: qtype = stype::LONG;
     35 const qsingle: qtype = stype::SINGLE;
     36 const qdouble: qtype = stype::DOUBLE;
     37 
     38 fn qtype_lookup(
     39 	ctx: *context,
     40 	_type: const *types::_type,
     41 	extype: bool,
     42 ) const *qtype = {
     43 	match (_type.repr) {
     44 	case let bi: builtin =>
     45 		switch (bi) {
     46 		case builtin::I8, builtin::U8 =>
     47 			return if (extype) &qbyte else &qword;
     48 		case builtin::I16, builtin::U16 =>
     49 			return if (extype) &qhalf else &qword;
     50 		case builtin::BOOL =>
     51 			return &qword;
     52 		case builtin::F32 =>
     53 			return &qsingle;
     54 		case builtin::F64 =>
     55 			return &qdouble;
     56 		case builtin::I32, builtin::U32, builtin::RUNE =>
     57 			return &qword;
     58 		case builtin::I64, builtin::U64 =>
     59 			return &qlong;
     60 		case builtin::INT, builtin::UINT, builtin::NULL,
     61 			builtin::UINTPTR, builtin::SIZE =>
     62 			switch (_type.sz) {
     63 			case 4 =>
     64 				return &qword;
     65 			case 8 =>
     66 				return &qlong;
     67 			case => abort();
     68 			};
     69 		case builtin::STR =>
     70 			return qtype_aggregate(ctx, _type);
     71 		case builtin::VOID => abort();
     72 		case => abort(); // TODO
     73 		};
     74 	case => abort();
     75 	};
     76 };
     77 
     78 fn qtype_aggregate(ctx: *context, _type: const *types::_type) const *qtype = {
     79 	abort(); // TODO
     80 };
     81 
     82 fn qtype_repr(qtype: const *qtype) const str = {
     83 	// XXX: This dereference should not be necessary after the match
     84 	// overhaul
     85 	match (*qtype) {
     86 	case let st: stype =>
     87 		static let buf: [1]u8 = [0...];
     88 		return fmt::bsprintf(buf, "{}", st: rune);
     89 	case let qc: qcompound => abort(); // TODO
     90 	case void =>
     91 		return "";
     92 	};
     93 };