Allow creating arrays from code

This commit is contained in:
Konstantin Nazarov 2024-09-01 16:13:28 +01:00
parent 7d160da5d0
commit 8816c62a9e
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
6 changed files with 17 additions and 6 deletions

View file

@ -45,10 +45,10 @@ target_sources(vm_lib
src/lineedit.hpp
)
add_executable(vli src/vli.cpp)
target_link_libraries(vli vm_lib)
add_executable(valeri src/valeri.cpp)
target_link_libraries(valeri vm_lib)
install(TARGETS vli)
install(TARGETS valeri)
# TESTING
@ -87,6 +87,6 @@ endforeach()
foreach(testname IN LISTS LISP_TESTS)
add_test(
NAME "test_lisp_${testname}"
COMMAND sh -c "$<TARGET_FILE:vli> '${CMAKE_CURRENT_SOURCE_DIR}/test/${testname}.vli'"
COMMAND sh -c "$<TARGET_FILE:valeri> '${CMAKE_CURRENT_SOURCE_DIR}/test/${testname}.vli'"
)
endforeach()

View file

@ -69,7 +69,7 @@ To run:
```sh
cmake .
make
./vli example.vli
./valeri example.vli
```
To run tests:

View file

@ -202,6 +202,7 @@ class Array : public Object {
uint64_t res_size = lhs_size + rhs_size;
auto pod = TRY(arena_alloc<PodArray>(res_size * sizeof(PodObject*)));
pod->header.tag = Tag::Array;
pod->size = res_size;
for (uint64_t i = 0; i < size(); i++) {
pod->data[i] = _value->data[i].get();
@ -217,6 +218,7 @@ class Array : public Object {
if (start > end) return ERROR(IndexOutOfRange);
uint64_t res_size = end - start;
auto pod = TRY(arena_alloc<PodArray>(res_size * sizeof(PodObject*)));
pod->header.tag = Tag::Array;
pod->size = res_size;
for (uint64_t i = 0; i < end - start; i++) {
pod->data[i] = _value->data[start + i];
@ -1049,6 +1051,7 @@ class Stack : public Object {
if (start > end || end > gettop()) return ERROR(IndexOutOfRange);
uint64_t res_size = end - start;
auto pod = TRY(arena_alloc<PodArray>(res_size * sizeof(PodObject*)));
pod->header.tag = Tag::Array;
pod->size = res_size;
for (uint64_t i = 0; i < end - start; i++) {
pod->data[i] = _value->data[start + i];

View file

@ -74,6 +74,12 @@ Result<Value> stdlib_list(const Array& params) {
return d;
}
Result<Value> stdlib_array(const Array& params) {
debug_print(params);
Array array_copy = TRY(params.copy());
return Value(std::move(array_copy));
}
Result<Value> stdlib_get(const Array& params) {
if (params.size() != 2) return ERROR(ArgumentCountMismatch);
Value collection = TRY(params.get(0));
@ -93,6 +99,7 @@ static StdlibFunctionEntry function_entries[] = {
STDLIB_FUNCTION(assert, Assert),
STDLIB_FUNCTION(dict, Dict),
STDLIB_FUNCTION(list, List),
STDLIB_FUNCTION(array, Array),
STDLIB_FUNCTION(get, Get),
[(uint64_t)StdlibFunctionId::Max] = {0, StdlibFunctionId::Max,
stdlib_unknown},

View file

@ -12,6 +12,7 @@ enum class StdlibFunctionId : uint64_t {
Assert,
Dict,
List,
Array,
Get,
Max,
};

View file

@ -31,7 +31,7 @@ Result<void> run_repl() {
Dict globals = TRY(Dict::create());
while (true) {
auto src = TRY(read_line("vli> "));
auto src = TRY(read_line("valeri> "));
auto parsed = TRY(read_multiple(src));
auto compiled = TRY(compile(parsed));
Module& mod = *compiled.to<Module>();