Skip to content

Commit

Permalink
testing other Module definition
Browse files Browse the repository at this point in the history
  • Loading branch information
pherrymason committed Sep 13, 2024
1 parent a339dae commit 3acbda6
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
1 change: 1 addition & 0 deletions server/internal/lsp/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Module struct {
Functions []Declaration
Macros []Declaration
Declarations []Declaration
Variables []VariableDecl
Imports []Import
}

Expand Down
4 changes: 2 additions & 2 deletions server/internal/lsp/ast/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func ConvertToAST(cstNode *sitter.Node, sourceCode string, fileName string) File
lastMod.Imports = append(lastMod.Imports, convert_imports(node, source).(Import))

case "global_declaration":
lastMod.Declarations = append(lastMod.Declarations, convert_global_declaration(node, source))
lastMod.Variables = append(lastMod.Variables, convert_global_declaration(node, source))

case "enum_declaration":
lastMod.Declarations = append(lastMod.Declarations, convert_enum_declaration(node, source))
Expand Down Expand Up @@ -139,7 +139,7 @@ func convert_imports(node *sitter.Node, source []byte) Expression {
return imports
}

func convert_global_declaration(node *sitter.Node, source []byte) Expression {
func convert_global_declaration(node *sitter.Node, source []byte) VariableDecl {
variable := VariableDecl{
Names: []Identifier{},
ASTBaseNode: NewBaseNodeBuilder().
Expand Down
4 changes: 2 additions & 2 deletions server/internal/lsp/ast/convert_declarations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestConvertToAST_global_variables(t *testing.T) {
},
Initializer: IntegerLiteral{Value: "3"},
}
assert.Equal(t, expectedHello, ast.Modules[0].Declarations[0])
assert.Equal(t, expectedHello, ast.Modules[0].Variables[0])

expectedAnimals := VariableDecl{
ASTBaseNode: aWithPos(2, 1, 2, 24),
Expand All @@ -186,7 +186,7 @@ func TestConvertToAST_global_variables(t *testing.T) {
ASTBaseNode: aWithPos(2, 1, 2, 4),
},
}
assert.Equal(t, expectedAnimals, ast.Modules[0].Declarations[1])
assert.Equal(t, expectedAnimals, ast.Modules[0].Variables[1])
}

func TestConvertToAST_enum_decl(t *testing.T) {
Expand Down
27 changes: 10 additions & 17 deletions server/internal/lsp/ast/convert_expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ func TestConvertToAST_declaration_with_assignment(t *testing.T) {

ast := ConvertToAST(GetCST(source), source, "file.c3")

varDecl := ast.Modules[0].Declarations[0].(VariableDecl)
assert.Equal(t, tt.expected, varDecl.Initializer)
assert.Equal(t, tt.expected, ast.Modules[0].Variables[0].Initializer)
})
}
}
Expand Down Expand Up @@ -290,8 +289,7 @@ func TestConvertToAST_declaration_with_initializer_list_assingment(t *testing.T)

ast := ConvertToAST(GetCST(source), source, "file.c3")

