func list1() (*ast.ListCell, *ast.ListCell) { l0 := ast.NewList() l0.Add(ast.NewSym("+")) l0.Add(ast.NewSym("x")) l1 := l0.Add(ast.NewList()).(*ast.ListCell) l1.Add(ast.NewSym("*")) l1.Add(ast.NewNum(4)) l1.Add(ast.NewNum(7)) l0.Add(ast.NewNum(1)) return l0, l1 }
func (p *Parser) parseQuote(t token.Item) (ast.Cell, error) { qList := ast.NewList() qCell, err1 := ast.NewSym("quote") val, err2 := p.parseAST(<-p.tokens) if err1 != nil { return nil, err1 } if err2 != nil { return nil, err2 } qList.Add(qCell) qList.Add(val) return qList, nil }
func list2() (*ast.ListCell, *ast.ListCell, *ast.ListCell, *ast.ListCell, *ast.ListCell) { // l0 is the top list, l1 and l2 are elements of l0 // l3 and l4 are elements of l2 l0 := ast.NewList() l0.Add(ast.NewNum(3)) l1 := l0.Add(ast.NewList()).(*ast.ListCell) l2 := l0.Add(ast.NewList()).(*ast.ListCell) l3 := l2.Add(ast.NewList()).(*ast.ListCell) l2.Add(ast.NewSym("g")) l4 := l2.Add(ast.NewList()).(*ast.ListCell) return l0, l1, l2, l3, l4 }
func (p *Parser) parseAST(t token.Item) (ast.Cell, error) { var val ast.Cell var err error switch t.Type { // use following tokens to recursivly build the tree case token.LParen: val, err = p.parseTree(ast.NewList()) // this "tree" is a single sym case token.Sym: val, err = ast.NewSym(t.Value) // this "tree" is a single constant case token.Const: val, err = p.parseConst(t) // the next token is quoted case token.Quote: val, err = p.parseQuote(t) // close paren found without open paren, error case token.RParen: val, err = nil, errors.New("syntax error: close parenthese ')' without s-expression") case token.EOF: val, err = ast.NewEOF(), nil } return val, err }