tiny.termtex.operator
Defined in tiny.termtex.
API (7)
Actions
Public operations.
Types and contracts
Public types and contracts.
Source
Source: lib/termtex/src/operator.zig
zig
const std = @import("std");const ast = @import("ast.zig");pub const StretchArrowShaft = enum { single, double, squiggle,};pub const StretchArrow = struct { left_head: ?[]const u8 = null, right_head: ?[]const u8 = null, shaft: StretchArrowShaft = .single, left_bar: bool = false,};const StretchArrowEntry = struct { value: []const u8, arrow: StretchArrow,};pub fn limitsBase(expr: *const ast.Expr) bool { switch (expr.*) { .operator => |value| return switch (value.limit_policy) { .auto => limitsBase(value.body), .limits => true, .nolimits => false, }, .annotation => |value| return value.kind == .overbrace or value.kind == .underbrace, else => {}, } const value = ast.textValue(expr) orelse return false; return limitsText(value);}pub fn limitsText(value: []const u8) bool { inline for (.{ "∑", "∏", "∐", "lim", "max", "min", "sup", "inf", }) |candidate| { if (std.mem.eql(u8, value, candidate)) return true; } return stretchArrowText(value) != null;}pub fn displayOperatorText(value: []const u8) bool { inline for (.{ "∑", "⨁", "⨂", "⨀", "∏", "∐", "∫", "∬", "∭", "∮", "⋀", "⋁", "⋃", "⋂", }) |candidate| { if (std.mem.eql(u8, value, candidate)) return true; } return false;}pub fn stretchArrowBase(expr: *const ast.Expr) ?StretchArrow { switch (expr.*) { .operator => |value| return switch (value.limit_policy) { .auto, .limits => stretchArrowBase(value.body), .nolimits => null, }, else => {}, } const value = ast.textValue(expr) orelse return null; return stretchArrowText(value);}pub fn stretchArrowText(value: []const u8) ?StretchArrow { inline for (stretch_arrows) |entry| { if (std.mem.eql(u8, value, entry.value)) return entry.arrow; } return null;}const stretch_arrows = [_]StretchArrowEntry{ .{ .value = "→", .arrow = .{ .right_head = "→" } }, .{ .value = "←", .arrow = .{ .left_head = "←" } }, .{ .value = "↔", .arrow = .{ .left_head = "←", .right_head = "→" } }, .{ .value = "⇒", .arrow = .{ .right_head = "⇒", .shaft = .double } }, .{ .value = "⇐", .arrow = .{ .left_head = "⇐", .shaft = .double } }, .{ .value = "⇔", .arrow = .{ .left_head = "⇐", .right_head = "⇒", .shaft = .double } }, .{ .value = "⟶", .arrow = .{ .right_head = "→" } }, .{ .value = "⟵", .arrow = .{ .left_head = "←" } }, .{ .value = "⟷", .arrow = .{ .left_head = "←", .right_head = "→" } }, .{ .value = "⟹", .arrow = .{ .right_head = "⇒", .shaft = .double } }, .{ .value = "⟸", .arrow = .{ .left_head = "⇐", .shaft = .double } }, .{ .value = "⟺", .arrow = .{ .left_head = "⇐", .right_head = "⇒", .shaft = .double } }, .{ .value = "↦", .arrow = .{ .right_head = "→", .left_bar = true } }, .{ .value = "⟼", .arrow = .{ .right_head = "→", .left_bar = true } }, .{ .value = "↪", .arrow = .{ .right_head = "↪" } }, .{ .value = "↩", .arrow = .{ .left_head = "↩" } }, .{ .value = "↠", .arrow = .{ .right_head = "↠" } }, .{ .value = "↞", .arrow = .{ .left_head = "↞" } }, .{ .value = "⇀", .arrow = .{ .right_head = "⇀" } }, .{ .value = "⇁", .arrow = .{ .right_head = "⇁" } }, .{ .value = "↼", .arrow = .{ .left_head = "↼" } }, .{ .value = "↽", .arrow = .{ .left_head = "↽" } }, .{ .value = "⇌", .arrow = .{ .left_head = "↼", .right_head = "⇁", .shaft = .double } }, .{ .value = "↝", .arrow = .{ .right_head = "↝", .shaft = .squiggle } }, .{ .value = "⇝", .arrow = .{ .right_head = "⇝", .shaft = .squiggle } }, .{ .value = "⊸", .arrow = .{ .right_head = "⊸" } },};test "detect limit-bearing operator bases" { var sum = ast.Expr{ .text = "∑" }; var lim = ast.Expr{ .text = "lim" }; var x = ast.Expr{ .text = "x" }; const limited = ast.Expr{ .operator = .{ .body = &x, .limit_policy = .limits } }; const nolimits = ast.Expr{ .operator = .{ .body = &sum, .limit_policy = .nolimits } }; try std.testing.expect(limitsBase(&sum)); try std.testing.expect(limitsBase(&lim)); try std.testing.expect(!limitsBase(&x)); try std.testing.expect(limitsBase(&limited)); try std.testing.expect(!limitsBase(&nolimits)); try std.testing.expect(limitsText("max")); try std.testing.expect(displayOperatorText("∑")); try std.testing.expect(displayOperatorText("∫")); try std.testing.expect(!displayOperatorText("lim"));}test "detect stretchable arrow bases" { var right = ast.Expr{ .text = "→" }; var mapsto = ast.Expr{ .text = "↦" }; var sum = ast.Expr{ .text = "∑" }; const nolimits = ast.Expr{ .operator = .{ .body = &right, .limit_policy = .nolimits } }; try std.testing.expect(stretchArrowBase(&right) != null); try std.testing.expect(stretchArrowBase(&mapsto).?.left_bar); try std.testing.expect(stretchArrowBase(&sum) == null); try std.testing.expect(stretchArrowBase(&nolimits) == null);}Source: lib/termtex/src/root.zig:14
zig
pub const operator = @import("operator.zig");Audit
| Definitions | 8 |
|---|---|
| Public names | 8 |
| Members | 7 |
| Version | 26.7.0 |
| Revision | daab053ee433 |