builtin.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. package muesli
  2. import "fmt"
  3. import "log"
  4. import "runtime"
  5. import "strings"
  6. import "sort"
  7. type LogTracer struct {
  8. }
  9. func (t LogTracer) Trace(vm VM, fmt string, args ... interface{}) bool {
  10. args = append([]interface{}{vm.BackTrace()}, args...)
  11. log.Printf("%s: " + fmt, args...)
  12. return false
  13. }
  14. type FmtTracer struct {
  15. }
  16. func (t FmtTracer) Trace(vm VM, fm string, args ... interface{}) bool {
  17. args = append([]interface{}{vm.BackTrace()}, args...)
  18. fmt.Printf("%s: " + fm + "\n", args...)
  19. return false
  20. }
  21. func printf(vm *VM, args ...Value) []Value {
  22. var format string
  23. rest, err := ParseArgs(args, &format)
  24. if err != nil {
  25. return Fail(err)
  26. }
  27. extra := ListFromList(rest)
  28. fmt.Printf(format, extra...)
  29. return None()
  30. }
  31. func println(vm *VM, args ...Value) []Value {
  32. var msg string
  33. _, err := ParseArgs(args, &msg)
  34. if err != nil {
  35. return Fail(err)
  36. } else {
  37. fmt.Println(msg)
  38. }
  39. return None()
  40. }
  41. func p(vm *VM, args ...Value) []Value {
  42. for _, arg := range args {
  43. fmt.Printf("%v\n", arg)
  44. }
  45. return None()
  46. }
  47. func trace(vm *VM, args ...Value) []Value {
  48. var b bool = true
  49. fmt.Printf("command trace: %v\n", args)
  50. _, err := ParseArgs(args, &b)
  51. if err != nil {
  52. fmt.Printf("Error: %s\n", err.Error())
  53. return Fail(err)
  54. }
  55. fmt.Printf("command trace: bool: %v\n", b)
  56. if b {
  57. vm.Tracer = FmtTracer{}
  58. } else {
  59. vm.Tracer = nil
  60. }
  61. return Ok(BoolValue(b))
  62. }
  63. func sumi(vm *VM, args ...Value) []Value {
  64. slice, err := ParseArgsToIntSlice(args)
  65. if err != nil {
  66. fmt.Printf("Error: %s\n", err.Error())
  67. return Fail(err)
  68. }
  69. res := int(0)
  70. for _, val := range slice {
  71. res += val
  72. }
  73. return IntOk(res)
  74. }
  75. func sumf(vm *VM, args ...Value) []Value {
  76. slice, err := ParseArgsToFloat64Slice(args)
  77. if err != nil {
  78. fmt.Printf("Error: %s\n", err.Error())
  79. return Fail(err)
  80. }
  81. res := float64(0)
  82. for _, val := range slice {
  83. res += val
  84. }
  85. return FloatOk(res)
  86. }
  87. func addi(vm *VM, args ...Value) []Value {
  88. var v1, v2 int
  89. ParseArgs(args, &v1, &v2)
  90. return IntOk(v1 + v2)
  91. }
  92. func addf(vm *VM, args ...Value) []Value {
  93. var v1, v2 float64
  94. _, err := ParseArgs(args, &v1, &v2)
  95. if err != nil {
  96. return Fail(err)
  97. }
  98. return FloatOk(v1 + v2)
  99. }
  100. func subi(vm *VM, args ...Value) []Value {
  101. var v1, v2 int
  102. _, err := ParseArgs(args, &v1, &v2)
  103. if err != nil {
  104. return Fail(err)
  105. }
  106. return IntOk(v1 - v2)
  107. }
  108. func subf(vm *VM, args ...Value) []Value {
  109. var v1, v2 float64
  110. _, err := ParseArgs(args, &v1, &v2)
  111. if err != nil {
  112. return Fail(err)
  113. }
  114. return FloatOk(v1 - v2)
  115. }
  116. func muli(vm *VM, args ...Value) []Value {
  117. var v1, v2 int
  118. _, err := ParseArgs(args, &v1, &v2)
  119. if err != nil {
  120. return Fail(err)
  121. }
  122. return IntOk(v1 * v2)
  123. }
  124. func mulf(vm *VM, args ...Value) []Value {
  125. var v1, v2 float64
  126. _, err := ParseArgs(args, &v1, &v2)
  127. if err != nil {
  128. return Fail(err)
  129. }
  130. return FloatOk(v1 * v2)
  131. }
  132. func divi(vm *VM, args ...Value) []Value {
  133. var v1, v2 int
  134. _, err := ParseArgs(args, &v1, &v2)
  135. if err != nil {
  136. return Fail(err)
  137. }
  138. return IntOk(v1 / v2)
  139. }
  140. func divf(vm *VM, args ...Value) []Value {
  141. var v1, v2 float64
  142. _, err := ParseArgs(args, &v1, &v2)
  143. if err != nil {
  144. return Fail(err)
  145. }
  146. return FloatOk(v1 / v2)
  147. }
  148. func andb(vm * VM, args ...Value) []Value {
  149. var v1, v2 bool
  150. _, err := ParseArgs(args, &v1, &v2)
  151. if err != nil {
  152. return Fail(err)
  153. }
  154. return BoolOk(v1 && v2)
  155. }
  156. func orb(vm * VM, args ...Value) []Value {
  157. var v1, v2 bool
  158. _, err := ParseArgs(args, &v1, &v2)
  159. if err != nil {
  160. return Fail(err)
  161. }
  162. return BoolOk(v1 || v2)
  163. }
  164. func val(vm *VM, args ...Value) []Value {
  165. if len(args) < 1 {
  166. return []Value{NewErrorValuef("val requres at least one argument.")}
  167. }
  168. return args
  169. }
  170. func builtin_return(vm *VM, args ...Value) []Value {
  171. vm.Frame.returned = true
  172. vm.Frame.results = args
  173. vm.Trace("Returning... %v", vm.Frame)
  174. return args
  175. }
  176. func to(vm *VM, args ...Value) []Value {
  177. var name string
  178. rest, err := ParseArgs(args, &name)
  179. if err != nil {
  180. return Fail(err)
  181. }
  182. if len(rest) < 1 {
  183. return Fail(NewErrorValuef("Need at least 2 arguments: %v", args))
  184. }
  185. last := rest[len(rest)-1]
  186. block, isBlock := last.(*BlockValue)
  187. if ! isBlock {
  188. return Fail(NewErrorValuef("Not a block: %v", last))
  189. }
  190. param := rest[0:len(rest)-1]
  191. sign, err := NewSignatureWithNames(param...)
  192. if err != nil {
  193. return Fail(NewErrorValuef("Not a word value: %v", name))
  194. }
  195. // To must register one level up.
  196. return Ok(vm.RegisterDefined(name, sign, block, 1))
  197. }
  198. func cover(vm *VM, args ...Value) []Value {
  199. var name, target string
  200. rest, err := ParseArgs(args, &name, &target)
  201. if err != nil {
  202. return Fail(err)
  203. }
  204. types := []TypeValue{}
  205. for i, arg := range rest {
  206. if typ, ok := arg.(TypeValue) ; ok {
  207. types = append(types, typ)
  208. } else {
  209. return Fail(NewErrorValuef("Argument %d: not a type: %v %s", i+2, arg, arg.String()))
  210. }
  211. }
  212. // Overload must be defined one scope up.
  213. err = vm.AddOverload(name, target, 1, types...)
  214. if err != nil {
  215. return Fail(err)
  216. }
  217. return Ok()
  218. }
  219. func types(vm *VM, args ...Value) []Value {
  220. result := []Value{}
  221. for _, arg := range args {
  222. typ := arg.Type()
  223. result = append(result, typ)
  224. }
  225. return Ok(NewListValue(result...))
  226. }
  227. func set(vm *VM, val ...Value) []Value {
  228. if len(val) < 2 {
  229. return Fail(fmt.Errorf("set needs at least 2 arguments: received %v", val))
  230. }
  231. target := val[0].String()
  232. value := val[1]
  233. if target == "(" || target == "$" {
  234. if len(val) < 3 {
  235. return Fail(fmt.Errorf("indirect get needs results: received %v", val))
  236. }
  237. target = val[1].String()
  238. value = val[2]
  239. }
  240. vm.RegisterUp(target, value, 1)
  241. return Ok(value)
  242. }
  243. func get(vm *VM, val ...Value) []Value {
  244. if len(val) < 1 {
  245. return Fail(fmt.Errorf("get needs at least 1 argument"))
  246. }
  247. target := val[0].String()
  248. if target == "(" || target == "$" {
  249. if len(val) < 2 {
  250. return Fail(fmt.Errorf("indirect get needs results: received %v", val))
  251. }
  252. target = val[1].String()
  253. }
  254. return Ok(vm.Lookup(target))
  255. }
  256. func addl(vm *VM, args ...Value) []Value {
  257. var value Value
  258. var list *ListValue
  259. _, err := ParseArgs(args, &list, &value)
  260. if err != nil {
  261. return Fail(err)
  262. }
  263. list.Append(value)
  264. return Ok(list)
  265. }
  266. func fetchl(vm *VM, args ...Value) []Value {
  267. var index int
  268. var list *ListValue
  269. _, err := ParseArgs(args, &list, &index)
  270. if err != nil {
  271. return Fail(err)
  272. }
  273. if (index < 0) || (index >= len(list.List)) {
  274. return Fail(fmt.Errorf("index out of range: %d<->%d", index, len(list.List)))
  275. }
  276. return Ok(list.List[index])
  277. }
  278. func storel(vm *VM, args ...Value) []Value {
  279. var index int
  280. var list *ListValue
  281. rest, err := ParseArgs(args, &list, &index)
  282. if err != nil {
  283. return Fail(err)
  284. }
  285. if len(rest) < 1 {
  286. return Fail(fmt.Errorf("fetch: need 3 arguments"))
  287. }
  288. if (index < 0) || (index >= len(list.List)) {
  289. return Fail(fmt.Errorf("index out of range: %d<->%d", index, len(list.List)))
  290. }
  291. list.List[index] = rest[0]
  292. return Ok(list.List[index])
  293. }
  294. func fetchm(vm *VM, args ...Value) []Value {
  295. var index Value
  296. var hmap *MapValue
  297. _, err := ParseArgs(args, &hmap, &index)
  298. if err != nil {
  299. return Fail(err)
  300. }
  301. return Ok(hmap.Map[index])
  302. }
  303. func storem(vm *VM, args ...Value) []Value {
  304. var index Value
  305. var hmap *MapValue
  306. rest, err := ParseArgs(args, &hmap, &index)
  307. if err != nil {
  308. return Fail(err)
  309. }
  310. if len (rest) < 1 {
  311. return Fail(fmt.Errorf("fetch: need 3 arguments"))
  312. }
  313. hmap.Map[index] = rest[0]
  314. return Ok(hmap.Map[index])
  315. }
  316. func newmap(vm *VM, args ...Value) []Value {
  317. result := make(map[Value] Value)
  318. for i := 1; i < len(args) ; i+=2 {
  319. result[args[i-1]] = args[i]
  320. }
  321. return Ok(NewMapValue(result))
  322. }
  323. func help(vm *VM, val ...Value) [] Value {
  324. if len(val) < 1 {
  325. fmt.Printf("help <callable> will display help on the callable\n\nThe following commands are available:\n")
  326. helpers := vm.DefinedHelpers()
  327. sort.SliceStable(helpers, func(i, j int) bool {
  328. return helpers[i].HelperName() < helpers[j].HelperName()
  329. })
  330. for _, helper := range helpers {
  331. fl := strings.SplitN(helper.Help(),"\n", 2)
  332. fmt.Printf("%s %s: \n", helper.HelperName(), fl[0])
  333. }
  334. return Ok()
  335. }
  336. targetName := val[0].String()
  337. target := vm.Lookup(targetName)
  338. if target == nil {
  339. fmt.Printf("help: %s not found.\n", targetName)
  340. }
  341. if helper, isHelper := target.(Helper) ; isHelper {
  342. help := helper.Help()
  343. if call, isCall := target.(Callable); isCall {
  344. fmt.Printf("%s %s: %s.\n", targetName, call.Signature().String(), help)
  345. } else {
  346. fmt.Printf("%s: %s.\n", targetName, help)
  347. }
  348. return Ok(StringValue(help))
  349. }
  350. return Ok()
  351. }
  352. func explain(vm *VM, val ...Value) [] Value {
  353. var target, help string
  354. _, err := ParseArgs(val, &target, &help)
  355. if err != nil {
  356. return Fail(err)
  357. }
  358. err = vm.SetHelp(target, help)
  359. if err != nil {
  360. return Fail(err)
  361. }
  362. return Ok(StringValue(help))
  363. }
  364. func exit(vm *VM, val ...Value) [] Value {
  365. var code int
  366. _, err := ParseArgs(val, &code)
  367. if err != nil {
  368. runtime.Goexit()
  369. }
  370. vm.ExitStatus = code
  371. runtime.Goexit()
  372. return Ok()
  373. }
  374. func (vm *VM) RegisterBuiltinTypes() {
  375. vm.RegisterTop("Int", IntType)
  376. vm.RegisterTop("Float", FloatType)
  377. vm.RegisterTop("String",StringType)
  378. vm.RegisterTop("Bool", BoolType)
  379. vm.RegisterTop("Word", WordType)
  380. vm.RegisterTop("Error", ErrorType)
  381. vm.RegisterTop("Type", TypeType)
  382. vm.RegisterTop("Empty", EmptyType)
  383. vm.RegisterTop("List", ListType)
  384. vm.RegisterTop("Map", MapType)
  385. vm.RegisterTop("Any", AnyType)
  386. vm.RegisterTop("ZeroType", ZeroType)
  387. }
  388. func (vm *VM) RegisterBuiltins() {
  389. vm.RegisterBuiltinTypes()
  390. vm.RegisterBuiltinWithHelp("addl", addl, "adds an element to a list").Takes(ListType, AnyType).Returns(ListType)
  391. vm.RegisterBuiltinWithHelp("addi", addi, `adds two integers together`).Takes(IntType, IntType).Returns(BoolType)
  392. vm.RegisterBuiltinWithHelp("addf", addf, `adds two floats together`).Takes(IntType, IntType).Returns(IntType)
  393. vm.RegisterBuiltinWithHelp("andb", andb, `returns true if all it's arguments are true`).Takes(BoolType, BoolType).Returns(BoolType)
  394. vm.RegisterBuiltin("cover", cover)
  395. vm.RegisterBuiltin("fetchl", fetchl).Takes(ListType, IntType).Returns(AnyType)
  396. vm.RegisterBuiltin("fetchm", fetchm).Takes(MapType, AnyType).Returns(AnyType)
  397. vm.RegisterBuiltin("sumi", sumi).Takes(IntType, IntType).Returns(IntType)
  398. vm.RegisterBuiltin("sumf", sumf).Takes(FloatType, FloatType).Returns(FloatType)
  399. vm.RegisterBuiltin("subi", subi).Takes(IntType, IntType).Returns(IntType)
  400. vm.RegisterBuiltin("subf", subf).Takes(FloatType, FloatType).Returns(FloatType)
  401. vm.RegisterBuiltin("divi", divi).Takes(IntType, IntType).Returns(IntType)
  402. vm.RegisterBuiltin("divf", divf).Takes(FloatType, FloatType).Returns(FloatType)
  403. vm.RegisterBuiltin("map", newmap)
  404. vm.RegisterBuiltin("muli", muli).Takes(IntType, IntType).Returns(IntType)
  405. vm.RegisterBuiltin("mulf", mulf).Takes(FloatType, FloatType).Returns(FloatType)
  406. vm.RegisterBuiltinWithHelp("orb", orb, `[Bool Bool] -> Bool: returns true if on of it's arguments is true`)
  407. // vm.RegisterCover("add")
  408. vm.RegisterBuiltin("p", p)
  409. vm.RegisterBuiltin("println", println)
  410. vm.RegisterBuiltin("printf", printf)
  411. vm.RegisterBuiltin("storel", storel).Takes(ListType, IntType).Returns(AnyType)
  412. vm.RegisterBuiltin("storem", storem).Takes(MapType, AnyType).Returns(AnyType)
  413. vm.RegisterBuiltin("trace", trace)
  414. vm.RegisterBuiltin("to", to)
  415. vm.RegisterBuiltin("types", types)
  416. vm.RegisterBuiltin("return", builtin_return)
  417. vm.RegisterBuiltin("val", val)
  418. vm.RegisterBuiltin("set", set)
  419. vm.RegisterBuiltin("get", get)
  420. vm.RegisterBuiltin("help", help)
  421. vm.RegisterBuiltin("explain", explain)
  422. vm.RegisterBuiltin("exit", exit)
  423. vm.AddOverloads("mul",
  424. Over("mulf", 0, FloatType, FloatType),
  425. Over("muli", 0, IntType, IntType),
  426. Over("mulf", 0, FloatType, IntType),
  427. Over("mulf", 0, IntType, FloatType))
  428. vm.AddOverloads("add",
  429. Over("addf", 0, FloatType, FloatType),
  430. Over("addi", 0, IntType, IntType),
  431. Over("addf", 0, FloatType, IntType),
  432. Over("addf", 0, IntType, FloatType))
  433. vm.SetHelp("mul", " Num: Multiplies two numbers. Cover for muli and mulf.")
  434. vm.AddOverloads("fetch",
  435. Over("fetchl", 0, ListType, IntType),
  436. Over("fetchm", 0, MapType, AnyType),
  437. )
  438. vm.SetHelp("fetch", " storage, index. Fetch value in storage at given index.")
  439. /*
  440. vm.AddOverloads("store",
  441. Over("storel", ListType, IntType, AnyType),
  442. Over("storem", MapType, AnyType, AnyType),
  443. )
  444. vm.SetHelp("store", " storage, index, value. Store value in storage at given index.")
  445. */
  446. RegisterDoor(vm)
  447. }