pocketlang/src/core.h

59 lines
2.0 KiB
C
Raw Normal View History

2021-02-11 01:23:48 +08:00
/*
* Copyright (c) 2021 Thakee Nathees
* Licensed under: MIT License
*/
#ifndef CORE_H
#define CORE_H
#include "var.h"
#include "common.h"
2021-02-12 01:35:43 +08:00
2021-04-25 23:19:39 +08:00
// Initialize core language, builtin function and "std" scripts.
// Note (TODO: refactore required):
// Since the builtin function doesn't require any allocation they're
// elements of a static `builtins` array but the "std" scripts are `Script`
// objects they required memory management and they're bound with the VM.
// It contradicts `initializeCore()` to be called for each VM or only once.
// 1. Make the scripts share between VMs.
// 2. Destroy scripts buffer only when the last VM die.
2021-02-12 01:35:43 +08:00
void initializeCore(MSVM* vm);
2021-04-26 17:34:30 +08:00
// Mark the heap allocated core object at the mark phase.
void markCoreObjects(MSVM* vm);
2021-02-12 01:35:43 +08:00
// Find the builtin function name and returns it's index in the builtins array
// if not found returns -1.
int findBuiltinFunction(const char* name, uint32_t length);
2021-02-12 01:35:43 +08:00
// Returns the builtin function at index [index].
2021-02-12 01:35:43 +08:00
Function* getBuiltinFunction(int index);
// Returns the builtin function's name at index [index].
const char* getBuiltinFunctionName(int index);
2021-02-12 01:35:43 +08:00
// Operators //////////////////////////////////////////////////////////////////
2021-02-11 01:23:48 +08:00
Var varAdd(MSVM* vm, Var v1, Var v2);
Var varSubtract(MSVM* vm, Var v1, Var v2);
Var varMultiply(MSVM* vm, Var v1, Var v2);
Var varDivide(MSVM* vm, Var v1, Var v2);
2021-02-16 02:51:00 +08:00
bool varGreater(MSVM* vm, Var v1, Var v2);
bool varLesser(MSVM* vm, Var v1, Var v2);
Var varGetAttrib(MSVM* vm, Var on, String* attrib);
void varSetAttrib(MSVM* vm, Var on, String* name, Var value);
Var varGetSubscript(MSVM* vm, Var on, Var key);
void varsetSubscript(MSVM* vm, Var on, Var key, Var value);
2021-02-15 20:49:19 +08:00
// Functions //////////////////////////////////////////////////////////////////
// Parameter [iterator] should be VAR_NULL before starting the iteration.
// If an element is obtained by iteration it'll return true otherwise returns
// false indicating that the iteration is over.
bool varIterate(MSVM* vm, Var seq, Var* iterator, Var* value);
2021-02-12 01:35:43 +08:00
2021-02-11 01:23:48 +08:00
#endif // CORE_H