raku.go 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. // raku
  2. /* Raku is an easy to use scripting language that can also be used easily interactively
  3. Desired simplified syntax:
  4. PROGRAM -> STATEMENTS.
  5. STATEMENTS -> STATEMENT STATEMENTS | .
  6. STATEMENT -> EXPRESSION eox | BLOCK .
  7. EXPRESSION -> word PARAMETERS.
  8. PARAMETERS -> PARAMETER PARAMETERS | .
  9. PARAMETER -> word | VALUE | PARENTHESIS | BLOCK | operator.
  10. PARENTHESIS -> bep PARAMETER eop .
  11. BLOCK -> bob STATEMENTS eob .
  12. VALUE -> string | long | double | symbol.
  13. Desrired syntax (verified LL(1) on smlweb.cpsc.ucalgary.ca)
  14. PROGRAM -> STATEMENTS.
  15. STATEMENTS -> STATEMENT STATEMENTS | .
  16. STATEMENT -> EXPRESSION EOX | DEFINITION | BLOCK .
  17. DEFINITION -> define WORDOP WORDOPS BLOCK.
  18. WORDOPS -> WORDOP WORDOPS | .
  19. EXPRESSION -> WORDVALUE PARAMETERS.
  20. PARAMETERS -> PARAMETER PARAMETERS | .
  21. PARAMETER -> WORDVALUE | PARENTHESIS | BLOCK | operator.
  22. PARENTHESIS -> '(' EXPRESSION ')' | ot EXPRESSION ct.
  23. BLOCK -> oe STATEMENTS ce | do STATEMENTS end .
  24. WORDOP -> word | operator | a | the.
  25. WORDVALUE -> word | VALUE | a | the.
  26. VALUE -> string | number | symbol.
  27. EOX -> eol | period.
  28. Lexer:
  29. Yet another syntax, which supports operators, but requires () to use them,
  30. and [ ] to indicate expressions inside expressions.
  31. PROGRAM -> STATEMENTS.
  32. STATEMENTS -> STATEMENT STATEMENTS | .
  33. STATEMENT ->
  34. EXPRESSION EOX
  35. | BLOCK .
  36. EXPRESSION ->
  37. CALL
  38. | PARENTHESIS
  39. | RECTANGLE
  40. .
  41. CALL -> WORD PARAMETERS .
  42. RECTANGLE -> '[' EXPRESSION ']' |
  43. with EXPRESSION end .
  44. PARENTHESIS -> '(' OPERATION ')'
  45. | let OPERATION end .
  46. OPERATION -> PARAMETER OPLIST .
  47. OPLIST -> op OPLIST | .
  48. OP -> operator PARAMETER .
  49. PARAMETERS -> PARAMETER PARAMETERS | .
  50. PARAMETER -> WORDVALUE | BLOCK |
  51. PARENTHESIS | RECTANGLE .
  52. BLOCK -> '{' STATEMENTS '}' | do STATEMENTS end .
  53. WORDVALUE -> word | VALUE | a | the.
  54. VALUE -> string | number | symbol.
  55. EOX -> '\n' .
  56. Most simple "lisp but with less parenthesis" syntax:
  57. PROGRAM -> STATEMENTS.
  58. STATEMENTS -> STATEMENT STATEMENTS | .
  59. STATEMENT -> CALL | EOX | BLOCK .
  60. BLOCK -> '{' STATEMENTS '}' .
  61. CALL -> word PARAMETERS EOX .
  62. PARAMETERS -> PARAMETER PARAMETERS | .
  63. PARAMETER -> WORDVALUE | BLOCK .
  64. WORDVALUE -> word | VALUE .
  65. VALUE -> string | number | symbol.
  66. EOX -> '\n' .
  67. LMore TCL-is allows operators in () and forces evaluation of blocks in [].
  68. PROGRAM -> STATEMENTS.
  69. STATEMENTS -> STATEMENT STATEMENTS | .
  70. STATEMENT ->
  71. COMMAND
  72. | SUBSTITUTION
  73. | BLOCK
  74. | EXPRESSION
  75. | EOX
  76. .
  77. BLOCK ->
  78. '{' STATEMENTS '}'
  79. | do STATEMENTS end .
  80. SUBSTITUTION ->
  81. '[' STATEMENTS ']'
  82. | evaluate STATEMENTS end .
  83. EXPRESSION ->
  84. '(' EXPRBODY ')'
  85. | calculate EXPRBODY end .
  86. EXPRBODY -> OPERAND OPERANDS.
  87. OPERANDS -> operator OPERANDS | .
  88. OPERAND -> PARAMETER .
  89. COMMAND -> word PARAMETERS EOX .
  90. ARGUMENTS -> WORDVALUE ARGUMENTS | .
  91. PARAMETERS -> PARAMETER PARAMETERS | .
  92. PARAMETER -> WORDVALUE | SUBSTITUTION | EXPRESSION | BLOCK .
  93. WORDVALUE -> word | VALUE .
  94. VALUE -> string | number | symbol | true | false | nothing .
  95. EOX -> '\n' .
  96. set (door's state) to closed .
  97. # Type a grammar here:
  98. PROGRAM -> STATEMENTS.
  99. STATEMENTS -> STATEMENT STATEMENTS | .
  100. STATEMENT ->
  101. COMMAND
  102. | SUBSTITUTION
  103. | BLOCK
  104. | EXPRESSION
  105. | EOX
  106. .
  107. BLOCK ->
  108. '{' STATEMENTS '}'
  109. | do STATEMENTS end .
  110. SUBSTITUTION ->
  111. '[' STATEMENTS ']'
  112. | evaluate STATEMENTS end .
  113. EXPRESSION ->
  114. '(' EXPRBODY ')'
  115. | calculate EXPRBODY end .
  116. EXPRBODY -> OPERAND OPERANDS.
  117. OPERANDS -> operator OPERANDS | .
  118. OPERAND -> PARAMETER .
  119. COMMAND -> word PARAMETERS EOX .
  120. ARGUMENTS -> WORDVALUE ARGUMENTS | .
  121. PARAMETERS -> PARAMETER PARAMETERS | .
  122. PARAMETER -> WORDVALUE | SUBSTITUTION | EXPRESSION | BLOCK .
  123. WORDVALUE -> word | VALUE .
  124. VALUE -> string | number | symbol | true | false | nothing .
  125. EOX -> '\n' .
  126. # Or, this gramar, also useful as a generic command parser for
  127. # AIF or MUx itself. Though necessarily more complex .
  128. PROGRAM -> STATEMENTS.
  129. STATEMENTS -> STATEMENT STATEMENTS | .
  130. STATEMENT ->
  131. COMMAND
  132. | SUBSTITUTION
  133. | BLOCK
  134. | EXPRESSION
  135. | EOX
  136. .
  137. BLOCK ->
  138. '{' STATEMENTS '}'
  139. | do STATEMENTS end .
  140. SUBSTITUTION ->
  141. '[' STATEMENTS ']'
  142. | evaluate STATEMENTS end .
  143. EXPRESSION ->
  144. '(' EXPRBODY ')'
  145. | calculate EXPRBODY end .
  146. EXPRBODY -> OPERAND OPERANDS.
  147. OPERANDS -> operator OPERANDS | .
  148. OPERAND -> PARAMETER .
  149. COMMAND -> word ARGUMENTS EOX .
  150. ARGUMENTS -> ARGUMENT ARGUMENT_SEP ARGUMENTS | .
  151. ARGUMENT_SEP -> ',' | preposition | article | .
  152. ARGUMENT -> LITERAL | SUBSTITUTION | EXPRESSION | BLOCK .
  153. WORDLIT -> word | LITERAL .
  154. LITERAL -> string | number | symbol | true | false | nothing .
  155. EOX -> '\n' .
  156. type Duration (is a) number
  157. ( also could say a Duration (is a) number )
  158. type Effect (is an) integer
  159. constant No Effect is 1
  160. constant Healing Effect is an Effect which is 1
  161. the Damaging Effect is an Effect which is 2
  162. ( the is another way to say constant / variable )
  163. type Spell (is a) record (which) has
  164. (a) name (which is a/as a) String
  165. (a) Duration
  166. (an) Effect
  167. end
  168. variable cure light is a Spell which has
  169. name is "Cure Light"
  170. Duration is Duration 0.0
  171. Effect is Healing Effect
  172. end
  173. ( could have been the cure light is a spell ... )
  174. to cast (a) Spell at (a) Being do
  175. ( ... )
  176. end
  177. to cast (a) s which is a Spell at (a) b which is a Being do
  178. ( ... )
  179. end
  180. to add n1 which is a Number to n2 which is a Number do
  181. end
  182. to add one Number to another Number do
  183. one becomes one plus another
  184. end
  185. to duck do
  186. let text be "You duck"
  187. one becomes one plus another
  188. end
  189. type spellike (is an) interface which has
  190. cast (a) at Being
  191. end
  192. type Spell aliases spell
  193. cast cure light at Ben
  194. English single word prepositions :
  195. in
  196. aboard
  197. about
  198. above
  199. absent
  200. across
  201. after
  202. against
  203. along
  204. alongside
  205. amid
  206. amidst
  207. among
  208. apropos
  209. apud
  210. around
  211. as
  212. astride
  213. at
  214. on
  215. atop
  216. ontop
  217. bar
  218. before
  219. behind
  220. below
  221. beneath
  222. beside
  223. besides
  224. between
  225. beyond
  226. but
  227. by
  228. chez
  229. circa
  230. come
  231. dehors
  232. despite
  233. down
  234. during
  235. except
  236. for
  237. from
  238. in
  239. inside
  240. into
  241. less
  242. like
  243. minus
  244. near
  245. nearer
  246. nearest
  247. notwithstanding
  248. of
  249. off
  250. on
  251. onto
  252. opposite
  253. out
  254. outside
  255. over
  256. pace
  257. past
  258. per
  259. post
  260. pre
  261. pro
  262. qua
  263. re
  264. sans
  265. save
  266. short
  267. since
  268. than
  269. through
  270. thru
  271. throughout
  272. to
  273. toward
  274. towards
  275. under
  276. underneath
  277. unlike
  278. until
  279. up
  280. upon
  281. upside
  282. versus
  283. via
  284. vice
  285. vis-à-vis
  286. with
  287. within
  288. without
  289. worth
  290. */
  291. package raku
  292. import (
  293. "bytes"
  294. "errors"
  295. "fmt"
  296. "io"
  297. "reflect"
  298. "runtime"
  299. "strings"
  300. "unicode"
  301. // "gitlab.com/beoran/woe/graphviz"
  302. "gitlab.com/beoran/woe/monolog"
  303. "gitlab.com/beoran/woe/tree"
  304. )
  305. type TokenChannel chan *Token
  306. type Lexer struct {
  307. Reader io.Reader
  308. Positions []Position
  309. Token Token
  310. rule LexerRule
  311. Output TokenChannel
  312. buffer []byte
  313. runes []rune
  314. }
  315. type LexerRule func(lexer *Lexer) LexerRule
  316. func (me *Lexer) Last() Position {
  317. return me.Positions[1]
  318. }
  319. func (me *Lexer) Current() Position {
  320. return me.Positions[0]
  321. }
  322. func (me *Lexer) LastPtr() * Position {
  323. return &me.Positions[1]
  324. }
  325. func (me *Lexer) CurrentPtr() * Position {
  326. return &me.Positions[0]
  327. }
  328. func (me *Lexer) PushPosition(pos Position) {
  329. newpos := make([]Position, len(me.Positions) + 1)
  330. newpos[0] = pos
  331. for i := 1 ; i < len(me.Positions); i++ {
  332. newpos[i] = me.Positions[i-1]
  333. }
  334. me.Positions = newpos
  335. }
  336. func (me *Lexer) PushCurrentPosition() {
  337. current := me.Current()
  338. me.PushPosition(current)
  339. }
  340. func (me *Lexer) PopPosition() * Position {
  341. if (len(me.Positions) <= 2) {
  342. return nil
  343. }
  344. result := &me.Positions[0];
  345. newpos := make([]Position, len(me.Positions) - 1)
  346. for i := 1 ; i < len(me.Positions); i++ {
  347. newpos[i-1] = me.Positions[i]
  348. }
  349. me.Positions = newpos
  350. return result
  351. }
  352. func (me *Lexer) Emit(t TokenType, v TokenText) {
  353. tok := &Token{t, v, me.Current()}
  354. me.Output <- tok
  355. }
  356. func (me *Lexer) Error(message string, args ...interface{}) {
  357. value := fmt.Sprintf(message, args...)
  358. monolog.Error("Lex Error: %s", value)
  359. me.Emit(TokenError, TokenText(value))
  360. }
  361. func LexError(me *Lexer) LexerRule {
  362. me.Error("Error")
  363. return nil
  364. }
  365. func (me *Lexer) SkipComment() bool {
  366. if me.Peek() == '#' {
  367. if me.Next() == '(' {
  368. return me.SkipNotIn(")")
  369. } else {
  370. return me.SkipNotIn("\r\n")
  371. }
  372. }
  373. return true
  374. }
  375. /* Returns whether or not a keyword was found, and if so, the TokenType
  376. of the keyword.*/
  377. func LookupKeyword(word string) (bool, TokenType) {
  378. kind, found := keywordMap[word]
  379. return found, kind
  380. }
  381. /* Returns whether or not a special operator or sigil was found, and if so,
  382. returns the TokenTyp of the sigil.*/
  383. func LookupSigil(sigil string) (bool, TokenType) {
  384. fmt.Printf("LookupSigil: %s\n", sigil)
  385. kind, found := sigilMap[sigil]
  386. return found, kind
  387. }
  388. func LexSigil(me *Lexer) LexerRule {
  389. me.Found(TokenType(me.Peek()))
  390. _ = me.Next()
  391. me.Advance()
  392. return LexNormal
  393. }
  394. func LexWord(me *Lexer) LexerRule {
  395. me.SkipNotIn(" \t\r\n'({[]})")
  396. iskw, kind := LookupKeyword(me.CurrentStringValue())
  397. if iskw {
  398. me.Found(kind)
  399. } else {
  400. me.Found(TokenWord)
  401. }
  402. return LexNormal
  403. }
  404. func LexSymbol(me *Lexer) LexerRule {
  405. me.SkipNotIn(" \t\r\n'({[]})")
  406. me.Found(TokenSymbol)
  407. return LexNormal
  408. }
  409. func LexNumber(me *Lexer) LexerRule {
  410. me.SkipNotIn(" \t\r\n'({[]})")
  411. me.Found(TokenNumber)
  412. return LexNormal
  413. }
  414. func LexWhitespace(me *Lexer) LexerRule {
  415. me.SkipWhitespace()
  416. me.Advance()
  417. return LexNormal
  418. }
  419. func LexComment(me *Lexer) LexerRule {
  420. if !me.SkipComment() {
  421. me.Error("Unterminated comment")
  422. return LexError
  423. }
  424. me.Advance()
  425. return LexNormal
  426. }
  427. func LexPunctuator(me *Lexer) LexerRule {
  428. me.Found(TokenType(me.Peek()))
  429. _ = me.Next()
  430. me.Advance()
  431. return LexNormal
  432. }
  433. func LexEOL(me *Lexer) LexerRule {
  434. me.SkipIn("\r\n")
  435. me.Found(TokenEOL)
  436. return LexNormal
  437. }
  438. func LexOperator(me *Lexer) LexerRule {
  439. me.SkipIn(operator_chars)
  440. me.Found(TokenOperator)
  441. return LexNormal
  442. }
  443. func lexEscape(me *Lexer) error {
  444. _ = me.Next()
  445. return nil
  446. }
  447. func LexString(me *Lexer) LexerRule {
  448. open := me.Peek()
  449. do_escape := open == '"'
  450. peek := me.Next()
  451. me.Advance()
  452. for ; peek != '\000'; peek = me.Next() {
  453. if do_escape && peek == '\\' {
  454. if err := lexEscape(me); err != nil {
  455. return LexError
  456. }
  457. } else if peek == open {
  458. me.Found(TokenString)
  459. _ = me.Next()
  460. me.Advance()
  461. return LexNormal
  462. }
  463. }
  464. me.Error("Unexpected EOF in string.")
  465. return nil
  466. }
  467. func LexNumberOrOperator(me *Lexer) LexerRule {
  468. if unicode.IsDigit(me.Next()) {
  469. return LexNumber
  470. } else {
  471. _ = me.Previous()
  472. return LexOperator
  473. }
  474. }
  475. func LexNormal(me *Lexer) LexerRule {
  476. peek := me.Peek()
  477. if peek == '#' {
  478. return LexComment
  479. } else if strings.ContainsRune(" \t", peek) {
  480. return LexWhitespace
  481. } else if strings.ContainsRune(".,;:", peek) {
  482. return LexPunctuator
  483. } else if strings.ContainsRune("([{}])", peek) {
  484. return LexSigil
  485. } else if strings.ContainsRune("$", peek) {
  486. return LexSymbol
  487. } else if strings.ContainsRune("\r\n", peek) {
  488. return LexEOL
  489. } else if strings.ContainsRune("+-", peek) {
  490. return LexNumberOrOperator
  491. } else if strings.ContainsRune("\"`", peek) {
  492. return LexString
  493. } else if peek == '\000' {
  494. me.Emit(TokenEOF, "")
  495. return nil
  496. } else if unicode.IsLetter(peek) {
  497. return LexWord
  498. } else if unicode.IsDigit(peek) {
  499. return LexNumber
  500. } else if strings.ContainsRune(operator_chars, peek) {
  501. return LexOperator
  502. } else {
  503. return LexError
  504. }
  505. }
  506. func OpenLexer(reader io.Reader) *Lexer {
  507. lexer := &Lexer{}
  508. lexer.Reader = reader
  509. lexer.Output = make(TokenChannel)
  510. lexer.Positions = make([]Position, 2)
  511. // lexer.buffer = new(byte[1024])
  512. return lexer
  513. }
  514. func (me *Lexer) ReadReaderOnce() (bool, error) {
  515. buffer := make([]byte, 1024)
  516. n, err := me.Reader.Read(buffer)
  517. monolog.Debug("read %v %d %v\n", buffer[:n], n, err)
  518. if n > 0 {
  519. me.buffer = append(me.buffer, buffer[:n]...)
  520. monolog.Debug("append %s", me.buffer)
  521. }
  522. if err == io.EOF {
  523. return true, nil
  524. } else if err != nil {
  525. me.Error("Error reading from reader: %s", err)
  526. return true, err
  527. }
  528. return false, nil
  529. }
  530. func (me *Lexer) ReadReader() error {
  531. me.buffer = make([]byte, 0)
  532. more, err := me.ReadReaderOnce()
  533. for err == nil && more {
  534. more, err = me.ReadReaderOnce()
  535. }
  536. me.runes = bytes.Runes(me.buffer)
  537. return err
  538. }
  539. func (me *Lexer) Peek() rune {
  540. if (me.Current().Index) >= len(me.runes) {
  541. return '\000'
  542. }
  543. return me.runes[me.Current().Index]
  544. }
  545. func (me *Lexer) PeekNext() rune {
  546. if (me.Current().Index + 1) >= len(me.runes) {
  547. return '\000'
  548. }
  549. return me.runes[me.Current().Index+1]
  550. }
  551. func (me *Lexer) Next() rune {
  552. if me.Peek() == '\n' {
  553. me.CurrentPtr().Column = 0
  554. me.CurrentPtr().Row++
  555. }
  556. me.CurrentPtr().Index++
  557. if me.Current().Index >= len(me.runes) {
  558. //me.Emit(TokenEOF, "")
  559. }
  560. return me.Peek()
  561. }
  562. func (me *Lexer) Previous() rune {
  563. if me.Current().Index > 0 {
  564. me.CurrentPtr().Index--
  565. if me.Peek() == '\n' {
  566. me.CurrentPtr().Column = 0
  567. me.CurrentPtr().Row++
  568. }
  569. }
  570. return me.Peek()
  571. }
  572. func (me *Lexer) SkipRune() {
  573. _ = me.Next()
  574. }
  575. func (me *Lexer) SkipIn(set string) bool {
  576. for strings.ContainsRune(set, me.Next()) {
  577. monolog.Debug("SkipIn: %s %c\n", set, me.Peek())
  578. if me.Peek() == '\000' {
  579. return false
  580. }
  581. }
  582. return true
  583. }
  584. func (me *Lexer) SkipNotIn(set string) bool {
  585. _ = me.Next()
  586. for !strings.ContainsRune(set, me.Peek()) {
  587. if me.Next() == '\000' {
  588. return false
  589. }
  590. }
  591. return true
  592. }
  593. func (me *Lexer) SkipWhile(should_skip func(r rune) bool) bool {
  594. for should_skip(me.Peek()) {
  595. if me.Next() == '\000' {
  596. return false
  597. }
  598. }
  599. return true
  600. }
  601. func (me *Lexer) SkipWhitespace() {
  602. me.SkipIn(" \t")
  603. }
  604. func (me *Lexer) Advance() {
  605. (*me.LastPtr()) = me.Current()
  606. }
  607. func (me *Lexer) Rewind() {
  608. (*me.CurrentPtr()) = me.Last()
  609. }
  610. func (me *Lexer) CurrentRuneValue() []rune {
  611. return me.runes[me.Last().Index:me.Current().Index]
  612. }
  613. func (me *Lexer) CurrentStringValue() string {
  614. return string(me.CurrentRuneValue())
  615. }
  616. func (me *Lexer) Found(kind TokenType) {
  617. me.Emit(kind, TokenText(me.CurrentStringValue()))
  618. me.Advance()
  619. }
  620. func GetFunctionName(fun interface{}) string {
  621. return runtime.FuncForPC(reflect.ValueOf(fun).Pointer()).Name()
  622. }
  623. func (me *Lexer) Start() {
  624. if err := me.ReadReader(); err == nil || err == io.EOF {
  625. rule := LexNormal
  626. for rule != nil {
  627. monolog.Debug("Lexer Rule: %s\n", GetFunctionName(rule))
  628. rule = rule(me)
  629. }
  630. } else {
  631. me.Error("Could not read in input buffer: %s", err)
  632. }
  633. close(me.Output)
  634. }
  635. func (me *Lexer) TryLexing() {
  636. go me.Start()
  637. for token := range me.Output {
  638. monolog.Info("Token %s", token)
  639. }
  640. }
  641. type AstType int
  642. const (
  643. AstTypeProgram = AstType(iota)
  644. AstTypeStatements
  645. AstTypeStatement
  646. AstTypeDefinition
  647. AstTypeWords
  648. AstTypeExpression
  649. AstTypeWordExpression
  650. AstTypeWordCallop
  651. AstTypeOperation
  652. AstTypeOperations
  653. AstTypeCallArgs
  654. AstTypeValueExpression
  655. AstTypeValueCallop
  656. AstTypeParametersNonempty
  657. AstTypeParameters
  658. AstTypeParameter
  659. AstTypeBlock
  660. AstTypeWordValue
  661. AstTypeWord
  662. AstTypeValue
  663. AstTypeEox
  664. AstTypeOperator
  665. AstTypeParenthesis
  666. AstTypeModifier
  667. AstTypeError
  668. )
  669. var astTypeMap map[AstType]string = map[AstType]string{
  670. AstTypeProgram: "Program",
  671. AstTypeStatements: "Statements",
  672. AstTypeStatement: "Statement",
  673. AstTypeDefinition: "Definition",
  674. AstTypeWords: "Words",
  675. AstTypeExpression: "Expression",
  676. AstTypeWordExpression: "WordExpression",
  677. AstTypeWordCallop: "WordCallop",
  678. AstTypeOperation: "Operation",
  679. AstTypeOperations: "Operations",
  680. AstTypeCallArgs: "CallArgs",
  681. AstTypeValueExpression: "ValueExpression",
  682. AstTypeValueCallop: "ValueCallop",
  683. AstTypeParametersNonempty: "ParametersNonempty",
  684. AstTypeParameters: "Parameters",
  685. AstTypeParameter: "Parameter",
  686. AstTypeBlock: "Block",
  687. AstTypeWordValue: "WordValue",
  688. AstTypeWord: "Word",
  689. AstTypeValue: "Value",
  690. AstTypeEox: "Eox",
  691. AstTypeOperator: "Operator",
  692. AstTypeParenthesis: "Parenthesis",
  693. AstTypeModifier: "Modifier",
  694. AstTypeError: "Error",
  695. }
  696. func (me AstType) String() string {
  697. name, found := astTypeMap[me]
  698. if found {
  699. return name
  700. } else {
  701. return fmt.Sprintf("Unknown AstType %d", int(me))
  702. }
  703. }
  704. type Ast struct {
  705. tree.Node
  706. AstType
  707. *Token
  708. }
  709. func (me *Ast) Run(run *Runtime) (*Value, error) {
  710. switch me.AstType {
  711. case AstTypeProgram:
  712. return me.RunProgram(run)
  713. case AstTypeStatements:
  714. return me.RunStatements(run)
  715. case AstTypeStatement:
  716. return me.RunStatement(run)
  717. case AstTypeDefinition:
  718. return me.RunDefinition(run)
  719. case AstTypeWords:
  720. return me.RunWords(run)
  721. case AstTypeExpression:
  722. return me.RunExpression(run)
  723. case AstTypeWordExpression:
  724. return me.RunWord(run)
  725. case AstTypeWordCallop:
  726. return me.RunWordCallop(run)
  727. case AstTypeOperation:
  728. return me.RunOperation(run)
  729. case AstTypeOperations:
  730. return me.RunOperations(run)
  731. case AstTypeCallArgs:
  732. return me.RunCallArgs(run)
  733. case AstTypeValueExpression:
  734. return me.RunValueExpression(run)
  735. case AstTypeValueCallop:
  736. return me.RunValueCallop(run)
  737. case AstTypeParametersNonempty:
  738. return me.RunParametersNonempty(run)
  739. case AstTypeParameters:
  740. return me.RunParameters(run)
  741. case AstTypeParameter:
  742. return me.RunParameter(run)
  743. case AstTypeBlock:
  744. return me.RunBlock(run)
  745. case AstTypeWordValue:
  746. return me.RunWordValue(run)
  747. case AstTypeWord:
  748. return me.RunWord(run)
  749. case AstTypeValue:
  750. return me.RunValue(run)
  751. case AstTypeEox:
  752. return me.RunEox(run)
  753. case AstTypeOperator:
  754. return me.RunOperator(run)
  755. case AstTypeParenthesis:
  756. return me.RunParenthesis(run)
  757. case AstTypeModifier:
  758. return me.RunModifier(run)
  759. case AstTypeError:
  760. return me.RunError(run)
  761. default:
  762. return nil, errors.New("Shoudln't happen")
  763. }
  764. }
  765. func (me *Ast) RunProgram(run *Runtime) (*Value, error) {
  766. return nil, errors.New("Not implemented")
  767. }
  768. func (me *Ast) RunStatements(run *Runtime) (*Value, error) {
  769. return nil, errors.New("Not implemented")
  770. }
  771. func (me *Ast) RunStatement(run *Runtime) (*Value, error) {
  772. return nil, errors.New("Not implemented")
  773. }
  774. func (me *Ast) RunDefinition(run *Runtime) (*Value, error) {
  775. return nil, errors.New("Not implemented")
  776. }
  777. func (me *Ast) RunWords(run *Runtime) (*Value, error) {
  778. return nil, errors.New("Not implemented")
  779. }
  780. func (me *Ast) RunExpression(run *Runtime) (*Value, error) {
  781. return nil, errors.New("Not implemented")
  782. }
  783. func (me *Ast) RunWordExpression(run *Runtime) (*Value, error) {
  784. return nil, errors.New("Not implemented")
  785. }
  786. func (me *Ast) RunWordCallop(run *Runtime) (*Value, error) {
  787. return nil, errors.New("Not implemented")
  788. }
  789. func (me *Ast) RunOperation(run *Runtime) (*Value, error) {
  790. return nil, errors.New("Not implemented")
  791. }
  792. func (me *Ast) RunOperations(run *Runtime) (*Value, error) {
  793. return nil, errors.New("Not implemented")
  794. }
  795. func (me *Ast) RunCallArgs(run *Runtime) (*Value, error) {
  796. return nil, errors.New("Not implemented")
  797. }
  798. func (me *Ast) RunValueExpression(run *Runtime) (*Value, error) {
  799. return nil, errors.New("Not implemented")
  800. }
  801. func (me *Ast) RunValueCallop(run *Runtime) (*Value, error) {
  802. return nil, errors.New("Not implemented")
  803. }
  804. func (me *Ast) RunParametersNonempty(run *Runtime) (*Value, error) {
  805. return nil, errors.New("Not implemented")
  806. }
  807. func (me *Ast) RunParameters(run *Runtime) (*Value, error) {
  808. return nil, errors.New("Not implemented")
  809. }
  810. func (me *Ast) RunParameter(run *Runtime) (*Value, error) {
  811. return nil, errors.New("Not implemented")
  812. }
  813. func (me *Ast) RunBlock(run *Runtime) (*Value, error) {
  814. return nil, errors.New("Not implemented")
  815. }
  816. func (me *Ast) RunWordValue(run *Runtime) (*Value, error) {
  817. return nil, errors.New("Not implemented")
  818. }
  819. func (me *Ast) RunWord(run *Runtime) (*Value, error) {
  820. return nil, errors.New("Not implemented")
  821. }
  822. func (me *Ast) RunValue(run *Runtime) (*Value, error) {
  823. return nil, errors.New("Not implemented")
  824. }
  825. func (me *Ast) RunEox(run *Runtime) (*Value, error) {
  826. return nil, errors.New("Not implemented")
  827. }
  828. func (me *Ast) RunOperator(run *Runtime) (*Value, error) {
  829. return nil, errors.New("Not implemented")
  830. }
  831. func (me *Ast) RunParenthesis(run *Runtime) (*Value, error) {
  832. return nil, errors.New("Not implemented")
  833. }
  834. func (me *Ast) RunModifier(run *Runtime) (*Value, error) {
  835. return nil, errors.New("Not implemented")
  836. }
  837. func (me *Ast) RunError(run *Runtime) (*Value, error) {
  838. return nil, errors.New("Not implemented")
  839. }
  840. func (me *Ast) NewChild(kind AstType, token *Token) *Ast {
  841. child := &Ast{}
  842. child.AstType = kind
  843. child.Token = token
  844. tree.AppendChild(me, child)
  845. return child
  846. }
  847. func (me *Ast) Walk(walker func(ast *Ast) *Ast) *Ast {
  848. node_res := tree.Walk(me,
  849. func(node tree.Noder) tree.Noder {
  850. ast_res := walker(node.(*Ast))
  851. if ast_res == nil {
  852. return nil
  853. } else {
  854. return ast_res
  855. }
  856. })
  857. if node_res != nil {
  858. return node_res.(*Ast)
  859. } else {
  860. return nil
  861. }
  862. }
  863. func (me *Ast) Remove() {
  864. _ = tree.Remove(me)
  865. }
  866. func NewAst(kind AstType) *Ast {
  867. ast := &Ast{}
  868. ast.AstType = kind
  869. ast.Token = nil
  870. return ast
  871. }
  872. type DefineType int
  873. const (
  874. DefineTypeNone = DefineType(iota)
  875. DefineTypeGo
  876. DefineTypeUser
  877. DefineTypeVar
  878. )
  879. type Variable struct {
  880. Value
  881. Name string
  882. }
  883. type DefinePattern struct {
  884. Parts []string
  885. }
  886. type GoDefineFunc func(runtime *Runtime, args ...Value) Value
  887. type ScriptDefine struct {
  888. DefineType
  889. DefinePattern
  890. *Ast
  891. }
  892. type GoDefine struct {
  893. DefineType
  894. *DefinePattern
  895. GoDefineFunc
  896. }
  897. type Define interface {
  898. }
  899. type Environment struct {
  900. Parent *Environment
  901. Defines map[string]Define
  902. Variables map[string]*Variable
  903. Stack []Value
  904. }
  905. type Instruction int
  906. const (
  907. InstructionNop = Instruction(iota)
  908. InstructionCall
  909. InstructionPush
  910. InstructionPop
  911. )
  912. func (env *Environment) AddDefine(name string, def Define) {
  913. env.Defines[name] = def
  914. }
  915. func (env *Environment) NewGoDefine(name string, fn GoDefineFunc, pattern ...string) {
  916. defpattern := new(DefinePattern)
  917. defpattern.Parts = append(defpattern.Parts, pattern...)
  918. godefine := &GoDefine{DefineTypeGo, defpattern, fn}
  919. env.AddDefine(name, godefine)
  920. }
  921. type Runtime struct {
  922. Environment
  923. start *Ast
  924. now *Ast
  925. }
  926. func RuntimePuts(runtime *Runtime, args ...Value) Value {
  927. var iargs []interface{}
  928. for arg := range args {
  929. iargs = append(iargs, arg)
  930. }
  931. fmt.Print(iargs)
  932. return true
  933. }
  934. func (run *Runtime) Init() {
  935. run.NewGoDefine("puts", RuntimePuts, "$", "*")
  936. }
  937. func (run *Runtime) Start(ast *Ast) {
  938. run.start = ast
  939. run.now = ast
  940. }
  941. func (run *Runtime) RunOnce() {
  942. // run.now.Node
  943. }
  944. func main() {
  945. fmt.Println("Hello World!")
  946. }