parser.go 4.0 KB

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