Skip to content

[Draft Feature] Adding Support for Custom Literal Constants#12

Draft
HenryAWE wants to merge 14 commits into
anjo76:masterfrom
HenryAWE:feature/user_literal
Draft

[Draft Feature] Adding Support for Custom Literal Constants#12
HenryAWE wants to merge 14 commits into
anjo76:masterfrom
HenryAWE:feature/user_literal

Conversation

@HenryAWE

Copy link
Copy Markdown

TL;DR
Once this is implemented, AngelScript will allow user to make their own literals. For example, if you want a fixed point number instead of built-in floating point number for consistent math result across platforms, you can write script like this:

// The function for constructing fixed32 values are registered in C++ side
fixed32 val = 3.14f32;

This is moved from gamedev.net to here (original thread link)

This branch is still work-in-progress (only the part of modification to parser is done), so I mark it as draft PR for now.

@anjo76

anjo76 commented Sep 14, 2025

Copy link
Copy Markdown
Owner

Thanks for sharing. I will have a closer look at what you have so far.

@anjo76 anjo76 added the enhancement New feature or request label Sep 14, 2025
@anjo76

anjo76 commented Oct 27, 2025

Copy link
Copy Markdown
Owner

@HenryAWE can summarize what you've done so far, and what is still left to do?

@HenryAWE

HenryAWE commented Oct 27, 2025

Copy link
Copy Markdown
Author

@HenryAWE can summarize what you've done so far, and what is still left to do?

Currently, only parser for literal pattern (e.g. void f() {'_f32' suffix} for constructor) is done. Update to RegisterObjectBehaviour only completed partially: the return value type, etc. of registered function is checked, but it's not actually stored in the script engine yet.

I'm still working on where is the best place to add parsing logic for custom literals within script (e.g. 3.14_f32)

I was employed recently and might no longer have many free time compared to a student in university😂. This PR is expected to take long time to complete.

@HenryAWE HenryAWE mentioned this pull request Nov 29, 2025
Resolved conflict in main.cpp: kept Test_Addon_Autowrapper
from master, moved TestCompiler before addon tests so it
runs first.
@HenryAWE

Copy link
Copy Markdown
Author

Finally, I'm coming back to this PR 😀. Now it can compile & run a simple fixed point number test. I will continue working on it.

Here is an example extracted from the test case in test_compiler.cpp.

// C++ side: Fixed32 is a fixed-point type (16-bit integer + 16-bit fraction)
class Fixed32
{
public:
    int value;

    Fixed32(int v) : value(v) {}

    static void LiteralConstructDouble(double dblVal, void* mem)
    {
        int integer = int(dblVal);
        int fraction = int((dblVal - integer) * double(1 << 15));
        new(mem) Fixed32((integer << 15) + fraction);
    }

    operator int() const { return value >> 15; }
};

// Register the type and its literal suffix
engine->RegisterObjectType("Fixed32", sizeof(int),
    asGetTypeTraits<Fixed32>() | asOBJ_POD | asOBJ_VALUE | asOBJ_APP_CLASS_ALLINTS);

engine->RegisterObjectBehaviour("Fixed32", asBEHAVE_LITERAL_CONSTRUCT,
    "void f(double) {'_f32' suffix}",
    asFUNCTION(Fixed32::LiteralConstructDouble), asCALL_CDECL_OBJLAST);

// Script side
asIScriptModule* m = engine->GetModule("test", asGM_ALWAYS_CREATE);
m->AddScriptSection("test", "Fixed32 test() { return 3.14_f32; }");
m->Build();

// Execute and verify
asIScriptContext* ctx = engine->CreateContext();
ctx->Prepare(m->GetFunctionByName("test"));
ctx->Execute();
Fixed32 result = *(Fixed32*)ctx->GetAddressOfReturnValue();
assert(static_cast<int>(result) == 3);

@HenryAWE

Copy link
Copy Markdown
Author

@anjo76 Can you approve the workflow for this PR? Previously I was developing on Windows using MSVC, and now I'm developing on arm64 macOS with Apple Clang. The CI can help me find if any code that cannot be compiled by other compilers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants