// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: MIT using System; using System.Collections.Generic; using Progmasoft.Tazel.Dsl.Common; namespace Progmasoft.Tazel.Dsl.Configuration; /// What a condition or a part of one stands for. public enum ConditionType { /// true or false. Boolean, /// A string. String, } /// A condition, or a part of one, of .if and .elif. public abstract record Condition { /// What it stands for. public abstract ConditionType Type { get; } /// Whether it names target_system, so it holds only once the platform is chosen. public abstract bool UsesTarget { get; } } /// true, false or a string in quotes. /// A bool or a string. public sealed record LiteralCondition(object Value) : Condition { /// public override ConditionType Type => Value is bool ? ConditionType.Boolean : ConditionType.String; /// public override bool UsesTarget => false; } /// host_system.os and the like. /// host_system or target_system. /// os, arch, abi or platform. public sealed record MemberCondition(string System, string Field) : Condition { /// public override ConditionType Type => ConditionType.String; /// public override bool UsesTarget => System == "target_system"; } /// defined("NAME") or env("NAME"). /// defined or env. /// The name of the environment variable. public sealed record CallCondition(string Function, string Name) : Condition { /// public override ConditionType Type => Function == "defined" ? ConditionType.Boolean : ConditionType.String; /// public override bool UsesTarget => false; } /// ! of a condition. /// The condition. public sealed record NotCondition(Condition Operand) : Condition { /// public override ConditionType Type => ConditionType.Boolean; /// public override bool UsesTarget => Operand.UsesTarget; } /// ==, !=, && or || of two conditions. /// The operator. /// Its left side. /// Its right side. public sealed record BinaryCondition(string Operator, Condition Left, Condition Right) : Condition { /// public override ConditionType Type => ConditionType.Boolean; /// public override bool UsesTarget => Left.UsesTarget || Right.UsesTarget; } /// A condition that cannot be read. public sealed class ConditionException : Exception { /// What is wrong. public ConditionException(string message) : base(message) { } /// Nothing to say. public ConditionException() { } /// What is wrong, and why. public ConditionException(string message, Exception inner) : base(message, inner) { } } /// What a condition sees of a system: host_system or target_system. /// linux, windows, macos, freebsd, openbsd or dragonfly. /// amd64 (x86-64) or aarch64 (ARM64), the same on every system. /// gnu, musl, msvc or default. /// The name of the platform, as --platform takes it, such as linux-amd64. public sealed record SystemFacts(string Os, string Arch, string Abi, string Platform) { /// The facts of . public static SystemFacts Of(Platform platform) => new( platform.GetOS() switch { OS.Linux => "linux", OS.Windows => "windows", OS.MacOS => "macos", OS.FreeBSD => "freebsd", OS.OpenBSD => "openbsd", _ => "dragonfly", }, platform.GetArch() == Common.Arch.X64 ? "amd64" : "aarch64", platform.GetOS() switch { OS.Linux => "gnu", OS.Windows => "msvc", _ => "default", }, platform.Name()); /// The value of . public string this[string field] => field switch { "os" => Os, "arch" => Arch, "abi" => Abi, _ => Platform, }; } /// Reads the text of a condition: the lexer and parser of the condition language in one. internal sealed class ConditionReader(string text) { private static readonly HashSet Fields = new(StringComparer.Ordinal) { "os", "arch", "abi", "platform" }; private int _position; public Condition Read() { if (text.Length == 0) { throw new ConditionException("the condition is missing"); } var condition = Or(); Space(); if (_position < text.Length) { // The usual slips of other languages get their own words. var rest = text[_position..]; throw new ConditionException(rest[0] switch { '=' => "'=' is not an operator of conditions; compare with ==", '&' => "'&' is not an operator of conditions; write &&", '|' => "'|' is not an operator of conditions; write ||", _ => $"'{rest}' cannot follow '{text[.._position].TrimEnd()}'", }); } return condition; } private Condition Or() { var left = And(); while (Take("||")) { left = new BinaryCondition("||", Booleans(left, "||"), Booleans(And(), "||")); } return left; } private Condition And() { var left = Equality(); while (Take("&&")) { left = new BinaryCondition("&&", Booleans(left, "&&"), Booleans(Equality(), "&&")); } return left; } private Condition Equality() { var left = Unary(); while (true) { var op = Take("==") ? "==" : Take("!=") ? "!=" : null; if (op is null) { return left; } var right = Unary(); if (left.Type != right.Type) { throw new ConditionException($"{op} compares a {Name(left.Type)} with a {Name(right.Type)}"); } left = new BinaryCondition(op, left, right); } } private Condition Unary() { Space(); if (_position < text.Length && text[_position] == '!' && !At("!=")) { _position++; return new NotCondition(Booleans(Unary(), "!")); } return Primary(); } private Condition Primary() { Space(); if (_position >= text.Length) { throw new ConditionException("the condition ends where a value should be"); } var letter = text[_position]; if (letter == '(') { _position++; var inner = Or(); return Take(")") ? inner : throw new ConditionException("a '(' has no ')'"); } if (letter is '"' or '\'' or '`') { return new LiteralCondition(Quoted()); } var name = Word(); return name switch { "" => throw new ConditionException($"'{letter}' is not an operator of conditions"), "true" => new LiteralCondition(true), "false" => new LiteralCondition(false), "host_system" or "target_system" => Member(name), "defined" or "env" => Call(name), _ => throw new ConditionException($"'{name}' is not a name conditions know" + (Cli.Suggestions.Nearest(name, ["true", "false", "host_system", "target_system", "defined", "env"]) is { } near ? $"; did you mean {near}?" : "")), }; } // host_system.os and the like, after its name. private MemberCondition Member(string name) { if (!Take(".")) { throw new ConditionException($"{name} needs a field: {name}.os, .arch, .abi or .platform"); } var field = Word(); return Fields.Contains(field) ? new MemberCondition(name, field) : throw new ConditionException($"{name}.{field} is not a field; the fields are os, arch, abi and platform"); } // defined("NAME") or env("NAME"), after its name. private CallCondition Call(string name) { if (!Take("(")) { throw new ConditionException($"{name} is called: {name}(\"NAME\")"); } Space(); if (_position >= text.Length || text[_position] is not ('"' or '\'' or '`')) { throw new ConditionException($"{name} takes the name of a variable in quotes"); } var variable = Quoted(); return Take(")") ? new CallCondition(name, variable) : throw new ConditionException($"{name}(\"{variable}\" has no ')'"); } private static Condition Booleans(Condition condition, string op) => condition.Type == ConditionType.Boolean ? condition : throw new ConditionException($"{op} takes true or false, and a string stands beside it; compare the string with == or !="); private static string Name(ConditionType type) => type == ConditionType.Boolean ? "boolean" : "string"; private string Quoted() { var quote = text[_position++]; var end = text.IndexOf(quote, _position); if (end < 0) { throw new ConditionException($"a string has no closing {quote}"); } var value = text[_position..end]; _position = end + 1; return value; } private string Word() { var begin = _position; while (_position < text.Length && (char.IsAsciiLetterOrDigit(text[_position]) || text[_position] == '_')) { _position++; } return text[begin.._position]; } private bool At(string what) => string.CompareOrdinal(text, _position, what, 0, what.Length) == 0; private bool Take(string what) { Space(); if (!At(what)) { return false; } _position += what.Length; return true; } private void Space() { while (_position < text.Length && char.IsWhiteSpace(text[_position])) { _position++; } } } /// Evaluates conditions against what the configuration sees. public static class ConditionEvaluator { /// /// The value of : true, false, or null when it needs target_system and /// is not chosen yet. && and || stop at their first side when it /// decides, so the second may need nothing that is unknown. /// public static bool? Evaluate( Condition condition, SystemFacts host, SystemFacts? target, IReadOnlyDictionary environment) { ArgumentNullException.ThrowIfNull(condition); return new Evaluator(host, target, environment).Value(condition) as bool?; } private sealed class Evaluator(SystemFacts host, SystemFacts? target, IReadOnlyDictionary environment) { // A bool, a string, or null for unknown. public object? Value(Condition condition) => condition switch { LiteralCondition literal => literal.Value, MemberCondition member => member.System == "host_system" ? host[member.Field] : target?[member.Field], CallCondition call => call.Function == "defined" ? environment.ContainsKey(call.Name) : environment.GetValueOrDefault(call.Name, ""), NotCondition not => Value(not.Operand) is bool truth ? !truth : null, BinaryCondition { Operator: "&&" or "||" } logical => Logical(logical), BinaryCondition binary => Compare(binary), _ => throw new ArgumentException($"no condition {condition.GetType().Name}", nameof(condition)), }; // && and ||: the first side decides when it can; an unknown first side decides only with the second. private bool? Logical(BinaryCondition binary) { var decides = binary.Operator == "||"; var left = Value(binary.Left) as bool?; if (left == decides) { return decides; } var right = Value(binary.Right) as bool?; return left is null ? (right == decides ? decides : null) : right; } private bool? Compare(BinaryCondition binary) { var (left, right) = (Value(binary.Left), Value(binary.Right)); return left is null || right is null ? null : Equals(left, right) == (binary.Operator == "=="); } } }