Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Implement Firmament V2 let arithmetic expressions

Yuechen Li831e84834ca9
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentV2ParserTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentV2ParserTests.cs
index ba5971b2..62dbaecd 100644
--- a/Aetheris.Kernel.Firmament.Tests/FirmamentV2ParserTests.cs
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentV2ParserTests.cs
@@ -891,7 +891,7 @@ model BadPmiDiameter {
[InlineData("let draftAngle: angle = 3mm", FirmamentV2Parser.LetUnitMismatch)]
[InlineData("let scale: float = 1.25mm", FirmamentV2Parser.LetUnitMismatch)]
[InlineData("let unknownThing: banana = 1", FirmamentV2Parser.LetUnknownType)]
- [InlineData("let radius: length = holeDiameter / 2", FirmamentV2Parser.LetLiteralOnly)]
+ [InlineData("let radius: length = holeDiameter / 2", FirmamentV2Parser.ExpressionUnknownSymbol)]
[InlineData("let holeDiameter: length = 6.0mm tol 0.05mm", FirmamentV2Parser.LetInvalidLiteral)]
public void FirmamentV2Parser_LetPrimitiveLiterals_InvalidCasesAreDiagnostics(string letSource, string expectedDiagnostic)
{
@@ -979,6 +979,47 @@ model BadPmiDiameter {
AssertLet(Assert.Single(result.Document!.BoundLets!), "exportedHoleDiameter", FirmamentV2PrimitiveType.Length, 6.0d, "mm");
}
+ [Fact]
+ public void FirmamentV2Parser_LetArithmeticExpressions_EvaluateStrictTypedGraph()
+ {
+ var result = FirmamentV2Parser.Parse(Source("Language/valid/let-arithmetic-expressions.valid.firmfixture"));
+
+ Assert.True(result.IsSuccess, string.Join(", ", result.Diagnostics));
+ var bound = result.Document!.BoundLets!.ToDictionary(l => l.Name);
+ AssertLet(bound["radius"], "radius", FirmamentV2PrimitiveType.Length, 3.0d, "mm");
+ AssertLet(bound["drill"], "drill", FirmamentV2PrimitiveType.Length, 6.25d, "mm");
+ AssertLet(bound["doubled"], "doubled", FirmamentV2PrimitiveType.Int, 8, null);
+ AssertLet(bound["ratio"], "ratio", FirmamentV2PrimitiveType.Float, 2.0d, null);
+ AssertLet(bound["halfAngle"], "halfAngle", FirmamentV2PrimitiveType.Angle, 45.0d, "deg");
+ AssertLet(bound["scaled"], "scaled", FirmamentV2PrimitiveType.Length, 7.5d, "mm");
+ Assert.Contains("diameter", bound["radius"].Dependencies!);
+ }
+
+ [Theory]
+ [InlineData("let diameter: length = 6.0mm\nlet bad: length = diameter + 2", FirmamentV2Parser.ExpressionInvalidOperator)]
+ [InlineData("let diameter: length = 6.0mm\nlet draftAngle: angle = 90deg\nlet bad: length = diameter + draftAngle", FirmamentV2Parser.ExpressionInvalidOperator)]
+ [InlineData("let name: string = \"Aluminum6061\"\nlet bad: string = name + name", FirmamentV2Parser.ExpressionInvalidOperator)]
+ [InlineData("let flag: bool = true\nlet bad: bool = flag * 2", FirmamentV2Parser.ExpressionInvalidOperator)]
+ [InlineData("let diameter: length = 6.0mm\nlet bad: length = diameter / diameter", FirmamentV2Parser.ExpressionTypeMismatch)]
+ [InlineData("let bad: float = 1.0 / 0.0", FirmamentV2Parser.ExpressionDivisionByZero)]
+ [InlineData("let badLength: length = 6.0mm / 0", FirmamentV2Parser.ExpressionDivisionByZero)]
+ [InlineData("let a: length = b + 1.0mm\nlet b: length = a + 1.0mm", FirmamentV2Parser.ExpressionCycle)]
+ [InlineData("let x: length = if true { 1.0mm } else { 2.0mm }", FirmamentV2Parser.ExpressionUnsupported)]
+ [InlineData("let x: length = foo(1.0mm)", FirmamentV2Parser.ExpressionUnsupported)]
+ public void FirmamentV2Parser_LetArithmeticExpressions_InvalidCasesAreDiagnostics(string letSource, string expectedDiagnostic)
+ {
+ var result = FirmamentV2Parser.Parse($$"""
+ model InvalidArithmetic {
+ units mm
+ solid base: Box { size: [10, 8, 6] }
+ {{letSource}}
+ }
+ """);
+
+ Assert.False(result.IsSuccess);
+ Assert.Contains(expectedDiagnostic, result.Diagnostics);
+ }
+
[Theory]
[InlineData("let MountingPattern { holeDiameter: length = 6.0mm holeDiameter: length = 7.0mm }", FirmamentV2Parser.LetInvalidLiteral)]
[InlineData("let MountingPattern { holeDiameter: length = 6.0mm tol 0.05mm }", FirmamentV2Parser.LetInvalidLiteral)]
diff --git a/Aetheris.Kernel.Firmament/FirmamentV2/FirmamentV2Ast.cs b/Aetheris.Kernel.Firmament/FirmamentV2/FirmamentV2Ast.cs
index 889f84c2..5a5175fd 100644
--- a/Aetheris.Kernel.Firmament/FirmamentV2/FirmamentV2Ast.cs
+++ b/Aetheris.Kernel.Firmament/FirmamentV2/FirmamentV2Ast.cs
@@ -18,6 +18,7 @@ public abstract record FirmamentV2ValueExpression;
public sealed record FirmamentV2LiteralExpression(FirmamentV2LiteralValue Value) : FirmamentV2ValueExpression;
public sealed record FirmamentV2DottedReferenceExpression(string RecordName, string FieldName, string Source) : FirmamentV2ValueExpression;
public sealed record FirmamentV2IdentifierReferenceExpression(string Name, string Source) : FirmamentV2ValueExpression;
+public sealed record FirmamentV2BinaryExpression(FirmamentV2ValueExpression Left, string Operator, FirmamentV2ValueExpression Right, string Source) : FirmamentV2ValueExpression;
public sealed record FirmamentV2LetDeclaration(string Name, FirmamentV2PrimitiveType DeclaredType, FirmamentV2ValueExpression ValueExpression, FirmamentV2SourceSpan SourceSpan)
{
public FirmamentV2LiteralValue LiteralValue => ValueExpression is FirmamentV2LiteralExpression literal ? literal.Value : throw new InvalidOperationException("Let value is not a literal.");
@@ -27,7 +28,8 @@ public sealed record FirmamentV2LetRecordField(string Name, FirmamentV2Primitive
{
public FirmamentV2LiteralValue LiteralValue => ValueExpression is FirmamentV2LiteralExpression literal ? literal.Value : throw new InvalidOperationException("Record field value is not a literal.");
}
-public sealed record FirmamentV2BoundLet(string Name, FirmamentV2PrimitiveType Type, FirmamentV2LiteralValue Value, FirmamentV2SourceSpan SourceSpan);
+public sealed record FirmamentV2BoundExpression(FirmamentV2PrimitiveType InferredType, FirmamentV2LiteralValue Value, IReadOnlySet<string> Dependencies, FirmamentV2SourceSpan SourceSpan);
+public sealed record FirmamentV2BoundLet(string Name, FirmamentV2PrimitiveType Type, FirmamentV2LiteralValue Value, FirmamentV2SourceSpan SourceSpan, FirmamentV2BoundExpression? Expression = null, IReadOnlySet<string>? Dependencies = null);
public sealed record FirmamentV2BoundLetRecord(string Name, IReadOnlyDictionary<string, FirmamentV2BoundLet> Fields, FirmamentV2SourceSpan SourceSpan);
public sealed record FirmamentV2SolidBinding(string Name, string RecordType, FirmamentV2PrimitiveRecord Primitive, string? DerivedFrom = null, IReadOnlyDictionary<string, IReadOnlyList<double>>? Overrides = null)
diff --git a/Aetheris.Kernel.Firmament/FirmamentV2/FirmamentV2Parser.cs b/Aetheris.Kernel.Firmament/FirmamentV2/FirmamentV2Parser.cs
index e3e3696f..08875f4d 100644
--- a/Aetheris.Kernel.Firmament/FirmamentV2/FirmamentV2Parser.cs
+++ b/Aetheris.Kernel.Firmament/FirmamentV2/FirmamentV2Parser.cs
@@ -51,6 +51,16 @@ public static class FirmamentV2Parser
public const string LetReferenceUnknownField = "firmament-v2-let-reference-unknown-field";
public const string LetReferenceNonRecord = "firmament-v2-let-reference-non-record";
public const string LetReferenceRecordUsedAsValue = "firmament-v2-let-reference-record-used-as-value";
+ public const string ExpressionUnknownSymbol = "firmament-v2-expression-unknown-symbol";
+ public const string ExpressionUnknownRecord = "firmament-v2-expression-unknown-record";
+ public const string ExpressionUnknownField = "firmament-v2-expression-unknown-field";
+ public const string ExpressionRecordUsedAsValue = "firmament-v2-expression-record-used-as-value";
+ public const string ExpressionScalarUsedAsRecord = "firmament-v2-expression-scalar-used-as-record";
+ public const string ExpressionTypeMismatch = "firmament-v2-expression-type-mismatch";
+ public const string ExpressionInvalidOperator = "firmament-v2-expression-invalid-operator";
+ public const string ExpressionDivisionByZero = "firmament-v2-expression-division-by-zero";
+ public const string ExpressionCycle = "firmament-v2-expression-cycle";
+ public const string ExpressionUnsupported = "firmament-v2-expression-unsupported";
public const string ModifyTargetUnresolved = "firmament-v2-modify-target-unresolved";
public const string ModifyTargetNotSolid = "firmament-v2-modify-target-not-solid";
@@ -125,7 +135,7 @@ public static class FirmamentV2Parser
private static readonly Regex UnitsRegex = new(@"\bunits\s+(?<units>[A-Za-z_][A-Za-z0-9_]*)\b", RegexOptions.CultureInvariant);
private static readonly HashSet<string> ReservedWords = new(StringComparer.Ordinal) { "let", "model", "units", "solid", "modify", "region", "cut", "template", "concept", "pmi", "recognize", "replace", "with", "true", "false", "int", "float", "length", "angle", "string", "bool" };
private static readonly Regex LetRegex = new(@"\blet\s+(?<name>[A-Za-z_][A-Za-z0-9_]*)\s*:\s*(?<type>[A-Za-z_][A-Za-z0-9_]*)\s*=\s*(?<literal>[^\r\n{}]+)", RegexOptions.CultureInvariant);
- private static readonly Regex LetRecordHeaderRegex = new(@"\blet\s+(?<name>[A-Za-z_][A-Za-z0-9_]*)\s*\{", RegexOptions.CultureInvariant);
+ private static readonly Regex LetRecordHeaderRegex = new(@"^\s*let\s+(?<name>[A-Za-z_][A-Za-z0-9_]*)\s*\{", RegexOptions.CultureInvariant | RegexOptions.Multiline);
private static readonly Regex LetRecordFieldRegex = new(@"^\s*(?<name>[A-Za-z_][A-Za-z0-9_]*)\s*:\s*(?<type>[A-Za-z_][A-Za-z0-9_]*)\s*=\s*(?<literal>.+?)\s*$", RegexOptions.CultureInvariant);
private static readonly Regex DottedReferenceRegex = new(@"^(?<record>[A-Za-z_][A-Za-z0-9_]*)\.(?<field>[A-Za-z_][A-Za-z0-9_]*)$", RegexOptions.CultureInvariant);
private static readonly Regex SolidHeaderRegex = new(@"\bsolid\s+(?<name>[A-Za-z_][A-Za-z0-9_]*)\s*:\s*(?<target>[A-Za-z_][A-Za-z0-9_]*)(?<with>\s+with)?\s*\{", RegexOptions.CultureInvariant);
@@ -213,8 +223,7 @@ public static class FirmamentV2Parser
}
var letRecords = ParseLetRecords(source, byName.Keys, diagnostics);
- var topLevelNames = byName.Keys.Concat(letRecords.Select(r => r.Name));
- var lets = ParseLets(source, topLevelNames, diagnostics);
+ var lets = ParseLets(source, byName.Keys.Concat(letRecords.Select(r => r.Name)), diagnostics);
var boundLetRecords = BindLetRecords(letRecords);
var boundLets = BindLets(lets, boundLetRecords, byName.Keys, diagnostics);
@@ -301,6 +310,13 @@ public static class FirmamentV2Parser
private static FirmamentV2ValueExpression? ParseLetValueExpression(FirmamentV2PrimitiveType type, string literal, List<string> diagnostics)
{
+ if (literal.Contains(" tol ", StringComparison.Ordinal) || literal.Contains(" if ", StringComparison.Ordinal) || literal.StartsWith("if ", StringComparison.Ordinal) || Regex.IsMatch(literal, @"^[A-Za-z_][A-Za-z0-9_]*\s*\(", RegexOptions.CultureInvariant))
+ { diagnostics.Add(literal.Contains("tol", StringComparison.Ordinal) ? LetInvalidLiteral : ExpressionUnsupported); return null; }
+ if (literal.IndexOfAny(['+', '-', '*', '/', '(', ')']) >= 0)
+ {
+ var parser = new LetExpressionParser(literal, diagnostics);
+ return parser.Parse();
+ }
var refMatch = DottedReferenceRegex.Match(literal);
if (refMatch.Success) return new FirmamentV2DottedReferenceExpression(refMatch.Groups["record"].Value, refMatch.Groups["field"].Value, literal);
if (Regex.IsMatch(literal, @"^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.CultureInvariant) && type is not FirmamentV2PrimitiveType.Bool) return new FirmamentV2IdentifierReferenceExpression(literal, literal);
@@ -344,29 +360,151 @@ public static class FirmamentV2Parser
}
private static IReadOnlyList<FirmamentV2BoundLetRecord> BindLetRecords(IReadOnlyList<FirmamentV2LetRecordDeclaration> records) =>
- records.Select(r => new FirmamentV2BoundLetRecord(r.Name, r.Fields.Where(f => f.ValueExpression is FirmamentV2LiteralExpression).Select(f => new FirmamentV2BoundLet(f.Name, f.DeclaredType, f.LiteralValue, f.SourceSpan)).ToDictionary(f => f.Name, StringComparer.Ordinal), r.SourceSpan)).ToArray();
+ records.Select(r => new FirmamentV2BoundLetRecord(r.Name, r.Fields.Where(f => f.ValueExpression is FirmamentV2LiteralExpression).Select(f => new FirmamentV2BoundLet(f.Name, f.DeclaredType, f.LiteralValue, f.SourceSpan, new FirmamentV2BoundExpression(f.DeclaredType, f.LiteralValue, new HashSet<string>(StringComparer.Ordinal), f.SourceSpan), new HashSet<string>(StringComparer.Ordinal))).ToDictionary(f => f.Name, StringComparer.Ordinal), r.SourceSpan)).ToArray();
private static IReadOnlyList<FirmamentV2BoundLet> BindLets(IReadOnlyList<FirmamentV2LetDeclaration> lets, IReadOnlyList<FirmamentV2BoundLetRecord> records, IEnumerable<string> solidNames, List<string> diagnostics)
{
- var recordMap = records.ToDictionary(r => r.Name, StringComparer.Ordinal);
- var scalarNames = new HashSet<string>(lets.Select(l => l.Name).Concat(solidNames), StringComparer.Ordinal);
- var bound = new List<FirmamentV2BoundLet>();
- foreach (var let in lets)
+ var binder = new ExpressionBinder(lets, records, solidNames, diagnostics);
+ return binder.BindLets();
+ }
+
+ private sealed class LetExpressionParser(string source, List<string> diagnostics)
+ {
+ private int _pos;
+ public FirmamentV2ValueExpression? Parse()
{
- if (let.ValueExpression is FirmamentV2LiteralExpression literal) { bound.Add(new(let.Name, let.DeclaredType, literal.Value, let.SourceSpan)); continue; }
- if (let.ValueExpression is FirmamentV2IdentifierReferenceExpression identifier)
+ var expr = ParseAdditive();
+ Skip();
+ if (expr is null || _pos != source.Length) { diagnostics.Add(ExpressionUnsupported); return null; }
+ return expr;
+ }
+ private FirmamentV2ValueExpression? ParseAdditive()
+ {
+ var left = ParseMultiplicative();
+ while (true)
{
- diagnostics.Add(recordMap.ContainsKey(identifier.Name) ? LetReferenceRecordUsedAsValue : LetLiteralOnly);
- continue;
+ Skip();
+ if (!Match('+') && !Match('-')) return left;
+ var op = source[_pos - 1].ToString();
+ var right = ParseMultiplicative();
+ if (left is null || right is null) return null;
+ left = new FirmamentV2BinaryExpression(left, op, right, source);
+ }
+ }
+ private FirmamentV2ValueExpression? ParseMultiplicative()
+ {
+ var left = ParsePrimary();
+ while (true)
+ {
+ Skip();
+ if (!Match('*') && !Match('/')) return left;
+ var op = source[_pos - 1].ToString();
+ var right = ParsePrimary();
+ if (left is null || right is null) return null;
+ left = new FirmamentV2BinaryExpression(left, op, right, source);
}
- var reference = (FirmamentV2DottedReferenceExpression)let.ValueExpression;
- if (scalarNames.Contains(reference.RecordName)) { diagnostics.Add(LetReferenceNonRecord); continue; }
- if (!recordMap.TryGetValue(reference.RecordName, out var record)) { diagnostics.Add(LetReferenceUnknownRecord); continue; }
- if (!record.Fields.TryGetValue(reference.FieldName, out var field)) { diagnostics.Add(LetReferenceUnknownField); continue; }
- if (field.Type != let.DeclaredType) { diagnostics.Add(LetTypeMismatch); continue; }
- bound.Add(new(let.Name, let.DeclaredType, field.Value, let.SourceSpan));
}
- return bound;
+ private FirmamentV2ValueExpression? ParsePrimary()
+ {
+ Skip();
+ if (Match('('))
+ {
+ var e = ParseAdditive();
+ Skip();
+ if (!Match(')')) diagnostics.Add(ExpressionUnsupported);
+ return e;
+ }
+ var start = _pos;
+ while (_pos < source.Length && !char.IsWhiteSpace(source[_pos]) && "+-*/()".IndexOf(source[_pos]) < 0) _pos++;
+ if (_pos == start) { diagnostics.Add(ExpressionUnsupported); return null; }
+ var token = source[start.._pos];
+ var dm = DottedReferenceRegex.Match(token);
+ if (dm.Success) return new FirmamentV2DottedReferenceExpression(dm.Groups["record"].Value, dm.Groups["field"].Value, token);
+ if (Regex.IsMatch(token, @"^[A-Za-z_][A-Za-z0-9_]*$", RegexOptions.CultureInvariant)) return new FirmamentV2IdentifierReferenceExpression(token, token);
+ var literal = ParseLetLiteral(TokenType(token), token, diagnostics);
+ return literal is null ? null : new FirmamentV2LiteralExpression(literal);
+ }
+ private FirmamentV2PrimitiveType TokenType(string token) => token.EndsWith("mm", StringComparison.Ordinal) ? FirmamentV2PrimitiveType.Length : token.EndsWith("deg", StringComparison.Ordinal) ? FirmamentV2PrimitiveType.Angle : token.Contains('.', StringComparison.Ordinal) ? FirmamentV2PrimitiveType.Float : FirmamentV2PrimitiveType.Int;
+ private void Skip() { while (_pos < source.Length && char.IsWhiteSpace(source[_pos])) _pos++; }
+ private bool Match(char c) { if (_pos >= source.Length || source[_pos] != c) return false; _pos++; return true; }
+ }
+
+ private sealed class ExpressionBinder
+ {
+ private readonly IReadOnlyList<FirmamentV2LetDeclaration> _lets;
+ private readonly Dictionary<string, FirmamentV2BoundLetRecord> _records;
+ private readonly HashSet<string> _solidNames;
+ private readonly List<string> _diagnostics;
+ private readonly Dictionary<string, FirmamentV2LetDeclaration> _letMap;
+ private readonly Dictionary<string, FirmamentV2BoundLet> _bound = new(StringComparer.Ordinal);
+ private readonly HashSet<string> _visiting = new(StringComparer.Ordinal);
+ public ExpressionBinder(IReadOnlyList<FirmamentV2LetDeclaration> lets, IReadOnlyList<FirmamentV2BoundLetRecord> records, IEnumerable<string> solidNames, List<string> diagnostics)
+ { _lets = lets; _records = records.ToDictionary(r => r.Name, StringComparer.Ordinal); _solidNames = new(solidNames, StringComparer.Ordinal); _diagnostics = diagnostics; _letMap = lets.ToDictionary(l => l.Name, StringComparer.Ordinal); }
+ public IReadOnlyList<FirmamentV2BoundLet> BindLets() { foreach (var l in _lets) Bind(l.Name); return _lets.Where(l => _bound.ContainsKey(l.Name)).Select(l => _bound[l.Name]).ToArray(); }
+ private FirmamentV2BoundLet? Bind(string name)
+ {
+ if (_bound.TryGetValue(name, out var existing)) return existing;
+ if (!_letMap.TryGetValue(name, out var let)) return null;
+ if (!_visiting.Add(name)) { _diagnostics.Add(ExpressionCycle); return null; }
+ var expr = Eval(let.ValueExpression);
+ _visiting.Remove(name);
+ if (expr is null) return null;
+ if (expr.InferredType != let.DeclaredType) { _diagnostics.Add(ExpressionTypeMismatch); _diagnostics.Add(LetTypeMismatch); return null; }
+ return _bound[name] = new FirmamentV2BoundLet(let.Name, let.DeclaredType, expr.Value, let.SourceSpan, expr, expr.Dependencies);
+ }
+ private FirmamentV2BoundExpression? Eval(FirmamentV2ValueExpression e) => e switch
+ {
+ FirmamentV2LiteralExpression l => new(l.Value.Type, l.Value, new HashSet<string>(StringComparer.Ordinal), new(0, 0)),
+ FirmamentV2IdentifierReferenceExpression r => EvalId(r),
+ FirmamentV2DottedReferenceExpression r => EvalDotted(r),
+ FirmamentV2BinaryExpression b => EvalBinary(b),
+ _ => null
+ };
+ private FirmamentV2BoundExpression? EvalId(FirmamentV2IdentifierReferenceExpression r)
+ {
+ if (_records.ContainsKey(r.Name)) { _diagnostics.Add(ExpressionRecordUsedAsValue); _diagnostics.Add(LetReferenceRecordUsedAsValue); return null; }
+ var b = Bind(r.Name);
+ if (b is null) { _diagnostics.Add(ExpressionUnknownSymbol); return null; }
+ return new(b.Type, b.Value, new HashSet<string>((b.Dependencies ?? new HashSet<string>()).Append(r.Name), StringComparer.Ordinal), b.SourceSpan);
+ }
+ private FirmamentV2BoundExpression? EvalDotted(FirmamentV2DottedReferenceExpression r)
+ {
+ if (_solidNames.Contains(r.RecordName) || _letMap.ContainsKey(r.RecordName)) { _diagnostics.Add(ExpressionScalarUsedAsRecord); _diagnostics.Add(LetReferenceNonRecord); return null; }
+ if (!_records.TryGetValue(r.RecordName, out var record)) { _diagnostics.Add(ExpressionUnknownRecord); _diagnostics.Add(LetReferenceUnknownRecord); return null; }
+ if (!record.Fields.TryGetValue(r.FieldName, out var f)) { _diagnostics.Add(ExpressionUnknownField); _diagnostics.Add(LetReferenceUnknownField); return null; }
+ return new(f.Type, f.Value, new HashSet<string> { $"{r.RecordName}.{r.FieldName}" }, f.SourceSpan);
+ }
+ private FirmamentV2BoundExpression? EvalBinary(FirmamentV2BinaryExpression b)
+ {
+ var l = Eval(b.Left); var r = Eval(b.Right); if (l is null || r is null) return null;
+ var type = ResultType(l.InferredType, b.Operator, r.InferredType);
+ if (type is null) { _diagnostics.Add(ExpressionInvalidOperator); return null; }
+ if (b.Operator == "/" && IsZero(r.Value)) { _diagnostics.Add(ExpressionDivisionByZero); return null; }
+ var val = Compute(type.Value, l.Value, b.Operator, r.Value, b.Source);
+ return new(type.Value, val, new HashSet<string>(l.Dependencies.Concat(r.Dependencies), StringComparer.Ordinal), new(0, b.Source.Length));
+ }
+ private static bool IsZero(FirmamentV2LiteralValue v) => v.NumericValue is double d && d == 0d;
+ private static FirmamentV2PrimitiveType? ResultType(FirmamentV2PrimitiveType a, string op, FirmamentV2PrimitiveType b) => op switch
+ {
+ "+" or "-" when a == b && a is FirmamentV2PrimitiveType.Int or FirmamentV2PrimitiveType.Float or FirmamentV2PrimitiveType.Length or FirmamentV2PrimitiveType.Angle => a,
+ "+" or "-" when IsNumeric(a) && IsNumeric(b) => FirmamentV2PrimitiveType.Float,
+ "*" when IsNumeric(a) && IsNumeric(b) => a == FirmamentV2PrimitiveType.Int && b == FirmamentV2PrimitiveType.Int ? FirmamentV2PrimitiveType.Int : FirmamentV2PrimitiveType.Float,
+ "*" when IsDim(a) && IsNumeric(b) => a,
+ "*" when IsNumeric(a) && IsDim(b) => b,
+ "/" when IsNumeric(a) && IsNumeric(b) => FirmamentV2PrimitiveType.Float,
+ "/" when IsDim(a) && IsNumeric(b) => a,
+ "/" when IsDim(a) && a == b => FirmamentV2PrimitiveType.Float,
+ _ => null
+ };
+ private static bool IsNumeric(FirmamentV2PrimitiveType t) => t is FirmamentV2PrimitiveType.Int or FirmamentV2PrimitiveType.Float;
+ private static bool IsDim(FirmamentV2PrimitiveType t) => t is FirmamentV2PrimitiveType.Length or FirmamentV2PrimitiveType.Angle;
+ private static FirmamentV2LiteralValue Compute(FirmamentV2PrimitiveType type, FirmamentV2LiteralValue l, string op, FirmamentV2LiteralValue r, string raw)
+ {
+ var a = Convert.ToDouble(l.NumericValue, CultureInfo.InvariantCulture); var b = Convert.ToDouble(r.NumericValue, CultureInfo.InvariantCulture);
+ var d = op switch { "+" => a + b, "-" => a - b, "*" => a * b, "/" => a / b, _ => 0 };
+ object value = type == FirmamentV2PrimitiveType.Int ? (object)(int)d : d;
+ return new(type, value, d, type == FirmamentV2PrimitiveType.Length ? "mm" : type == FirmamentV2PrimitiveType.Angle ? "deg" : null, raw);
+ }
}
private static FirmamentV2SolidBinding? ParseDirect(string name, string recordType, string body, List<string> diagnostics, string? sourceDirectory)
@@ -476,7 +614,7 @@ public static class FirmamentV2Parser
return new(name, "Box", new FirmamentV2BoxRecord(values, []), baseName, new Dictionary<string, IReadOnlyList<double>>(StringComparer.Ordinal) { ["size"] = values });
}
- private static bool IsFatalDiagnostic(string code) => code is PrimitiveFieldMissing or PrimitiveFieldUnknown or PrimitiveFieldInvalid or MissingModel or MissingUnits or MissingSolid or UnsupportedConstruct or UnknownRecordType or BoxMissingSize or BoxSizeArity or DegenerateDimension or NameUnresolved or DuplicateName or WithRequiresRecord or WithRequiresBoxRecord or WithFieldNotFound or WithFieldTypeMismatch or WithForwardReference or WithDerivedRecordInvalid or ExposeBlockUnsupported or ExposeRequiresBoxRecord or ExposeAliasDuplicate or ExposeAliasInvalid or SelectorUnsupported or SelectorAxisInvalid or SelectorSubselectorUnsupported or FatArrowOutsideExpose or RawBackendIdReferenceForbidden or ModifyTargetUnresolved or ModifyTargetNotSolid or RegionUnsupported or RegionAttachmentSelectorUnsupported or CutUnsupported or CutToolUnsupported or CylinderRadiusMissing or CylinderRadiusInvalid or CylinderRadiusNotFinite or ThroughSelectorUnsupported or AliasUnresolved or AliasRefTypeUnsupported or SideHoleAliasMustResolveToFace or SideHoleAliasResolvesToUnsupportedFace or SideHoleOnlyPlusXMinusXSupported or SideHoleRouteUnsupported or SideHoleSameFaceUnsupported or SideHoleAxisNotYetSupported or SideHoleRadiusExceedsClearance or CylinderCenterInvalid or CylinderCenterArityInvalid or CylinderCenterNotFinite or SideHoleCenterExceedsClearance or HoleVariantUnknown or HoleEntryFaceMissing or HoleCenterMissing or HoleShaftMissing or HoleEndMissing or HoleDiameterInvalid or HoleDepthInvalid or HoleCounterboreInvalid or HoleCountersinkInvalid or PmiKindUnknown or PmiTargetMissing or PmiTargetUnresolved or PmiDiameterInvalid or PmiDuplicateName or InlineStepUnknownBody or InlineStepUnknownFace or PmiImportedTargetNotFace or PmiImportedTargetRequiresCanonicalStep or PmiInvalidImportedTarget or InlineStepPathMissing or InlineStepPathInvalid or InlineStepFileMissing or InlineStepRequiresCanonical or UnknownRecognitionBody or UnknownRecognitionFace or DuplicateRegion or UnknownRecognitionRegion or InvalidRecognitionKind or InvalidRecognitionConfidence or PmiRecognizedRegionKindMismatch or UnknownReplacementBody or UnknownReplacementRegion or ReplacementKindMismatch or ReplacementFaceUnresolved or ReplacementUnsupportedKind or ReplacementVerificationFailed or ReplacementRadiusInvalid or ReplacementEndUnsupported or LetDuplicateName or LetUnknownType or LetTypeMismatch or LetInvalidLiteral or LetUnitMismatch or LetLiteralOnly or LetRecordDuplicateName or LetRecordDuplicateField or LetReferenceUnknownRecord or LetReferenceUnknownField or LetReferenceNonRecord or LetReferenceRecordUsedAsValue or RecognitionEvidenceRadiusInvalid or RecognitionEvidenceSurfaceFamilyUnknown or RecognitionEvidenceAxisInvalid or SemanticProposalKindMismatch or SemanticProposalRadiusInvalid or SemanticProposalTargetUnresolved or SemanticProposalEndUnsupported;
+ private static bool IsFatalDiagnostic(string code) => code is PrimitiveFieldMissing or PrimitiveFieldUnknown or PrimitiveFieldInvalid or MissingModel or MissingUnits or MissingSolid or UnsupportedConstruct or UnknownRecordType or BoxMissingSize or BoxSizeArity or DegenerateDimension or NameUnresolved or DuplicateName or WithRequiresRecord or WithRequiresBoxRecord or WithFieldNotFound or WithFieldTypeMismatch or WithForwardReference or WithDerivedRecordInvalid or ExposeBlockUnsupported or ExposeRequiresBoxRecord or ExposeAliasDuplicate or ExposeAliasInvalid or SelectorUnsupported or SelectorAxisInvalid or SelectorSubselectorUnsupported or FatArrowOutsideExpose or RawBackendIdReferenceForbidden or ModifyTargetUnresolved or ModifyTargetNotSolid or RegionUnsupported or RegionAttachmentSelectorUnsupported or CutUnsupported or CutToolUnsupported or CylinderRadiusMissing or CylinderRadiusInvalid or CylinderRadiusNotFinite or ThroughSelectorUnsupported or AliasUnresolved or AliasRefTypeUnsupported or SideHoleAliasMustResolveToFace or SideHoleAliasResolvesToUnsupportedFace or SideHoleOnlyPlusXMinusXSupported or SideHoleRouteUnsupported or SideHoleSameFaceUnsupported or SideHoleAxisNotYetSupported or SideHoleRadiusExceedsClearance or CylinderCenterInvalid or CylinderCenterArityInvalid or CylinderCenterNotFinite or SideHoleCenterExceedsClearance or HoleVariantUnknown or HoleEntryFaceMissing or HoleCenterMissing or HoleShaftMissing or HoleEndMissing or HoleDiameterInvalid or HoleDepthInvalid or HoleCounterboreInvalid or HoleCountersinkInvalid or PmiKindUnknown or PmiTargetMissing or PmiTargetUnresolved or PmiDiameterInvalid or PmiDuplicateName or InlineStepUnknownBody or InlineStepUnknownFace or PmiImportedTargetNotFace or PmiImportedTargetRequiresCanonicalStep or PmiInvalidImportedTarget or InlineStepPathMissing or InlineStepPathInvalid or InlineStepFileMissing or InlineStepRequiresCanonical or UnknownRecognitionBody or UnknownRecognitionFace or DuplicateRegion or UnknownRecognitionRegion or InvalidRecognitionKind or InvalidRecognitionConfidence or PmiRecognizedRegionKindMismatch or UnknownReplacementBody or UnknownReplacementRegion or ReplacementKindMismatch or ReplacementFaceUnresolved or ReplacementUnsupportedKind or ReplacementVerificationFailed or ReplacementRadiusInvalid or ReplacementEndUnsupported or LetDuplicateName or LetUnknownType or LetTypeMismatch or LetInvalidLiteral or LetUnitMismatch or LetLiteralOnly or LetRecordDuplicateName or LetRecordDuplicateField or LetReferenceUnknownRecord or LetReferenceUnknownField or LetReferenceNonRecord or LetReferenceRecordUsedAsValue or ExpressionUnknownSymbol or ExpressionUnknownRecord or ExpressionUnknownField or ExpressionRecordUsedAsValue or ExpressionScalarUsedAsRecord or ExpressionTypeMismatch or ExpressionInvalidOperator or ExpressionDivisionByZero or ExpressionCycle or ExpressionUnsupported or RecognitionEvidenceRadiusInvalid or RecognitionEvidenceSurfaceFamilyUnknown or RecognitionEvidenceAxisInvalid or SemanticProposalKindMismatch or SemanticProposalRadiusInvalid or SemanticProposalTargetUnresolved or SemanticProposalEndUnsupported;
private static IReadOnlyList<FirmamentV2Exposure> ParseExposures(string body, List<string> diagnostics)
{
diff --git a/docs/implementation/firmament-v2-l3-arithmetic-expression-graph.md b/docs/implementation/firmament-v2-l3-arithmetic-expression-graph.md
new file mode 100644
index 00000000..ea264ae5
--- /dev/null
+++ b/docs/implementation/firmament-v2-l3-arithmetic-expression-graph.md
@@ -0,0 +1,43 @@
+# Firmament V2 L3 arithmetic expression graph
+
+L3 implements pure arithmetic expressions for Firmament V2 scalar `let` declarations. The feature remains a typed manufacturing-intent data layer: expressions are immutable, side-effect-free, acyclic, and evaluated during binding.
+
+## Supported syntax
+
+Scalar lets may use literals, top-level let references, dotted record-field references, parentheses, and binary `+`, `-`, `*`, `/` operators:
+
+```firmament
+let diameter: length = 6.0mm
+let radius: length = diameter / 2
+let drill: length = diameter + 0.25mm
+let scaled: length = diameter * 1.25
+let exported: length = MountingPattern.holeDiameter / 2
+```
+
+Record fields remain literal-bound in L3. Same-record field expression binding is intentionally deferred to avoid broadening record scope before field dependency ordering is designed.
+
+## Type rules
+
+| Operator | Allowed result families |
+| --- | --- |
+| `+`, `-` | `int` with `int` -> `int`; `float` with `float` -> `float`; `int`/`float` mix -> `float`; `length` with `length` -> `length`; `angle` with `angle` -> `angle` |
+| `*` | numeric with numeric -> `int` or `float`; `length`/`angle` multiplied by `int` or `float` -> same dimensional type |
+| `/` | numeric divided by numeric -> `float`; `length`/`angle` divided by numeric -> same dimensional type; `length / length` and `angle / angle` -> `float` |
+
+Rejected combinations include string or bool arithmetic, dimensional addition with unitless values, cross-dimensional arithmetic, `length * length`, `angle * angle`, and assignment of a `float` expression to a dimensional or `int` declaration.
+
+## Unit rules
+
+L3 reuses existing literal unit normalization: `length` literals require `mm`, and `angle` literals require `deg`. Unitless numerics are not implicitly assigned to dimensional declarations, and dimensional division by the same dimension yields an explicit `float` ratio.
+
+## Acyclic graph behavior
+
+Top-level scalar lets are resolved through a full-document dependency graph, so forward references are accepted when acyclic. Cycles are rejected deterministically with `firmament-v2-expression-cycle`. Bound lets carry the evaluated value and a dependency set for downstream inspection.
+
+## Diagnostics
+
+L3 adds expression diagnostics for unknown symbols, unknown records or fields, record/scalar misuse, declared-type mismatch, invalid operators, division by zero, cycles, and unsupported expression syntax. Legacy L1/L2 let diagnostics are still emitted where callers already assert them.
+
+## Explicit non-scope
+
+L3 does not implement tolerance syntax, tolerance propagation, PMI/Forge wiring, functions, conditionals, loops, mutation, user-defined operators, arrays, string concatenation, recursion, or area/volume dimensional exponents.
diff --git a/docs/v2/aetheris-v2-phase1-scope-contract.md b/docs/v2/aetheris-v2-phase1-scope-contract.md
index 6b200139..4f672dfa 100644
--- a/docs/v2/aetheris-v2-phase1-scope-contract.md
+++ b/docs/v2/aetheris-v2-phase1-scope-contract.md
@@ -98,7 +98,7 @@ This doctrine exists because Firmament files must be auditable. Humans, LLMs, su
- L1 primitive immutable typed `let` declarations are implemented.
- L2 grouped one-level `let` records and exact dotted record-field references are implemented for parser/model/binder exposure.
-- L3 arithmetic remains deferred.
+- L3 scalar arithmetic expression graph is implemented with strict primitive type rules, evaluated dependencies, and acyclic validation; same-record field expression binding remains deferred.
- L4 tolerance syntax remains deferred.
## 5. `let` declarations and typed values
diff --git a/fixtures/FirmamentV2/Language/valid/let-arithmetic-expressions.valid.firmfixture b/fixtures/FirmamentV2/Language/valid/let-arithmetic-expressions.valid.firmfixture
new file mode 100644
index 00000000..69236cfc
--- /dev/null
+++ b/fixtures/FirmamentV2/Language/valid/let-arithmetic-expressions.valid.firmfixture
@@ -0,0 +1,25 @@
+// case: let-arithmetic-expressions
+// expected-stage: parsed
+// syntax-version: FirmamentV2
+// implementation: parser-backed
+
+model LetArithmeticExpressions {
+ units mm
+ solid base: Box { size: [10, 8, 6] }
+
+ let diameter: length = 6.0mm
+ let radius: length = diameter / 2
+ let clearance: length = 0.25mm
+ let drill: length = diameter + clearance
+
+ let count: int = 4
+ let doubled: int = count * 2
+ let ratio: float = count / 2
+
+ let draftAngle: angle = 90deg
+ let halfAngle: angle = draftAngle / 2
+
+ let scale: float = 1.25
+ let scaled: length = diameter * scale
+
+}