This commit is contained in:
parent
f2ebcf560f
commit
b95d2954ba
5 changed files with 77 additions and 30 deletions
27
cli/main.go
27
cli/main.go
|
|
@ -28,17 +28,38 @@ type DirectoryResolver struct {
|
|||
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 {
|
||||
pth := filepath.Join(d, path)
|
||||
f, err := os.ReadFile(pth)
|
||||
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
|
@ -21,8 +20,13 @@ type Compiler struct {
|
|||
stack *Stack[LocalVariable]
|
||||
}
|
||||
|
||||
type ImportResult struct {
|
||||
Source string
|
||||
Path string
|
||||
}
|
||||
|
||||
type ImportsResolver interface {
|
||||
Resolve(path string) (string, error)
|
||||
Resolve(from, path string) (*ImportResult, error)
|
||||
IsSame(a, b string) bool
|
||||
}
|
||||
|
||||
|
|
@ -34,7 +38,7 @@ type LocalVariable struct {
|
|||
|
||||
type CompilerError struct {
|
||||
Description string
|
||||
Causer Node
|
||||
Boundary Boundary
|
||||
Source []rune
|
||||
Trace []string
|
||||
}
|
||||
|
|
@ -51,7 +55,7 @@ func (e CompilerError) Format() string {
|
|||
b.WriteString(e.Description)
|
||||
|
||||
// highlight offending area
|
||||
start, end := e.Causer.Bounds()
|
||||
start, end := e.Boundary.Bounds()
|
||||
|
||||
lineEnd := 0
|
||||
lineStart := 0
|
||||
|
|
@ -1254,7 +1258,7 @@ func (c *Compiler) addDescend() {
|
|||
c.add(InstructionDescend)
|
||||
}
|
||||
|
||||
func (c *Compiler) error(msg string, causer Node) CompilerError {
|
||||
func (c *Compiler) error(msg string, causer Boundary) CompilerError {
|
||||
return CompilerError{
|
||||
msg,
|
||||
causer,
|
||||
|
|
@ -1267,34 +1271,34 @@ func (c *Compiler) warn(msg string, causer Node) {
|
|||
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
|
||||
for _, i := range c.imports {
|
||||
if c.resolver.IsSame(path, i) {
|
||||
if c.resolver.IsSame(res.Path, i) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// stop recursive imports
|
||||
for i := c.fileStack.Current - 1; i >= 0; i-- {
|
||||
if c.resolver.IsSame(path, c.fileStack.items[i]) {
|
||||
return errors.New("recursive import")
|
||||
if c.resolver.IsSame(res.Path, c.fileStack.items[i]) {
|
||||
return c.error("recursive import", imp)
|
||||
}
|
||||
}
|
||||
|
||||
src, err := c.resolver.Resolve(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
l := NewLexer(src)
|
||||
l := NewLexer(res.Source)
|
||||
tokens, err := l.Tokenize()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
parser := NewParser(src, append(c.fileStack.Slice(), path), tokens)
|
||||
p, err := parser.Parse(path)
|
||||
parser := NewParser(res.Source, append(c.fileStack.Slice(), res.Path), tokens)
|
||||
p, err := parser.Parse(res.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -1302,7 +1306,7 @@ func (c *Compiler) resolveImport(path string) error {
|
|||
oldSrc := c.source
|
||||
|
||||
// update source for more descriptive errors
|
||||
c.source = []rune(src)
|
||||
c.source = []rune(res.Source)
|
||||
if err := c.Compile(p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,10 @@ type Node interface {
|
|||
Bounds() (Pos, Pos)
|
||||
}
|
||||
|
||||
type Boundary interface {
|
||||
Bounds() (Pos, Pos)
|
||||
}
|
||||
|
||||
const (
|
||||
StringNodeType NodeType = iota
|
||||
NumberNodeType
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
11
wasm/wasm.go
11
wasm/wasm.go
|
|
@ -17,18 +17,21 @@ func (r *JsResolver) IsSame(a, b string) bool {
|
|||
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)
|
||||
|
||||
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 {
|
||||
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{} {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue