Add importing, lists and objects, and add basic functions (global and on values)
This commit is contained in:
parent
449f7cd815
commit
634a4a2b61
11 changed files with 919 additions and 60 deletions
51
cli/main.go
51
cli/main.go
|
|
@ -5,6 +5,7 @@ import (
|
|||
"log"
|
||||
"neemek.com/anglais/core"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
|
|
@ -16,6 +17,37 @@ type RunCmd struct {
|
|||
File string `arg:"" name:"file" help:"File to read program from" type:"existingfile"`
|
||||
}
|
||||
|
||||
// WorkingDirectoryResolver resolves imports relative to the working directory
|
||||
type WorkingDirectoryResolver struct {
|
||||
workingDirectory string
|
||||
}
|
||||
|
||||
func (r *WorkingDirectoryResolver) Resolve(path string) (core.Node, error) {
|
||||
pth := filepath.Join(r.workingDirectory, path)
|
||||
f, err := os.ReadFile(pth)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
str := string(f)
|
||||
|
||||
l := core.NewLexer(str)
|
||||
|
||||
tokens, err := l.Tokenize()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p := core.NewParser(tokens)
|
||||
|
||||
tree, err := p.Parse()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tree, nil
|
||||
}
|
||||
|
||||
func (cmd *RunCmd) Run(ctx *Context) error {
|
||||
if ctx.Debug {
|
||||
log.Println("Reading file")
|
||||
|
|
@ -72,6 +104,15 @@ func (cmd *RunCmd) Run(ctx *Context) error {
|
|||
}
|
||||
c := core.NewCompiler()
|
||||
|
||||
if ctx.Debug {
|
||||
log.Println("Setting imports resolver")
|
||||
}
|
||||
|
||||
dir, _ := filepath.Split(cmd.File)
|
||||
c.SetImportsResolver(&WorkingDirectoryResolver{
|
||||
dir,
|
||||
})
|
||||
|
||||
if ctx.Debug {
|
||||
log.Println("Compiling parse tree")
|
||||
}
|
||||
|
|
@ -162,8 +203,18 @@ func (cmd *CompileCmd) Run(ctx *Context) error {
|
|||
if ctx.Debug {
|
||||
log.Println("Initialized compiler")
|
||||
}
|
||||
|
||||
c := core.NewCompiler()
|
||||
|
||||
if ctx.Debug {
|
||||
log.Println("Setting import resolver")
|
||||
}
|
||||
|
||||
dir, _ := filepath.Split(cmd.File)
|
||||
c.SetImportsResolver(&WorkingDirectoryResolver{
|
||||
dir,
|
||||
})
|
||||
|
||||
if ctx.Debug {
|
||||
log.Println("Compiling parse tree")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue