Skip to content

Language‐Specification

nguyenhungtran18 edited this page Sep 8, 2026 · 4 revisions

TokenVector (.tkv) Language Specification

Tập tài liệu đặc tả cú pháp, hệ thống kiểu dữ liệu tĩnh và ánh xạ sang Common Intermediate Language (CIL/MSIL) của ngôn ngữ TokenVector.


1. Type System (Hệ thống kiểu dữ liệu)

TokenVector là ngôn ngữ định kiểu tĩnh độc lập. Mọi biến và tham số hàm đều được ánh xạ trực tiếp về kiểu dữ liệu CTS (Common Type System) của .NET runtime nhằm loại bỏ boxing/unboxing runtime overhead.

Kiểu dữ liệu .tkv Kiểu .NET tương ứng Kích thước IL Instruction nạp giá trị
int System.Int32 4 bytes ldc.i4, ldc.i4.s
long System.Int64 8 bytes ldc.i8
float System.Double 8 bytes ldc.r8
bool System.Boolean 1 byte ldc.i4.1 (true) / ldc.i4.0 (false)
str System.String Con trỏ đối tượng ldstr
void System.Void 0 ret (không trả về giá trị)

2. Syntax & Variable Declarations

Khai báo biến trong TokenVector sử dụng cú pháp tường minh, phân tách phạm vi (scope) rõ ràng.

Khai báo biến cơ bản (.tkv)

# Cú pháp: [tên_biến]: [kiểu_dữ_liệu] = [biểu_thức]
x: int = 42
pi: float = 3.14159
is_ready: bool = True
message: str = "Native TokenVector Executable"
Tương đương IL Assembly được sinh ra:Code snippet.locals init (
    [0] int32 x,
    [1] float64 pi,
    [2] bool is_ready,
    [3] string message
)
ldc.i4.s 42
stloc.0
ldc.r8 3.14159
stloc.1
ldc.i4.1
stloc.2
ldstr "Native TokenVector Executable"
stloc.3

3. Control Flow Constructs

🔹 Khối điều kiện if / else (.tkv)

if x > 100:
    print("Greater")
else:
    print("Smaller or Equal")

Cấu trúc CIL phát sinh:

Code snippetldloc.0
ldc.i4.s 100
ble.s IL_ELSE_0001
ldstr "Greater"
call void [mscorlib]System.Console::WriteLine(string)
br.s IL_EXIT_0001
IL_ELSE_0001:
ldstr "Smaller or Equal"
call void [mscorlib]System.Console::WriteLine(string)
IL_EXIT_0001:

🔹 Vòng lặp while (.tkv):

count: int = 0
while count < 10:
    count = count + 1

4. Function Signatures & Calling Conventions

Mọi hàm trong TokenVector được biên dịch thành phương thức tĩnh (static) nhằm tối ưu tốc độ gọi lệnh và kích thước nhị phân.

def add(a: int, b: int) -> int:
    return a + b

def main() -> void:
    result: int = add(15, 27)
    print(result)

Tương đương CIL Backend:

.method public static int32 add(int32 a, int32 b) cil managed
{
    .maxstack 2
    ldarg.0
    ldarg.1
    add
    ret
}

5. Direct Win32 Interoperability (P/Invoke)

TokenVector hỗ trợ gọi trực tiếp các API của hệ điều hành Windows thông qua P/Invoke mà không phụ thuộc vào bất kỳ runtime phụ trợ nào:

extern def MessageBoxA(hwnd: int, text: str, title: str, uType: int) -> int:
    from "user32.dll"

def show_alert() -> void:
    MessageBoxA(0, "Operation Completed!", "TokenVector", 0)