Skip to content

Commit 8c11f33

Browse files
committed
refactor: replace pointer types with value types for expressions in AST and semantic checks
1 parent 8702db1 commit 8c11f33

File tree

20 files changed

+133
-94
lines changed

20 files changed

+133
-94
lines changed

app/cmd/test.fer

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@ let z: str; // Declaration only (no initialization)
77

88
const PI := 3.1416;
99
fn add() {
10-
later();
10+
//later();
1111
let rad := PI * 2 ** 2;
1212
}
1313

14-
fn later() -> i32 {
15-
return;
14+
// fn later() -> i32 {
15+
// return 0;
16+
// }
17+
18+
fn search(keyword: str, callback: fn()) {
19+
callback();
1620
}
1721

22+
const later := fn() -> i32 {
23+
return 0;
24+
};
25+
1826
// comment
19-
let x := a + ;
27+
//let x := a + ;

compiler/internal/frontend/ast/asttypes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ type Parameter struct {
125125

126126
type StructField struct {
127127
FieldIdentifier *IdentifierExpr
128-
FieldType DataType // nil for literal
129-
FieldValue *Expression // nil for type
128+
FieldType DataType // nil for literal
129+
FieldValue Expression // nil for type
130130
source.Location
131131
}
132132

compiler/internal/frontend/ast/block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (f *FunctionDecl) Loc() *source.Location { return &f.Location }
2525

2626
// IfStmt represents an if statement with optional else and else-if branches
2727
type IfStmt struct {
28-
Condition *Expression
28+
Condition Expression
2929
Body *Block
3030
Alternative Node
3131
source.Location

compiler/internal/frontend/ast/expr.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77

88
// Basic expression nodes
99
type BinaryExpr struct {
10-
Left *Expression
10+
Left Expression
1111
Operator lexer.Token
12-
Right *Expression
12+
Right Expression
1313
source.Location
1414
}
1515

@@ -19,7 +19,7 @@ func (b *BinaryExpr) Loc() *source.Location { return &b.Location }
1919

2020
type UnaryExpr struct {
2121
Operator lexer.Token
22-
Operand *Expression
22+
Operand Expression
2323
source.Location
2424
}
2525

@@ -29,7 +29,7 @@ func (u *UnaryExpr) Loc() *source.Location { return &u.Location }
2929

3030
type PrefixExpr struct {
3131
Operator lexer.Token // The operator token (++, --)
32-
Operand *Expression
32+
Operand Expression
3333
source.Location
3434
}
3535

@@ -38,7 +38,7 @@ func (p *PrefixExpr) Expr() {} // Expr is a marker interface for
3838
func (p *PrefixExpr) Loc() *source.Location { return &p.Location }
3939

4040
type PostfixExpr struct {
41-
Operand *Expression
41+
Operand Expression
4242
Operator lexer.Token // The operator token (++, --)
4343
source.Location
4444
}
@@ -59,7 +59,7 @@ func (i *IdentifierExpr) Loc() *source.Location { return &i.Location }
5959

6060
// FunctionCallExpr represents a function call expression
6161
type FunctionCallExpr struct {
62-
Caller *Expression // The function being called (can be an identifier or other expression)
62+
Caller Expression // The function being called (can be an identifier or other expression)
6363
Arguments []Expression // The arguments passed to the function
6464
source.Location
6565
}
@@ -70,7 +70,7 @@ func (f *FunctionCallExpr) Loc() *source.Location { return &f.Location }
7070

7171
// FieldAccessExpr represents a field access expression like struct.field
7272
type FieldAccessExpr struct {
73-
Object *Expression // The struct being accessed
73+
Object Expression // The struct being accessed
7474
Field *IdentifierExpr // The field being accessed
7575
source.Location
7676
}
@@ -82,8 +82,8 @@ func (f *FieldAccessExpr) Loc() *source.Location { return &f.Location }
8282

8383
// CastExpr represents a type cast expression like value as TargetType
8484
type CastExpr struct {
85-
Value *Expression // The value being cast
86-
TargetType DataType // The target type to cast to
85+
Value Expression // The value being cast
86+
TargetType DataType // The target type to cast to
8787
source.Location
8888
}
8989

@@ -92,7 +92,7 @@ func (c *CastExpr) Expr() {} // Expr is a marker interface for a
9292
func (c *CastExpr) Loc() *source.Location { return &c.Location }
9393

9494
type SpreadExpr struct {
95-
Expression *Expression
95+
Expression Expression
9696
source.Location
9797
}
9898

compiler/internal/frontend/ast/literals.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ func (b *ByteLiteral) Expr() {} // Expr is a marker interface fo
5151
func (b *ByteLiteral) Loc() *source.Location { return &b.Location }
5252

5353
type IndexableExpr struct {
54-
Indexable *Expression // The expression being indexed (array, map, etc.)
55-
Index *Expression // The index expression
54+
Indexable Expression // The expression being indexed (array, map, etc.)
55+
Index Expression // The index expression
5656
source.Location
5757
}
5858

compiler/internal/frontend/ast/stmt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (t *TypeDeclStmt) Loc() *source.Location { return &t.Location }
7575

7676
// ReturnStmt represents a return statement
7777
type ReturnStmt struct {
78-
Value *Expression
78+
Value Expression
7979
source.Location
8080
}
8181

compiler/internal/frontend/parser/expr.go

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func parseReturnStmt(p *Parser) ast.Statement {
111111
}
112112

113113
return &ast.ReturnStmt{
114-
Value: &value,
114+
Value: value,
115115
Location: *source.NewLocation(&start, &end),
116116
}
117117
}
@@ -143,11 +143,10 @@ func parseLogicalOr(p *Parser) ast.Expression {
143143
for p.match(lexer.OR_TOKEN) {
144144
operator := p.advance()
145145
right := parseLogicalAnd(p)
146-
left := expr // Create a copy to avoid circular reference
147146
expr = &ast.BinaryExpr{
148-
Left: &left,
147+
Left: expr,
149148
Operator: operator,
150-
Right: &right,
149+
Right: right,
151150
Location: *source.NewLocation(expr.Loc().Start, right.Loc().End),
152151
}
153152
}
@@ -162,11 +161,10 @@ func parseLogicalAnd(p *Parser) ast.Expression {
162161
for p.match(lexer.AND_TOKEN) {
163162
operator := p.advance()
164163
right := parseEquality(p)
165-
left := expr // Create a copy to avoid circular reference
166164
expr = &ast.BinaryExpr{
167-
Left: &left,
165+
Left: expr,
168166
Operator: operator,
169-
Right: &right,
167+
Right: right,
170168
Location: *source.NewLocation(expr.Loc().Start, right.Loc().End),
171169
}
172170
}
@@ -181,11 +179,10 @@ func parseEquality(p *Parser) ast.Expression {
181179
for p.match(lexer.DOUBLE_EQUAL_TOKEN, lexer.NOT_EQUAL_TOKEN) {
182180
operator := p.advance()
183181
right := parseComparison(p)
184-
left := expr // Create a copy to avoid circular reference
185182
expr = &ast.BinaryExpr{
186-
Left: &left,
183+
Left: expr,
187184
Operator: operator,
188-
Right: &right,
185+
Right: right,
189186
Location: *source.NewLocation(expr.Loc().Start, right.Loc().End),
190187
}
191188
}
@@ -200,11 +197,10 @@ func parseComparison(p *Parser) ast.Expression {
200197
for p.match(lexer.LESS_TOKEN, lexer.GREATER_TOKEN, lexer.LESS_EQUAL_TOKEN, lexer.GREATER_EQUAL_TOKEN) {
201198
operator := p.advance()
202199
right := parseAdditive(p)
203-
left := expr // Create a copy to avoid circular reference
204200
expr = &ast.BinaryExpr{
205-
Left: &left,
201+
Left: expr,
206202
Operator: operator,
207-
Right: &right,
203+
Right: right,
208204
Location: *source.NewLocation(expr.Loc().Start, right.Loc().End),
209205
}
210206
}
@@ -219,11 +215,10 @@ func parseAdditive(p *Parser) ast.Expression {
219215
for p.match(lexer.PLUS_TOKEN, lexer.MINUS_TOKEN) {
220216
operator := p.advance()
221217
right := parseMultiplicative(p)
222-
left := expr // Create a copy to avoid circular reference
223218
expr = &ast.BinaryExpr{
224-
Left: &left,
219+
Left: expr,
225220
Operator: operator,
226-
Right: &right,
221+
Right: right,
227222
Location: *source.NewLocation(expr.Loc().Start, right.Loc().End),
228223
}
229224
}
@@ -238,11 +233,10 @@ func parseMultiplicative(p *Parser) ast.Expression {
238233
for p.match(lexer.MUL_TOKEN, lexer.DIV_TOKEN, lexer.MOD_TOKEN, lexer.EXP_TOKEN) {
239234
operator := p.advance()
240235
right := parseUnary(p)
241-
left := expr // Create a copy to avoid circular reference
242236
expr = &ast.BinaryExpr{
243-
Left: &left,
237+
Left: expr,
244238
Operator: operator,
245-
Right: &right,
239+
Right: right,
246240
Location: *source.NewLocation(expr.Loc().Start, right.Loc().End),
247241
}
248242
}
@@ -274,7 +268,7 @@ func handleNotNegative(p *Parser) ast.Expression {
274268
right := parseUnary(p)
275269
return &ast.UnaryExpr{
276270
Operator: operator,
277-
Operand: &right,
271+
Operand: right,
278272
Location: *source.NewLocation(&operator.Start, right.Loc().End),
279273
}
280274
}
@@ -308,7 +302,7 @@ func handlePlusMinus(p *Parser) ast.Expression {
308302

309303
return &ast.PrefixExpr{
310304
Operator: operator,
311-
Operand: &operand,
305+
Operand: operand,
312306
Location: *source.NewLocation(&operator.Start, operand.Loc().End),
313307
}
314308
}
@@ -323,7 +317,7 @@ func handleSpread(p *Parser) ast.Expression {
323317
}
324318

325319
return &ast.SpreadExpr{
326-
Expression: &right,
320+
Expression: right,
327321
Location: *source.NewLocation(&operator.Start, right.Loc().End),
328322
}
329323
}
@@ -341,7 +335,7 @@ func parseCast(p *Parser) ast.Expression {
341335
}
342336

343337
return &ast.CastExpr{
344-
Value: &expr,
338+
Value: expr,
345339
TargetType: targetType,
346340
Location: *source.NewLocation(expr.Loc().Start, targetType.Loc().End),
347341
}
@@ -364,8 +358,8 @@ func parseIndexing(p *Parser, expr ast.Expression) (ast.Expression, bool) {
364358

365359
end := p.consume(lexer.CLOSE_BRACKET, "expected ']' after index expression")
366360
return &ast.IndexableExpr{
367-
Indexable: &expr,
368-
Index: &index,
361+
Indexable: expr,
362+
Index: index,
369363
Location: *source.NewLocation(start, &end.End),
370364
}, true
371365
}
@@ -382,7 +376,7 @@ func parseIncDec(p *Parser, expr ast.Expression) (ast.Expression, bool) {
382376
return nil, false
383377
}
384378
return &ast.PostfixExpr{
385-
Operand: &expr,
379+
Operand: expr,
386380
Operator: operator,
387381
Location: *source.NewLocation(expr.Loc().Start, &operator.End),
388382
}, true
@@ -477,7 +471,7 @@ func parseFunctionCall(p *Parser, caller ast.Expression) (ast.Expression, bool)
477471
end := p.consume(lexer.CLOSE_PAREN, "expected ')' after function arguments")
478472

479473
return &ast.FunctionCallExpr{
480-
Caller: &caller,
474+
Caller: caller,
481475
Arguments: arguments,
482476
Location: *source.NewLocation(start, &end.End),
483477
}, true

compiler/internal/frontend/parser/if.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func parseIfStatement(p *Parser) ast.BlockConstruct {
3434
}
3535

3636
ifStmt := &ast.IfStmt{
37-
Condition: &condition,
37+
Condition: condition,
3838
Body: body,
3939
Location: *source.NewLocation(&start.Start, body.Loc().End),
4040
}

compiler/internal/frontend/parser/structs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func parseStructFields(p *Parser) ([]ast.StructField, bool) {
5252
Name: fieldName.Value,
5353
Location: *source.NewLocation(&fieldName.Start, &fieldName.End),
5454
},
55-
FieldValue: &value,
55+
FieldValue: value,
5656
Location: *source.NewLocation(&fieldName.Start, value.Loc().End),
5757
})
5858

@@ -123,7 +123,7 @@ func parseFieldAccess(p *Parser, object ast.Expression) (ast.Expression, bool) {
123123
}
124124

125125
return &ast.FieldAccessExpr{
126-
Object: &object,
126+
Object: object,
127127
Field: field,
128128
Location: *source.NewLocation(object.Loc().Start, &fieldToken.End),
129129
}, true

compiler/internal/semantic/collector/collector.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ func CollectSymbols(c *analyzer.AnalyzerNode) {
4545
}
4646

4747
func collectSymbols(c *analyzer.AnalyzerNode, node ast.Node, cm *modules.Module) {
48+
49+
fmt.Printf("Collecting symbols from node: %T\n", node)
50+
4851
if node == nil {
4952
return
5053
}
@@ -72,26 +75,27 @@ func collectSymbols(c *analyzer.AnalyzerNode, node ast.Node, cm *modules.Module)
7275
collectExprStmt(c, n, cm)
7376
case *ast.SpreadExpr:
7477
fmt.Printf("collecting symbols from spread expression: %v\n", n)
75-
collectSymbols(c, *n.Expression, cm)
78+
collectSymbols(c, n.Expression, cm)
7679
case *ast.ReturnStmt:
77-
collectSymbols(c, *n.Value, cm)
80+
fmt.Printf("Return node: %v\n", (*n).Value)
81+
collectSymbols(c, n.Value, cm)
7882
case *ast.BinaryExpr:
7983
// Recursively collect from both operands
8084
if n.Left != nil {
81-
collectSymbols(c, *n.Left, cm)
85+
collectSymbols(c, n.Left, cm)
8286
}
8387
if n.Right != nil {
84-
collectSymbols(c, *n.Right, cm)
88+
collectSymbols(c, n.Right, cm)
8589
}
8690
case *ast.PrefixExpr:
8791
// Recursively collect from the operand
8892
if n.Operand != nil {
89-
collectSymbols(c, *n.Operand, cm)
93+
collectSymbols(c, n.Operand, cm)
9094
}
9195
case *ast.PostfixExpr:
9296
// Recursively collect from the operand
9397
if n.Operand != nil {
94-
collectSymbols(c, *n.Operand, cm)
98+
collectSymbols(c, n.Operand, cm)
9599
}
96100
// For other expressions and nodes, we don't need to collect symbols
97101
// (literals, identifiers, etc. don't contain nested function literals)
@@ -101,7 +105,7 @@ func collectSymbols(c *analyzer.AnalyzerNode, node ast.Node, cm *modules.Module)
101105
func collectCall(c *analyzer.AnalyzerNode, callExpr *ast.FunctionCallExpr, cm *modules.Module) {
102106
// Recursively collect from the caller (might be a function literal)
103107
if callExpr.Caller != nil {
104-
collectSymbols(c, *callExpr.Caller, cm)
108+
collectSymbols(c, callExpr.Caller, cm)
105109
}
106110
// Recursively collect from arguments (might contain function literals)
107111
for _, arg := range callExpr.Arguments {

0 commit comments

Comments
 (0)