Skip to content

Commit

Permalink
Merge pull request #13 from JunNishimura/feature/update_repl
Browse files Browse the repository at this point in the history
print parsing result in repl
  • Loading branch information
JunNishimura authored Jun 10, 2024
2 parents def37a1 + 4b193f6 commit e7222a8
Show file tree
Hide file tree
Showing 2 changed files with 38 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()
}
24 changes: 21 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,27 @@ 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
}

if _, err := io.WriteString(out, program.String()); err != nil {
return
}
if _, err := io.WriteString(out, "\n"); err != nil {
return
}
}
}

func printParserErrors(out io.Writer, errors []string) {
for _, msg := range errors {
if _, err := io.WriteString(out, "\t"+msg+"\n"); err != nil {
return
}
}
}

0 comments on commit e7222a8

Please sign in to comment.