builtins.go.old 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package main
  2. import "fmt"
  3. var Builtins = map[Symbol]ApplyFn{
  4. "lambda": lambda,
  5. "define": define,
  6. "assert": assert,
  7. "let": let,
  8. "set": set,
  9. "begin": begin,
  10. "apply": apply,
  11. "quote": quote,
  12. // "true?": True,
  13. "println": println,
  14. "if": iff,
  15. "dec": dec,
  16. "inc": inc,
  17. "+": plus,
  18. "*": times,
  19. "==": eq,
  20. }
  21. func lambda(s *Scope, args []Any) Any {
  22. c := &closure{s, args[0].([]Any), args[1:]}
  23. return c
  24. }
  25. func define(s *Scope, args []Any) Any {
  26. err := s.Add(args[0].(Symbol), s.Eval(args[1]))
  27. if err != nil {
  28. panic(err)
  29. }
  30. return nil
  31. }
  32. func assert(s *Scope, args []Any) Any {
  33. args = s.EvalAll(args)
  34. for _, v := range args {
  35. if True(s, []Any{v}) != true {
  36. panic("assert failed")
  37. }
  38. }
  39. return nil
  40. }
  41. func set(s *Scope, args []Any) Any {
  42. s.env[args[0].(Symbol)] = s.Eval(args[1])
  43. return nil
  44. }
  45. func let(s *Scope, args []Any) Any {
  46. for _, v := range args[0].([]Any) {
  47. set(s, v.([]Any))
  48. }
  49. return s.Eval(args[len(args)-1])
  50. }
  51. func begin(s *Scope, args []Any) Any {
  52. args = s.EvalAll(args)
  53. return args[len(args)-1]
  54. }
  55. func inc(s *Scope, args []Any) Any {
  56. args = s.EvalAll(args)
  57. return args[0].(int64) + 1
  58. }
  59. func dec(s *Scope, args []Any) Any {
  60. args = s.EvalAll(args)
  61. return args[0].(int64) - 1
  62. }
  63. func apply(s *Scope, args []Any) Any {
  64. f := s.Eval(args[0])
  65. arguments := s.Eval(args[1]).([]Any)
  66. switch fn := f.(type) {
  67. case Function:
  68. return fn.Apply(arguments)
  69. case ApplyFn:
  70. return fn(s, arguments)
  71. default:
  72. panic(fmt.Errorf("Not a function (apply): %s", args[0]))
  73. }
  74. }
  75. func quote(s *Scope, args []Any) Any {
  76. return args[0]
  77. }
  78. func True(s *Scope, args []Any) Any {
  79. switch x := args[0].(type) {
  80. case []Any:
  81. if len(x) < 1 {
  82. return false
  83. } else {
  84. for _, v := range x {
  85. if v.(bool) == false {
  86. return false
  87. }
  88. }
  89. }
  90. case bool:
  91. return x
  92. }
  93. return true
  94. }
  95. func println(s *Scope, args []Any) Any {
  96. args = s.EvalAll(args)
  97. for _, v := range args {
  98. fmt.Println("->>", v)
  99. }
  100. return nil
  101. }
  102. func iff(s *Scope, args []Any) Any {
  103. if True(s, s.EvalAll(args[:1])) == true {
  104. return s.Eval(args[1])
  105. } else {
  106. return s.Eval(args[2])
  107. }
  108. }
  109. func plus(s *Scope, args []Any) Any {
  110. var c int64 = 0
  111. args = s.EvalAll(args)
  112. for _, n := range args {
  113. c += n.(int64)
  114. }
  115. return c
  116. }
  117. func times(s *Scope, args []Any) Any {
  118. args = s.EvalAll(args)
  119. var c int64 = 1
  120. for _, n := range args {
  121. c *= n.(int64)
  122. }
  123. return c
  124. }
  125. func eq(s *Scope, args []Any) Any {
  126. args = s.EvalAll(args)
  127. for i := 1; i < len(args); i++ {
  128. if args[i] != args[i-1] {
  129. return false
  130. }
  131. }
  132. return true
  133. }