Skip to content

Commit

Permalink
Add more tests and move some tests from typechecker to symbol table g…
Browse files Browse the repository at this point in the history
…enerator (#367)
  • Loading branch information
marcauberer authored Nov 11, 2023
1 parent d6396bb commit e5bb859
Show file tree
Hide file tree
Showing 45 changed files with 112 additions and 40 deletions.
3 changes: 2 additions & 1 deletion media/test-project/test.spice
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![core.linker.flag]
type T dyn;
type T int|double;

f<int> main() {}
1 change: 1 addition & 0 deletions src/ast/ASTNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ class GenericTypeDefNode : public ASTNode {

// Public members
std::string typeName;
SymbolTableEntry *entry = nullptr;
};

// ========================================================= AliasDefNode ========================================================
Expand Down
20 changes: 12 additions & 8 deletions src/symboltablebuilder/SymbolTableBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ std::any SymbolTableBuilder::visitMainFctDef(MainFctDefNode *node) {
visit(node->attrs());

// Check if the function is already defined
if (rootScope->lookup(node->getSignature()))
if (rootScope->lookupStrict(node->getSignature()))
throw SemanticError(node, FUNCTION_DECLARED_TWICE, "Main function is declared twice");

// Insert symbol for main function
Expand Down Expand Up @@ -194,7 +194,7 @@ std::any SymbolTableBuilder::visitStructDef(StructDefNode *node) {
visit(node->attrs());

// Check if this name already exists
if (rootScope->lookup(node->structName))
if (rootScope->lookupStrict(node->structName))
throw SemanticError(node, DUPLICATE_SYMBOL, "Duplicate symbol '" + node->structName + "'");

// Create scope for the struct
Expand Down Expand Up @@ -238,7 +238,7 @@ std::any SymbolTableBuilder::visitStructDef(StructDefNode *node) {

std::any SymbolTableBuilder::visitInterfaceDef(InterfaceDefNode *node) {
// Check if this name already exists
if (rootScope->lookup(node->interfaceName))
if (rootScope->lookupStrict(node->interfaceName))
throw SemanticError(node, DUPLICATE_SYMBOL, "Duplicate symbol '" + node->interfaceName + "'");

// Create scope for the interface
Expand Down Expand Up @@ -272,7 +272,7 @@ std::any SymbolTableBuilder::visitInterfaceDef(InterfaceDefNode *node) {

std::any SymbolTableBuilder::visitEnumDef(EnumDefNode *node) {
// Check if this name already exists
if (rootScope->lookup(node->enumName))
if (rootScope->lookupStrict(node->enumName))
throw SemanticError(node, DUPLICATE_SYMBOL, "Duplicate symbol '" + node->enumName + "'");

// Create scope for the enum
Expand Down Expand Up @@ -305,15 +305,19 @@ std::any SymbolTableBuilder::visitEnumDef(EnumDefNode *node) {

std::any SymbolTableBuilder::visitGenericTypeDef(GenericTypeDefNode *node) {
// Check if this name already exists
if (rootScope->lookup(node->typeName))
if (rootScope->lookupStrict(node->typeName))
throw SemanticError(node, DUPLICATE_SYMBOL, "Duplicate symbol '" + node->typeName + "'");

// Create the generic type to the symbol table
node->entry = rootScope->insert(node->typeName, node);
node->entry->used = true; // Generic types are always used

return nullptr;
}

std::any SymbolTableBuilder::visitAliasDef(AliasDefNode *node) {
// Check if this name already exists
if (rootScope->lookup(node->aliasName))
if (rootScope->lookupStrict(node->aliasName))
throw SemanticError(node, DUPLICATE_SYMBOL, "Duplicate symbol '" + node->aliasName + "'");

// Add the alias to the symbol table
Expand All @@ -328,7 +332,7 @@ std::any SymbolTableBuilder::visitAliasDef(AliasDefNode *node) {

std::any SymbolTableBuilder::visitGlobalVarDef(GlobalVarDefNode *node) {
// Check if this name already exists
if (rootScope->lookup(node->varName))
if (rootScope->lookupStrict(node->varName))
throw SemanticError(node, DUPLICATE_SYMBOL, "Duplicate symbol '" + node->varName + "'");

// Check if global already exists in an imported source file
Expand All @@ -351,7 +355,7 @@ std::any SymbolTableBuilder::visitExtDecl(ExtDeclNode *node) {
visit(node->attrs());

// Check if this name already exists
if (rootScope->lookup(node->extFunctionName))
if (rootScope->lookupStrict(node->extFunctionName))
throw SemanticError(node, DUPLICATE_SYMBOL, "Duplicate symbol '" + node->extFunctionName + "'");

// Add the external declaration to the symbol table
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Error|Semantic] ./test-files/typechecker/aliases/error-duplicate-alias/source.spice:2:1:
[Error|Semantic] ./test-files/symboltablebuilder/aliases/error-duplicate-alias/source.spice:2:1:
Duplicate symbol: Duplicate symbol 'Alias'

2 type Alias alias double;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Error|Semantic] ./test-files/symboltablebuilder/enums/error-duplicate-enum-def/source.spice:7:1:
Duplicate symbol: Duplicate symbol 'Fruit'

7 type Fruit enum {
^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type Fruit enum {
Apple,
Banana,
Orange
}

type Fruit enum {
Kiwi,
Mango,
Pear
}

f<int> main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Error|Semantic] ./test-files/typechecker/enums/error-duplicate-item-name/source.spice:4:5:
[Error|Semantic] ./test-files/symboltablebuilder/enums/error-duplicate-item-name/source.spice:4:5:
Multiple declarations of the same variable: The enum item 'ITEM_NAME' was declared more than once

4 ITEM_NAME
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Error|Semantic] ./test-files/symboltablebuilder/ext-decl/error-duplicate-ext-decl/source.spice:2:1:
Duplicate symbol: Duplicate symbol 'malloc'

2 ext f<heap byte*> malloc(unsigned long);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ext f<heap byte*> malloc(unsigned long);
ext f<heap byte*> malloc(unsigned long);

f<int> main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Error|Semantic] ./test-files/typechecker/functions/error-duplicate-function-def/source.spice:5:1:
[Error|Semantic] ./test-files/symboltablebuilder/functions/error-duplicate-function-def/source.spice:5:1:
Multiple declarations of a function/procedure: The function/procedure 'string exampleFunc()' is declared twice

5 f<string> exampleFunc() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Error|Semantic] ./test-files/typechecker/functions/error-duplicate-main-function-def/source.spice:5:1:
[Error|Semantic] ./test-files/symboltablebuilder/functions/error-duplicate-main-function-def/source.spice:5:1:
Multiple declarations of a function/procedure: Main function is declared twice

5 f<int> main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Error|Semantic] ./test-files/typechecker/methods/error-method-before-struct/source.spice:1:1:
[Error|Semantic] ./test-files/symboltablebuilder/functions/error-method-before-struct/source.spice:1:1:
Referenced undefined struct: Struct 'Vec' could not be found

1 public p Vec.print() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Error|Semantic] ./test-files/symboltablebuilder/functions/error-must-contain-main-function/source.spice:1:1:
Spice programs must contain a main function: No main function found
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Error|Semantic] ./test-files/symboltablebuilder/generics/error-duplicate-gen-type-def/source.spice:2:1:
Duplicate symbol: Duplicate symbol 'T'

2 type T int|double;
^^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type T dyn;
type T int|double;

f<int> main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Error|Semantic] ./test-files/typechecker/variables/error-global-var-decl-twice-external/source.spice:3:1:
[Error|Semantic] ./test-files/symboltablebuilder/globals/error-global-var-decl-twice-external/source.spice:3:1:
Multiple declarations of the same global variable: Duplicate global variable 'GLOBAL_VARIABLE' in other module

3 double GLOBAL_VARIABLE = 14.4;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Error|Semantic] ./test-files/typechecker/variables/error-global-var-decl-twice/source.spice:3:1:
[Error|Semantic] ./test-files/symboltablebuilder/globals/error-global-var-decl-twice/source.spice:3:1:
Duplicate symbol: Duplicate symbol 'GLOBAL_VAR'

3 double GLOBAL_VAR = 4.567;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Error|Semantic] ./test-files/symboltablebuilder/interfaces/error-duplicate-interface-def/source.spice:5:1:
Duplicate symbol: Duplicate symbol 'Person'

5 type Person interface {
^^^^^^^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Error|Semantic] ./test-files/typechecker/procedures/error-duplicate-procedure-def/source.spice:5:1:
[Error|Semantic] ./test-files/symboltablebuilder/procedures/error-duplicate-procedure-def/source.spice:5:1:
Multiple declarations of a function/procedure: The function/procedure 'exampleProcedure()' is declared twice

5 p exampleProcedure() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Error|Semantic] ./test-files/symboltablebuilder/structs/error-duplicate-field/source.spice:4:5:
Multiple declarations of the same variable: The field 'a' was declared more than once

