XCTLEqualthanStatement.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // XCTLEqualthanStatement.swift
  3. // notebook
  4. //
  5. // Created by 邢铖 on 2023/5/25.
  6. //
  7. import Foundation
  8. internal class XCTLEqualthanStatement : XCTLStatement {
  9. var type: XCTLStatementType { .typeEqualthan }
  10. var holdingObject: XCTLRuntimeVariable { .void }
  11. var compareValueStmt: XCTLStatement!
  12. var childrenStmt: XCTLStatement = XCTLListStatement()
  13. weak var parent: XCTLStatement?
  14. weak var condStmt: XCTLConditionParentStatement?
  15. func matchSelfStatement(lex: XCTLLexer) throws -> XCTLStatement? {
  16. if try lex.next().type == .typeEqualthan {
  17. return XCTLEqualthanStatement()
  18. }
  19. return nil
  20. }
  21. func parseStatement(fromLexerToSelf lex: XCTLLexer, fromParent: XCTLStatement?) throws {
  22. guard let parent = fromParent as? XCTLListStatementProtocol,
  23. let condStmt = parent.conditionParent else {
  24. throw XCTLCompileTimeError.unexpectParentStatementType(expect: "any-cond-stmt", butGot: "\(fromParent?.type.rawValue ?? "void")")
  25. }
  26. self.parent = fromParent
  27. self.condStmt = condStmt
  28. try lex.next()
  29. self.compareValueStmt = try self.parseNextExpression(forLexer: lex, terminator: .typeOpenBrace)
  30. try self.childrenStmt.parseStatement(fromLexerToSelf: lex, fromParent: self)
  31. lex.lastStatement = self
  32. }
  33. func evaluate(inContext context: XCTLRuntimeAbstractContext) throws -> XCTLRuntimeVariable {
  34. guard let condFrame = context.findConditionFrame() else {
  35. throw XCTLRuntimeError.invalidConditionFrame
  36. }
  37. guard let originalValue = self.parent?.holdingObject,
  38. originalValue.type != .typeVoid else {
  39. throw XCTLRuntimeError.parentNoHoldingObject
  40. }
  41. if condFrame.doNext {
  42. condFrame.doNext = false
  43. condFrame.doElse = false
  44. return try self.childrenStmt.evaluate(inContext: context)
  45. }
  46. let compareValue = try self.compareValueStmt.evaluate(inContext: context)
  47. if compareValue.type == .typeVoid {
  48. throw XCTLRuntimeError.unexpectedVariableType(expect: "any", butGot: "void")
  49. }
  50. var equalTo = false
  51. if let compareValueNSObject = compareValue.nativeValue as? NSObject,
  52. let originalValueNSObject = originalValue.nativeValue as? NSObject {
  53. equalTo = compareValueNSObject == originalValueNSObject
  54. }
  55. if equalTo {
  56. condFrame.doElse = false
  57. return try self.childrenStmt.evaluate(inContext: context)
  58. }
  59. return .void
  60. }
  61. }