Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: " && ")
Expand Down Expand Up @@ -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
Expand Down
27 changes: 19 additions & 8 deletions Plugins/Libraries/LighterCodeGenAST/Nodes/Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ])

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading