From cc8138188ef95b69f93a150fbbe8f8c443c42234 Mon Sep 17 00:00:00 2001 From: Michele Campeotto Date: Tue, 5 Nov 2024 10:45:08 +0100 Subject: [PATCH] fixed default fractional julian day number calculation Formula deduced from information at https://aa.usno.navy.mil/data/JulianDate --- .../Generation/GenExpressions.swift | 7 +++-- .../LighterCodeGenAST/Nodes/Expression.swift | 27 +++++++++++++------ .../GenerateDefaultValues.swift | 10 +++++-- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/Plugins/Libraries/LighterCodeGenAST/Generation/GenExpressions.swift b/Plugins/Libraries/LighterCodeGenAST/Generation/GenExpressions.swift index c3f2d52..3e6e8a8 100644 --- a/Plugins/Libraries/LighterCodeGenAST/Generation/GenExpressions.swift +++ b/Plugins/Libraries/LighterCodeGenAST/Generation/GenExpressions.swift @@ -10,7 +10,7 @@ public extension CodeGenerator { switch expression { case .raw, .tuple, .varargs, .compare, .and, .conditional, .flatMap, - .nilCoalesce, .forceUnwrap, .array: + .arithmetic, .nilCoalesce, .forceUnwrap, .array: return "(\(string(for: expression)))" case .literal, .variableReference, .variablePath, @@ -83,6 +83,9 @@ public extension CodeGenerator { case .compare(let lhs, let op, let rhs): return "\(string(for: lhs)) \(op.rawValue) \(string(for: rhs))" + case .arithmetic(let lhs, let op, let rhs): + return "\(string(for: lhs)) \(op.rawValue) \(string(for: rhs))" + case .and(let expressions): return expressions.map({ "(\(string(for: $0)))" }) .joined(separator: " && ") @@ -142,7 +145,7 @@ public extension CodeGenerator { switch expression { case .raw, .literal, .variableReference, .keyPathLookup, .typeInit, .keyPath, .compare, .cast, .and, .conditional, .variablePath, - .flatMap, .nilCoalesce, .forceUnwrap: + .arithmetic, .flatMap, .nilCoalesce, .forceUnwrap: append(string(for: expression)) case .tuple, .array: // indent better diff --git a/Plugins/Libraries/LighterCodeGenAST/Nodes/Expression.swift b/Plugins/Libraries/LighterCodeGenAST/Nodes/Expression.swift index 552f718..7927b2d 100644 --- a/Plugins/Libraries/LighterCodeGenAST/Nodes/Expression.swift +++ b/Plugins/Libraries/LighterCodeGenAST/Nodes/Expression.swift @@ -9,11 +9,19 @@ public indirect enum Expression: Equatable { /// The operators for ``compare(lhs:operator:rhs:)`` expressions. - public enum Operator: String, Sendable { - case equal = "==" - case notEqual = "!=" - case greaterThanOrEqual = ">=" - case lessThan = "<" + public enum ComparisonOperator: String, Sendable { + case equal = "==" + case notEqual = "!=" + case greaterThanOrEqual = ">=" + case lessThan = "<" + } + + /// The operators for ``arithmetic(lhs:operator:rhs:)`` expressions. + public enum ArithmeticOperator: String, Sendable { + case divideBy = "/" + case multiplyBy = "*" + case add = "+" + case subtract = "-" } /// Just an arbitrary string to inject @@ -53,8 +61,11 @@ public indirect enum Expression: Equatable { case varargs([ Expression ]) /// `strcmp("hello", s) == 0` - case compare(lhs: Expression, operator: Operator, rhs: Expression) + case compare(lhs: Expression, operator: ComparisonOperator, rhs: Expression) + /// `a + b` + case arithmetic(lhs: Expression, operator: ArithmeticOperator, rhs: Expression) + /// `a && b && c` case and([ Expression ]) @@ -265,10 +276,10 @@ public extension Expression { .compare(lhs: expression, operator: .greaterThanOrEqual, rhs: .integer(0)) } - static func cmp(_ lhs: Expression, _ op: Operator, _ rhs: Expression) -> Self { + static func cmp(_ lhs: Expression, _ op: ComparisonOperator, _ rhs: Expression) -> Self { .compare(lhs: lhs, operator: op, rhs: rhs) } - static func cmp(_ lhs: Expression, _ op: Operator, _ rhs: Int) -> Self { + static func cmp(_ lhs: Expression, _ op: ComparisonOperator, _ rhs: Int) -> Self { .compare(lhs: lhs, operator: op, rhs: .integer(rhs)) } static func isNil(_ lhs: Expression) -> Self { diff --git a/Plugins/Libraries/LighterGeneration/RecordGeneration/GenerateDefaultValues.swift b/Plugins/Libraries/LighterGeneration/RecordGeneration/GenerateDefaultValues.swift index 84e42a6..7be5464 100644 --- a/Plugins/Libraries/LighterGeneration/RecordGeneration/GenerateDefaultValues.swift +++ b/Plugins/Libraries/LighterGeneration/RecordGeneration/GenerateDefaultValues.swift @@ -158,8 +158,14 @@ extension EnlighterASTGenerator { case .date: return .call(name: "Foundation.Date") case .double: - return .variableReference( - instance: "Foundation.Date()", name: "timeIntervalSince1970") + return .arithmetic( + lhs: .arithmetic( + lhs: .variableReference(instance: "Foundation.Date()", + name: "timeIntervalSince1970"), + operator: .divideBy, + rhs: .double(86400.0)), + operator: .add, + rhs: .double(2440587.5)) case .integer: return .cast( .variableReference(