Skip to content

Commit

Permalink
print parsing result in repl
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 10, 2024
1 parent def37a1 commit ecaed46
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
17 changes: 17 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,20 @@ func (cc *ConsCell) String() string {
type Program struct {
SExpressions []Cell
}

func (p *Program) TokenLiteral() string {
if len(p.SExpressions) > 0 {
return p.SExpressions[0].TokenLiteral()
}
return ""
}

func (p *Program) String() string {
var out bytes.Buffer

for _, s := range p.SExpressions {
out.WriteString(s.String())
}

return out.String()
}
18 changes: 15 additions & 3 deletions repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io"

"github.com/JunNishimura/go-lisp/lexer"
"github.com/JunNishimura/go-lisp/token"
"github.com/JunNishimura/go-lisp/parser"
)

const PROMPT = ">> "
Expand All @@ -23,9 +23,21 @@ func Start(in io.Reader, out io.Writer) {

line := scanner.Text()
l := lexer.New(line)
p := parser.New(l)

for tok := l.NextToken(); tok.Type != token.EOF; tok = l.NextToken() {
fmt.Printf("%+v\n", tok)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
printParserErrors(out, p.Errors())
continue
}

io.WriteString(out, program.String())

Check failure on line 34 in repl/repl.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `io.WriteString` is not checked (errcheck)
io.WriteString(out, "\n")

Check failure on line 35 in repl/repl.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `io.WriteString` is not checked (errcheck)
}
}

func printParserErrors(out io.Writer, errors []string) {
for _, msg := range errors {
io.WriteString(out, "\t"+msg+"\n")

Check failure on line 41 in repl/repl.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `io.WriteString` is not checked (errcheck)
}
}

0 comments on commit ecaed46

Please sign in to comment.