Skip to content

Commit 2fefe53

Browse files
committed
Refactor error messages and logging for consistency
- Updated various error messages and debug logs to use lowercase for improved readability and consistency across the codebase. - Removed the constants file for report messages as it was deemed unnecessary. - Adjusted import paths in test utilities to reflect changes in configuration. - Ensured that all error messages related to type checking, resolution, and imports follow a uniform style.
1 parent 65f4109 commit 2fefe53

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+299
-459
lines changed

compiler/cmd/cli/executor.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cli
33
import (
44
"compiler/cmd"
55
"compiler/colors"
6-
"compiler/constants"
76
"compiler/internal/modules"
87
"fmt"
98
"os"
@@ -270,7 +269,7 @@ func getRoot() string {
270269
}
271270

272271
// Enforce: must be run from project root (directory containing fer.ret)
273-
ferretPath := filepath.Join(cwd, constants.CONFIG_FILE)
272+
ferretPath := filepath.Join(cwd, config.CONFIG_FILE)
274273
if _, err := os.Stat(ferretPath); err != nil {
275274
colors.RED.Printf(CONFIG_LOAD_ERROR, err)
276275
os.Exit(1)

compiler/cmd/compiler.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"compiler/cmd/flags"
1212
"compiler/colors"
1313
"compiler/config"
14-
"compiler/constants"
1514
"compiler/internal/ctx"
1615
"compiler/internal/modules"
1716

@@ -146,7 +145,7 @@ func getRemoteDependencyPath(projectConfig *config.ProjectConfig, depKey string)
146145
host, owner, repo := repoParts[0], repoParts[1], repoParts[2]
147146

148147
// Construct cache path
149-
cachePath := filepath.Join(projectConfig.ProjectRoot, constants.CACHE_DIR)
148+
cachePath := filepath.Join(projectConfig.ProjectRoot, config.CACHE_DIR)
150149
return filepath.Join(cachePath, host, owner, modules.BuildPackageSpec(repo, version))
151150
}
152151

compiler/config/project.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,24 @@ import (
99

1010
"compiler/cmd/flags"
1111
"compiler/colors"
12-
"compiler/constants"
1312
"compiler/toml"
1413
)
1514

1615
const SHAREKEY = "allow-sharing"
1716
const REMOTEKEY = "allow-remote-import"
1817
const EXTERNALKEY = "allow-neighbor-import"
1918

19+
const (
20+
EXT = ".fer"
21+
CACHE_DIR = ".ferret"
22+
CONFIG_FILE = "fer.ret"
23+
LOCKFILE = "ferret.lock"
24+
LOCKFILE_VERSION = "1.0.0"
25+
GITHUB_HOST = "github.com"
26+
27+
STD_LIBS_PATH = "modules"
28+
)
29+
2030
// ProjectConfig represents the structure
2131
type ProjectConfig struct {
2232
Name string `toml:"name"`
@@ -84,7 +94,7 @@ func (conf *ProjectConfig) Save() error {
8494
}
8595

8696
// Save the configuration to the fer.ret file
87-
if err := toml.WriteTOMLFile(filepath.Join(conf.ProjectRoot, constants.CONFIG_FILE), tomData, nil); err != nil {
97+
if err := toml.WriteTOMLFile(filepath.Join(conf.ProjectRoot, CONFIG_FILE), tomData, nil); err != nil {
8898
colors.RED.Println(err)
8999
return fmt.Errorf("failed to save config file: %w", err)
90100
}
@@ -131,7 +141,7 @@ func CreateDefaultProjectConfig(projectName string) error {
131141
os.Exit(1)
132142
}
133143

134-
configPath := filepath.Join(cwd, constants.CONFIG_FILE)
144+
configPath := filepath.Join(cwd, CONFIG_FILE)
135145

136146
// if already exist, ask for overwrite
137147
if _, err := os.Stat(configPath); err == nil {
@@ -161,7 +171,7 @@ func CreateDefaultProjectConfig(projectName string) error {
161171
os.Exit(1)
162172
}
163173

164-
colors.GREEN.Printf("📁 Created %s successfully!\n", constants.CONFIG_FILE)
174+
colors.GREEN.Printf("📁 Created %s successfully!\n", CONFIG_FILE)
165175

166176
// remove old lockfiles and cache
167177
if err := os.RemoveAll(filepath.Join(cwd, ".ferret")); err != nil {
@@ -315,7 +325,7 @@ func ValidateProjectConfig(config *ProjectConfig) error {
315325

316326
// IsProjectRoot checks if the given directory contains a fer.ret file
317327
func IsProjectRoot(dir string) bool {
318-
configPath := filepath.Join(dir, constants.CONFIG_FILE)
328+
configPath := filepath.Join(dir, CONFIG_FILE)
319329
_, err := os.Stat(filepath.FromSlash(configPath))
320330
return err == nil
321331
}
@@ -331,7 +341,7 @@ func GetProjectRoot(filePath string) (string, error) {
331341

332342
// Walk up the directory tree until we find a fer.ret file
333343
for {
334-
configPath := filepath.Join(dir, constants.CONFIG_FILE)
344+
configPath := filepath.Join(dir, CONFIG_FILE)
335345
// Check if fer.ret exists in this directory
336346
if _, err := os.Stat(configPath); err == nil {
337347
// Found the project root
@@ -349,7 +359,7 @@ func GetProjectRoot(filePath string) (string, error) {
349359
dir = parent
350360
}
351361

352-
return "", fmt.Errorf("❌ %s not found (searched from: %s up to filesystem root)", constants.CONFIG_FILE, dir)
362+
return "", fmt.Errorf("❌ %s not found (searched from: %s up to filesystem root)", CONFIG_FILE, dir)
353363
}
354364

355365
func LoadProjectConfig(projectRoot string) (*ProjectConfig, error) {
@@ -360,11 +370,11 @@ func LoadProjectConfig(projectRoot string) (*ProjectConfig, error) {
360370
os.Exit(1)
361371
}
362372

363-
configPath := filepath.Join(projectRoot, constants.CONFIG_FILE)
373+
configPath := filepath.Join(projectRoot, CONFIG_FILE)
364374

365375
// if not exists, ask user to create one
366376
if _, err := os.Stat(configPath); os.IsNotExist(err) {
367-
colors.RED.Printf("❌ Configuration file %s not found in %s\nCreate project by running 'ferret init' command\n", constants.CONFIG_FILE, projectRoot)
377+
colors.RED.Printf("❌ Configuration file %s not found in %s\nCreate project by running 'ferret init' command\n", CONFIG_FILE, projectRoot)
368378
}
369379

370380
// Use our custom TOML parser

compiler/config/project_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package config
22

33
import (
4-
"compiler/constants"
54
"compiler/toml"
65
"os"
76
"path/filepath"
@@ -111,7 +110,7 @@ func TestIsProjectRoot(t *testing.T) {
111110
}
112111
defer os.RemoveAll(tempDir)
113112

114-
configPath := filepath.Join(tempDir, constants.CONFIG_FILE)
113+
configPath := filepath.Join(tempDir, CONFIG_FILE)
115114
if err := os.WriteFile(configPath, []byte("test"), 0644); err != nil {
116115
t.Fatalf("Failed to create config file: %v", err)
117116
}
@@ -208,7 +207,7 @@ func setupTestProjectStructure(t *testing.T) string {
208207
}
209208

210209
// Create config file in root
211-
configPath := filepath.Join(tempDir, constants.CONFIG_FILE)
210+
configPath := filepath.Join(tempDir, CONFIG_FILE)
212211
if err := os.WriteFile(configPath, []byte("test"), 0644); err != nil {
213212
t.Fatalf("Failed to create config file: %v", err)
214213
}

compiler/constants/constants_names.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

compiler/internal/ctx/context.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"compiler/colors"
1111
"compiler/config"
12-
"compiler/constants"
1312
"compiler/internal/frontend/ast"
1413
"compiler/internal/modules"
1514
"compiler/internal/symbol"
@@ -155,7 +154,7 @@ func (c *CompilerContext) validateResolvedPath(resolvedPath, importPath string)
155154
}
156155

157156
// Add extension and check if file exists
158-
finalPath := resolvedPath + constants.EXT
157+
finalPath := resolvedPath + config.EXT
159158
if !fs.IsValidFile(finalPath) {
160159
fmt.Printf("Final path: %s\n", finalPath)
161160
return "", fmt.Errorf("module %q does not exist", importPath)
@@ -421,7 +420,7 @@ func NewCompilerContext(projectConfig *config.ProjectConfig) *CompilerContext {
421420
entryPoint = filepath.ToSlash(entryPoint) // Ensure forward slashes for consistency
422421

423422
// Set up remote module cache path
424-
remoteCachePath := filepath.Join(projectConfig.ProjectRoot, constants.CACHE_DIR)
423+
remoteCachePath := filepath.Join(projectConfig.ProjectRoot, config.CACHE_DIR)
425424
remoteCachePath = filepath.ToSlash(remoteCachePath)
426425
os.MkdirAll(remoteCachePath, 0755)
427426

compiler/internal/ctx/context_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ const (
2121
mainEntry = "main.fer"
2222
mainOutput = "main.exe"
2323
ferretCacheDir = ".ferret"
24-
expectedPanicMsg = "Expected panic but didn't get one"
25-
expectedNonNilCtx = "Expected non-nil context"
24+
expectedPanicMsg = "expected panic but didn't get one"
25+
expectedNonNilCtx = "expected non-nil context"
2626
)
2727

2828
// Test helper to create a temporary project config
@@ -60,7 +60,7 @@ func createTempDir(t *testing.T) string {
6060
t.Helper()
6161
dir, err := os.MkdirTemp("", "ferret-test-")
6262
if err != nil {
63-
t.Fatalf("Failed to create temp dir: %v", err)
63+
t.Fatalf("failed to create temp dir: %v", err)
6464
}
6565
t.Cleanup(func() {
6666
os.RemoveAll(dir)

compiler/internal/frontend/parser/expr.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func parseExpressionList(p *Parser, first ast.Expression) ast.ExpressionList {
1515
next := parseExpression(p)
1616
if next == nil {
1717
token := p.peek()
18-
p.ctx.Reports.AddSyntaxError(p.fullPath, source.NewLocation(&token.Start, &token.End), "Expected expression after comma", report.PARSING_PHASE)
18+
p.ctx.Reports.AddSyntaxError(p.fullPath, source.NewLocation(&token.Start, &token.End), "expected expression after comma", report.PARSING_PHASE)
1919
break
2020
}
2121
exprs = append(exprs, next)
@@ -40,7 +40,7 @@ func parseExpressionStatement(p *Parser, first ast.Expression) ast.Statement {
4040

4141
// parseBlock parses a block of statements
4242
func parseBlock(p *Parser) *ast.Block {
43-
start := p.consume(lexer.OPEN_CURLY, report.EXPECTED_OPEN_BRACE).Start
43+
start := p.consume(lexer.OPEN_CURLY, "expected '{' before block").Start
4444

4545
nodes := make([]ast.Node, 0)
4646

@@ -51,7 +51,7 @@ func parseBlock(p *Parser) *ast.Block {
5151
}
5252
}
5353

54-
end := p.consume(lexer.CLOSE_CURLY, report.EXPECTED_CLOSE_BRACE).End
54+
end := p.consume(lexer.CLOSE_CURLY, "expected '}' after block").End
5555

5656
return &ast.Block{
5757
Nodes: nodes,
@@ -60,7 +60,9 @@ func parseBlock(p *Parser) *ast.Block {
6060
}
6161

6262
func parseReturnStmt(p *Parser) ast.Statement {
63-
start := p.consume(lexer.RETURN_TOKEN, report.EXPECTED_RETURN_KEYWORD).Start
63+
64+
start := p.advance().Start // consume 'return' and get start position
65+
6466
end := start
6567

6668
// Return immediately if there's a semicolon (no return value)
@@ -78,7 +80,7 @@ func parseReturnStmt(p *Parser) ast.Statement {
7880
p.ctx.Reports.AddSyntaxError(
7981
p.fullPath,
8082
source.NewLocation(&token.Start, &token.End),
81-
report.INVALID_EXPRESSION,
83+
"invalid expression after return keyword",
8284
report.PARSING_PHASE,
8385
).AddHint("add an expression after the return keyword")
8486

@@ -96,7 +98,7 @@ func parseReturnStmt(p *Parser) ast.Statement {
9698
p.ctx.Reports.AddSyntaxError(
9799
p.fullPath,
98100
source.NewLocation(&comma.Start, &comma.End),
99-
"Multiple return values are not supported",
101+
"multiple return values are not supported",
100102
report.PARSING_PHASE,
101103
).AddHint("functions can only return a single value")
102104

@@ -281,26 +283,26 @@ func handlePlusMinus(p *Parser) ast.Expression {
281283
operator := p.advance()
282284
// Check for consecutive operators
283285
if p.match(lexer.PLUS_PLUS_TOKEN, lexer.MINUS_MINUS_TOKEN) {
284-
errMsg := report.INVALID_CONSECUTIVE_INCREMENT
286+
errMsg := "invalid consecutive increment operators"
285287
if operator.Kind == lexer.MINUS_MINUS_TOKEN {
286-
errMsg = report.INVALID_CONSECUTIVE_DECREMENT
288+
errMsg = "invalid consecutive decrement operators"
287289
}
288290
p.ctx.Reports.AddSyntaxError(p.fullPath, source.NewLocation(&operator.Start, &operator.End), errMsg, report.PARSING_PHASE)
289291
return nil
290292
}
291293
operand := parseUnary(p)
292294
if operand == nil {
293-
errMsg := report.INVALID_INCREMENT_OPERAND
295+
errMsg := "invalid operand for increment operator"
294296
if operator.Kind == lexer.MINUS_MINUS_TOKEN {
295-
errMsg = report.INVALID_DECREMENT_OPERAND
297+
errMsg = "invalid operand for decrement operator"
296298
}
297299
p.ctx.Reports.AddSyntaxError(p.fullPath, source.NewLocation(&operator.Start, &operator.End), errMsg, report.PARSING_PHASE)
298300
return nil
299301
}
300302

301303
// Check if operand already has a postfix operator
302304
if _, ok := operand.(*ast.PostfixExpr); ok {
303-
p.ctx.Reports.AddSyntaxError(p.fullPath, source.NewLocation(&operator.Start, &operator.End), "Cannot mix prefix and postfix operators", report.PARSING_PHASE)
305+
p.ctx.Reports.AddSyntaxError(p.fullPath, source.NewLocation(&operator.Start, &operator.End), "cannot mix prefix and postfix operators", report.PARSING_PHASE)
304306
return nil
305307
}
306308

@@ -316,7 +318,7 @@ func handleSpread(p *Parser) ast.Expression {
316318
operator := p.advance()
317319
right := parseUnary(p)
318320
if right == nil {
319-
p.ctx.Reports.AddSyntaxError(p.fullPath, source.NewLocation(&operator.Start, &operator.End), "Expected expression after spread operator", report.PARSING_PHASE)
321+
p.ctx.Reports.AddSyntaxError(p.fullPath, source.NewLocation(&operator.Start, &operator.End), "expected expression after spread operator", report.PARSING_PHASE)
320322
return nil
321323
}
322324

@@ -334,7 +336,7 @@ func parseCast(p *Parser) ast.Expression {
334336
asToken := p.advance()
335337
targetType, ok := parseType(p)
336338
if !ok || targetType == nil {
337-
p.ctx.Reports.AddSyntaxError(p.fullPath, source.NewLocation(&asToken.Start, &asToken.End), "Expected type after 'as' keyword", report.PARSING_PHASE)
339+
p.ctx.Reports.AddSyntaxError(p.fullPath, source.NewLocation(&asToken.Start, &asToken.End), "expected type after 'as' keyword", report.PARSING_PHASE)
338340
return expr
339341
}
340342

@@ -356,11 +358,11 @@ func parseIndexing(p *Parser, expr ast.Expression) (ast.Expression, bool) {
356358
index := parseExpression(p)
357359
if index == nil {
358360
token := p.peek()
359-
p.ctx.Reports.AddSyntaxError(p.fullPath, source.NewLocation(&token.Start, &token.End), report.MISSING_INDEX_EXPRESSION, report.PARSING_PHASE)
361+
p.ctx.Reports.AddSyntaxError(p.fullPath, source.NewLocation(&token.Start, &token.End), "expected valid indexing expression", report.PARSING_PHASE)
360362
return nil, false
361363
}
362364

363-
end := p.consume(lexer.CLOSE_BRACKET, report.EXPECTED_CLOSE_BRACKET)
365+
end := p.consume(lexer.CLOSE_BRACKET, "expected ']' after index expression")
364366
return &ast.IndexableExpr{
365367
Indexable: &expr,
366368
Index: &index,
@@ -372,9 +374,9 @@ func parseIndexing(p *Parser, expr ast.Expression) (ast.Expression, bool) {
372374
func parseIncDec(p *Parser, expr ast.Expression) (ast.Expression, bool) {
373375
operator := p.advance()
374376
if p.match(lexer.PLUS_PLUS_TOKEN, lexer.MINUS_MINUS_TOKEN) {
375-
errMsg := report.INVALID_CONSECUTIVE_INCREMENT
377+
errMsg := "invalid consecutive increment operators"
376378
if operator.Kind == lexer.MINUS_MINUS_TOKEN {
377-
errMsg = report.INVALID_CONSECUTIVE_DECREMENT
379+
errMsg = "invalid consecutive decrement operators"
378380
}
379381
p.ctx.Reports.AddSyntaxError(p.fullPath, source.NewLocation(&operator.Start, &operator.End), errMsg, report.PARSING_PHASE)
380382
return nil, false
@@ -391,7 +393,7 @@ func handlePostfixOperator(p *Parser, expr ast.Expression) (ast.Expression, bool
391393
if p.match(lexer.PLUS_PLUS_TOKEN, lexer.MINUS_MINUS_TOKEN) {
392394
if _, ok := expr.(*ast.PrefixExpr); ok {
393395
current := p.peek()
394-
p.ctx.Reports.AddSyntaxError(p.fullPath, source.NewLocation(&current.Start, &current.End), "Cannot mix prefix and postfix operators", report.PARSING_PHASE)
396+
p.ctx.Reports.AddSyntaxError(p.fullPath, source.NewLocation(&current.Start, &current.End), "cannot mix prefix and postfix operators", report.PARSING_PHASE)
395397
return nil, false
396398
}
397399
return parseIncDec(p, expr)
@@ -441,7 +443,7 @@ func parsePostfix(p *Parser) ast.Expression {
441443
func parseGrouping(p *Parser) ast.Expression {
442444
p.advance() // consume '('
443445
expr := parseExpression(p)
444-
p.consume(lexer.CLOSE_PAREN, "Expected ')' after expression")
446+
p.consume(lexer.CLOSE_PAREN, "expected ')' after expression")
445447
return expr
446448
}
447449

@@ -456,23 +458,23 @@ func parseFunctionCall(p *Parser, caller ast.Expression) (ast.Expression, bool)
456458
arg := parseExpression(p)
457459
if arg == nil {
458460
token := p.peek()
459-
p.ctx.Reports.AddSyntaxError(p.fullPath, source.NewLocation(&token.Start, &token.End), "Expected function argument", report.PARSING_PHASE)
461+
p.ctx.Reports.AddSyntaxError(p.fullPath, source.NewLocation(&token.Start, &token.End), "expected function argument", report.PARSING_PHASE)
460462
return nil, false
461463
}
462464
arguments = append(arguments, arg)
463465

464466
if p.match(lexer.CLOSE_PAREN) {
465467
break
466468
} else {
467-
comma := p.consume(lexer.COMMA_TOKEN, report.EXPECTED_COMMA_OR_CLOSE_PAREN)
469+
comma := p.consume(lexer.COMMA_TOKEN, "expected ',' or ')' after parameter")
468470
if p.match(lexer.CLOSE_PAREN) {
469-
p.ctx.Reports.AddWarning(p.fullPath, source.NewLocation(&comma.Start, &comma.End), report.TRAILING_COMMA_NOT_ALLOWED, report.PARSING_PHASE).AddHint("remove the trailing comma")
471+
p.ctx.Reports.AddWarning(p.fullPath, source.NewLocation(&comma.Start, &comma.End), "unnecessary trailing comma after last argument", report.PARSING_PHASE).AddHint("remove the trailing comma")
470472
break
471473
}
472474
}
473475
}
474476

475-
end := p.consume(lexer.CLOSE_PAREN, report.EXPECTED_CLOSE_PAREN)
477+
end := p.consume(lexer.CLOSE_PAREN, "expected ')' after function arguments")
476478

477479
return &ast.FunctionCallExpr{
478480
Caller: &caller,

0 commit comments

Comments
 (0)