Skip to content

Commit

Permalink
Extract logic to SymbolTableBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Nov 19, 2022
1 parent 01b409c commit ad9245e
Show file tree
Hide file tree
Showing 12 changed files with 519 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .run/spice.run.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="spice" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="run -O2 -d ../../media/test-project/os-test.spice" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spice" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spice">
<configuration default="false" name="spice" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="run -O0 -d ../../media/test-project/os-test.spice" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spice" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spice">
<envs>
<env name="CONSOLE_WIDTH" value="300" />
<env name="RUN_TESTS" value="OFF" />
Expand Down
19 changes: 1 addition & 18 deletions media/test-project/os-test.spice
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
f<int> main() {
// Directly
printf("%d\n", String("").isEmpty());
printf("%d\n", String("Hello").isEmpty());
printf("%d\n", String("Hello!").getLength());
printf("%d\n", String("Hello World!").getLength());
printf("%d\n", String("Hello!").getCapacity());
printf("%d\n", String("Hello World!").getCapacity());
printf("%d\n", String("Hello").isFull());
printf("%d\n", String("Hello World!").isFull());
printf("%d\n", String("Hello World!").find("ell"));
printf("%d\n", String("Hello World!").find("Wort"));
printf("%d\n", String("Hello World!").find("H"));
printf("%d\n", String("Hello World!").find("!"));
printf("%d\n", String("Hello World!").find(" ", 12));
printf("%d\n", String("Hello World!").contains("abc"));
printf("%d\n", String("Hello World!").contains("Hello"));
printf("%d\n", String("Hello World!").contains("World!"));
printf("%d\n", String("Hello World!").contains("o W"));
printf("Hello World!");
}

