Implemented package imports
This commit is contained in:
@ -1,7 +0,0 @@
|
||||
package config
|
||||
|
||||
import "runtime/debug"
|
||||
|
||||
func init() {
|
||||
debug.SetGCPercent(-1)
|
||||
}
|
53
src/build/config/init.go
Normal file
53
src/build/config/init.go
Normal file
@ -0,0 +1,53 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
var (
|
||||
Executable string
|
||||
Root string
|
||||
Library string
|
||||
)
|
||||
|
||||
func init() {
|
||||
debug.SetGCPercent(-1)
|
||||
executable, err := os.Executable()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Executable = executable
|
||||
Root = filepath.Dir(executable)
|
||||
Library = filepath.Join(Root, "lib")
|
||||
stat, err := os.Stat(Library)
|
||||
|
||||
if !os.IsNotExist(err) && stat != nil && stat.IsDir() {
|
||||
return
|
||||
}
|
||||
|
||||
dir, err := os.Getwd()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for {
|
||||
Library = path.Join(dir, "lib")
|
||||
stat, err := os.Stat(Library)
|
||||
|
||||
if !os.IsNotExist(err) && stat != nil && stat.IsDir() {
|
||||
return
|
||||
}
|
||||
|
||||
if dir == "/" {
|
||||
panic("standard library not found")
|
||||
}
|
||||
|
||||
dir = filepath.Dir(dir)
|
||||
}
|
||||
}
|
@ -11,7 +11,15 @@ import (
|
||||
// Registers that are in use must be saved if they are modified by the function.
|
||||
// After the function call, they must be restored in reverse order.
|
||||
func (f *Function) CompileCall(root *expression.Expression) error {
|
||||
funcName := root.Children[0].Token.Text()
|
||||
funcNameRoot := root.Children[0]
|
||||
funcName := ""
|
||||
|
||||
if funcNameRoot.IsLeaf() {
|
||||
funcName = funcNameRoot.Token.Text()
|
||||
} else {
|
||||
funcName = funcNameRoot.Children[0].Token.Text() + funcNameRoot.Token.Text() + funcNameRoot.Children[1].Token.Text()
|
||||
}
|
||||
|
||||
isSyscall := funcName == "syscall"
|
||||
|
||||
if !isSyscall {
|
||||
|
@ -2,6 +2,7 @@ package keyword
|
||||
|
||||
const (
|
||||
If = "if"
|
||||
Import = "import"
|
||||
Loop = "loop"
|
||||
Return = "return"
|
||||
)
|
||||
@ -9,6 +10,7 @@ const (
|
||||
// Map is a map of all keywords used in the language.
|
||||
var Map = map[string][]byte{
|
||||
If: []byte(If),
|
||||
Import: []byte(Import),
|
||||
Loop: []byte(Loop),
|
||||
Return: []byte(Return),
|
||||
}
|
||||
|
@ -1,278 +1,20 @@
|
||||
package scanner
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
import "git.akyoto.dev/cli/q/src/build/core"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/build/arch/x64"
|
||||
"git.akyoto.dev/cli/q/src/build/core"
|
||||
"git.akyoto.dev/cli/q/src/build/errors"
|
||||
"git.akyoto.dev/cli/q/src/build/expression"
|
||||
"git.akyoto.dev/cli/q/src/build/fs"
|
||||
"git.akyoto.dev/cli/q/src/build/token"
|
||||
)
|
||||
|
||||
// Scan scans the directory.
|
||||
// Scan scans the list of files.
|
||||
func Scan(files []string) (<-chan *core.Function, <-chan error) {
|
||||
functions := make(chan *core.Function)
|
||||
errors := make(chan error)
|
||||
scanner := Scanner{
|
||||
functions: make(chan *core.Function),
|
||||
errors: make(chan error),
|
||||
}
|
||||
|
||||
go func() {
|
||||
scanFiles(files, functions, errors)
|
||||
close(functions)
|
||||
close(errors)
|
||||
scanner.queue(files...)
|
||||
scanner.group.Wait()
|
||||
close(scanner.functions)
|
||||
close(scanner.errors)
|
||||
}()
|
||||
|
||||
return functions, errors
|
||||
}
|
||||
|
||||
// scanFiles scans the list of files without channel allocations.
|
||||
func scanFiles(files []string, functions chan<- *core.Function, errors chan<- error) {
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
for _, file := range files {
|
||||
stat, err := os.Stat(file)
|
||||
|
||||
if err != nil {
|
||||
errors <- err
|
||||
return
|
||||
}
|
||||
|
||||
if stat.IsDir() {
|
||||
err = fs.Walk(file, func(name string) {
|
||||
if !strings.HasSuffix(name, ".q") {
|
||||
return
|
||||
}
|
||||
|
||||
fullPath := filepath.Join(file, name)
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
err := scanFile(fullPath, functions)
|
||||
|
||||
if err != nil {
|
||||
errors <- err
|
||||
}
|
||||
}()
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
errors <- err
|
||||
}
|
||||
} else {
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
err := scanFile(file, functions)
|
||||
|
||||
if err != nil {
|
||||
errors <- err
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
// scanFile scans a single file.
|
||||
func scanFile(path string, functions chan<- *core.Function) error {
|
||||
contents, err := os.ReadFile(path)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tokens := token.Tokenize(contents)
|
||||
|
||||
file := &fs.File{
|
||||
Tokens: tokens,
|
||||
Path: path,
|
||||
}
|
||||
|
||||
var (
|
||||
i = 0
|
||||
groupLevel = 0
|
||||
blockLevel = 0
|
||||
nameStart = -1
|
||||
paramsStart = -1
|
||||
paramsEnd = -1
|
||||
bodyStart = -1
|
||||
)
|
||||
|
||||
for {
|
||||
// Function name
|
||||
for i < len(tokens) {
|
||||
if tokens[i].Kind == token.Identifier {
|
||||
nameStart = i
|
||||
i++
|
||||
break
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.NewLine || tokens[i].Kind == token.Comment {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.Invalid {
|
||||
return errors.New(&errors.InvalidCharacter{Character: tokens[i].Text()}, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.EOF {
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New(errors.ExpectedFunctionName, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
// Function parameters
|
||||
for i < len(tokens) {
|
||||
if tokens[i].Kind == token.GroupStart {
|
||||
groupLevel++
|
||||
i++
|
||||
|
||||
if groupLevel == 1 {
|
||||
paramsStart = i
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.GroupEnd {
|
||||
groupLevel--
|
||||
|
||||
if groupLevel < 0 {
|
||||
return errors.New(errors.MissingGroupStart, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if groupLevel == 0 {
|
||||
paramsEnd = i
|
||||
i++
|
||||
break
|
||||
}
|
||||
|
||||
i++
|
||||
continue
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.Invalid {
|
||||
return errors.New(&errors.InvalidCharacter{Character: tokens[i].Text()}, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.EOF {
|
||||
if groupLevel > 0 {
|
||||
return errors.New(errors.MissingGroupEnd, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if paramsStart == -1 {
|
||||
return errors.New(errors.ExpectedFunctionParameters, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if groupLevel > 0 {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
|
||||
return errors.New(errors.ExpectedFunctionParameters, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
// Function definition
|
||||
for i < len(tokens) {
|
||||
if tokens[i].Kind == token.BlockStart {
|
||||
blockLevel++
|
||||
i++
|
||||
|
||||
if blockLevel == 1 {
|
||||
bodyStart = i
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.BlockEnd {
|
||||
blockLevel--
|
||||
|
||||
if blockLevel < 0 {
|
||||
return errors.New(errors.MissingBlockStart, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if blockLevel == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
i++
|
||||
continue
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.Invalid {
|
||||
return errors.New(&errors.InvalidCharacter{Character: tokens[i].Text()}, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.EOF {
|
||||
if blockLevel > 0 {
|
||||
return errors.New(errors.MissingBlockEnd, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if bodyStart == -1 {
|
||||
return errors.New(errors.ExpectedFunctionDefinition, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if blockLevel > 0 {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
|
||||
return errors.New(errors.ExpectedFunctionDefinition, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
name := tokens[nameStart].Text()
|
||||
body := tokens[bodyStart:i]
|
||||
function := core.NewFunction(name, file, body)
|
||||
parameters := tokens[paramsStart:paramsEnd]
|
||||
count := 0
|
||||
|
||||
err := expression.EachParameter(parameters, func(tokens token.List) error {
|
||||
if len(tokens) != 1 {
|
||||
return errors.New(errors.NotImplemented, file, tokens[0].Position)
|
||||
}
|
||||
|
||||
name := tokens[0].Text()
|
||||
register := x64.CallRegisters[count]
|
||||
uses := token.Count(function.Body, token.Identifier, name)
|
||||
|
||||
if uses == 0 {
|
||||
return errors.New(&errors.UnusedVariable{Name: name}, file, tokens[0].Position)
|
||||
}
|
||||
|
||||
variable := &core.Variable{
|
||||
Name: name,
|
||||
Register: register,
|
||||
Alive: uses,
|
||||
}
|
||||
|
||||
function.AddVariable(variable)
|
||||
count++
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
functions <- function
|
||||
nameStart = -1
|
||||
paramsStart = -1
|
||||
bodyStart = -1
|
||||
i++
|
||||
}
|
||||
return scanner.functions, scanner.errors
|
||||
}
|
||||
|
14
src/build/scanner/Scanner.go
Normal file
14
src/build/scanner/Scanner.go
Normal file
@ -0,0 +1,14 @@
|
||||
package scanner
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/build/core"
|
||||
)
|
||||
|
||||
// Scanner is used to scan files before the actual compilation step.
|
||||
type Scanner struct {
|
||||
functions chan *core.Function
|
||||
errors chan error
|
||||
group sync.WaitGroup
|
||||
}
|
21
src/build/scanner/queue.go
Normal file
21
src/build/scanner/queue.go
Normal file
@ -0,0 +1,21 @@
|
||||
package scanner
|
||||
|
||||
import "os"
|
||||
|
||||
// queue scans the list of files.
|
||||
func (s *Scanner) queue(files ...string) {
|
||||
for _, file := range files {
|
||||
stat, err := os.Stat(file)
|
||||
|
||||
if err != nil {
|
||||
s.errors <- err
|
||||
return
|
||||
}
|
||||
|
||||
if stat.IsDir() {
|
||||
s.queueDirectory(file, "")
|
||||
} else {
|
||||
s.queueFile(file, "")
|
||||
}
|
||||
}
|
||||
}
|
24
src/build/scanner/queueDirectory.go
Normal file
24
src/build/scanner/queueDirectory.go
Normal file
@ -0,0 +1,24 @@
|
||||
package scanner
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/build/fs"
|
||||
)
|
||||
|
||||
// queueDirectory queues an entire directory to be scanned.
|
||||
func (s *Scanner) queueDirectory(directory string, pkg string) {
|
||||
err := fs.Walk(directory, func(name string) {
|
||||
if !strings.HasSuffix(name, ".q") {
|
||||
return
|
||||
}
|
||||
|
||||
fullPath := filepath.Join(directory, name)
|
||||
s.queueFile(fullPath, pkg)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
s.errors <- err
|
||||
}
|
||||
}
|
15
src/build/scanner/queueFile.go
Normal file
15
src/build/scanner/queueFile.go
Normal file
@ -0,0 +1,15 @@
|
||||
package scanner
|
||||
|
||||
// queueFile queues a single file to be scanned.
|
||||
func (s *Scanner) queueFile(file string, pkg string) {
|
||||
s.group.Add(1)
|
||||
|
||||
go func() {
|
||||
defer s.group.Done()
|
||||
err := s.scanFile(file, pkg)
|
||||
|
||||
if err != nil {
|
||||
s.errors <- err
|
||||
}
|
||||
}()
|
||||
}
|
238
src/build/scanner/scanFile.go
Normal file
238
src/build/scanner/scanFile.go
Normal file
@ -0,0 +1,238 @@
|
||||
package scanner
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"git.akyoto.dev/cli/q/src/build/arch/x64"
|
||||
"git.akyoto.dev/cli/q/src/build/config"
|
||||
"git.akyoto.dev/cli/q/src/build/core"
|
||||
"git.akyoto.dev/cli/q/src/build/errors"
|
||||
"git.akyoto.dev/cli/q/src/build/expression"
|
||||
"git.akyoto.dev/cli/q/src/build/fs"
|
||||
"git.akyoto.dev/cli/q/src/build/keyword"
|
||||
"git.akyoto.dev/cli/q/src/build/token"
|
||||
)
|
||||
|
||||
// scanFile scans a single file.
|
||||
func (s *Scanner) scanFile(path string, pkg string) error {
|
||||
contents, err := os.ReadFile(path)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tokens := token.Tokenize(contents)
|
||||
|
||||
file := &fs.File{
|
||||
Tokens: tokens,
|
||||
Path: path,
|
||||
}
|
||||
|
||||
var (
|
||||
i = 0
|
||||
groupLevel = 0
|
||||
blockLevel = 0
|
||||
nameStart = -1
|
||||
paramsStart = -1
|
||||
paramsEnd = -1
|
||||
bodyStart = -1
|
||||
)
|
||||
|
||||
for {
|
||||
for i < len(tokens) && tokens[i].Kind == token.Keyword && tokens[i].Text() == keyword.Import {
|
||||
i++
|
||||
|
||||
if tokens[i].Kind != token.Identifier {
|
||||
panic("expected package name")
|
||||
}
|
||||
|
||||
packageName := tokens[i].Text()
|
||||
s.queueDirectory(filepath.Join(config.Library, packageName), packageName)
|
||||
|
||||
i++
|
||||
|
||||
if tokens[i].Kind != token.NewLine && tokens[i].Kind != token.EOF {
|
||||
panic("expected newline or eof")
|
||||
}
|
||||
|
||||
i++
|
||||
}
|
||||
|
||||
// Function name
|
||||
for i < len(tokens) {
|
||||
if tokens[i].Kind == token.Identifier {
|
||||
nameStart = i
|
||||
i++
|
||||
break
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.NewLine || tokens[i].Kind == token.Comment {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.Invalid {
|
||||
return errors.New(&errors.InvalidCharacter{Character: tokens[i].Text()}, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.EOF {
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New(errors.ExpectedFunctionName, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
// Function parameters
|
||||
for i < len(tokens) {
|
||||
if tokens[i].Kind == token.GroupStart {
|
||||
groupLevel++
|
||||
i++
|
||||
|
||||
if groupLevel == 1 {
|
||||
paramsStart = i
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.GroupEnd {
|
||||
groupLevel--
|
||||
|
||||
if groupLevel < 0 {
|
||||
return errors.New(errors.MissingGroupStart, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if groupLevel == 0 {
|
||||
paramsEnd = i
|
||||
i++
|
||||
break
|
||||
}
|
||||
|
||||
i++
|
||||
continue
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.Invalid {
|
||||
return errors.New(&errors.InvalidCharacter{Character: tokens[i].Text()}, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.EOF {
|
||||
if groupLevel > 0 {
|
||||
return errors.New(errors.MissingGroupEnd, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if paramsStart == -1 {
|
||||
return errors.New(errors.ExpectedFunctionParameters, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if groupLevel > 0 {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
|
||||
return errors.New(errors.ExpectedFunctionParameters, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
// Function definition
|
||||
for i < len(tokens) {
|
||||
if tokens[i].Kind == token.BlockStart {
|
||||
blockLevel++
|
||||
i++
|
||||
|
||||
if blockLevel == 1 {
|
||||
bodyStart = i
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.BlockEnd {
|
||||
blockLevel--
|
||||
|
||||
if blockLevel < 0 {
|
||||
return errors.New(errors.MissingBlockStart, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if blockLevel == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
i++
|
||||
continue
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.Invalid {
|
||||
return errors.New(&errors.InvalidCharacter{Character: tokens[i].Text()}, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if tokens[i].Kind == token.EOF {
|
||||
if blockLevel > 0 {
|
||||
return errors.New(errors.MissingBlockEnd, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
if bodyStart == -1 {
|
||||
return errors.New(errors.ExpectedFunctionDefinition, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if blockLevel > 0 {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
|
||||
return errors.New(errors.ExpectedFunctionDefinition, file, tokens[i].Position)
|
||||
}
|
||||
|
||||
name := tokens[nameStart].Text()
|
||||
body := tokens[bodyStart:i]
|
||||
|
||||
if pkg != "" {
|
||||
name = fmt.Sprintf("%s.%s", pkg, name)
|
||||
}
|
||||
|
||||
function := core.NewFunction(name, file, body)
|
||||
parameters := tokens[paramsStart:paramsEnd]
|
||||
count := 0
|
||||
|
||||
err := expression.EachParameter(parameters, func(tokens token.List) error {
|
||||
if len(tokens) != 1 {
|
||||
return errors.New(errors.NotImplemented, file, tokens[0].Position)
|
||||
}
|
||||
|
||||
name := tokens[0].Text()
|
||||
register := x64.CallRegisters[count]
|
||||
uses := token.Count(function.Body, token.Identifier, name)
|
||||
|
||||
if uses == 0 {
|
||||
return errors.New(&errors.UnusedVariable{Name: name}, file, tokens[0].Position)
|
||||
}
|
||||
|
||||
variable := &core.Variable{
|
||||
Name: name,
|
||||
Register: register,
|
||||
Alive: uses,
|
||||
}
|
||||
|
||||
function.AddVariable(variable)
|
||||
count++
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.functions <- function
|
||||
nameStart = -1
|
||||
paramsStart = -1
|
||||
bodyStart = -1
|
||||
i++
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user