generator.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. package glisp
  2. import (
  3. "errors"
  4. "fmt"
  5. )
  6. type Generator struct {
  7. env *Glisp
  8. funcname string
  9. tail bool
  10. scopes int
  11. instructions []Instruction
  12. }
  13. type Loop struct {
  14. stmtname SexpSymbol
  15. loopStart int
  16. loopLen int
  17. breakOffset int // i.e. relative to loopStart
  18. continueOffset int // i.e. relative to loopStart
  19. }
  20. func (loop *Loop) IsStackElem() {}
  21. func NewGenerator(env *Glisp) *Generator {
  22. gen := new(Generator)
  23. gen.env = env
  24. gen.instructions = make([]Instruction, 0)
  25. // tail marks whether or not we are in the tail position
  26. gen.tail = false
  27. // scopes is the number of extra (non-function) scopes we've created
  28. gen.scopes = 0
  29. return gen
  30. }
  31. func (gen *Generator) AddInstructions(instr []Instruction) {
  32. gen.instructions = append(gen.instructions, instr...)
  33. }
  34. func (gen *Generator) AddInstruction(instr Instruction) {
  35. gen.instructions = append(gen.instructions, instr)
  36. }
  37. func (gen *Generator) GenerateBegin(expressions []Sexp) error {
  38. size := len(expressions)
  39. oldtail := gen.tail
  40. gen.tail = false
  41. if size == 0 {
  42. return errors.New("No expressions found")
  43. }
  44. for _, expr := range expressions[:size-1] {
  45. err := gen.Generate(expr)
  46. if err != nil {
  47. return err
  48. }
  49. // insert pops after all but the last instruction
  50. // that way the stack remains clean
  51. gen.AddInstruction(PopInstr(0))
  52. }
  53. gen.tail = oldtail
  54. return gen.Generate(expressions[size-1])
  55. }
  56. func buildSexpFun(env *Glisp, name string, funcargs SexpArray,
  57. funcbody []Sexp) (SexpFunction, error) {
  58. gen := NewGenerator(env)
  59. gen.tail = true
  60. if len(name) == 0 {
  61. gen.funcname = env.GenSymbol("__anon").name
  62. } else {
  63. gen.funcname = name
  64. }
  65. argsyms := make([]SexpSymbol, len(funcargs))
  66. for i, expr := range funcargs {
  67. switch t := expr.(type) {
  68. case SexpSymbol:
  69. argsyms[i] = t
  70. default:
  71. return MissingFunction,
  72. errors.New("function argument must be symbol")
  73. }
  74. }
  75. varargs := false
  76. nargs := len(funcargs)
  77. if len(argsyms) >= 2 && argsyms[len(argsyms)-2].name == "&" {
  78. argsyms[len(argsyms)-2] = argsyms[len(argsyms)-1]
  79. argsyms = argsyms[0 : len(argsyms)-1]
  80. varargs = true
  81. nargs = len(argsyms) - 1
  82. }
  83. for i := len(argsyms) - 1; i >= 0; i-- {
  84. gen.AddInstruction(PutInstr{argsyms[i]})
  85. }
  86. err := gen.GenerateBegin(funcbody)
  87. if err != nil {
  88. return MissingFunction, err
  89. }
  90. gen.AddInstruction(ReturnInstr{nil})
  91. newfunc := GlispFunction(gen.instructions)
  92. return MakeFunction(gen.funcname, nargs, varargs, newfunc), nil
  93. }
  94. func (gen *Generator) GenerateFn(args []Sexp) error {
  95. if len(args) < 2 {
  96. return errors.New("malformed function definition")
  97. }
  98. var funcargs SexpArray
  99. switch expr := args[0].(type) {
  100. case SexpArray:
  101. funcargs = expr
  102. default:
  103. return errors.New("function arguments must be in vector")
  104. }
  105. funcbody := args[1:]
  106. sfun, err := buildSexpFun(gen.env, "", funcargs, funcbody)
  107. if err != nil {
  108. return err
  109. }
  110. gen.AddInstruction(PushInstrClosure{sfun})
  111. return nil
  112. }
  113. func (gen *Generator) GenerateDef(args []Sexp) error {
  114. if len(args) != 2 {
  115. return errors.New("Wrong number of arguments to def")
  116. }
  117. var sym SexpSymbol
  118. switch expr := args[0].(type) {
  119. case SexpSymbol:
  120. sym = expr
  121. default:
  122. return errors.New("Definition name must by symbol")
  123. }
  124. gen.tail = false
  125. err := gen.Generate(args[1])
  126. if err != nil {
  127. return err
  128. }
  129. gen.AddInstruction(PutInstr{sym})
  130. gen.AddInstruction(PushInstr{SexpNull})
  131. return nil
  132. }
  133. func (gen *Generator) GenerateDefn(args []Sexp) error {
  134. if len(args) < 3 {
  135. return errors.New("Wrong number of arguments to defn")
  136. }
  137. var funcargs SexpArray
  138. switch expr := args[1].(type) {
  139. case SexpArray:
  140. funcargs = expr
  141. default:
  142. return errors.New("function arguments must be in vector")
  143. }
  144. var sym SexpSymbol
  145. switch expr := args[0].(type) {
  146. case SexpSymbol:
  147. sym = expr
  148. default:
  149. return errors.New("Definition name must by symbol")
  150. }
  151. sfun, err := buildSexpFun(gen.env, sym.name, funcargs, args[2:])
  152. if err != nil {
  153. return err
  154. }
  155. gen.AddInstruction(PushInstr{sfun})
  156. gen.AddInstruction(PutInstr{sym})
  157. gen.AddInstruction(PushInstr{SexpNull})
  158. return nil
  159. }
  160. func (gen *Generator) GenerateDefmac(args []Sexp) error {
  161. if len(args) < 3 {
  162. return errors.New("Wrong number of arguments to defmac")
  163. }
  164. var funcargs SexpArray
  165. switch expr := args[1].(type) {
  166. case SexpArray:
  167. funcargs = expr
  168. default:
  169. return errors.New("function arguments must be in vector")
  170. }
  171. var sym SexpSymbol
  172. switch expr := args[0].(type) {
  173. case SexpSymbol:
  174. sym = expr
  175. default:
  176. return errors.New("Definition name must by symbol")
  177. }
  178. sfun, err := buildSexpFun(gen.env, sym.name, funcargs, args[2:])
  179. if err != nil {
  180. return err
  181. }
  182. gen.env.macros[sym.number] = sfun
  183. gen.AddInstruction(PushInstr{SexpNull})
  184. return nil
  185. }
  186. func (gen *Generator) GenerateMacexpand(args []Sexp) error {
  187. if len(args) != 1 {
  188. return WrongNargs
  189. }
  190. var list SexpPair
  191. var islist bool
  192. var ismacrocall bool
  193. switch t := args[0].(type) {
  194. case SexpPair:
  195. if IsList(t.tail) {
  196. list = t
  197. islist = true
  198. }
  199. default:
  200. islist = false
  201. }
  202. var macro SexpFunction
  203. if islist {
  204. switch t := list.head.(type) {
  205. case SexpSymbol:
  206. macro, ismacrocall = gen.env.macros[t.number]
  207. default:
  208. ismacrocall = false
  209. }
  210. }
  211. if !ismacrocall {
  212. gen.AddInstruction(PushInstr{args[0]})
  213. return nil
  214. }
  215. macargs, err := ListToArray(list.tail)
  216. if err != nil {
  217. return err
  218. }
  219. env := gen.env.Duplicate()
  220. expr, err := env.Apply(macro, macargs)
  221. if err != nil {
  222. return err
  223. }
  224. gen.AddInstruction(PushInstr{expr})
  225. return nil
  226. }
  227. func (gen *Generator) GenerateShortCircuit(or bool, args []Sexp) error {
  228. size := len(args)
  229. subgen := NewGenerator(gen.env)
  230. subgen.scopes = gen.scopes
  231. subgen.tail = gen.tail
  232. subgen.funcname = gen.funcname
  233. subgen.Generate(args[size-1])
  234. instructions := subgen.instructions
  235. for i := size - 2; i >= 0; i-- {
  236. subgen = NewGenerator(gen.env)
  237. subgen.Generate(args[i])
  238. subgen.AddInstruction(DupInstr(0))
  239. subgen.AddInstruction(BranchInstr{or, len(instructions) + 2})
  240. subgen.AddInstruction(PopInstr(0))
  241. instructions = append(subgen.instructions, instructions...)
  242. }
  243. gen.AddInstructions(instructions)
  244. return nil
  245. }
  246. func (gen *Generator) GenerateCond(args []Sexp) error {
  247. if len(args)%2 == 0 {
  248. return errors.New("missing default case")
  249. }
  250. subgen := NewGenerator(gen.env)
  251. subgen.tail = gen.tail
  252. subgen.scopes = gen.scopes
  253. subgen.funcname = gen.funcname
  254. err := subgen.Generate(args[len(args)-1])
  255. if err != nil {
  256. return err
  257. }
  258. instructions := subgen.instructions
  259. for i := len(args)/2 - 1; i >= 0; i-- {
  260. subgen.Reset()
  261. err := subgen.Generate(args[2*i])
  262. if err != nil {
  263. return err
  264. }
  265. pred_code := subgen.instructions
  266. subgen.Reset()
  267. subgen.tail = gen.tail
  268. subgen.scopes = gen.scopes
  269. subgen.funcname = gen.funcname
  270. err = subgen.Generate(args[2*i+1])
  271. if err != nil {
  272. return err
  273. }
  274. body_code := subgen.instructions
  275. subgen.Reset()
  276. subgen.AddInstructions(pred_code)
  277. subgen.AddInstruction(BranchInstr{false, len(body_code) + 2})
  278. subgen.AddInstructions(body_code)
  279. subgen.AddInstruction(JumpInstr{len(instructions) + 1})
  280. subgen.AddInstructions(instructions)
  281. instructions = subgen.instructions
  282. }
  283. gen.AddInstructions(instructions)
  284. return nil
  285. }
  286. func (gen *Generator) GenerateQuote(args []Sexp) error {
  287. for _, expr := range args {
  288. gen.AddInstruction(PushInstr{expr})
  289. }
  290. return nil
  291. }
  292. func (gen *Generator) GenerateLet(name string, args []Sexp) error {
  293. if len(args) < 2 {
  294. return errors.New("malformed let statement")
  295. }
  296. lstatements := make([]SexpSymbol, 0)
  297. rstatements := make([]Sexp, 0)
  298. var bindings []Sexp
  299. switch expr := args[0].(type) {
  300. case SexpArray:
  301. bindings = []Sexp(expr)
  302. default:
  303. return errors.New("let bindings must be in array")
  304. }
  305. if len(bindings)%2 != 0 {
  306. return errors.New("uneven let binding list")
  307. }
  308. for i := 0; i < len(bindings)/2; i++ {
  309. switch t := bindings[2*i].(type) {
  310. case SexpSymbol:
  311. lstatements = append(lstatements, t)
  312. default:
  313. return errors.New("cannot bind to non-symbol")
  314. }
  315. rstatements = append(rstatements, bindings[2*i+1])
  316. }
  317. gen.AddInstruction(AddScopeInstr(0))
  318. gen.scopes++
  319. if name == "let*" {
  320. for i, rs := range rstatements {
  321. err := gen.Generate(rs)
  322. if err != nil {
  323. return err
  324. }
  325. gen.AddInstruction(PutInstr{lstatements[i]})
  326. }
  327. } else if name == "let" {
  328. for _, rs := range rstatements {
  329. err := gen.Generate(rs)
  330. if err != nil {
  331. return err
  332. }
  333. }
  334. for i := len(lstatements) - 1; i >= 0; i-- {
  335. gen.AddInstruction(PutInstr{lstatements[i]})
  336. }
  337. }
  338. err := gen.GenerateBegin(args[1:])
  339. if err != nil {
  340. return err
  341. }
  342. gen.AddInstruction(RemoveScopeInstr(0))
  343. gen.scopes--
  344. return nil
  345. }
  346. func (gen *Generator) GenerateAssert(args []Sexp) error {
  347. if len(args) != 1 {
  348. return WrongNargs
  349. }
  350. err := gen.Generate(args[0])
  351. if err != nil {
  352. return err
  353. }
  354. reterrmsg := fmt.Sprintf("Assertion failed: %s\n",
  355. args[0].SexpString())
  356. gen.AddInstruction(BranchInstr{true, 2})
  357. gen.AddInstruction(ReturnInstr{errors.New(reterrmsg)})
  358. gen.AddInstruction(PushInstr{SexpNull})
  359. return nil
  360. }
  361. func (gen *Generator) GenerateInclude(args []Sexp) error {
  362. if len(args) < 1 {
  363. return WrongNargs
  364. }
  365. var err error
  366. var exps []Sexp
  367. var sourceItem func(item Sexp) error
  368. sourceItem = func(item Sexp) error {
  369. switch t := item.(type) {
  370. case SexpArray:
  371. for _, v := range t {
  372. if err := sourceItem(v); err != nil {
  373. return err
  374. }
  375. }
  376. case SexpPair:
  377. expr := item
  378. for expr != SexpNull {
  379. list := expr.(SexpPair)
  380. if err := sourceItem(list.head); err != nil {
  381. return err
  382. }
  383. expr = list.tail
  384. }
  385. case SexpStr:
  386. exps, err = gen.env.ParseFile(string(t))
  387. if err != nil {
  388. return err
  389. }
  390. err = gen.GenerateBegin(exps)
  391. if err != nil {
  392. return err
  393. }
  394. default:
  395. return fmt.Errorf("include: Expected `string`, `list`, `array` given type %T val %v", item, item)
  396. }
  397. return nil
  398. }
  399. for _, v := range args {
  400. err = sourceItem(v)
  401. if err != nil {
  402. return err
  403. }
  404. }
  405. return nil
  406. }
  407. func (gen *Generator) GenerateCallBySymbol(sym SexpSymbol, args []Sexp) error {
  408. switch sym.name {
  409. case "and":
  410. return gen.GenerateShortCircuit(false, args)
  411. case "or":
  412. return gen.GenerateShortCircuit(true, args)
  413. case "cond":
  414. return gen.GenerateCond(args)
  415. case "quote":
  416. return gen.GenerateQuote(args)
  417. case "def":
  418. return gen.GenerateDef(args)
  419. case "fn":
  420. return gen.GenerateFn(args)
  421. case "defn":
  422. return gen.GenerateDefn(args)
  423. case "begin":
  424. return gen.GenerateBegin(args)
  425. case "let":
  426. return gen.GenerateLet("let", args)
  427. case "let*":
  428. return gen.GenerateLet("let*", args)
  429. case "assert":
  430. return gen.GenerateAssert(args)
  431. case "defmac":
  432. return gen.GenerateDefmac(args)
  433. case "macexpand":
  434. return gen.GenerateMacexpand(args)
  435. case "syntax-quote":
  436. return gen.GenerateSyntaxQuote(args)
  437. case "include":
  438. return gen.GenerateInclude(args)
  439. }
  440. macro, found := gen.env.macros[sym.number]
  441. if found {
  442. // calling Apply on the current environment will screw up
  443. // the stack, creating a duplicate environment is safer
  444. env := gen.env.Duplicate()
  445. expr, err := env.Apply(macro, args)
  446. if err != nil {
  447. return err
  448. }
  449. return gen.Generate(expr)
  450. }
  451. oldtail := gen.tail
  452. gen.tail = false
  453. err := gen.GenerateAll(args)
  454. if err != nil {
  455. return err
  456. }
  457. if oldtail && sym.name == gen.funcname {
  458. // to do a tail call
  459. // pop off all the extra scopes
  460. // then jump to beginning of function
  461. for i := 0; i < gen.scopes; i++ {
  462. gen.AddInstruction(RemoveScopeInstr(0))
  463. }
  464. gen.AddInstruction(GotoInstr{0})
  465. } else {
  466. gen.AddInstruction(CallInstr{sym, len(args)})
  467. }
  468. gen.tail = oldtail
  469. return nil
  470. }
  471. func (gen *Generator) GenerateDispatch(fun Sexp, args []Sexp) error {
  472. gen.GenerateAll(args)
  473. gen.Generate(fun)
  474. gen.AddInstruction(DispatchInstr{len(args)})
  475. return nil
  476. }
  477. func (gen *Generator) GenerateCall(expr SexpPair) error {
  478. arr, _ := ListToArray(expr.tail)
  479. switch head := expr.head.(type) {
  480. case SexpSymbol:
  481. return gen.GenerateCallBySymbol(head, arr)
  482. }
  483. return gen.GenerateDispatch(expr.head, arr)
  484. }
  485. func (gen *Generator) GenerateArray(arr SexpArray) error {
  486. err := gen.GenerateAll(arr)
  487. if err != nil {
  488. return err
  489. }
  490. gen.AddInstruction(CallInstr{gen.env.MakeSymbol("array"), len(arr)})
  491. return nil
  492. }
  493. func (gen *Generator) Generate(expr Sexp) error {
  494. switch e := expr.(type) {
  495. case SexpSymbol:
  496. gen.AddInstruction(GetInstr{e})
  497. return nil
  498. case SexpPair:
  499. if IsList(e) {
  500. err := gen.GenerateCall(e)
  501. if err != nil {
  502. return errors.New(
  503. fmt.Sprintf("Error generating %s:\n%v",
  504. expr.SexpString(), err))
  505. }
  506. return nil
  507. } else {
  508. gen.AddInstruction(PushInstr{expr})
  509. }
  510. case SexpArray:
  511. return gen.GenerateArray(e)
  512. default:
  513. gen.AddInstruction(PushInstr{expr})
  514. return nil
  515. }
  516. return nil
  517. }
  518. func (gen *Generator) GenerateAll(expressions []Sexp) error {
  519. for _, expr := range expressions {
  520. err := gen.Generate(expr)
  521. if err != nil {
  522. return err
  523. }
  524. }
  525. return nil
  526. }
  527. func (gen *Generator) Reset() {
  528. gen.instructions = make([]Instruction, 0)
  529. gen.tail = false
  530. gen.scopes = 0
  531. }
  532. // side-effect (or main effect) has to be pushing an expression on the top of
  533. // the datastack that represents the expanded and substituted expression
  534. func (gen *Generator) GenerateSyntaxQuote(args []Sexp) error {
  535. if len(args) != 1 {
  536. return errors.New("syntax-quote takes exactly one argument")
  537. }
  538. arg := args[0]
  539. // need to handle arrays, since they can have unquotes
  540. // in them too.
  541. switch arg.(type) {
  542. case SexpArray:
  543. gen.generateSyntaxQuoteArray(arg)
  544. return nil
  545. case SexpPair:
  546. if !IsList(arg) {
  547. break
  548. }
  549. gen.generateSyntaxQuoteList(arg)
  550. return nil
  551. case SexpHash:
  552. gen.generateSyntaxQuoteHash(arg)
  553. return nil
  554. }
  555. gen.AddInstruction(PushInstr{arg})
  556. return nil
  557. }
  558. func (gen *Generator) generateSyntaxQuoteList(arg Sexp) error {
  559. switch a := arg.(type) {
  560. case SexpPair:
  561. //good, required here
  562. default:
  563. return fmt.Errorf("arg to generateSyntaxQuoteList() must be list; got %T", a)
  564. }
  565. // things that need unquoting end up as
  566. // (unquote mysym)
  567. // i.e. a pair
  568. // list of length 2 exactly, with first atom
  569. // being "unquote" and second being the symbol
  570. // to substitute.
  571. quotebody, _ := ListToArray(arg)
  572. if len(quotebody) == 2 {
  573. var issymbol bool
  574. var sym SexpSymbol
  575. switch t := quotebody[0].(type) {
  576. case SexpSymbol:
  577. sym = t
  578. issymbol = true
  579. default:
  580. issymbol = false
  581. }
  582. if issymbol {
  583. if sym.name == "unquote" {
  584. gen.Generate(quotebody[1])
  585. return nil
  586. } else if sym.name == "unquote-splicing" {
  587. gen.Generate(quotebody[1])
  588. gen.AddInstruction(ExplodeInstr(0))
  589. return nil
  590. }
  591. }
  592. }
  593. gen.AddInstruction(PushInstr{SexpMarker})
  594. for _, expr := range quotebody {
  595. gen.GenerateSyntaxQuote([]Sexp{expr})
  596. }
  597. gen.AddInstruction(SquashInstr(0))
  598. return nil
  599. }
  600. func (gen *Generator) generateSyntaxQuoteArray(arg Sexp) error {
  601. var arr SexpArray
  602. switch a := arg.(type) {
  603. case SexpArray:
  604. //good, required here
  605. arr = a
  606. default:
  607. return fmt.Errorf("arg to generateSyntaxQuoteArray() must be an array; got %T", a)
  608. }
  609. gen.AddInstruction(PushInstr{SexpMarker})
  610. for _, expr := range arr {
  611. gen.AddInstruction(PushInstr{SexpMarker})
  612. gen.GenerateSyntaxQuote([]Sexp{expr})
  613. gen.AddInstruction(SquashInstr(0))
  614. gen.AddInstruction(ExplodeInstr(0))
  615. }
  616. gen.AddInstruction(VectorizeInstr(0))
  617. return nil
  618. }
  619. func (gen *Generator) generateSyntaxQuoteHash(arg Sexp) error {
  620. var hash SexpHash
  621. switch a := arg.(type) {
  622. case SexpHash:
  623. //good, required here
  624. hash = a
  625. default:
  626. return fmt.Errorf("arg to generateSyntaxQuoteHash() must be a hash; got %T", a)
  627. }
  628. n := HashCountKeys(hash)
  629. gen.AddInstruction(PushInstr{SexpMarker})
  630. for i := 0; i < n; i++ {
  631. // must reverse order here to preserve order on rebuild
  632. key := (*hash.KeyOrder)[(n-i)-1]
  633. val, err := hash.HashGet(key)
  634. if err != nil {
  635. return err
  636. }
  637. // value first, since value comes second on rebuild
  638. gen.AddInstruction(PushInstr{SexpMarker})
  639. gen.GenerateSyntaxQuote([]Sexp{val})
  640. gen.AddInstruction(SquashInstr(0))
  641. gen.AddInstruction(ExplodeInstr(0))
  642. gen.AddInstruction(PushInstr{SexpMarker})
  643. gen.GenerateSyntaxQuote([]Sexp{key})
  644. gen.AddInstruction(SquashInstr(0))
  645. gen.AddInstruction(ExplodeInstr(0))
  646. }
  647. gen.AddInstruction(HashizeInstr{
  648. HashLen: n,
  649. TypeName: *(hash.TypeName),
  650. })
  651. return nil
  652. }