initial commit

This commit is contained in:
Roland Conybeare 2025-11-17 22:28:31 -05:00
commit 442d24805d
6 changed files with 215 additions and 0 deletions

26
CMakeLists.txt Normal file
View file

@ -0,0 +1,26 @@
# xo-symboltable/CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(xo_symboltable VERSION 0.1)
include(GNUInstallDirs)
include(cmake/xo-bootstrap-macros.cmake)
xo_cxx_toplevel_options3()
# ----------------------------------------------------------------
# c++ settings
set(PROJECT_CXX_FLAGS "")
add_definitions(${PROJECT_CXX_FLAGS})
# ----------------------------------------------------------------
add_subdirectory(src/symboltable)
xo_export_cmake_config(${PROJECT_NAME} ${PROJECT_VERSION} ${PROJECT_NAME}Targets)
# ----------------------------------------------------------------
#add_subdirectory(utest)

View file

@ -0,0 +1,35 @@
# ----------------------------------------------------------------
# for example:
# $ PREFIX=/usr/local # for example
# $ cmake -DCMAKE_MODULE_PATH=prefix -DCMAKE_INSTALL_PREFIX=$PREFIX -B .build
#
# will get
# CMAKE_MODULE_PATH
# from xo-cmake-config --cmake-module-path
#
# and expect .cmake macros in
# CMAKE_MODULE_PATH/xo_macros/xo_cxx.cmake
# ----------------------------------------------------------------
find_program(XO_CMAKE_CONFIG_EXECUTABLE NAMES xo-cmake-config REQUIRED)
if ("${XO_CMAKE_CONFIG_EXECUTABLE}" STREQUAL "XO_CMAKE_CONFIG_EXECUTABLE-NOT_FOUND")
message(FATAL "could not find xo-cmake-config executable")
endif()
message(STATUS "XO_CMAKE_CONFIG_EXECUTABLE=${XO_CMAKE_CONFIG_EXECUTABLE}")
if (NOT XO_SUBMODULE_BUILD)
if (("${CMAKE_MODULE_PATH}" STREQUAL "") OR ("${CMAKE_MODULE_PATH}" STREQUAL prefix))
# default to typical install location for xo-project-macros
execute_process(COMMAND ${XO_CMAKE_CONFIG_EXECUTABLE} --cmake-module-path OUTPUT_VARIABLE CMAKE_MODULE_PATH)
message(STATUS "CMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}")
endif()
endif()
# needs to have been installed somewhere on CMAKE_MODULE_PATH,
# (e.g. from xo-cmake with the same value for CMAKE_INSTALL_PREFIX)
#
include(xo_macros/xo_cxx)
xo_cxx_bootstrap_message()

View file

@ -0,0 +1,8 @@
@PACKAGE_INIT@
include(CMakeFindDependencyMacro)
# reminder: deps here must also appear in xo-object/src/object/CMakeLists.txt
find_dependency(xo_alloc)
#find_dependency(xo_flatstring)
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
check_required_components("@PROJECT_NAME@")

View file

@ -0,0 +1,49 @@
/** @file Symbol.hpp
*
* @author Roland Conybeare, Nov 2025
**/
#pragma once
#include "xo/alloc/Object.hpp"
#include <cstdint>
namespace xo {
/** @class Symbol
* @brief represent a schematika symbol.
*
* Each uniquely-spelled variable associates to a distinct symbol.
*
**/
namespace scm {
class Symbol : public Object {
public:
using TaggedPtr = xo::reflect::TaggedPtr;
public:
/** create new Symbol instance with name @p n **/
static gp<Symbol> make(gc::IAlloc * mm, const char * n);
// ----- reflection helpers ------
virtual TaggedPtr self_tp() const override final;
virtual void display(std::ostream & os) const override final;
// ----- GC helpers -----
virtual std::size_t _shallow_size() const override final;
virtual Object * _shallow_copy() const override final;
virtual std::size_t _forward_children() override final;
private:
Symbol(gc::IAlloc * mm, const char * name);
private:
/** name of this symbol **/
const char * name_;
/** size in bytes (= ascii chars) for this symbol. includes null terminator **/
std::uint32_t name_z_;
};
} /*namespace scm*/
} /*namesapce xo*/
/* end Symbol.hpp */

View file

@ -0,0 +1,8 @@
# symboltable/CMakeLists.txt
set(SELF_LIB xo_symboltable)
set(SELF_SRCS
Symbol.cpp)
xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS})
xo_dependency(${SELF_LIB} xo_alloc)

View file

@ -0,0 +1,89 @@
/** @file Symbol.cpp
*
* @author Roland Conybeare, Nov 2025
**/
#include "Symbol.hpp"
#include "xo/reflect/Reflect.hpp"
namespace xo {
using xo::reflect::Reflect;
using xo::reflect::TaggedPtr;
namespace scm {
gp<Symbol>
Symbol::make(gc::IAlloc * mm, const char * n)
{
return new (MMPtr(mm)) Symbol(mm, n);
}
Symbol::Symbol(gc::IAlloc * mm,
const char * name)
{
std::size_t nz = ::strlen(name) + 1;
assert(nz > 0);
assert(nz < 4096);
char * mem = reinterpret_cast<char *>(mm->alloc(nz));
strncpy(mem, name, nz);
this->name_ = mem;
this->name_z_ = nz;
}
// ------ Reflection helpers -----
TaggedPtr
Symbol::self_tp() const
{
return Reflect::make_tp(const_cast<Symbol *>(this));
}
void
Symbol::display(std::ostream & os) const
{
os << name_;
}
// ------ GC helpers -----
std::size_t
Symbol::_shallow_size() const
{
std::size_t retval = sizeof(Symbol);
/* note: to remove all doubt:
* in case ::strlen(name_) != name_z,
* compute string length here.
*
* Alternative would be to pass length to ctor
* + ensure that's what gets allocated
*/
retval += gc::IAlloc::with_padding(::strlen(name_) + 1);
return retval;
}
Object *
Symbol::_shallow_copy() const
{
Cpof cpof(Object::mm, this);
Symbol * copy = new (cpof) Symbol(cpof.mm_, name_);
return copy;
}
std::size_t
Symbol::_forward_children()
{
/* note: zero embedded gp<T> pointers to fixup */
return _shallow_size();
}
} /*namespace scm*/
} /*namespace xo*/
/* end Symbol.cpp */