restructure
This commit is contained in:
parent
edcbfde351
commit
eebaadc16e
21 changed files with 937 additions and 626 deletions
43
run.go
Normal file
43
run.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package loxgo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"git.red-panda.pet/pandaware/lox-go/ast"
|
||||
"git.red-panda.pet/pandaware/lox-go/lexer"
|
||||
"git.red-panda.pet/pandaware/lox-go/parser"
|
||||
"git.red-panda.pet/pandaware/lox-go/reporter"
|
||||
)
|
||||
|
||||
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)
|
||||
tokens, ok := s.ScanTokens()
|
||||
if !ok {
|
||||
return errors.New("lexer error")
|
||||
}
|
||||
|
||||
p := parser.New(tokens)
|
||||
expr, err := p.Parse()
|
||||
if err != nil {
|
||||
return errors.New("parser error")
|
||||
}
|
||||
|
||||
printer := &ast.Printer{}
|
||||
reporter.Debug(0, "astPrinter", "stdin", printer.Print(expr))
|
||||
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue