61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
package loxgo
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"git.red-panda.pet/pandaware/lox-go/lexer"
|
|
"git.red-panda.pet/pandaware/lox-go/parser"
|
|
"git.red-panda.pet/pandaware/lox-go/reporter"
|
|
"git.red-panda.pet/pandaware/lox-go/runtime"
|
|
)
|
|
|
|
func RunFile(filename string) error {
|
|
bs, err := os.ReadFile(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := Run(string(bs)); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func Run(source string) error {
|
|
s := lexer.New(source)
|
|
|
|
lexStart := time.Now()
|
|
tokens, ok := s.ScanTokens()
|
|
lexDuration := time.Since(lexStart)
|
|
|
|
if !ok {
|
|
return errors.New("lexer error")
|
|
}
|
|
|
|
p := parser.New(tokens)
|
|
|
|
parseStart := time.Now()
|
|
expr, err := p.Parse()
|
|
parseDuration := time.Since(parseStart)
|
|
|
|
if err != nil {
|
|
return errors.New("parser error")
|
|
}
|
|
|
|
interpreter := runtime.NewInterpreter()
|
|
|
|
evalStart := time.Now()
|
|
reporter.Info(1, "interpreter", "repl", interpreter.Evaluate(expr).Debug())
|
|
evalDuration := time.Since(evalStart)
|
|
|
|
fmt.Println("")
|
|
reporter.Debug(-1, "lexer", "repl", fmt.Sprintf("took %s", lexDuration))
|
|
reporter.Debug(-1, "parser", "repl", fmt.Sprintf("took %s", parseDuration))
|
|
reporter.Debug(-1, "interp", "repl", fmt.Sprintf("took %s", evalDuration))
|
|
|
|
return nil
|
|
}
|