From b6be9357bfe1d4775a6c72e401d884ff283a7ed1 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 31 Oct 2023 12:17:03 +0100 Subject: [PATCH] Added a function scanner --- src/build/Function.go | 6 +++++- src/build/Scan.go | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/build/Function.go b/src/build/Function.go index d3734e3..8bb5108 100644 --- a/src/build/Function.go +++ b/src/build/Function.go @@ -1,3 +1,7 @@ package build -type Function struct{} +import "git.akyoto.dev/cli/q/src/token" + +type Function struct { + Tokens token.List +} diff --git a/src/build/Scan.go b/src/build/Scan.go index 229cead..d1cba26 100644 --- a/src/build/Scan.go +++ b/src/build/Scan.go @@ -7,7 +7,6 @@ import ( "sync" "git.akyoto.dev/cli/q/src/directory" - "git.akyoto.dev/cli/q/src/log" "git.akyoto.dev/cli/q/src/token" ) @@ -60,8 +59,39 @@ func scanFile(path string, functions chan<- *Function) error { tokens := token.Tokenize(contents) - for _, t := range tokens { - log.Info.Println(t.Kind, t.Position, strings.TrimSpace(t.String())) + var ( + groupLevel = 0 + blockLevel = 0 + functionStart = -1 + ) + + for i, t := range tokens { + switch t.Kind { + case token.Identifier: + if blockLevel == 0 && groupLevel == 0 { + functionStart = i + } + + case token.GroupStart: + groupLevel++ + + case token.GroupEnd: + groupLevel-- + + case token.BlockStart: + blockLevel++ + + case token.BlockEnd: + blockLevel-- + + if blockLevel == 0 { + function := &Function{ + Tokens: tokens[functionStart : i+1], + } + + functions <- function + } + } } return nil