4 string a
^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type TestStruct struct {
long a
short b
string a
}

f<int> main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Error|Semantic] ./test-files/symboltablebuilder/structs/error-duplicate-struct-def/source.spice:7:1:
Duplicate symbol: Duplicate symbol 'Person'

7 type Person struct {
^^^^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Error|Compiler]:
Unresolved soft errors: There are unresolved errors. Please fix them and recompile.

[Error|Semantic] ./test-files/typechecker/variables/error-variable-ambiguity/source.spice:4:9:
[Error|Semantic] ./test-files/symboltablebuilder/variables/error-variable-ambiguity/source.spice:4:9:
Used before declared: Symbol 'variable' was used before declared.

4 variable++;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Error|Compiler]:
Unresolved soft errors: There are unresolved errors. Please fix them and recompile.

[Error|Semantic] ./test-files/typechecker/variables/error-variable-declared-before-referenced/source.spice:2:26:
[Error|Semantic] ./test-files/symboltablebuilder/variables/error-variable-declared-before-referenced/source.spice:2:26:
Referenced undefined variable: The variable 'test' could not be found

2 rintf("Output: %s", test);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[Error|Semantic] ./test-files/typechecker/variables/error-variable-declared-only-once/source.spice:3:5:
[Error|Semantic] ./test-files/symboltablebuilder/variables/error-variable-declared-only-once/source.spice:3:5:
Multiple declarations of the same variable: The variable 'i' was declared more than once

