continue cmd

This commit is contained in:
Robin 2025-01-21 20:48:58 +01:00
parent c33321c259
commit ad486b7900
2 changed files with 58 additions and 1 deletions

View File

@ -3,11 +3,18 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"io"
"os" "os"
"path/filepath"
"strings"
"robaertschi.xyz/robaertschi/tt/lexer"
"robaertschi.xyz/robaertschi/tt/parser"
"robaertschi.xyz/robaertschi/tt/token"
"robaertschi.xyz/robaertschi/tt/typechecker"
) )
func main() { func main() {
flag.Usage = func() { flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s [flags] input\nPossible flags:\n", os.Args[0]) fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s [flags] input\nPossible flags:\n", os.Args[0])
flag.PrintDefaults() flag.PrintDefaults()
@ -23,4 +30,50 @@ func main() {
flag.Usage() flag.Usage()
os.Exit(1) os.Exit(1)
} }
if output == "" {
output = strings.TrimRight(input, filepath.Ext(input))
}
file, err := os.Open(input)
if err != nil {
fmt.Printf("Could not open file %q because: %e", input, err)
os.Exit(1)
}
defer file.Close()
inputText, err := io.ReadAll(file)
if err != nil {
fmt.Printf("Could not read file %q because: %e", input, err)
os.Exit(1)
}
l, err := lexer.New(string(inputText), input)
if err != nil {
fmt.Printf("Error while creating lexer: %e", err)
os.Exit(1)
}
l.WithErrorCallback(func(l token.Loc, s string, a ...any) {
fmt.Printf("%s:%d:%d: %s\n", l.File, l.Line, l.Col, fmt.Sprintf(s, a...))
})
p := parser.New(l)
p.WithErrorCallback(func(t token.Token, s string, a ...any) {
loc := t.Loc
fmt.Printf("%s:%d:%d: %s\n", loc.File, loc.Line, loc.Col, fmt.Sprintf(s, a...))
})
program := p.ParseProgram()
if p.Errors() > 0 {
fmt.Printf("Parser encountered 1 or more errors, quiting...\n")
os.Exit(1)
}
tprogram, err := typechecker.New().CheckProgram(program)
if err != nil {
fmt.Printf("Typechecker failed with %e\n", err)
os.Exit(1)
}
} }

View File

@ -51,6 +51,10 @@ func (p *Parser) WithErrorCallback(errorCallback ErrorCallback) {
p.errorCallback = errorCallback p.errorCallback = errorCallback
} }
func (p *Parser) Errors() int {
return p.errors
}
func (p *Parser) registerInfixFn(tt token.TokenType, infix infixParseFn) { func (p *Parser) registerInfixFn(tt token.TokenType, infix infixParseFn) {
p.infixParseFns[tt] = infix p.infixParseFns[tt] = infix
} }