You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.2 KiB

  1. //
  2. // Nodes.swift
  3. // Kaleidoscope
  4. //
  5. // Created by Matthew Cheok on 15/11/15.
  6. // Copyright © 2015 Matthew Cheok. All rights reserved.
  7. //
  8. import Foundation
  9. public class ExprNode: CustomStringConvertible, Equatable {
  10. public var range: CountableRange<Int> = 0..<0
  11. public let name: String
  12. public var description: String {
  13. return "ExprNode(name: \"\(name)\")"
  14. }
  15. public init(name: String) {
  16. self.name = name
  17. }
  18. }
  19. public func == (lhs: ExprNode, rhs: ExprNode) -> Bool {
  20. return lhs.description == rhs.description
  21. }
  22. public class NumberNode: ExprNode {
  23. public let value: Float
  24. public override var description: String {
  25. return "NumberNode(value: \(value))"
  26. }
  27. public init(value: Float) {
  28. self.value = value
  29. super.init(name: "\(value)")
  30. }
  31. }
  32. public class VariableNode: ExprNode {
  33. public override var description: String {
  34. return "VariableNode(name: \"\(name)\")"
  35. }
  36. }
  37. public class BinaryOpNode: ExprNode {
  38. public let lhs: ExprNode
  39. public let rhs: ExprNode
  40. public override var description: String {
  41. return "BinaryOpNode(name: \"\(name)\", lhs: \(lhs), rhs: \(rhs))"
  42. }
  43. public init(name: String, lhs: ExprNode, rhs: ExprNode) {
  44. self.lhs = lhs
  45. self.rhs = rhs
  46. super.init(name: "\(name)")
  47. }
  48. }
  49. public class CallNode: ExprNode {
  50. public let arguments: [ExprNode]
  51. public override var description: String {
  52. return "CallNode(name: \"\(name)\", arguments: \(arguments))"
  53. }
  54. public init(name: String, arguments: [ExprNode]) {
  55. self.arguments = arguments
  56. super.init(name: "\(name)")
  57. }
  58. }
  59. public class PrototypeNode: ExprNode {
  60. public let argumentNames: [String]
  61. public override var description: String {
  62. return "PrototypeNode(name: \"\(name)\", argumentNames: \(argumentNames))"
  63. }
  64. public init(name: String, argumentNames: [String]) {
  65. self.argumentNames = argumentNames
  66. super.init(name: "\(name)")
  67. }
  68. }
  69. public class FunctionNode: ExprNode {
  70. public let prototype: PrototypeNode
  71. public let body: ExprNode
  72. public override var description: String {
  73. return "FunctionNode(prototype: \(prototype), body: \(body))"
  74. }
  75. public init(prototype: PrototypeNode, body: ExprNode) {
  76. self.prototype = prototype
  77. self.body = body
  78. super.init(name: "\(prototype.name)")
  79. }
  80. }