XCTLFunctionCallStatement.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // XCTLFunctionCallStatement.swift
  3. // notebook
  4. //
  5. // Created by 邢铖 on 2023/5/18.
  6. //
  7. import Foundation
  8. internal class XCTLFunctionCallStatement: XCTLStatement, XCTLExpressionPart {
  9. var type: XCTLStatementType { .typeFunctionCall }
  10. var expressionValue: XCTLExpressionValue { .consume }
  11. var holdingObject: XCTLRuntimeVariable { .void }
  12. var argumentStatements = [XCTLStatement]()
  13. weak var parent: XCTLStatement?
  14. func matchSelfStatement(lex: XCTLLexer) throws -> XCTLStatement? {
  15. if try lex.next().type == .typeOpenBracket {
  16. return XCTLFunctionCallStatement()
  17. }
  18. return nil
  19. }
  20. func parseStatement(fromLexerToSelf lex: XCTLLexer, fromParent: XCTLStatement?) throws {
  21. self.parent = fromParent
  22. _ = try lex.next()
  23. while try lex.peek().type != .typeCloseBracket {
  24. self.argumentStatements.append(try self.parseNextExpression(forLexer: lex))
  25. }
  26. try lex.next()
  27. }
  28. func evaluate(inContext context: XCTLRuntimeAbstractContext) throws -> XCTLRuntimeVariable {
  29. if let funcValue = context.variableStack.popVariable() {
  30. let arg = try argumentStatements.map({ try $0.evaluate(inContext: context) })
  31. if funcValue.type == .typeFuncImpl,
  32. let funcStmt = funcValue.rawFunctionStatement as? XCTLLateExecuteStatement {
  33. let result = try funcStmt.doRealEvaluate(inContext: context, withArgs: arg)
  34. context.variableStack.pushVariable(result)
  35. return result
  36. }
  37. if funcValue.type != .typeFuncIntrinsic {
  38. throw XCTLRuntimeError.unexpectedVariableType(expect: XCTLRuntimeVariableType.typeFuncIntrinsic.rawValue, butGot: funcValue.type.rawValue)
  39. }
  40. let result = funcValue.executeFunc(arg: arg)
  41. context.variableStack.pushVariable(result)
  42. return result
  43. }
  44. throw XCTLRuntimeError.variableStackNoObject
  45. }
  46. }