erick-alcachofa f235a72671
feat(semantic): introduce SymbolTable and core semantic data structures
Signed-off-by: erick-alcachofa <erick@artichoke.dev>

- Define core symbol types: Function, Struct, Enum, and Variable.
- Implement recursive TypeSymbol structure with qualifier support.
- Add TemplatedSymbol and TemplateParam for upcoming generic support.
- Implement scoped SymbolTable with parent/inner hierarchy.
2025-12-30 23:16:11 -06:00

189 lines
4.8 KiB
C++

//============================================================================//
// //
// artichoke programming language //
// //
// Copyright (C) 2025 Erick Saul Guzman Ramos, whoami.artichoke.dev //
// //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU Affero General Public License as published //
// by the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU Affero General Public License for more details. //
// //
// You should have received a copy of the GNU Affero General Public License //
// along with this program. If not, see <https://www.gnu.org/licenses/>. //
// //
//============================================================================//
#pragma once
#include <variant>
#include <artichoke/Util/Common.hpp>
namespace arti::lang::sym {
struct Function;
struct FunctionType;
struct Enum;
struct EnumMember;
struct Struct;
struct StructMember;
struct Variable;
struct Primitive;
struct TypeSymbol;
struct TemplateParam;
struct TemplatedSymbol;
using Symbol = std::variant<
SharedPtr<Function>,
SharedPtr<Variable>,
SharedPtr<TypeSymbol>,
SharedPtr<FunctionType>,
SharedPtr<Primitive>,
SharedPtr<Struct>,
SharedPtr<Enum>,
SharedPtr<TemplatedSymbol>,
SharedPtr<TemplateParam>
>;
using WeakSymbol = std::variant<
WeakPtr<Function>,
WeakPtr<Variable>,
WeakPtr<TypeSymbol>,
WeakPtr<FunctionType>,
WeakPtr<Primitive>,
WeakPtr<Struct>,
WeakPtr<Enum>,
WeakPtr<TemplatedSymbol>
>;
struct SymbolTable {
String name;
SourceLocation location;
WeakPtr<SymbolTable> parent;
HashMap<String, SharedPtr<SymbolTable>> inner;
HashMap<String, Symbol> symbols;
};
struct Function {
String name;
SourceLocation location;
WeakPtr<SymbolTable> parent;
SharedPtr<SymbolTable> body;
WeakPtr<FunctionType> type;
};
struct FunctionType {
String name;
SourceLocation location;
WeakPtr<SymbolTable> parent;
WeakPtr<Function> symbol;
Vector<WeakPtr<TypeSymbol>> arguments;
Optional<WeakPtr<TypeSymbol>> returnType;
};
struct EnumMember {
String name;
SourceLocation location;
std::uint64_t index;
Optional<WeakPtr<TypeSymbol>> type;
};
struct Enum {
String name;
SourceLocation location;
WeakPtr<SymbolTable> parent;
std::uint64_t size;
std::uint64_t alignment;
Vector<EnumMember> membersList;
HashMap<String, std::size_t> membersLookup;
HashMap<String, WeakPtr<Function>> methods;
};
struct StructMember {
String name;
SourceLocation location;
std::uint64_t index;
std::uint64_t offset;
WeakPtr<TypeSymbol> type;
};
struct Struct {
String name;
SourceLocation location;
WeakPtr<SymbolTable> parent;
std::uint64_t size;
std::uint64_t alignment;
Vector<StructMember> membersList;
HashMap<String, std::size_t> membersLookup;
HashMap<String, WeakPtr<Function>> methods;
};
struct TemplateParam {
String name;
SourceLocation location;
WeakPtr<SymbolTable> parent;
};
struct TemplatedSymbol {
String name;
SourceLocation location;
WeakPtr<SymbolTable> parent;
/* It's parent is ^ */
WeakPtr<SymbolTable> scope;
/* Lives in scope */
WeakSymbol symbol;
Vector<TemplateParam> params;
};
struct Variable {
String name;
SourceLocation location;
WeakPtr<TypeSymbol> type;
};
struct Primitive {
String name;
std::uint64_t size;
std::uint64_t alignment;
};
struct TypeSymbol {
using Type = std::variant<
SharedPtr<TypeSymbol>,
WeakPtr<FunctionType>,
WeakPtr<Primitive>,
WeakPtr<Struct>,
WeakPtr<Enum>,
WeakPtr<TemplatedSymbol>,
WeakPtr<TemplateParam>
>;
Type type;
Optional<TypeQualifier> qualifier;
};
} // namespace arti::lang