raku.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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 Value interface {
  880. }
  881. type StringValue string
  882. type SymbolValue string
  883. type IntegerValue int64
  884. type FloatValue float64
  885. type ArrayValue []Value
  886. type MapValue map[string]Value
  887. type BoolValue bool
  888. type Variable struct {
  889. Value
  890. Name string
  891. }
  892. type DefinePattern struct {
  893. Parts []string
  894. }
  895. type GoDefineFunc func(runtime *Runtime, args ...Value) Value
  896. type ScriptDefine struct {
  897. DefineType
  898. DefinePattern
  899. *Ast
  900. }
  901. type GoDefine struct {
  902. DefineType
  903. *DefinePattern
  904. GoDefineFunc
  905. }
  906. type Define interface {
  907. }
  908. type Environment struct {
  909. Parent *Environment
  910. Defines map[string]Define
  911. Variables map[string]*Variable
  912. Stack []Value
  913. }
  914. type Instruction int
  915. const (
  916. InstructionNop = Instruction(iota)
  917. InstructionCall
  918. InstructionPush
  919. InstructionPop
  920. )
  921. func (env *Environment) AddDefine(name string, def Define) {
  922. env.Defines[name] = def
  923. }
  924. func (env *Environment) NewGoDefine(name string, fn GoDefineFunc, pattern ...string) {
  925. defpattern := new(DefinePattern)
  926. defpattern.Parts = append(defpattern.Parts, pattern...)
  927. godefine := &GoDefine{DefineTypeGo, defpattern, fn}
  928. env.AddDefine(name, godefine)
  929. }
  930. type Runtime struct {
  931. Environment
  932. start *Ast
  933. now *Ast
  934. }
  935. func RuntimePuts(runtime *Runtime, args ...Value) Value {
  936. var iargs []interface{}
  937. for arg := range args {
  938. iargs = append(iargs, arg)
  939. }
  940. fmt.Print(iargs)
  941. return true
  942. }
  943. func (run *Runtime) Init() {
  944. run.NewGoDefine("puts", RuntimePuts, "$", "*")
  945. }
  946. func (run *Runtime) Start(ast *Ast) {
  947. run.start = ast
  948. run.now = ast
  949. }
  950. func (run *Runtime) RunOnce() {
  951. // run.now.Node
  952. }
  953. func main() {
  954. fmt.Println("Hello World!")
  955. }