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

@ -28,17 +28,38 @@ type DirectoryResolver struct {
directories []string directories []string
} }
func (r *DirectoryResolver) Resolve(path string) (string, error) { func (r *DirectoryResolver) Resolve(from, path string) (*core.ImportResult, error) {
// relative imports
relpath := filepath.Join(filepath.Dir(from), path)
if f, err := os.ReadFile(relpath); err == nil {
return &core.ImportResult{
Source: string(f),
Path: relpath,
}, nil
}
for _, d := range r.directories { for _, d := range r.directories {
pth := filepath.Join(d, path) pth := filepath.Join(d, path)
f, err := os.ReadFile(pth) f, err := os.ReadFile(pth)
if err == nil { if err == nil {
return string(f), nil return &core.ImportResult{
Source: string(f),
Path: pth,
}, nil
}
pth = filepath.Join(pth, "main.ang")
f, err = os.ReadFile(pth)
if err == nil {
return &core.ImportResult{
Source: string(f),
Path: pth,
}, nil
} }
} }
return "", errors.New("couldn't resolve import") return nil, errors.New("couldn't resolve import")
} }
func (r *DirectoryResolver) IsSame(a, b string) bool { func (r *DirectoryResolver) IsSame(a, b string) bool {

View file

@ -1,7 +1,6 @@
package core package core
import ( import (
"errors"
"fmt" "fmt"
"strings" "strings"
) )
@ -21,8 +20,13 @@ type Compiler struct {
stack *Stack[LocalVariable] stack *Stack[LocalVariable]
} }
type ImportResult struct {
Source string
Path string
}
type ImportsResolver interface { type ImportsResolver interface {
Resolve(path string) (string, error) Resolve(from, path string) (*ImportResult, error)
IsSame(a, b string) bool IsSame(a, b string) bool
} }
@ -34,7 +38,7 @@ type LocalVariable struct {
type CompilerError struct { type CompilerError struct {
Description string Description string
Causer Node Boundary Boundary
Source []rune Source []rune
Trace []string Trace []string
} }
@ -51,7 +55,7 @@ func (e CompilerError) Format() string {
b.WriteString(e.Description) b.WriteString(e.Description)
// highlight offending area // highlight offending area
start, end := e.Causer.Bounds() start, end := e.Boundary.Bounds()
lineEnd := 0 lineEnd := 0
lineStart := 0 lineStart := 0
@ -1254,7 +1258,7 @@ func (c *Compiler) addDescend() {
c.add(InstructionDescend) c.add(InstructionDescend)
} }
func (c *Compiler) error(msg string, causer Node) CompilerError { func (c *Compiler) error(msg string, causer Boundary) CompilerError {
return CompilerError{ return CompilerError{
msg, msg,
causer, causer,
@ -1267,34 +1271,34 @@ func (c *Compiler) warn(msg string, causer Node) {
c.Warnings = append(c.Warnings, c.error(msg, causer)) c.Warnings = append(c.Warnings, c.error(msg, causer))
} }
func (c *Compiler) resolveImport(path string) error { func (c *Compiler) resolveImport(imp Import) error {
res, err := c.resolver.Resolve(c.fileStack.Peek(), imp.path)
if err != nil {
return err
}
// if already imported and available // if already imported and available
for _, i := range c.imports { for _, i := range c.imports {
if c.resolver.IsSame(path, i) { if c.resolver.IsSame(res.Path, i) {
return nil return nil
} }
} }
// stop recursive imports // stop recursive imports
for i := c.fileStack.Current - 1; i >= 0; i-- { for i := c.fileStack.Current - 1; i >= 0; i-- {
if c.resolver.IsSame(path, c.fileStack.items[i]) { if c.resolver.IsSame(res.Path, c.fileStack.items[i]) {
return errors.New("recursive import") return c.error("recursive import", imp)
} }
} }
src, err := c.resolver.Resolve(path) l := NewLexer(res.Source)
if err != nil {
return err
}
l := NewLexer(src)
tokens, err := l.Tokenize() tokens, err := l.Tokenize()
if err != nil { if err != nil {
return err return err
} }
parser := NewParser(src, append(c.fileStack.Slice(), path), tokens) parser := NewParser(res.Source, append(c.fileStack.Slice(), res.Path), tokens)
p, err := parser.Parse(path) p, err := parser.Parse(res.Path)
if err != nil { if err != nil {
return err return err
} }
@ -1302,7 +1306,7 @@ func (c *Compiler) resolveImport(path string) error {
oldSrc := c.source oldSrc := c.source
// update source for more descriptive errors // update source for more descriptive errors
c.source = []rune(src) c.source = []rune(res.Source)
if err := c.Compile(p); err != nil { if err := c.Compile(p); err != nil {
return err return err
} }

View file

@ -15,6 +15,10 @@ type Node interface {
Bounds() (Pos, Pos) Bounds() (Pos, Pos)
} }
type Boundary interface {
Bounds() (Pos, Pos)
}
const ( const (
StringNodeType NodeType = iota StringNodeType NodeType = iota
NumberNodeType NumberNodeType

View file

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

View file

@ -17,18 +17,21 @@ func (r *JsResolver) IsSame(a, b string) bool {
return a == b return a == b
} }
func (r *JsResolver) Resolve(name string) (string, error) { func (r *JsResolver) Resolve(_, name string) (*core.ImportResult, error) {
jsv := r.jsResolver.Invoke(name) jsv := r.jsResolver.Invoke(name)
if jsv.Type() == js.TypeUndefined { if jsv.Type() == js.TypeUndefined {
return "", errors.New("cannot find import with name " + name) return nil, errors.New("cannot find import with name " + name)
} }
if jsv.Type() != js.TypeString { if jsv.Type() != js.TypeString {
return "", errors.New("invalid value for source: " + jsv.String()) return nil, errors.New("invalid value for source: " + jsv.String())
} }
return jsv.String(), nil return &core.ImportResult{
Source: jsv.String(),
Path: name,
}, nil
} }
func jsError(err error) interface{} { func jsError(err error) interface{} {