/*f<int> main() {
Expand Down
4 changes: 2 additions & 2 deletions src/ast/ASTBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ std::any ASTBuilder::visitEnumItem(SpiceParser::EnumItemContext *ctx) {

for (const auto &subTree : ctx->children) {
if (auto t1 = dynamic_cast<antlr4::tree::TerminalNode *>(subTree); t1->getSymbol()->getType() == SpiceParser::IDENTIFIER)
enumItemNode->name = getIdentifier(t1);
enumItemNode->itemName = getIdentifier(t1);
else if (auto t2 = dynamic_cast<antlr4::tree::TerminalNode *>(subTree); t2->getSymbol()->getType() == SpiceParser::INT_LIT) {
enumItemNode->itemValue = parseInt(t2);
enumItemNode->hasValue = true;
Expand All @@ -690,7 +690,7 @@ std::any ASTBuilder::visitField(SpiceParser::FieldContext *ctx) {
saveErrorMessage(fieldNode, ctx);

// Extract field name
fieldNode->name = getIdentifier(ctx->IDENTIFIER());
fieldNode->fieldName = getIdentifier(ctx->IDENTIFIER());

for (const auto &subTree : ctx->children) {
antlr4::ParserRuleContext *rule;
Expand Down
50 changes: 38 additions & 12 deletions src/ast/ASTNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,11 @@ class FctDefNode : public ASTNode {
[[nodiscard]] DataTypeNode *returnType() const { return getChild<DataTypeNode>(); }
[[nodiscard]] TypeLstNode *templateTypeLst() const { return getChild<TypeLstNode>(); }
[[nodiscard]] ParamLstNode *paramLst() const { return getChild<ParamLstNode>(); }
[[nodiscard]] StmtLstNode *stmtLst() const { return getChild<StmtLstNode>(); }
[[nodiscard]] StmtLstNode *body() const { return getChild<StmtLstNode>(); }

// Other methods
[[nodiscard]] std::string getScopeId() const { return "fct:" + codeLoc.toString(); }
[[nodiscard]] std::string getTemporaryName() const { return functionName + ":" + codeLoc.toString(); }

// Public members
std::string functionName;
Expand Down Expand Up @@ -214,10 +215,11 @@ class ProcDefNode : public ASTNode {
[[nodiscard]] SpecifierLstNode *specifierLst() const { return getChild<SpecifierLstNode>(); }
[[nodiscard]] TypeLstNode *templateTypeLst() const { return getChild<TypeLstNode>(); }
[[nodiscard]] ParamLstNode *paramLst() const { return getChild<ParamLstNode>(); }
[[nodiscard]] StmtLstNode *stmtLst() const { return getChild<StmtLstNode>(); }
[[nodiscard]] StmtLstNode *body() const { return getChild<StmtLstNode>(); }

// Other methods
[[nodiscard]] std::string getScopeId() const { return "proc:" + codeLoc.toString(); }
[[nodiscard]] std::string getTemporaryName() const { return procedureName + ":" + codeLoc.toString(); }

// Public members
std::string procedureName;
Expand Down Expand Up @@ -253,6 +255,7 @@ class StructDefNode : public ASTNode {
bool isGeneric = false;
bool hasInterfaces = false;
SymbolTableEntry *entry = nullptr;
Scope *structScope = nullptr;
Struct *spiceStruct;
};

Expand All @@ -273,6 +276,7 @@ class InterfaceDefNode : public ASTNode {
// Public members
std::string interfaceName;
SymbolTableEntry *entry = nullptr;
Scope *interfaceScope = nullptr;
Interface *spiceInterface;
};

Expand Down Expand Up @@ -368,10 +372,13 @@ class ThreadDefNode : public ASTNode {
std::any accept(AbstractASTVisitor *visitor) override { return visitor->visitThreadDef(this); }

// Public get methods
[[nodiscard]] StmtLstNode *stmtLst() const { return getChild<StmtLstNode>(); }
[[nodiscard]] StmtLstNode *body() const { return getChild<StmtLstNode>(); }

// Other methods
[[nodiscard]] std::string getScopeId() const { return "thread:" + codeLoc.toString(); }

// Public members
Scope *bodyScope = nullptr;
};

// ====================================================== UnsafeBlockDefNode =====================================================
Expand All @@ -385,10 +392,13 @@ class UnsafeBlockDefNode : public ASTNode {
std::any accept(AbstractASTVisitor *visitor) override { return visitor->visitUnsafeBlockDef(this); }

// Public get methods
[[nodiscard]] StmtLstNode *stmtLst() const { return getChild<StmtLstNode>(); }
[[nodiscard]] StmtLstNode *body() const { return getChild<StmtLstNode>(); }

// Other methods
[[nodiscard]] std::string getScopeId() const { return "unsafe:" + codeLoc.toString(); }

// Public members
Scope *bodyScope = nullptr;
};

// ========================================================== ForLoopNode ========================================================
Expand All @@ -405,10 +415,13 @@ class ForLoopNode : public ASTNode {
[[nodiscard]] DeclStmtNode *initDecl() const { return getChild<DeclStmtNode>(); }
[[nodiscard]] AssignExprNode *condAssign() const { return getChild<AssignExprNode>(0); }
[[nodiscard]] AssignExprNode *incAssign() const { return getChild<AssignExprNode>(1); }
[[nodiscard]] StmtLstNode *stmtLst() const { return getChild<StmtLstNode>(); }
[[nodiscard]] StmtLstNode *body() const { return getChild<StmtLstNode>(); }

// Other methods
[[nodiscard]] std::string getScopeId() const { return "for:" + codeLoc.toString(); }

// Public members
Scope *bodyScope = nullptr;
};

// ======================================================== ForeachLoopNode ======================================================
Expand All @@ -428,10 +441,13 @@ class ForeachLoopNode : public ASTNode {
}
[[nodiscard]] DeclStmtNode *itemDecl() const { return getChildren<DeclStmtNode>().back(); }
[[nodiscard]] AssignExprNode *arrayAssign() const { return getChild<AssignExprNode>(); }
[[nodiscard]] StmtLstNode *stmtLst() const { return getChild<StmtLstNode>(); }
[[nodiscard]] StmtLstNode *body() const { return getChild<StmtLstNode>(); }

// Other methods
[[nodiscard]] std::string getScopeId() const { return "foreach:" + codeLoc.toString(); }

// Public members
Scope *bodyScope = nullptr;
};

// ========================================================= WhileLoopNode =======================================================
Expand All @@ -446,10 +462,13 @@ class WhileLoopNode : public ASTNode {

// Public get methods
[[nodiscard]] AssignExprNode *condition() const { return getChild<AssignExprNode>(); }
[[nodiscard]] StmtLstNode *stmtLst() const { return getChild<StmtLstNode>(); }
[[nodiscard]] StmtLstNode *body() const { return getChild<StmtLstNode>(); }

// Other methods
[[nodiscard]] std::string getScopeId() const { return "while:" + codeLoc.toString(); }

// Public members
Scope *bodyScope = nullptr;
};

// ========================================================== IfStmtNode =========================================================
Expand All @@ -464,11 +483,14 @@ class IfStmtNode : public ASTNode {

// Public get methods
[[nodiscard]] AssignExprNode *condition() const { return getChild<AssignExprNode>(); }
[[nodiscard]] StmtLstNode *stmtLst() const { return getChild<StmtLstNode>(); }
[[nodiscard]] StmtLstNode *thenBody() const { return getChild<StmtLstNode>(); }
[[nodiscard]] ElseStmtNode *elseStmt() const { return getChild<ElseStmtNode>(); }

// Other methods
[[nodiscard]] std::string getScopeId() const { return "if:" + codeLoc.toString(); }

// Public members
Scope *thenBodyScope = nullptr;
};

// ========================================================= ElseStmtNode ========================================================
Expand All @@ -483,13 +505,14 @@ class ElseStmtNode : public ASTNode {

// Public get methods
[[nodiscard]] IfStmtNode *ifStmt() const { return getChild<IfStmtNode>(); }
[[nodiscard]] StmtLstNode *stmtLst() const { return getChild<StmtLstNode>(); }
[[nodiscard]] StmtLstNode *body() const { return getChild<StmtLstNode>(); }

// Other methods
[[nodiscard]] std::string getScopeId() const { return "if:" + codeLoc.toString(); }

// Public members
bool isElseIf = false;
Scope *elseBodyScope = nullptr;
};

// ======================================================== AssertStmtNode =======================================================
Expand Down Expand Up @@ -520,10 +543,13 @@ class AnonymousBlockStmtNode : public ASTNode {
std::any accept(AbstractASTVisitor *visitor) override { return visitor->visitAnonymousBlockStmt(this); }

// Public get methods
[[nodiscard]] StmtLstNode *stmtLst() const { return getChild<StmtLstNode>(); }
[[nodiscard]] StmtLstNode *body() const { return getChild<StmtLstNode>(); }

// Other methods
[[nodiscard]] std::string getScopeId() const { return "anon:" + codeLoc.toString(); }

// Public members
Scope *bodyScope = nullptr;
};

// ========================================================= StmtLstNode =========================================================
Expand Down Expand Up @@ -629,7 +655,7 @@ class EnumItemNode : public ASTNode {
// Public get methods

// Public members
std::string name;
std::string itemName;
uint32_t itemValue;
bool hasValue = false;
SymbolTableEntry *entry = nullptr;
Expand All @@ -650,7 +676,7 @@ class FieldNode : public ASTNode {
[[nodiscard]] DataTypeNode *dataType() const { return getChild<DataTypeNode>(); }

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

Expand Down
2 changes: 2 additions & 0 deletions src/exception/SemanticError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ std::string SemanticError::getMessagePrefix(SemanticErrorType type) {
return "Struct ambiguity";
case VARIABLE_DECLARED_TWICE:
return "Multiple declarations of the same variable";
case GLOBAL_DECLARED_TWICE:
return "Multiple declarations of the same global variable";
case FUNCTION_DECLARED_TWICE:
return "Multiple declarations of a function/procedure";
case GENERIC_TYPE_DECLARED_TWICE:
Expand Down
1 change: 1 addition & 0 deletions src/exception/SemanticError.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum SemanticErrorType {
FUNCTION_AMBIGUITY,
STRUCT_AMBIGUITY,
VARIABLE_DECLARED_TWICE,
GLOBAL_DECLARED_TWICE,
FUNCTION_DECLARED_TWICE,
GENERIC_TYPE_DECLARED_TWICE,
STRUCT_WITH_ILLEGAL_NAME,
Expand Down
6 changes: 3 additions & 3 deletions src/scope/Scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Function *Scope::matchFunction(const std::string &callFunctionName, const Symbol
auto oldManifestations = manifestations;
for (auto &[mangledName, f] : oldManifestations) {
// Check name requirement
if (f.name != callFunctionName)
if (f.fieldName != callFunctionName)
continue;

// Initialize mapping from generic type name to concrete symbol type
Expand Down Expand Up @@ -258,7 +258,7 @@ Function *Scope::matchFunction(const std::string &callFunctionName, const Symbol
callTemplateTypeIndex++;
}
// If types remain in the callTemplateTypes vector, skip this function substantiation (useful for generic return types)
if (callTemplateTypeIndex < callTemplateTypes.size() && f.name != CTOR_FUNCTION_NAME)
if (callTemplateTypeIndex < callTemplateTypes.size() && f.fieldName != CTOR_FUNCTION_NAME)
continue;

// Duplicate function
Expand Down Expand Up @@ -370,7 +370,7 @@ Struct *Scope::matchStruct(Scope *currentScope, const std::string &structName, /
auto oldManifestations = manifestations;
for (auto &[mangledName, s] : oldManifestations) {
// Check name requirement
if (s.name != structName)
if (s.fieldName != structName)
continue;

// Check template types requirement
Expand Down
2 changes: 1 addition & 1 deletion src/scope/Scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum ScopeType {
SCOPE_FOREACH_BODY,
SCOPE_THREAD_BODY,
SCOPE_UNSAFE_BODY,
SCOPE_ANONYMOUS
SCOPE_ANONYMOUS_BLOCK_BODY
};

/**
Expand Down

0 comments on commit ad9245e

Please sign in to comment.