value.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. package muesli
  2. import "fmt"
  3. /* Run time values */
  4. type Value interface {
  5. String() string
  6. Type() TypeValue
  7. // Convert the value to a different go value which must be passed
  8. // in as a pointer to which the value must be set,
  9. // or return an error if the conversion is not possible.
  10. Convert(to interface{}) error
  11. }
  12. type IntValue int64
  13. type FloatValue float64
  14. type StringValue string
  15. type BoolValue bool
  16. type WordValue string
  17. type TypeValue string
  18. type ErrorValue struct {
  19. error
  20. }
  21. type EmptyValue struct {
  22. }
  23. type ListValue struct {
  24. RedispatchCallable
  25. List []Value
  26. }
  27. type MapValue struct {
  28. Map map[Value]Value
  29. }
  30. // Values is simply a shorthand alias for []Value
  31. type Values = []Value
  32. const (
  33. TrueValue = BoolValue(true)
  34. FalseValue = BoolValue(false)
  35. IntType = TypeValue("Int")
  36. FloatType = TypeValue("Float")
  37. StringType = TypeValue("String")
  38. BoolType = TypeValue("Bool")
  39. WordType = TypeValue("Word")
  40. ErrorType = TypeValue("Error")
  41. TypeType = TypeValue("Type")
  42. EmptyType = TypeValue("Empty")
  43. ListType = TypeValue("List")
  44. MapType = TypeValue("Map")
  45. AnyType = TypeValue("Any")
  46. ZeroType = TypeValue("")
  47. )
  48. var NilValue = Value(nil)
  49. func (val IntValue) String() string {
  50. return fmt.Sprintf("%d", int64(val))
  51. }
  52. func (val FloatValue) String() string {
  53. return fmt.Sprintf("%f", float64(val))
  54. }
  55. func (val BoolValue) String() string {
  56. if bool(val) {
  57. return "true"
  58. } else {
  59. return "false"
  60. }
  61. }
  62. func (val StringValue) String() string {
  63. return string(val)
  64. }
  65. func (val WordValue) String() string {
  66. return string(val)
  67. }
  68. func (val TypeValue) String() string {
  69. return string(val)
  70. }
  71. func (val ErrorValue) String() string {
  72. return fmt.Sprintf("%s", val.Error())
  73. }
  74. func (val EmptyValue) String() string {
  75. return "<empty>"
  76. }
  77. func (val ListValue) String() string {
  78. res := "["
  79. sep := ""
  80. for _, elt := range val.List {
  81. if elt == nil {
  82. res = res + sep + "nil"
  83. } else {
  84. res = res + sep + elt.String()
  85. }
  86. sep = ", "
  87. }
  88. res += "]"
  89. return res
  90. }
  91. func (val MapValue) String() string {
  92. res := "{"
  93. sep := ""
  94. for k, v := range val.Map {
  95. res = res + sep + k.String() + "=>" + v.String()
  96. sep = ", "
  97. }
  98. res += "}"
  99. return res
  100. }
  101. func (v IntValue) Type() TypeValue { return IntType }
  102. func (v FloatValue) Type() TypeValue { return FloatType }
  103. func (v StringValue) Type() TypeValue { return StringType }
  104. func (v BoolValue) Type() TypeValue { return BoolType }
  105. func (v WordValue) Type() TypeValue { return WordType }
  106. func (v TypeValue) Type() TypeValue { return TypeType }
  107. func (v ErrorValue) Type() TypeValue { return ErrorType }
  108. func (v EmptyValue) Type() TypeValue { return EmptyType }
  109. func (v ListValue) Type() TypeValue { return ListType }
  110. func (v MapValue) Type() TypeValue { return MapType }
  111. func NewErrorValuef(format string, args ...interface{}) ErrorValue {
  112. err := fmt.Errorf(format, args...)
  113. return ErrorValue{err}
  114. }
  115. func NewListValue(elements ...Value) * ListValue {
  116. return &ListValue{ NewRedispatchCallable(), elements}
  117. }
  118. func (list *ListValue) Append(elements ...Value) {
  119. list.List = append(list.List, elements...)
  120. }
  121. func (list *ListValue) AppendList(toAppend ListValue) {
  122. list.List = append(list.List, toAppend.List...)
  123. }
  124. func (list ListValue) Length() int {
  125. return len(list.List)
  126. }
  127. func (list *ListValue) Fetch(i int) Value {
  128. if i >= len(list.List) {
  129. return NilValue
  130. }
  131. return list.List[i]
  132. }
  133. func (list *ListValue) Place(i int, v Value) Value {
  134. if i >= len(list.List) {
  135. return NilValue
  136. }
  137. list.List[i] = v
  138. return list.List[i]
  139. }
  140. func (list *ListValue) First() Value {
  141. return list.Fetch(0)
  142. }
  143. func (list *ListValue) Last() Value {
  144. return list.Fetch(list.Length()-1)
  145. }
  146. func EmptyListValue() ListValue {
  147. return ListValue{NewRedispatchCallable(), make([]Value, 0)}
  148. }
  149. func EmptyValueArray() []Value {
  150. return make([]Value, 0)
  151. }
  152. func NewValueArray(elements ...Value) []Value {
  153. return elements
  154. }
  155. func NewMapValue(elements map[Value]Value) * MapValue {
  156. return &MapValue{elements}
  157. }
  158. func (m *MapValue) Fetch(key Value) Value {
  159. var res Value
  160. var ok bool
  161. if res, ok = m.Map[key] ; !ok {
  162. return NilValue
  163. }
  164. return res
  165. }
  166. func (m *MapValue) Place(key Value, value Value) Value {
  167. m.Map[key] = value
  168. return m.Map[key]
  169. }
  170. func (from IntValue) Convert(to interface{}) error {
  171. switch toPtr := to.(type) {
  172. case *string:
  173. (*toPtr) = from.String()
  174. case *int8:
  175. (*toPtr) = int8(from)
  176. case *int16:
  177. (*toPtr) = int16(from)
  178. case *int32:
  179. (*toPtr) = int32(from)
  180. case *int64:
  181. (*toPtr) = int64(from)
  182. case *int:
  183. (*toPtr) = int(from)
  184. case *bool:
  185. (*toPtr) = (from != 0)
  186. case *float32:
  187. (*toPtr) = float32(from)
  188. case *float64:
  189. (*toPtr) = float64(from)
  190. case *IntValue:
  191. (*toPtr) = from
  192. case *Value:
  193. (*toPtr) = from
  194. default:
  195. return NewErrorValuef("Cannot convert IntValue value %v to %v", from, to)
  196. }
  197. return nil
  198. }
  199. func (from FloatValue) Convert(to interface{}) error {
  200. switch toPtr := to.(type) {
  201. case *string:
  202. (*toPtr) = from.String()
  203. case *int8:
  204. (*toPtr) = int8(from)
  205. case *int16:
  206. (*toPtr) = int16(from)
  207. case *int32:
  208. (*toPtr) = int32(from)
  209. case *int64:
  210. (*toPtr) = int64(from)
  211. case *int:
  212. (*toPtr) = int(from)
  213. case *bool:
  214. (*toPtr) = (from != 0)
  215. case *float32:
  216. (*toPtr) = float32(from)
  217. case *float64:
  218. (*toPtr) = float64(from)
  219. case *FloatValue:
  220. (*toPtr) = from
  221. case *Value:
  222. (*toPtr) = from
  223. default:
  224. return NewErrorValuef("Cannot convert FloatValue value %v to %v", from, to)
  225. }
  226. return nil
  227. }
  228. func (from StringValue) Convert(to interface{}) error {
  229. switch toPtr := to.(type) {
  230. case *string:
  231. (*toPtr) = from.String()
  232. case *StringValue:
  233. (*toPtr) = from
  234. case *Value:
  235. (*toPtr) = from
  236. default:
  237. return NewErrorValuef("Cannot convert StringValue %v to %v", from, to)
  238. }
  239. return nil
  240. }
  241. func (from WordValue) Convert(to interface{}) error {
  242. switch toPtr := to.(type) {
  243. case *string:
  244. (*toPtr) = from.String()
  245. case *WordValue:
  246. (*toPtr) = from
  247. case *Value:
  248. (*toPtr) = from
  249. default:
  250. return NewErrorValuef("Cannot convert WordValue %v to %v", from, to)
  251. }
  252. return nil
  253. }
  254. func (from TypeValue) Convert(to interface{}) error {
  255. switch toPtr := to.(type) {
  256. case *string:
  257. (*toPtr) = from.String()
  258. case *TypeValue:
  259. (*toPtr) = from
  260. case *Value:
  261. (*toPtr) = from
  262. default:
  263. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  264. }
  265. return nil
  266. }
  267. func (from BoolValue) Convert(to interface{}) error {
  268. switch toPtr := to.(type) {
  269. case *bool:
  270. (*toPtr) = bool(from)
  271. case *BoolValue:
  272. (*toPtr) = from
  273. case *Value:
  274. (*toPtr) = from
  275. default:
  276. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  277. }
  278. return nil
  279. }
  280. func (from ErrorValue) Convert(to interface{}) error {
  281. switch toPtr := to.(type) {
  282. case *string:
  283. (*toPtr) = from.String()
  284. case *error:
  285. (*toPtr) = from.error
  286. case *ErrorValue:
  287. (*toPtr) = from
  288. case *Value:
  289. (*toPtr) = from
  290. default:
  291. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  292. }
  293. return nil
  294. }
  295. func (from EmptyValue) Convert(to interface{}) error {
  296. return NewErrorValuef("Cannot convert the empty value %v to %v", from, to)
  297. }
  298. func (from * ListValue) Convert(to interface{}) error {
  299. switch toPtr := to.(type) {
  300. case *[]Value:
  301. (*toPtr) = from.List
  302. case **ListValue:
  303. (*toPtr) = from
  304. case *Value:
  305. (*toPtr) = from
  306. default:
  307. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  308. }
  309. return nil
  310. }
  311. func (from * MapValue) Convert(to interface{}) error {
  312. switch toPtr := to.(type) {
  313. case *map[Value]Value:
  314. (*toPtr) = from.Map
  315. case **MapValue:
  316. (*toPtr) = from
  317. case *Value:
  318. (*toPtr) = from
  319. default:
  320. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  321. }
  322. return nil
  323. }
  324. /* Helpers to easily convert Muesli values to "normal" Go values. */
  325. func From(from Value, to interface{}) error {
  326. return from.Convert(to)
  327. }
  328. // ParseOptArgs is helper to easily convert Muesli value lists to "normal"
  329. // Go values. args is the input, required is the amount of required aruments,
  330. // and to are pointers to where to store the data. The args are converted by
  331. // calling the Convert() method on them.
  332. // The returned array contains the remaining unparsed arguments.
  333. func ParseOptArgs(args []Value, required int, to...interface{}) ([]Value, error) {
  334. if required > len(args) {
  335. return nil, NewErrorValuef("Too few arguments, expected %d, got %d", required, len(args))
  336. }
  337. stop := len(args)
  338. if len(to) < stop {
  339. stop = len(to)
  340. }
  341. i:= 0
  342. for ; i < stop ; i ++ {
  343. fromElt := args[i]
  344. toElt := to[i]
  345. if fromElt == nil {
  346. return nil, NewErrorValuef("Nil pointer to result %d", i)
  347. }
  348. err := fromElt.Convert(toElt)
  349. if err != nil {
  350. return nil, err
  351. }
  352. }
  353. rest := args[i:len(args)]
  354. return rest, nil
  355. }
  356. // ParseArgs is helper to easily convert Muesli value lists to "normal"
  357. // Go values. It is the same as ParseOptArgs(args, len(to), to...)
  358. func ParseArgs(args []Value, to...interface{}) ([]Value, error) {
  359. return ParseOptArgs(args, len(to), to...)
  360. }
  361. /* Helpers to easily convert Muesli value lists to "normal" Go values in slices. */
  362. func ParseArgsToIntSlice(args []Value) ([]int, error) {
  363. res := []int{}
  364. for _, arg := range args{
  365. value := int(0)
  366. err := arg.Convert(&value)
  367. if err != nil {
  368. return res, err
  369. }
  370. res = append(res, value)
  371. }
  372. return res, nil
  373. }
  374. /* Helpers to easily convert Muesli value lists to "normal" Go values in slices. */
  375. func ParseArgsToFloat64Slice(args []Value) ([]float64, error) {
  376. res := []float64{}
  377. for _, arg := range args{
  378. value := float64(0.0)
  379. err := arg.Convert(&value)
  380. if err != nil {
  381. return res, err
  382. }
  383. res = append(res, value)
  384. }
  385. return res, nil
  386. }
  387. /* Helpers to easily convert Muesli values from "normal" Go values. */
  388. func To(from interface{}) Value {
  389. switch val := from.(type) {
  390. case string:
  391. return StringValue(val)
  392. case int8:
  393. return IntValue(val)
  394. case int16:
  395. return IntValue(val)
  396. case int32:
  397. return IntValue(val)
  398. case int64:
  399. return IntValue(val)
  400. case int:
  401. return IntValue(val)
  402. case bool:
  403. return BoolValue(val)
  404. case float32:
  405. return FloatValue(val)
  406. case float64:
  407. return FloatValue(val)
  408. case error:
  409. return ErrorValue{val}
  410. case Value:
  411. return val
  412. default:
  413. return NewErrorValuef("Cannot convert value %v", from)
  414. }
  415. }
  416. func ListTo(froms ...interface{}) []Value {
  417. list := make([]Value, 0)
  418. for _, from := range froms {
  419. val := To(from)
  420. list = append(list, val)
  421. }
  422. return list
  423. }
  424. func ListFrom(froms []Value, tos ...interface{}) error {
  425. for i, from := range froms {
  426. if i >= len(tos) {
  427. break
  428. }
  429. err := From(from, tos[i])
  430. if err != nil {
  431. return err
  432. }
  433. }
  434. return nil
  435. }
  436. func ListFromList(froms []Value) []interface{} {
  437. res := make([]interface{}, len(froms))
  438. for i, from := range froms {
  439. res[i] = from
  440. }
  441. return res
  442. }
  443. func Return(args ... Value) []Value {
  444. return args
  445. }
  446. func ReturnEmpty() []Value {
  447. return []Value{EmptyValue{}}
  448. }
  449. func ReturnError(err error) []Value {
  450. return []Value{ErrorValue{err}}
  451. }
  452. func Ok(args ... Value) []Value {
  453. return args
  454. }
  455. func Fail(err error) []Value {
  456. return []Value{ErrorValue{err}}
  457. }
  458. func None() []Value {
  459. return []Value{EmptyValue{}}
  460. }
  461. func BoolOk(b bool) []Value{
  462. return []Value{BoolValue(b)}
  463. }
  464. func IntOk(i int) []Value{
  465. return []Value{IntValue(i)}
  466. }
  467. func FloatOk(f float64) []Value{
  468. return []Value{FloatValue(f)}
  469. }
  470. // CallableValue is is both a Value and a Callable.
  471. type CallableValue interface {
  472. Callable
  473. Value
  474. }
  475. // Redispath takes the first argument i args, and if that is a word,
  476. // calls the command named by that word, replacing the from CalableValue
  477. // as the first argument. This allows some OOP style trickery where
  478. // custom_value foo gets called as foo custom_value where custom_value is
  479. // of CustomType.
  480. // In this case foo should the be an overloaded command that takes CustomType.
  481. func Redispatch(from CallableValue, vm *VM, args...Value) []Value {
  482. if len(args) < 1 {
  483. return EmptyValueArray()
  484. }
  485. var word WordValue
  486. _, err := ParseOptArgs(args, 1, &word)
  487. if err != nil {
  488. return Fail(err)
  489. }
  490. args[0] = from
  491. return vm.CallNamed(string(word), args...)
  492. }
  493. // RedispatchCallable can be embedded into a type to allow it to
  494. // become redispatchable, that is, it can support OOP style
  495. // calling of pseudo methods by rearranging the command.
  496. type RedispatchCallable struct {
  497. BasicCallable
  498. }
  499. func (rc * RedispatchCallable) Call(vm *VM, args...Value) []Value {
  500. return Redispatch(rc, vm, args...)
  501. }
  502. var _ Callable = &RedispatchCallable{}
  503. func NewRedispatchCallable() RedispatchCallable {
  504. return RedispatchCallable{}
  505. }
  506. // Implement callable ...
  507. /*
  508. func (list * ListValue) Position() *Position {
  509. return &Position{"value.go", 572, 8}
  510. }
  511. func (list * ListValue) Call(vm *VM, args...Value) []Value {
  512. return Redispatch(list, vm, args...)
  513. }
  514. var listSignature = NewSignature(WordType, IntType)
  515. func (list * ListValue) Signature() Signature {
  516. return listSignature
  517. }
  518. */
  519. var _ Callable = &ListValue{}