varDecl := ast.Modules[0].Declarations[0].(VariableDecl)
assert.Equal(t, tt.expected, varDecl.Initializer)
assert.Equal(t, tt.expected, ast.Modules[0].Variables[0].Initializer)
})
}
}
Expand Down Expand Up @@ -409,8 +407,7 @@ func TestConvertToAST_compile_time_call(t *testing.T) {
source := `module foo;
int x = ` + input + `;`
ast := ConvertToAST(GetCST(source), source, "file.c3")
varDecl := ast.Modules[0].Declarations[0].(VariableDecl)
initializer := varDecl.Initializer.(FunctionCall)
initializer := ast.Modules[0].Variables[0].Initializer.(FunctionCall)

assert.Equal(t, method, initializer.Identifier.(Identifier).Name)

Expand Down Expand Up @@ -480,8 +477,7 @@ func TestConvertToAST_compile_time_argument_call(t *testing.T) {
int x = ` + method + `(id);`

ast := ConvertToAST(GetCST(source), source, "file.c3")
varDecl := ast.Modules[0].Declarations[0].(VariableDecl)
initializer := varDecl.Initializer.(FunctionCall)
initializer := ast.Modules[0].Variables[0].Initializer.(FunctionCall)

assert.Equal(t, FunctionCall{
ASTBaseNode: NewBaseNodeBuilder().WithStartEnd(1, 12, 1, 16+length).Build(),
Expand Down Expand Up @@ -545,7 +541,7 @@ func TestConvertToAST_compile_time_analyse(t *testing.T) {

ast := ConvertToAST(GetCST(source), source, "file.c3")

assert.Equal(t, tt.expected, ast.Modules[0].Declarations[0].(VariableDecl).Initializer.(FunctionCall))
assert.Equal(t, tt.expected, ast.Modules[0].Variables[0].Initializer.(FunctionCall))
})
}
}
Expand Down Expand Up @@ -621,7 +617,7 @@ func TestConvertToAST_lambda_declaration(t *testing.T) {

ast := ConvertToAST(GetCST(source), source, "file.c3")

assert.Equal(t, tt.expected, ast.Modules[0].Declarations[0].(VariableDecl).Initializer.(LambdaDeclaration))
assert.Equal(t, tt.expected, ast.Modules[0].Variables[0].Initializer.(LambdaDeclaration))
})
}
}
Expand Down Expand Up @@ -738,7 +734,7 @@ func TestConvertToAST_lambda_expr(t *testing.T) {
},
}

lambda := ast.Modules[0].Declarations[0].(VariableDecl).Initializer.(LambdaDeclaration)
lambda := ast.Modules[0].Variables[0].Initializer.(LambdaDeclaration)
assert.Equal(t, expected, lambda)

}
Expand Down Expand Up @@ -781,8 +777,7 @@ func TestConvertToAST_elvis_or_else_expr(t *testing.T) {
func(t *testing.T) {
ast := ConvertToAST(GetCST(tt.source), tt.source, "file.c3")

varDecl := ast.Modules[0].Declarations[0].(VariableDecl)
assert.Equal(t, tt.expected, varDecl.Initializer.(TernaryExpression))
assert.Equal(t, tt.expected, ast.Modules[0].Variables[0].Initializer.(TernaryExpression))
})
}
}
Expand Down Expand Up @@ -833,8 +828,7 @@ func TestConvertToAST_optional_expr(t *testing.T) {
func(t *testing.T) {
ast := ConvertToAST(GetCST(tt.source), tt.source, "file.c3")

varDecl := ast.Modules[0].Declarations[0].(VariableDecl)
assert.Equal(t, tt.expected, varDecl.Initializer.(OptionalExpression))
assert.Equal(t, tt.expected, ast.Modules[0].Variables[0].Initializer.(OptionalExpression))
})
}
}
Expand Down Expand Up @@ -931,8 +925,7 @@ func TestConvertToAST_rethrow_expr(t *testing.T) {
func(t *testing.T) {
ast := ConvertToAST(GetCST(tt.source), tt.source, "file.c3")

varDecl := ast.Modules[0].Declarations[0].(VariableDecl)
assert.Equal(t, tt.expected, varDecl.Initializer.(RethrowExpression))
assert.Equal(t, tt.expected, ast.Modules[0].Variables[0].Initializer.(RethrowExpression))
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion server/internal/lsp/ast/json_visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func (v *JSONVisitor) VisitModule(node *Module) {
declarationsV := JSONVisitor{}
declarations := []interface{}{}
for _, decl := range node.Declarations {
Visit(decl, &declarationsV)
vdecl := decl.(*VariableDecl)
Visit(vdecl, &declarationsV)
if declarationsV.Result != nil {
declarations = append(declarations, declarationsV.Result)
}
Expand Down

0 comments on commit 3acbda6

Please sign in to comment.