make compiler errors general, and better imports
Some checks failed
/ test (push) Failing after 43s

This commit is contained in:
Neemek 2025-10-02 13:03:40 +02:00
parent f2ebcf560f
commit b95d2954ba
Signed by: neemek
GPG key ID: 84FFE4D7D40AB25E
5 changed files with 77 additions and 30 deletions

View file

@ -93,17 +93,27 @@ func NewParser(source string, trace []string, tokens []Token) *Parser {
}
type Program struct {
Imports []string
Imports []Import
Block *BlockNode
Path string
}
type Import struct {
path string
start Pos
end Pos
}
func (i Import) Bounds() (Pos, Pos) {
return i.start, i.end
}
func (p *Program) String() string {
builder := strings.Builder{}
builder.WriteString("=== Imports ===\n")
for _, i := range p.Imports {
builder.WriteString(i)
builder.WriteString(i.path)
builder.WriteString("\n")
}
builder.WriteString("===============\n")
@ -114,7 +124,7 @@ func (p *Program) String() string {
}
func (p *Parser) Parse(path string) (*Program, error) {
imports := make([]string, 0)
imports := make([]Import, 0)
// top level statements
statements := make([]Node, 0)
@ -124,11 +134,16 @@ func (p *Parser) Parse(path string) (*Program, error) {
for int(p.pos) < len(p.tokens) && p.curr.Type != TokenEOF {
if p.accept(TokenImport) {
start := p.prev.Start
if err := p.expect(TokenString, "import requires a path/name to import"); err != nil {
return nil, err
}
imports = append(imports, p.prev.Lexeme[1:len(p.prev.Lexeme)-1])
imports = append(imports, Import{
p.prev.Lexeme[1 : len(p.prev.Lexeme)-1],
start,
p.prev.Start + p.prev.Length,
})
continue
}
@ -582,7 +597,7 @@ func (p *Parser) condition() (Node, error) {
p.advance()
c, err := p.comparison()
c, err := p.condition()
if err != nil {
return left, err
}