runtime.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package raku
  2. type Value interface {
  3. }
  4. type StringValue string
  5. type SymbolValue string
  6. type IntegerValue int64
  7. type FloatValue float64
  8. type ArrayValue []Value
  9. type MapValue map[string]Value
  10. type BoolValue bool
  11. /** An instance of a class. */
  12. type Instance interface {
  13. Send Method
  14. }
  15. /** A class. */
  16. type Class interface {
  17. Instance
  18. }
  19. type Method func (raku * Raku, message string, target Object, args ... interface{}) (Object, error)
  20. type Command struct {
  21. Name string
  22. Method
  23. }
  24. type CommandMap = map[string] Command
  25. type Class interface {
  26. Object
  27. Parent() * Class
  28. Selector(name string) * Method
  29. }
  30. type Object interface {
  31. Class() Class
  32. Send(raku * Raku, message string, args ... interface {}) (Object, error)
  33. }
  34. var RootClass * DefaultClass = &DefaultClass{}
  35. type DefaultObject struct {
  36. class Class
  37. * Raku
  38. CommandMap
  39. }
  40. func (object DefaultObject) Selector(name string) Method {
  41. command, ok := object.CommandMap[name]
  42. if !ok {
  43. method := object.class.Selector(name)
  44. if method == nil {
  45. return nil
  46. }
  47. return method
  48. }
  49. return command.Method
  50. }
  51. func (object DefaultObject) Class() Object {
  52. return RootClass
  53. }
  54. const METHOD_MISSING = "method_missing"
  55. func (object DefaultObject) Send(raku * Raku, message string, args ... interface {}) (Object, error) {
  56. method := object.Selector(message)
  57. if (method != nil) {
  58. return method(object.Raku, message, object, args ...)
  59. }
  60. method = object.Selector(METHOD_MISSING)
  61. if (method != nil) {
  62. return method(object.Raku, message, object, args ...)
  63. }
  64. return nil, fmt.Errorf("Cannot send message %s to object %v.", message, object)
  65. }
  66. type DefaultClass struct {
  67. parent * Class
  68. DefaultObject
  69. }
  70. func (class DefaultClass) Parent() Object {
  71. return class.parent
  72. }
  73. type NilClass struct {
  74. DefaultClass
  75. }
  76. var Nil Object = &DefaultObject{NilClass};
  77. var Root * Object = &DefaultObject{RootClass}
  78. type Boolean struct {
  79. DefaultObject
  80. }
  81. var True * Boolean = &Boolean{Root}
  82. var False * Boolean = &Boolean{Root}
  83. func (boolean Boolean) Parent() Object {
  84. return Nil
  85. }
  86. func (boolean Boolean) Send(raku * Raku, message string, args ... interface {}) (Object, error) {
  87. return Root.Send(raku, message, args ...);
  88. }
  89. func (method Method) Parent() Object {
  90. return Nil
  91. }
  92. func (method Method) Send(raku * Raku, message string, args ... interface {}) (Object, error) {
  93. return method(raku, message, method, args...)
  94. }
  95. type Raku struct {
  96. * Parser
  97. CommandMap
  98. Root * Object
  99. Nil * NilClass
  100. True * Boolean
  101. False * Boolean
  102. }
  103. func New(peg_filename string) (*Raku, error) {
  104. var err error
  105. result := &Raku{}
  106. result.Parser, err = LoadParser(peg_filename)
  107. if (err == nil) {
  108. }
  109. return result, err
  110. }
  111. func Evaluate(raku * Raku, result Result) {
  112. _
  113. }