Skip to documentation
SLOP

tiny.chant.ast.types.numeric

Reference tiny.chant ast types numeric

Defined in ast.types.

API (5)

Actions

Public operations.

No direct callersNo direct callsast.typesnumeric
Static calls · unresolved targets: unknown · external targets: unknown.

Source

Called byCallstest sourcelib.chant.src.ast.type.numerictest: usual arithmetic conversions pr...private sourcelib.chant.src.ast.type.numericarithmeticIntegerTypeast.types.numericintegerRankprivate sourcelib.chant.src.ast.type.numericpromoteIntegerast.types.numericcommonArithmetic
Static calls · unresolved targets: 1 · external targets: 0.
Called byCallsast.types.numericcommonArithmeticprivate sourcelib.chant.src.ast.type.numericpromoteIntegerprivate sourcelib.chant.src.ast.type.numericbitintRankprivate sourcelib.chant.src.ast.type.numericstandardRankast.types.numericintegerRank
Static calls · unresolved targets: 0 · external targets: 0.
Called byCallsNo direct callersast.types.numericisFloatast.types.numericisIntegerast.types.numericisArithmetic
Static calls · unresolved targets: 0 · external targets: 0.
Called byCallsNo direct callsast.types.numericisArithmeticast.types.numericisFloat
Static calls · unresolved targets: 0 · external targets: 0.
Called byCallsNo direct callsast.types.numericisArithmeticprivate sourcelib.chant.src.ast.type.numericpromoteIntegerast.types.numericisInteger
Static calls · unresolved targets: 0 · external targets: 0.

Source: lib/chant/src/ast/type/numeric.zig

zig
const std = @import("std");const types = @import("types.zig");const Kind = types.Kind;const Type = types.Type;pub fn isInteger(c_type: *const Type) bool {    return switch (c_type.kind) {        .char_type, .short_type, .int_type, .long_type, .bitint_type, .enum_type => true,        else => false,    };}pub fn isFloat(c_type: *const Type) bool {    return switch (c_type.kind) {        .float_type, .double_type, .decimal32_type, .decimal64_type, .decimal128_type => true,        else => false,    };}pub fn isArithmetic(c_type: *const Type) bool {    return isInteger(c_type) or isFloat(c_type);}pub fn integerRank(c_type: *const Type) u32 {    return switch (c_type.kind) {        .char_type => standardRank(8),        .short_type => standardRank(16),        .int_type => standardRank(32),        .long_type => standardRank(64),        .bitint_type => bitintRank(c_type.bit_width),        .enum_type => integerRank(c_type.child orelse &types.int_type),        else => 0,    };}fn standardRank(width: u16) u32 {    return @as(u32, width) * 2 + 1;}fn bitintRank(width: u16) u32 {    return @as(u32, width) * 2;}pub fn commonArithmetic(lhs: *const Type, rhs: *const Type) *const Type {    const lhs_promoted = promoteInteger(arithmeticIntegerType(lhs));    const rhs_promoted = promoteInteger(arithmeticIntegerType(rhs));    if (lhs_promoted != lhs or rhs_promoted != rhs) return commonArithmetic(lhs_promoted, rhs_promoted);    if (lhs.kind == .decimal128_type or rhs.kind == .decimal128_type) return &types.decimal128_type;    if (lhs.kind == .decimal64_type or rhs.kind == .decimal64_type) return &types.decimal64_type;    if (lhs.kind == .decimal32_type or rhs.kind == .decimal32_type) return &types.decimal32_type;    if (lhs.kind == .double_type or rhs.kind == .double_type) return &types.double_type;    if (lhs.kind == .float_type or rhs.kind == .float_type) return &types.float_type;    const lhs_rank = integerRank(lhs);    const rhs_rank = integerRank(rhs);    const wider = if (lhs_rank >= rhs_rank) lhs else rhs;    if (wider.kind != .bitint_type and integerRank(wider) < integerRank(&types.int_type)) {        return &types.int_type;    }    if (integerRank(lhs) == integerRank(rhs) and (lhs.is_unsigned or rhs.is_unsigned)) {        return if (lhs.is_unsigned) lhs else rhs;    }    return wider;}fn promoteInteger(c_type: *const Type) *const Type {    if (c_type.kind == .bitint_type) return c_type;    if (isInteger(c_type) and integerRank(c_type) < integerRank(&types.int_type)) return &types.int_type;    return c_type;}fn arithmeticIntegerType(c_type: *const Type) *const Type {    if (c_type.kind == .enum_type) return c_type.child orelse &types.int_type;    return c_type;}test "usual arithmetic conversions prefer floats then rank" {    try std.testing.expectEqual(Kind.double_type, commonArithmetic(&types.int_type, &types.double_type).kind);    try std.testing.expectEqual(Kind.float_type, commonArithmetic(&types.float_type, &types.long_type).kind);    try std.testing.expectEqual(Kind.decimal64_type, commonArithmetic(&types.decimal32_type, &types.decimal64_type).kind);    try std.testing.expectEqual(Kind.decimal32_type, commonArithmetic(&types.double_type, &types.decimal32_type).kind);    try std.testing.expectEqual(Kind.long_type, commonArithmetic(&types.int_type, &types.long_type).kind);    try std.testing.expectEqual(Kind.int_type, commonArithmetic(&types.char_type, &types.short_type).kind);    try std.testing.expect(commonArithmetic(&types.uint_type, &types.int_type).is_unsigned);    const bitint17 = Type{ .kind = .bitint_type, .bit_width = 17 };    const bitint33 = Type{ .kind = .bitint_type, .bit_width = 33 };    const fixed_enum = Type{ .kind = .enum_type, .child = &types.uchar_type };    const wide_enum = Type{ .kind = .enum_type, .child = &types.uint_type };    try std.testing.expectEqual(Kind.bitint_type, commonArithmetic(&bitint17, &bitint33).kind);    try std.testing.expectEqual(@as(u16, 33), commonArithmetic(&bitint17, &bitint33).bit_width);    try std.testing.expectEqual(Kind.int_type, commonArithmetic(&bitint17, &types.int_type).kind);    try std.testing.expectEqual(Kind.int_type, commonArithmetic(&bitint17, &types.short_type).kind);    try std.testing.expectEqual(Kind.bitint_type, commonArithmetic(&bitint33, &types.int_type).kind);    try std.testing.expectEqual(Kind.int_type, commonArithmetic(&fixed_enum, &types.int_type).kind);    try std.testing.expectEqual(Kind.int_type, commonArithmetic(&wide_enum, &types.int_type).kind);    try std.testing.expect(commonArithmetic(&wide_enum, &types.int_type).is_unsigned);}

Source: lib/chant/src/ast/type/root.zig:7

zig
pub const numeric = numeric_mod;

Audit

Definitions6
Public names11
Members0
Version26.7.0
Revisiondaab053ee433