builtin.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. package attl
  2. func p(env *Environment, args ...Value) (Value, Effect) {
  3. for _, arg := range args {
  4. print(arg, " ")
  5. }
  6. print("\n")
  7. return nil, nil
  8. }
  9. func print_(env *Environment, args ...Value) (Value, Effect) {
  10. var msg string
  11. erra := Args(args, &msg)
  12. if erra != nil {
  13. return env.FailString("printf: ${1}", erra)
  14. }
  15. extra := []Value{}
  16. if len(args) > 1 {
  17. extra = args[1:len(args)]
  18. }
  19. n, err := env.Printi(msg, extra...)
  20. if err == nil {
  21. return Int(n), nil
  22. }
  23. return Int(n), ErrorFromError(err)
  24. }
  25. func write(env *Environment, args ...Value) (Value, Effect) {
  26. var msg string
  27. erra := Args(args, &msg)
  28. if erra != nil {
  29. return env.FailString("write: ${1}", erra)
  30. }
  31. n, err := env.Write(msg)
  32. if err == nil {
  33. return Int(n), nil
  34. }
  35. return Int(n), ErrorFromError(err)
  36. }
  37. func iadd(env *Environment, args ...Value) (Value, Effect) {
  38. var i, j int
  39. err := Args(args, &i, &j)
  40. if err != nil {
  41. return env.Fail(err)
  42. }
  43. return Int(i + j), nil
  44. }
  45. func isub(env *Environment, args ...Value) (Value, Effect) {
  46. var v1, v2 int
  47. err := Args(args, &v1, &v2)
  48. if err != nil {
  49. return env.Fail(err)
  50. }
  51. return Int(v1 - v2), nil
  52. }
  53. func imul(env *Environment, args ...Value) (Value, Effect) {
  54. var v1, v2 int
  55. err := Args(args, &v1, &v2)
  56. if err != nil {
  57. return env.Fail(err)
  58. }
  59. return Int(v1 * v2), nil
  60. }
  61. func idiv(env *Environment, args ...Value) (Value, Effect) {
  62. var v1, v2 int
  63. err := Args(args, &v1, &v2)
  64. if err != nil {
  65. return env.Fail(err)
  66. }
  67. if v2 == 0 {
  68. return nil, ErrorFromString("division by 0")
  69. }
  70. return Int(v1 / v2), nil
  71. }
  72. func igt(env *Environment, args ...Value) (Value, Effect) {
  73. var v1, v2 int
  74. err := Args(args, &v1, &v2)
  75. if err != nil {
  76. return env.Fail(err)
  77. }
  78. return Bool(v1 > v2), nil
  79. }
  80. func ilt(env *Environment, args ...Value) (Value, Effect) {
  81. var v1, v2 int
  82. err := Args(args, &v1, &v2)
  83. if err != nil {
  84. return env.Fail(err)
  85. }
  86. return Bool(v1 < v2), nil
  87. }
  88. func ige(env *Environment, args ...Value) (Value, Effect) {
  89. var v1, v2 int
  90. err := Args(args, &v1, &v2)
  91. if err != nil {
  92. return env.Fail(err)
  93. }
  94. return Bool(v1 >= v2), nil
  95. }
  96. func ile(env *Environment, args ...Value) (Value, Effect) {
  97. var v1, v2 int
  98. err := Args(args, &v1, &v2)
  99. if err != nil {
  100. return env.Fail(err)
  101. }
  102. return Bool(v1 <= v2), nil
  103. }
  104. func ieq(env *Environment, args ...Value) (Value, Effect) {
  105. var v1, v2 int
  106. err := Args(args, &v1, &v2)
  107. if err != nil {
  108. return env.Fail(err)
  109. }
  110. return Bool(v1 == v2), nil
  111. }
  112. func seq(env *Environment, args ...Value) (Value, Effect) {
  113. var v1, v2 string
  114. err := Args(args, &v1, &v2)
  115. if err != nil {
  116. return env.Fail(err)
  117. }
  118. return Bool(v1 == v2), nil
  119. }
  120. func teq(env *Environment, args ...Value) (Value, Effect) {
  121. var t1, t2 Type
  122. err := Args(args, &t1, &t2)
  123. if err != nil {
  124. return env.Fail(err)
  125. }
  126. return Bool(t1 == t2), nil
  127. }
  128. func updateIntByName(update func(in Int) Int, env *Environment, args ...Value) (Int, Effect) {
  129. var name Word
  130. err := Args(args, &name)
  131. if err != nil {
  132. return Int(0), err
  133. }
  134. val := env.Lookup(name.String())
  135. vi, ok := val.(Int)
  136. if !ok {
  137. return Int(0), ErrorFromString("Not an integer.")
  138. }
  139. newi := update(vi)
  140. env.Set(name.String(), newi)
  141. return newi, nil
  142. }
  143. func inc(env *Environment, args ...Value) (Value, Effect) {
  144. return updateIntByName(func(in Int) Int {
  145. return in + 1
  146. }, env, args...)
  147. }
  148. func dec(env *Environment, args ...Value) (Value, Effect) {
  149. return updateIntByName(func(in Int) Int {
  150. return in - 1
  151. }, env, args...)
  152. }
  153. func str(env *Environment, args ...Value) (Value, Effect) {
  154. var v1 Value
  155. err := Args(args, &v1)
  156. if err != nil {
  157. return env.Fail(err)
  158. }
  159. return String(v1.String()), nil
  160. }
  161. func int_(env *Environment, args ...Value) (Value, Effect) {
  162. var v1 Value
  163. err := Args(args, &v1)
  164. if err != nil {
  165. return env.Fail(err)
  166. }
  167. rs := []rune(v1.String() + " ")
  168. index := 0
  169. return ParseInteger(rs, &index)
  170. }
  171. func boolBinop(op func(b1, b2 bool) bool, env *Environment, args ...Value) (Value, Effect) {
  172. var v1, v2 bool
  173. err := Args(args, &v1, &v2)
  174. if err != nil {
  175. return env.Fail(err)
  176. }
  177. return Bool(op(v1, v2)), nil
  178. }
  179. func isnil(env *Environment, args ...Value) (Value, Effect) {
  180. if len(args) < 1 {
  181. return env.FailString("isnil requires 1 argument")
  182. }
  183. return Bool(args[0] == nil), nil
  184. }
  185. func band(env *Environment, args ...Value) (Value, Effect) {
  186. return boolBinop(func(b1, b2 bool) bool {
  187. return b1 && b2
  188. }, env, args...)
  189. }
  190. func bor(env *Environment, args ...Value) (Value, Effect) {
  191. return boolBinop(func(b1, b2 bool) bool {
  192. return b1 || b2
  193. }, env, args...)
  194. }
  195. func bxor(env *Environment, args ...Value) (Value, Effect) {
  196. return boolBinop(func(b1, b2 bool) bool {
  197. return b1 != b2
  198. }, env, args...)
  199. }
  200. func bnot(env *Environment, args ...Value) (Value, Effect) {
  201. var v1 bool
  202. err := Args(args, &v1)
  203. if err != nil {
  204. return env.Fail(err)
  205. }
  206. return Bool(!v1), nil
  207. }
  208. func val(env *Environment, args ...Value) (Value, Effect) {
  209. if len(args) < 1 {
  210. return env.FailString("val requres at least one argument.")
  211. }
  212. return List(args), nil
  213. }
  214. func ret(env *Environment, args ...Value) (Value, Effect) {
  215. if len(args) < 1 {
  216. return env.Return(nil)
  217. } else if len(args) == 1 {
  218. return env.Return(args[0])
  219. } else {
  220. return env.Return(List(args))
  221. }
  222. }
  223. func fail(env *Environment, args ...Value) (Value, Effect) {
  224. if len(args) < 1 {
  225. return env.Fail(ErrorFromString("fail"))
  226. } else {
  227. return env.FailString(args[0].String(), args[1:len(args)]...)
  228. }
  229. }
  230. func break_(env *Environment, args ...Value) (Value, Effect) {
  231. if len(args) < 1 {
  232. return env.Break(nil)
  233. } else if len(args) == 1 {
  234. return env.Break(args[0])
  235. } else {
  236. return env.Break(List(args))
  237. }
  238. }
  239. func nop(env *Environment, args ...Value) (Value, Effect) {
  240. return nil, nil
  241. }
  242. func typeof_(env *Environment, args ...Value) (Value, Effect) {
  243. var val Value
  244. err := Args(args, &val)
  245. if err != nil {
  246. return nil, err
  247. }
  248. return TypeOf(val), nil
  249. }
  250. func type_(env *Environment, args ...Value) (Value, Effect) {
  251. var val Value
  252. err := Args(args, &val)
  253. if err != nil {
  254. return nil, err
  255. }
  256. name := val.String()
  257. return Type(name), nil
  258. }
  259. func to(env *Environment, args ...Value) (Value, Effect) {
  260. var name string
  261. if len(args) < 2 {
  262. return env.FailString("to needs at least 2 arguments")
  263. }
  264. err := Convert(args[0], &name)
  265. if err != nil {
  266. return env.Fail(err)
  267. }
  268. block, ok := (args[len(args)-1]).(Block)
  269. if !ok {
  270. return env.FailString("to: last argument must be a block")
  271. }
  272. last := args[len(args)-1]
  273. block, isBlock := last.(Block)
  274. if !isBlock {
  275. return env.FailString("Not a block")
  276. }
  277. params := args[1 : len(args)-1]
  278. defined := Defined{name, params, block}
  279. env.Define(name, defined, 1)
  280. return defined, nil
  281. }
  282. func do(env *Environment, args ...Value) (Value, Effect) {
  283. var name string
  284. var doArgs List
  285. err := Args(args, &name, &doArgs)
  286. if err != nil {
  287. return env.Fail(err)
  288. }
  289. fun := env.Lookup(name)
  290. if fun == nil {
  291. return env.FailString("Cannot evaluate unknown order: " + name)
  292. }
  293. eva, ok := fun.(Evaler)
  294. if !ok {
  295. return env.FailString("Cannot evaluate: " + name)
  296. }
  297. return eva.Eval(env, doArgs...)
  298. }
  299. func if_(env *Environment, args ...Value) (Value, Effect) {
  300. var cond, ok, haveElse bool
  301. var ifBlock, elseBlock Block
  302. if len(args) < 2 {
  303. return env.FailString("if needs at least 2 arguments")
  304. }
  305. if len(args) > 4 {
  306. return env.FailString("if needs at most 4 arguments")
  307. }
  308. err := Convert(args[0], &cond)
  309. if err != nil {
  310. return env.Fail(err)
  311. }
  312. ifBlock, ok = (args[1]).(Block)
  313. if !ok {
  314. return env.FailString("if: second argument must be a block")
  315. }
  316. elseIndex := 2
  317. if 2 < len(args) {
  318. // look for an else keyword but don't mind if it really is else
  319. _, ok = (args[2]).(Word)
  320. if ok {
  321. // block after else keyword
  322. elseIndex = 3
  323. }
  324. }
  325. if elseIndex < len(args) {
  326. // There should be an else block...
  327. elseBlock, ok = (args[elseIndex]).(Block)
  328. if !ok {
  329. return env.FailString("if: missing else block")
  330. }
  331. haveElse = true
  332. }
  333. if cond {
  334. return ifBlock.Eval(env, args...)
  335. } else {
  336. if haveElse {
  337. return elseBlock.Eval(env, args...)
  338. } else {
  339. return nil, nil
  340. }
  341. }
  342. }
  343. func switch_(env *Environment, args ...Value) (Value, Effect) {
  344. var defaultBlock Block
  345. var haveDefault bool = false
  346. if len(args) < 3 {
  347. return env.FailString("switch needs at least 3 arguments")
  348. }
  349. compareTo := args[0]
  350. for i := 2; i < len(args); i += 2 {
  351. case_ := args[i-1]
  352. block, blockOk := args[i].(Block)
  353. if !blockOk {
  354. return env.FailString("switch: argument ${1} is not a block",
  355. Int(i))
  356. }
  357. if kw, kwOk := case_.(Word); kwOk && kw.String() == "default" {
  358. if haveDefault {
  359. return env.FailString("switch: duplicate default block ${1}",
  360. Int(i))
  361. }
  362. haveDefault = true
  363. defaultBlock = block
  364. } else {
  365. if compareTo.String() == case_.String() {
  366. return block.Eval(env, args...)
  367. }
  368. }
  369. }
  370. if haveDefault {
  371. return defaultBlock.Eval(env, args...)
  372. }
  373. return nil, nil
  374. }
  375. func while(env *Environment, args ...Value) (Value, Effect) {
  376. var blockRes Value
  377. var blockEff Effect
  378. if len(args) != 2 {
  379. return env.FailString("while needs exactly 3 arguments")
  380. }
  381. cond, condOk := args[0].(Block)
  382. block, blockOk := args[1].(Block)
  383. if !condOk {
  384. return env.FailString("while condition must be a block")
  385. }
  386. if !blockOk {
  387. return env.FailString("while body must be a block")
  388. }
  389. for res, eff := cond.Eval(env, args...); ValToBool(res); res, eff = cond.Eval(env, args...) {
  390. if eff != nil && eff.Flow() > NormalFlow {
  391. return res, eff
  392. }
  393. blockRes, blockEff = block.Eval(env, args...)
  394. if blockEff != nil && blockEff.Flow() > NormalFlow {
  395. return blockRes, blockEff
  396. }
  397. }
  398. return blockRes, blockEff
  399. }
  400. func rescue(env *Environment, args ...Value) (Value, Effect) {
  401. var block Block
  402. err := Args(args, &block)
  403. if err != nil {
  404. return env.Fail(err)
  405. }
  406. return env.Prevent(block)
  407. }
  408. func set(env *Environment, args ...Value) (Value, Effect) {
  409. if len(args) < 2 {
  410. return env.FailString("set needs at 2 arguments")
  411. }
  412. if args[0] == nil {
  413. return env.FailString("set $1 is nil")
  414. }
  415. return env.Set(args[0].String(), args[1])
  416. }
  417. func let(env *Environment, args ...Value) (Value, Effect) {
  418. if len(args) < 2 {
  419. return env.FailString("def needs at 2 arguments")
  420. }
  421. if args[0] == nil {
  422. return env.FailString("def $1 is nil")
  423. }
  424. return env.Define(args[0].String(), args[1], 1)
  425. }
  426. func get(env *Environment, val ...Value) (Value, Effect) {
  427. if len(val) < 1 {
  428. return env.FailString("get needs at least 1 argument")
  429. }
  430. target := val[0].String()
  431. return env.Lookup(target), nil
  432. }
  433. func list(env *Environment, args ...Value) (Value, Effect) {
  434. return List(args), nil
  435. }
  436. func sadd(env *Environment, args ...Value) (Value, Effect) {
  437. var value Value
  438. var str String
  439. err := Args(args, &str, &value)
  440. if err != nil {
  441. return env.Fail(err)
  442. }
  443. str = str + String(value.String())
  444. return str, nil
  445. }
  446. func sget(env *Environment, args ...Value) (Value, Effect) {
  447. var index int
  448. var str String
  449. err := Args(args, &str, &index)
  450. if err != nil {
  451. return env.Fail(err)
  452. }
  453. runes := []rune(str)
  454. if (index < 0) || (index >= len(runes)) {
  455. return env.FailString("index out of range")
  456. }
  457. return Int(runes[index]), nil
  458. }
  459. func runes(env *Environment, args ...Value) (Value, Effect) {
  460. var str String
  461. err := Args(args, &str)
  462. if err != nil {
  463. return env.Fail(err)
  464. }
  465. res := List{}
  466. runes := []rune(str)
  467. for i := 0; i < len(runes); i++ {
  468. res = append(res, Int(runes[i]))
  469. }
  470. return res, nil
  471. }
  472. func wire(env *Environment, args ...Value) (Value, Effect) {
  473. var str String
  474. for i := 0; i < len(args); i++ {
  475. var ch Int
  476. err := Convert(args[i], &ch)
  477. if err != nil {
  478. return str, err
  479. }
  480. str = str + String([]rune{rune(ch)})
  481. }
  482. return str, nil
  483. }
  484. func slen(env *Environment, args ...Value) (Value, Effect) {
  485. var str String
  486. err := Args(args, &str)
  487. if err != nil {
  488. return env.Fail(err)
  489. }
  490. runes := []rune(str)
  491. return Int(len(runes)), nil
  492. }
  493. func ladd(env *Environment, args ...Value) (Value, Effect) {
  494. var value Value
  495. var list List
  496. err := Args(args, &list, &value)
  497. if err != nil {
  498. return env.Fail(err)
  499. }
  500. list = append(list, value)
  501. return list, nil
  502. }
  503. func lget(env *Environment, args ...Value) (Value, Effect) {
  504. var index int
  505. var list List
  506. err := Args(args, &list, &index)
  507. if err != nil {
  508. return env.Fail(err)
  509. }
  510. if (index < 0) || (index >= len(list)) {
  511. return env.FailString("index out of range")
  512. }
  513. return list[index], nil
  514. }
  515. func lset(env *Environment, args ...Value) (Value, Effect) {
  516. var index int
  517. var list List
  518. var val Value
  519. err := Args(args, &list, &index, &val)
  520. if err != nil {
  521. return env.Fail(err)
  522. }
  523. if (index < 0) || (index >= len(list)) {
  524. return env.FailString("index out of range")
  525. }
  526. list[index] = val
  527. return list[index], nil
  528. }
  529. func llen(env *Environment, args ...Value) (Value, Effect) {
  530. var list List
  531. err := Args(args, &list)
  532. if err != nil {
  533. return env.Fail(err)
  534. }
  535. return Int(len(list)), nil
  536. }
  537. func lsort(env *Environment, args ...Value) (Value, Effect) {
  538. var list List
  539. err := Args(args, &list)
  540. if err != nil {
  541. return env.Fail(err)
  542. }
  543. return list.SortStrings(), nil
  544. }
  545. func leach(env *Environment, args ...Value) (Value, Effect) {
  546. var list List
  547. var key Word
  548. var name Word
  549. var block Block
  550. err := Args(args, &list, &key, &name, &block)
  551. if err != nil {
  552. return env.Fail(err)
  553. }
  554. for i, v := range list {
  555. env.Define(key.String(), Int(i), 0)
  556. env.Define(name.String(), v, 0)
  557. bval, berr := block.Eval(env, args...)
  558. if berr != nil {
  559. return bval, berr
  560. }
  561. }
  562. return list, nil
  563. }
  564. func lslice(env *Environment, args ...Value) (Value, Effect) {
  565. var list List
  566. var from Int
  567. var to Int
  568. err := Args(args, &list, &from, &to)
  569. if err != nil {
  570. return env.Fail(err)
  571. }
  572. length := Int(len(list))
  573. if length == 0 {
  574. return list, nil
  575. }
  576. if from < 0 {
  577. from = length - from
  578. }
  579. if to < 0 {
  580. from = length - from
  581. }
  582. if from >= length {
  583. from = length - 1
  584. }
  585. if to >= length {
  586. to = length - 1
  587. }
  588. if from > to {
  589. from, to = to, from
  590. }
  591. return list[from:to], nil
  592. }
  593. func map_(env *Environment, args ...Value) (Value, Effect) {
  594. res := make(Map)
  595. for i := 1; i < len(args); i += 2 {
  596. key := args[i-1]
  597. val := args[i]
  598. res[key.String()] = val
  599. }
  600. return res, nil
  601. }
  602. func mget(env *Environment, args ...Value) (Value, Effect) {
  603. var index string
  604. var hmap Map
  605. err := Args(args, &hmap, &index)
  606. if err != nil {
  607. return env.Fail(err)
  608. }
  609. return hmap[index], nil
  610. }
  611. func mset(env *Environment, args ...Value) (Value, Effect) {
  612. var index string
  613. var hmap Map
  614. var val Value
  615. err := Args(args, &hmap, &index, &val)
  616. if err != nil {
  617. return env.Fail(err)
  618. }
  619. hmap[index] = val
  620. return hmap[index], nil
  621. }
  622. func mkeys(env *Environment, args ...Value) (Value, Effect) {
  623. var hmap Map
  624. err := Args(args, &hmap)
  625. if err != nil {
  626. return env.Fail(err)
  627. }
  628. res := List{}
  629. for k, _ := range hmap {
  630. res = append(res, String(k))
  631. }
  632. return res, nil
  633. }
  634. func meach(env *Environment, args ...Value) (Value, Effect) {
  635. var map_ Map
  636. var key Word
  637. var name Word
  638. var block Block
  639. err := Args(args, &map_, &key, &name, &block)
  640. if err != nil {
  641. return env.Fail(err)
  642. }
  643. miter := Map{}
  644. for k, v := range map_ {
  645. miter[k] = v
  646. }
  647. for k, v := range miter {
  648. env.Define(key.String(), String(k), 0)
  649. env.Define(name.String(), v, 0)
  650. bval, beff := block.Eval(env, args...)
  651. if beff != nil {
  652. return bval, beff
  653. }
  654. }
  655. return map_, nil
  656. }
  657. func expand(env *Environment, args ...Value) (Value, Effect) {
  658. var msg string
  659. err := Args(args, &msg)
  660. if err != nil {
  661. return env.Fail(err)
  662. }
  663. res := env.Interpolate(msg)
  664. return String(res), nil
  665. }
  666. func help(env *Environment, args ...Value) (Value, Effect) {
  667. var name string
  668. err := Args(args, &name)
  669. if err != nil {
  670. return env.Fail(err)
  671. }
  672. helpMap := env.Lookup("HELP")
  673. if helpMap == nil {
  674. env.Printi("help: $1:No help available 1.\n", String(name))
  675. return nil, err
  676. }
  677. if name == "all" {
  678. keys := helpMap.(Map).SortedKeys()
  679. for _, k := range keys {
  680. v := helpMap.(Map)[k.String()]
  681. env.Printi("$1: $2\n", k, v)
  682. }
  683. return nil, nil
  684. }
  685. msg, ok := helpMap.(Map)[name]
  686. if ok {
  687. env.Printi("help: $1:\n$2\n", String(name), msg)
  688. } else {
  689. env.Printi("help: $1:No help available 2.\n", String(name))
  690. }
  691. return msg, nil
  692. }
  693. func explain(env *Environment, args ...Value) (Value, Effect) {
  694. var name string
  695. var help String
  696. err := Args(args, &name, &help)
  697. if err != nil {
  698. return env.Fail(err)
  699. }
  700. helpMap := env.Lookup("HELP")
  701. if helpMap == nil {
  702. helpMap = make(Map)
  703. }
  704. helpMap.(Map)[name] = help
  705. env.Define("HELP", helpMap, -1)
  706. return help, nil
  707. }
  708. func overload(env *Environment, args ...Value) (Value, Effect) {
  709. var name string
  710. var target Value
  711. err := Args(args, &name, &target)
  712. if err != nil {
  713. return env.Fail(err)
  714. }
  715. if len(args) < 3 {
  716. return env.FailString("overload needs at least 3 arguments")
  717. }
  718. return env.Overload(name, target, args[2:len(args)])
  719. }
  720. func (env *Environment) Register(name string,
  721. f func(e *Environment, args ...Value) (Value, Effect), help string) {
  722. env.Define(name, Proc(f), -1)
  723. explain(env, String(name), String(help))
  724. }
  725. func (env *Environment) RegisterBuiltins() {
  726. env.Define("true", Bool(true), -1)
  727. env.Define("false", Bool(false), -1)
  728. env.Register("sadd", sadd, "returns a string with $2 appended to string $1")
  729. env.Register("sget", sget, "gets a rune from a string by index")
  730. env.Register("slen", slen, "returns the length of a string")
  731. env.Register("iadd", iadd, "adds two integers together")
  732. env.Register("band", band, `returns true if $1 and $2 arguments are true`)
  733. env.Register("bor", bor, `returns true if $1 or $2 arguments are true`)
  734. env.Register("bxor", bxor, `returns true if $1 and $2 are different booleans`)
  735. env.Register("bnot", bnot, `returns true if $1 is false and false otherwise`)
  736. env.Register("ladd", ladd, "returns a list with $2 appended to List $1")
  737. env.Register("list", list, "creates a new array list")
  738. env.Register("lget", lget, "gets a value from a list by index")
  739. env.Register("lset", lset, "sets a value to a list by index and value")
  740. env.Register("llen", llen, "returns the length of a list")
  741. env.Register("lsort", lsort, "returns the List $1 sorted by string value")
  742. env.Register("leach", leach, "calls the block $4 for each entry in the list")
  743. env.Register("lslice", lslice, "slices the list $1 from $2 to $3")
  744. env.Register("iadd", iadd, "adds and Ints to and Int")
  745. env.Register("isub", isub, "subtracts an Int from an Int")
  746. env.Register("imul", imul, "multiplies an Ints by an Int")
  747. env.Register("idiv", idiv, "divides an Int by an Int")
  748. env.Register("ilt", ilt, "checks if $1 < $2, where $1 and $2 must be Int")
  749. env.Register("ile", ile, "checks if $1 <= $2, where $1 and $2 must be Int")
  750. env.Register("igt", igt, "checks if $1 > $2, where $1 and $2 must be Int")
  751. env.Register("ige", ige, "checks if $1 >= $2, where $1 and $2 must be Int")
  752. env.Register("ieq", ieq, "checks if $1 == $2, where $1 and $2 must be Int")
  753. env.Register("seq", seq, "checks if [str $1] == [str $2]")
  754. env.Register("str", str, "converts $1 to String")
  755. env.Register("wire", wire, "converts unicode character indexes or runes to String")
  756. env.Register("runes", runes, "converts String to alist of character indexes or runes")
  757. env.Register("int", int_, "converts $1 to Int")
  758. env.Register("inc", inc, "increments the named integer $1")
  759. env.Register("dec", dec, "decrements the named integer $1")
  760. env.Register("map", map_, "creates a new hash map")
  761. env.Register("mget", mget, "gets a value from a map by key")
  762. env.Register("mset", mset, "sets a value to a map by key and value")
  763. env.Register("mkeys", mkeys, "returns all keys of a map as an unsorted list")
  764. env.Register("meach", meach, "calls the block $4 for each entry in the map")
  765. env.Register("p", p, "print debug output")
  766. env.Register("print", print_, "print to the environnment's current writer with interpolation")
  767. env.Register("write", write, "write to the environnment's current writer")
  768. env.Register("to", to, "define a procedure")
  769. env.Register("do", do, "execute a command $1 with arguments in $2 as array")
  770. env.Register("ret", ret, "return from a procedure")
  771. env.Register("return", ret, "return from a procedure")
  772. env.Register("break", break_, "return from a block")
  773. env.Register("val", val, "gets the value of a value")
  774. env.Register("let", let, "creates a new vavariable with given value")
  775. env.Register("set", set, "sets an existing variable")
  776. env.Register("get", get, "get the contents of a variable")
  777. env.Register("help", help, "get help for a procedure")
  778. env.Register("explain", explain, "set the help for a procedure")
  779. env.Register("expand", expand, "interpolate strings from environment")
  780. env.Register("fail", fail, "fail execution of a procedure")
  781. env.Register("rescue", rescue, "call $1 as the error handler on failure")
  782. env.Register("if", if_, "if runs $1 if $0 is true, otherwise runs $2")
  783. env.Register("isnil", isnil, "returns true if $1 is nil, false if not")
  784. env.Register("switch", switch_, "selects one of many cases")
  785. env.Register("type", type_, "returns $1 converted to a type")
  786. env.Register("teq", teq, "checks if $1 and $2 are exactly the same type")
  787. env.Register("typeof", typeof_, "returns the type of $1 or Unknown if not known")
  788. env.Register("nop", nop, "does nothing and returns nil")
  789. env.Register("overload", overload, "creates a command overload named $1 targeting $2 for the types following $2")
  790. }
  791. // This function registers builtins that make Attl turing complete
  792. // Not to be used in situations where this is undesirable.
  793. func (env *Environment) RegisterTuringCompleteBuiltins() {
  794. env.Register("while", while, "executes $2 while $1 returns true")
  795. }