Implemented const keyword

This commit is contained in:
2025-02-15 14:38:01 +01:00
parent be028a52d1
commit 0a1a8f741d
21 changed files with 164 additions and 26 deletions

View File

@ -10,8 +10,9 @@ import (
)
// Scan scans the list of files.
func Scan(files []string) (<-chan *fs.File, <-chan *core.Function, <-chan *types.Struct, <-chan error) {
func Scan(files []string) (chan *core.Constant, <-chan *fs.File, <-chan *core.Function, <-chan *types.Struct, <-chan error) {
scanner := Scanner{
constants: make(chan *core.Constant),
files: make(chan *fs.File),
functions: make(chan *core.Function),
structs: make(chan *types.Struct),
@ -22,11 +23,12 @@ func Scan(files []string) (<-chan *fs.File, <-chan *core.Function, <-chan *types
scanner.queueDirectory(filepath.Join(config.Library, "mem"), "mem")
scanner.queue(files...)
scanner.group.Wait()
close(scanner.constants)
close(scanner.files)
close(scanner.functions)
close(scanner.structs)
close(scanner.errors)
}()
return scanner.files, scanner.functions, scanner.structs, scanner.errors
return scanner.constants, scanner.files, scanner.functions, scanner.structs, scanner.errors
}

View File

@ -10,6 +10,7 @@ import (
// Scanner is used to scan files before the actual compilation step.
type Scanner struct {
constants chan *core.Constant
files chan *fs.File
functions chan *core.Function
structs chan *types.Struct

47
src/scanner/scanConst.go Normal file
View File

@ -0,0 +1,47 @@
package scanner
import (
"git.akyoto.dev/cli/q/src/core"
"git.akyoto.dev/cli/q/src/errors"
"git.akyoto.dev/cli/q/src/fs"
"git.akyoto.dev/cli/q/src/token"
)
// scanConst scans a block of constants.
func (s *Scanner) scanConst(file *fs.File, tokens token.List, i int) (int, error) {
i++
if tokens[i].Kind != token.Identifier {
return i, errors.New(errors.ExpectedConstName, file, tokens[i].Position)
}
groupName := tokens[i].Text(file.Bytes)
i++
if tokens[i].Kind != token.BlockStart {
return i, errors.New(errors.MissingBlockStart, file, tokens[i].Position)
}
i++
for i < len(tokens) {
if tokens[i].Kind == token.Identifier {
name := tokens[i].Text(file.Bytes)
i++
s.constants <- &core.Constant{
Name: file.Package + "." + groupName + "." + name,
Value: tokens[i],
File: file,
}
}
if tokens[i].Kind == token.BlockEnd {
return i, nil
}
i++
}
return i, errors.New(errors.MissingBlockEnd, file, tokens[i].Position)
}

View File

@ -22,7 +22,6 @@ func (s *Scanner) scanExtern(file *fs.File, tokens token.List, i int) (int, erro
}
i++
closed := false
for i < len(tokens) {
if tokens[i].Kind == token.Identifier {
@ -39,16 +38,11 @@ func (s *Scanner) scanExtern(file *fs.File, tokens token.List, i int) (int, erro
}
if tokens[i].Kind == token.BlockEnd {
closed = true
break
return i, nil
}
i++
}
if !closed {
return i, errors.New(errors.MissingBlockEnd, file, tokens[i].Position)
}
return i, nil
return i, errors.New(errors.MissingBlockEnd, file, tokens[i].Position)
}

View File

@ -39,6 +39,8 @@ func (s *Scanner) scanFile(path string, pkg string) error {
i, err = s.scanFunction(file, tokens, i)
case token.Extern:
i, err = s.scanExtern(file, tokens, i)
case token.Const:
i, err = s.scanConst(file, tokens, i)
case token.EOF:
return nil
case token.Invalid: