pocketlang/src/var.c

116 lines
2.4 KiB
C
Raw Normal View History

2021-02-07 15:40:00 +08:00
/*
* Copyright (c) 2020-2021 Thakee Nathees
* Licensed under: MIT License
*/
#include "var.h"
#include "vm.h"
2021-02-08 02:30:29 +08:00
void varInitObject(Object* self, MSVM* vm, ObjectType type) {
2021-02-07 15:40:00 +08:00
self->type = type;
self->next = vm->first;
vm->first = self;
// TODO: set isGray = false;
}
#if VAR_NAN_TAGGING
// A union to reinterpret a double as raw bits and back.
typedef union {
uint64_t bits64;
uint32_t bits32[2];
double num;
} _DoubleBitsConv;
#endif
Var doubleToVar(double value) {
#if VAR_NAN_TAGGING
_DoubleBitsConv bits;
bits.num = value;
return bits.bits64;
#else
// TODO:
#endif // VAR_NAN_TAGGING
}
2021-02-11 01:23:48 +08:00
double varToDouble(Var value) {
2021-02-07 15:40:00 +08:00
#if VAR_NAN_TAGGING
_DoubleBitsConv bits;
bits.bits64 = value;
return bits.num;
#else
// TODO:
#endif // VAR_NAN_TAGGING
}
2021-02-08 02:30:29 +08:00
String* newString(MSVM* vm, const char* text, uint32_t length) {
2021-02-07 15:40:00 +08:00
ASSERT(length == 0 || text != NULL, "Unexpected NULL string.");
String* string = ALLOCATE_DYNAMIC(vm, String, length + 1, char);
varInitObject(&string->_super, vm, OBJ_STRING);
string->length = length;
if (length != 0) memcpy(string->data, text, length);
string->data[length] = '\0';
return string;
}
2021-02-08 02:30:29 +08:00
Script* newScript(MSVM* vm) {
2021-02-07 15:40:00 +08:00
Script* script = ALLOCATE(vm, Script);
varInitObject(&script->_super, vm, OBJ_SCRIPT);
varBufferInit(&script->globals);
nameTableInit(&script->global_names);
2021-02-09 16:21:10 +08:00
varBufferInit(&script->literals);
vmPushTempRef(vm, &script->_super);
script->body = newFunction(vm, "@(ScriptLevel)", script, false);
vmPopTempRef(vm);
2021-02-07 15:40:00 +08:00
functionBufferInit(&script->functions);
nameTableInit(&script->function_names);
2021-02-11 01:23:48 +08:00
stringBufferInit(&script->names);
2021-02-07 15:40:00 +08:00
return script;
}
2021-02-08 02:30:29 +08:00
Function* newFunction(MSVM* vm, const char* name, Script* owner,
2021-02-07 15:40:00 +08:00
bool is_native) {
Function* func = ALLOCATE(vm, Function);
varInitObject(&func->_super, vm, OBJ_FUNC);
func->name = name;
func->owner = owner;
func->arity = -1;
func->is_native = is_native;
if (is_native) {
func->native = NULL;
} else {
2021-02-09 16:21:10 +08:00
//vmPushTempRef(vm, &func->_super);
2021-02-07 15:40:00 +08:00
Fn* fn = ALLOCATE(vm, Fn);
2021-02-09 16:21:10 +08:00
//vmPopTempRef(vm);
2021-02-07 15:40:00 +08:00
byteBufferInit(&fn->opcodes);
intBufferInit(&fn->oplines);
fn->stack_size = 0;
func->fn = fn;
}
return func;
}
2021-02-09 16:21:10 +08:00
2021-02-11 01:23:48 +08:00
// Utility functions //////////////////////////////////////////////////////////
2021-02-09 16:21:10 +08:00
bool isVauesSame(Var v1, Var v2) {
#if VAR_NAN_TAGGING
// Bit representation of each values are unique so just compare the bits.
return v1 == v2;
#else
#error TODO:
#endif
2021-02-11 01:23:48 +08:00
}