From 31845dbc4838ee32fa7348a3a6565e8e98e5063d Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sun, 23 Jun 2024 14:46:04 +0200 Subject: [PATCH] Reduced number of tokens processed --- src/build/Function.go | 20 +++++++------------- src/build/scan.go | 4 ++-- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/build/Function.go b/src/build/Function.go index 3ec442f..3ba0646 100644 --- a/src/build/Function.go +++ b/src/build/Function.go @@ -62,12 +62,6 @@ func (f *Function) Compile() { case token.GroupEnd: groupLevel-- - - case token.BlockStart: - // Add scope - - case token.BlockEnd: - // Remove scope } } @@ -129,9 +123,8 @@ func (f *Function) CompileVariableDefinition(expr *expression.Expression) error } name := expr.Children[0].Token.Text() - _, exists := f.Variables[name] - if exists { + if f.identifierExists(name) { return errors.New(&errors.VariableAlreadyExists{Name: name}, f.File, expr.Children[0].Token.Position) } @@ -161,11 +154,6 @@ func (f *Function) CompileVariableDefinition(expr *expression.Expression) error return nil } -func (f *Function) identifierExists(name string) bool { - _, exists := f.Variables[name] - return exists -} - // CompileFunctionCall compiles a function call. func (f *Function) CompileFunctionCall(expr *expression.Expression) error { funcName := expr.Children[0].Token.Text() @@ -254,6 +242,12 @@ func (f *Function) String() string { return f.Name } +// identifierExists returns true if the identifier has been defined. +func (f *Function) identifierExists(name string) bool { + _, exists := f.Variables[name] + return exists +} + // isVariableDefinition returns true if the expression is a variable definition. func isVariableDefinition(expr *expression.Expression) bool { return expr.Token.Kind == token.Operator && expr.Token.Text() == ":=" diff --git a/src/build/scan.go b/src/build/scan.go index 710e53c..219c132 100644 --- a/src/build/scan.go +++ b/src/build/scan.go @@ -176,12 +176,12 @@ func scanFile(path string, functions chan<- *Function) error { for i < len(tokens) { if tokens[i].Kind == token.BlockStart { blockLevel++ + i++ if blockLevel == 1 { bodyStart = i } - i++ continue } @@ -225,7 +225,7 @@ func scanFile(path string, functions chan<- *Function) error { Name: tokens[nameStart].Text(), File: file, Head: tokens[paramsStart:bodyStart], - Body: tokens[bodyStart : i+1], + Body: tokens[bodyStart:i], Variables: map[string]*Variable{}, CPU: cpu.CPU{ General: x64.GeneralRegisters,