3 int i = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@
"isGlobal": true,
"isVolatile": false,
"name": "TestStruct",
"orderIndex": 0,
"orderIndex": 1,
"state": "declared",
"type": "TestStruct<long>"
},
Expand All @@ -280,7 +280,7 @@
"isGlobal": true,
"isVolatile": false,
"name": "Alias.container",
"orderIndex": 2,
"orderIndex": 3,
"state": "declared",
"type": "TestStruct<long>"
},
Expand All @@ -289,7 +289,7 @@
"isGlobal": true,
"isVolatile": false,
"name": "main()",
"orderIndex": 3,
"orderIndex": 4,
"state": "declared",
"type": "f<int>()"
},
Expand All @@ -298,7 +298,7 @@
"isGlobal": true,
"isVolatile": false,
"name": "Alias",
"orderIndex": 1,
"orderIndex": 2,
"state": "declared",
"type": "TestStruct<long>"
},
Expand All @@ -307,9 +307,18 @@
"isGlobal": true,
"isVolatile": false,
"name": "TestStruct",
"orderIndex": 0,
"orderIndex": 1,
"state": "declared",
"type": "TestStruct<long>"
},
{
"codeLoc": "L1C1",
"isGlobal": true,
"isVolatile": false,
"name": "T",
"orderIndex": 0,
"state": "declared",
"type": "invalid"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@
"isGlobal": true,
"isVolatile": false,
"name": "MockIterator",
"orderIndex": 2,
"orderIndex": 3,
"state": "declared",
"type": "MockIterator<short>"
},
Expand All @@ -585,16 +585,25 @@
"isGlobal": true,
"isVolatile": false,
"name": "main()",
"orderIndex": 3,
"orderIndex": 4,
"state": "declared",
"type": "f<int>()"
},
{
"codeLoc": "L4C1",
"isGlobal": true,
"isVolatile": false,
"name": "T",
"orderIndex": 2,
"state": "declared",
"type": "invalid"
},
{
"codeLoc": "L6C1",
"isGlobal": true,
"isVolatile": false,
"name": "MockIterator",
"orderIndex": 2,
"orderIndex": 3,
"state": "declared",
"type": "MockIterator<T>"
},
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit e5bb859

Please sign in to comment.