vm.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. package glisp
  2. import (
  3. "errors"
  4. "fmt"
  5. )
  6. type Instruction interface {
  7. InstrString() string
  8. Execute(env *Glisp) error
  9. }
  10. type JumpInstr struct {
  11. location int
  12. }
  13. var OutOfBounds error = errors.New("jump out of bounds")
  14. func (j JumpInstr) InstrString() string {
  15. return fmt.Sprintf("jump %d", j.location)
  16. }
  17. func (j JumpInstr) Execute(env *Glisp) error {
  18. newpc := env.pc + j.location
  19. if newpc < 0 || newpc > env.CurrentFunctionSize() {
  20. return OutOfBounds
  21. }
  22. env.pc = newpc
  23. return nil
  24. }
  25. type GotoInstr struct {
  26. location int
  27. }
  28. func (g GotoInstr) InstrString() string {
  29. return fmt.Sprintf("goto %d", g.location)
  30. }
  31. func (g GotoInstr) Execute(env *Glisp) error {
  32. if g.location < 0 || g.location > env.CurrentFunctionSize() {
  33. return OutOfBounds
  34. }
  35. env.pc = g.location
  36. return nil
  37. }
  38. type BranchInstr struct {
  39. direction bool
  40. location int
  41. }
  42. func (b BranchInstr) InstrString() string {
  43. var format string
  44. if b.direction {
  45. format = "br %d"
  46. } else {
  47. format = "brn %d"
  48. }
  49. return fmt.Sprintf(format, b.location)
  50. }
  51. func (b BranchInstr) Execute(env *Glisp) error {
  52. expr, err := env.datastack.PopExpr()
  53. if err != nil {
  54. return err
  55. }
  56. if b.direction == IsTruthy(expr) {
  57. return JumpInstr{b.location}.Execute(env)
  58. }
  59. env.pc++
  60. return nil
  61. }
  62. type PushInstrClosure struct {
  63. expr SexpFunction
  64. }
  65. func (p PushInstrClosure) InstrString() string {
  66. return "pushC " + p.expr.SexpString()
  67. }
  68. func (p PushInstrClosure) Execute(env *Glisp) error {
  69. if p.expr.fun != nil {
  70. p.expr.closeScope = NewStack(ScopeStackSize)
  71. p.expr.closeScope.PushScope()
  72. var sym SexpSymbol
  73. var exp Sexp
  74. var err error
  75. for _, v := range p.expr.fun {
  76. switch it := v.(type) {
  77. case GetInstr:
  78. sym = it.sym
  79. case PutInstr:
  80. sym = it.sym
  81. case CallInstr:
  82. sym = it.sym
  83. default:
  84. continue
  85. }
  86. exp, err = env.scopestack.LookupSymbolNonGlobal(sym)
  87. if err == nil {
  88. p.expr.closeScope.BindSymbol(sym, exp)
  89. }
  90. }
  91. } else {
  92. p.expr.closeScope = env.scopestack.Clone() // for a non script fuction I have no idea what it accesses, so we clone the whole thing
  93. }
  94. env.datastack.PushExpr(p.expr)
  95. env.pc++
  96. return nil
  97. }
  98. type PushInstr struct {
  99. expr Sexp
  100. }
  101. func (p PushInstr) InstrString() string {
  102. return "push " + p.expr.SexpString()
  103. }
  104. func (p PushInstr) Execute(env *Glisp) error {
  105. env.datastack.PushExpr(p.expr)
  106. env.pc++
  107. return nil
  108. }
  109. type PopInstr int
  110. func (p PopInstr) InstrString() string {
  111. return "pop"
  112. }
  113. func (p PopInstr) Execute(env *Glisp) error {
  114. _, err := env.datastack.PopExpr()
  115. env.pc++
  116. return err
  117. }
  118. type DupInstr int
  119. func (d DupInstr) InstrString() string {
  120. return "dup"
  121. }
  122. func (d DupInstr) Execute(env *Glisp) error {
  123. expr, err := env.datastack.GetExpr(0)
  124. if err != nil {
  125. return err
  126. }
  127. env.datastack.PushExpr(expr)
  128. env.pc++
  129. return nil
  130. }
  131. type GetInstr struct {
  132. sym SexpSymbol
  133. }
  134. func (g GetInstr) InstrString() string {
  135. return fmt.Sprintf("get %s", g.sym.name)
  136. }
  137. func (g GetInstr) Execute(env *Glisp) error {
  138. expr, err := env.scopestack.LookupSymbol(g.sym)
  139. if err != nil {
  140. return err
  141. }
  142. env.datastack.PushExpr(expr)
  143. env.pc++
  144. return nil
  145. }
  146. type PutInstr struct {
  147. sym SexpSymbol
  148. }
  149. func (p PutInstr) InstrString() string {
  150. return fmt.Sprintf("put %s", p.sym.name)
  151. }
  152. func (p PutInstr) Execute(env *Glisp) error {
  153. expr, err := env.datastack.PopExpr()
  154. if err != nil {
  155. return err
  156. }
  157. env.pc++
  158. return env.scopestack.BindSymbol(p.sym, expr)
  159. }
  160. type CallInstr struct {
  161. sym SexpSymbol
  162. nargs int
  163. }
  164. func (c CallInstr) InstrString() string {
  165. return fmt.Sprintf("call %s %d", c.sym.name, c.nargs)
  166. }
  167. func (c CallInstr) Execute(env *Glisp) error {
  168. f, ok := env.builtins[c.sym.number]
  169. if ok {
  170. return env.CallUserFunction(f, c.sym.name, c.nargs)
  171. }
  172. funcobj, err := env.scopestack.LookupSymbol(c.sym)
  173. if err != nil {
  174. return err
  175. }
  176. switch f := funcobj.(type) {
  177. case SexpFunction:
  178. if !f.user {
  179. return env.CallFunction(f, c.nargs)
  180. }
  181. return env.CallUserFunction(f, c.sym.name, c.nargs)
  182. }
  183. return errors.New(fmt.Sprintf("%s is not a function", c.sym.name))
  184. }
  185. type DispatchInstr struct {
  186. nargs int
  187. }
  188. func (d DispatchInstr) InstrString() string {
  189. return fmt.Sprintf("dispatch %d", d.nargs)
  190. }
  191. func (d DispatchInstr) Execute(env *Glisp) error {
  192. funcobj, err := env.datastack.PopExpr()
  193. if err != nil {
  194. return err
  195. }
  196. switch f := funcobj.(type) {
  197. case SexpFunction:
  198. if !f.user {
  199. return env.CallFunction(f, d.nargs)
  200. }
  201. return env.CallUserFunction(f, f.name, d.nargs)
  202. }
  203. return errors.New("not a function")
  204. }
  205. type ReturnInstr struct {
  206. err error
  207. }
  208. func (r ReturnInstr) Execute(env *Glisp) error {
  209. if r.err != nil {
  210. return r.err
  211. }
  212. return env.ReturnFromFunction()
  213. }
  214. func (r ReturnInstr) InstrString() string {
  215. if r.err == nil {
  216. return "ret"
  217. }
  218. return "ret \"" + r.err.Error() + "\""
  219. }
  220. type AddScopeInstr int
  221. func (a AddScopeInstr) InstrString() string {
  222. return "add scope"
  223. }
  224. func (a AddScopeInstr) Execute(env *Glisp) error {
  225. env.scopestack.PushScope()
  226. env.pc++
  227. return nil
  228. }
  229. type RemoveScopeInstr int
  230. func (a RemoveScopeInstr) InstrString() string {
  231. return "rem scope"
  232. }
  233. func (a RemoveScopeInstr) Execute(env *Glisp) error {
  234. env.pc++
  235. return env.scopestack.PopScope()
  236. }
  237. type ExplodeInstr int
  238. func (e ExplodeInstr) InstrString() string {
  239. return "explode"
  240. }
  241. func (e ExplodeInstr) Execute(env *Glisp) error {
  242. expr, err := env.datastack.PopExpr()
  243. if err != nil {
  244. return err
  245. }
  246. arr, err := ListToArray(expr)
  247. if err != nil {
  248. return err
  249. }
  250. for _, val := range arr {
  251. env.datastack.PushExpr(val)
  252. }
  253. env.pc++
  254. return nil
  255. }
  256. type SquashInstr int
  257. func (s SquashInstr) InstrString() string {
  258. return "squash"
  259. }
  260. func (s SquashInstr) Execute(env *Glisp) error {
  261. var list Sexp = SexpNull
  262. for {
  263. expr, err := env.datastack.PopExpr()
  264. if err != nil {
  265. return err
  266. }
  267. if expr == SexpMarker {
  268. break
  269. }
  270. list = Cons(expr, list)
  271. }
  272. env.datastack.PushExpr(list)
  273. env.pc++
  274. return nil
  275. }
  276. // bind these symbols to the SexpPair list found at
  277. // datastack top.
  278. type BindlistInstr struct {
  279. syms []SexpSymbol
  280. }
  281. func (b BindlistInstr) InstrString() string {
  282. joined := ""
  283. for _, s := range b.syms {
  284. joined += s.name + " "
  285. }
  286. return fmt.Sprintf("bindlist %s", joined)
  287. }
  288. func (b BindlistInstr) Execute(env *Glisp) error {
  289. expr, err := env.datastack.PopExpr()
  290. if err != nil {
  291. return err
  292. }
  293. arr, err := ListToArray(expr)
  294. if err != nil {
  295. return err
  296. }
  297. nsym := len(b.syms)
  298. narr := len(arr)
  299. if narr < nsym {
  300. return fmt.Errorf("bindlist failing: %d targets but only %d sources", nsym, narr)
  301. }
  302. for i, bindThisSym := range b.syms {
  303. env.scopestack.BindSymbol(bindThisSym, arr[i])
  304. }
  305. env.pc++
  306. return nil
  307. }
  308. type VectorizeInstr int
  309. func (s VectorizeInstr) InstrString() string {
  310. return "vectorize"
  311. }
  312. func (s VectorizeInstr) Execute(env *Glisp) error {
  313. vec := make([]Sexp, 0)
  314. for {
  315. expr, err := env.datastack.PopExpr()
  316. if err != nil {
  317. return err
  318. }
  319. if expr == SexpMarker {
  320. break
  321. }
  322. vec = append([]Sexp{expr}, vec...)
  323. }
  324. env.datastack.PushExpr(SexpArray(vec))
  325. env.pc++
  326. return nil
  327. }
  328. type HashizeInstr struct {
  329. HashLen int
  330. TypeName string
  331. }
  332. func (s HashizeInstr) InstrString() string {
  333. return "hashize"
  334. }
  335. func (s HashizeInstr) Execute(env *Glisp) error {
  336. a := make([]Sexp, 0)
  337. for {
  338. expr, err := env.datastack.PopExpr()
  339. if err != nil {
  340. return err
  341. }
  342. if expr == SexpMarker {
  343. break
  344. }
  345. a = append(a, expr)
  346. }
  347. hash, err := MakeHash(a, s.TypeName)
  348. if err != nil {
  349. return err
  350. }
  351. env.datastack.PushExpr(hash)
  352. env.pc++
  353. return nil
  354. }