parser.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package parser
  2. import (
  3. "../lexer"
  4. "fmt"
  5. "go/token"
  6. )
  7. type Node interface {
  8. Type() NodeType
  9. // Position() Pos
  10. String() string
  11. }
  12. type Pos int
  13. func (p Pos) Position() Pos {
  14. return p
  15. }
  16. type NodeType int
  17. func (t NodeType) Type() NodeType {
  18. return t
  19. }
  20. const (
  21. NodeIdent NodeType = iota
  22. NodeString
  23. NodeNumber
  24. NodeCall
  25. NodeVector
  26. )
  27. type IdentNode struct {
  28. // Pos
  29. NodeType
  30. Ident string
  31. }
  32. func (node *IdentNode) String() string {
  33. if node.Ident == "nil" {
  34. return "()"
  35. }
  36. return node.Ident
  37. }
  38. type StringNode struct {
  39. // Pos
  40. NodeType
  41. Value string
  42. }
  43. func (node *StringNode) String() string {
  44. return node.Value
  45. }
  46. type NumberNode struct {
  47. // Pos
  48. NodeType
  49. Value string
  50. NumberType token.Token
  51. }
  52. func (node *NumberNode) String() string {
  53. return node.Value
  54. }
  55. type VectorNode struct {
  56. // Pos
  57. NodeType
  58. Nodes []Node
  59. }
  60. func (node *VectorNode) String() string {
  61. return fmt.Sprint(node.Nodes)
  62. }
  63. type CallNode struct {
  64. // Pos
  65. NodeType
  66. Callee Node
  67. Args []Node
  68. }
  69. func (node *CallNode) String() string {
  70. args := fmt.Sprint(node.Args)
  71. return fmt.Sprintf("(%s %s)", node.Callee, args[1:len(args)-1])
  72. }
  73. var nilNode = newIdentNode("nil")
  74. func ParseFromString(name, program string) []Node {
  75. return Parse(lexer.Lex(name, program))
  76. }
  77. func Parse(l *lexer.Lexer) []Node {
  78. return parser(l, make([]Node, 0), ' ')
  79. }
  80. func parser(l *lexer.Lexer, tree []Node, lookingFor rune) []Node {
  81. for item := l.NextItem(); item.Type != lexer.ItemEOF; {
  82. switch t := item.Type; t {
  83. case lexer.ItemIdent:
  84. tree = append(tree, newIdentNode(item.Value))
  85. case lexer.ItemString:
  86. tree = append(tree, newStringNode(item.Value))
  87. case lexer.ItemInt:
  88. tree = append(tree, newIntNode(item.Value))
  89. case lexer.ItemFloat:
  90. tree = append(tree, newFloatNode(item.Value))
  91. case lexer.ItemComplex:
  92. tree = append(tree, newComplexNode(item.Value))
  93. case lexer.ItemLeftParen:
  94. tree = append(tree, newCallNode(parser(l, make([]Node, 0), ')')))
  95. case lexer.ItemLeftVect:
  96. tree = append(tree, newVectNode(parser(l, make([]Node, 0), ']')))
  97. case lexer.ItemRightParen:
  98. if lookingFor != ')' {
  99. panic(fmt.Sprintf("unexpected \")\" [%d]", item.Pos))
  100. }
  101. return tree
  102. case lexer.ItemRightVect:
  103. if lookingFor != ']' {
  104. panic(fmt.Sprintf("unexpected \"]\" [%d]", item.Pos))
  105. }
  106. return tree
  107. case lexer.ItemError:
  108. println(item.Value)
  109. default:
  110. panic("Bad Item type")
  111. }
  112. item = l.NextItem()
  113. }
  114. return tree
  115. }
  116. func newIdentNode(name string) *IdentNode {
  117. return &IdentNode{NodeType: NodeIdent, Ident: name}
  118. }
  119. func newStringNode(val string) *StringNode {
  120. return &StringNode{NodeType: NodeString, Value: val}
  121. }
  122. func newIntNode(val string) *NumberNode {
  123. return &NumberNode{NodeType: NodeNumber, Value: val, NumberType: token.INT}
  124. }
  125. func newFloatNode(val string) *NumberNode {
  126. return &NumberNode{NodeType: NodeNumber, Value: val, NumberType: token.FLOAT}
  127. }
  128. func newComplexNode(val string) *NumberNode {
  129. return &NumberNode{NodeType: NodeNumber, Value: val, NumberType: token.IMAG}
  130. }
  131. // We return Node here, because it could be that it's nil
  132. func newCallNode(args []Node) Node {
  133. if len(args) > 0 {
  134. return &CallNode{NodeType: NodeCall, Callee: args[0], Args: args[1:]}
  135. } else {
  136. return nilNode
  137. }
  138. }
  139. func newVectNode(content []Node) *VectorNode {
  140. return &VectorNode{NodeType: NodeVector, Nodes: content}
  141. }