From d35442f0920ccd53fe823cc44f3522358cd89766 Mon Sep 17 00:00:00 2001 From: servostar Date: Sun, 21 Jul 2024 11:49:38 +0200 Subject: [PATCH] added echo test --- CMakeLists.txt | 2 +- lib/src/io.gsc | 4 ++++ lib/src/mem.gsc | 6 +++--- lib/src/mem/api.h | 6 +++--- lib/src/mem/impl.c | 12 ++++++------ src/llvm/llvm-ir/types.c | 2 +- tests/stdlib/build.toml | 15 +++++++++++++++ tests/stdlib/src/echo.gsc | 27 +++++++++++++++++++++++++++ 8 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 tests/stdlib/build.toml create mode 100644 tests/stdlib/src/echo.gsc diff --git a/CMakeLists.txt b/CMakeLists.txt index f5eabfe..ee5a34a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ cmake_minimum_required(VERSION 3.15...3.25) # └─ test.c project(gemstone - VERSION 0.1.0 + VERSION 0.2.6 DESCRIPTION "programming language compiler" LANGUAGES C) diff --git a/lib/src/io.gsc b/lib/src/io.gsc index c729644..1adb655 100644 --- a/lib/src/io.gsc +++ b/lib/src/io.gsc @@ -14,6 +14,10 @@ include "def.gsc" # which can lead to errors and undefined behavior type ptr: handle +# NULL handle representing an invalid handle on +# all platforms +handle: nullHandle = 0 as handle + # Returns a handle to this processes standard input I/O handle # -- Implementation note # On Linux this will return 0 as is it convention under UNIX (see: https://www.man7.org/linux/man-pages/man3/stdin.3.html) diff --git a/lib/src/mem.gsc b/lib/src/mem.gsc index b9df686..0060323 100644 --- a/lib/src/mem.gsc +++ b/lib/src/mem.gsc @@ -10,14 +10,14 @@ include "def.gsc" # Allocate `len` bytes of heap memory # Returns a pointer to the memory as `ptr` -fun heap_alloc(in u32: len)(out ref u8: ptr) +fun heapAlloc(in u32: len)(out ref u8: ptr) # Rellocate `len` bytes of heap memory # Returns a pointer to the memory as `ptr` -fun heap_realloc(in u32: len, in out ref u8: ptr) +fun heapRealloc(in u32: len, in out ref u8: ptr) # Free a block of memory -fun heap_free(in ref u8: ptr) +fun heapFree(in ref u8: ptr) # Copy `len` bytes from `dst` into `src` fun copy(in ref u8: dst, in ref u8: src, in u32: len) diff --git a/lib/src/mem/api.h b/lib/src/mem/api.h index 8ca69c0..00f1577 100644 --- a/lib/src/mem/api.h +++ b/lib/src/mem/api.h @@ -4,11 +4,11 @@ #include -void heap_alloc(u32 len, u8** ptr); +void heapAlloc(u32 len, u8** ptr); -void heap_realloc(u32 len, u8** ptr); +void heapRealloc(u32 len, u8** ptr); -void heap_free(u8* ptr); +void heapFree(u8* ptr); void copy(u8* dst, u8* src, u32 len); diff --git a/lib/src/mem/impl.c b/lib/src/mem/impl.c index 9d384a8..aad36d0 100644 --- a/lib/src/mem/impl.c +++ b/lib/src/mem/impl.c @@ -8,17 +8,17 @@ #define HEAP_API_GLOBAL_FLAGS HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS -void heap_alloc(u32 len, u8** ptr) { +void heapAlloc(u32 len, u8** ptr) { HANDLE heap = GetProcessHeap(); *ptr = HeapAlloc(heap, HEAP_API_GLOBAL_FLAGS, len); } -void heap_realloc(u32 len, u8** ptr) { +void heapRealloc(u32 len, u8** ptr) { HANDLE heap = GetProcessHeap(); *ptr = HeapReAlloc(heap, HEAP_API_GLOBAL_FLAGS, *ptr, len); } -void heap_free(u8* ptr) { +void heapFree(u8* ptr) { HANDLE heap = GetProcessHeap(); HeapFree(heap, ptr); } @@ -27,15 +27,15 @@ void heap_free(u8* ptr) { #include -void heap_alloc(u32 len, u8** ptr) { +void heapAlloc(u32 len, u8** ptr) { *ptr = malloc(len); } -void heap_realloc(u32 len, u8** ptr) { +void heapRealloc(u32 len, u8** ptr) { *ptr = realloc(*ptr, len); } -void heap_free(u8* ptr) { +void heapFree(u8* ptr) { free(ptr); } diff --git a/src/llvm/llvm-ir/types.c b/src/llvm/llvm-ir/types.c index c64ed6e..e791bc5 100644 --- a/src/llvm/llvm-ir/types.c +++ b/src/llvm/llvm-ir/types.c @@ -42,7 +42,7 @@ static BackendError get_const_composite_value(CompositeType composite, BackendError impl_reference_const(LLVMBackendCompileUnit* unit, TypeValue* value, LLVMValueRef* llvm_value) { BackendError err = SUCCESS; - if (value->type->kind == TypeKindReference && compareTypes(value->type, (Type*) &StringLiteralType)) { + if (compareTypes(value->type, (Type*) &StringLiteralType)) { // is string literal LLVMValueRef string_value = LLVMConstString(value->value, strlen(value->value), false); diff --git a/tests/stdlib/build.toml b/tests/stdlib/build.toml new file mode 100644 index 0000000..456b20e --- /dev/null +++ b/tests/stdlib/build.toml @@ -0,0 +1,15 @@ +[project] +name = "Stdlib tests" +version = "0.1.0" +description = "Test applications for the GSC standard library." +license = "GPL-2.0" +authors = [ "Sven Vogel " ] + +[target.echo] +link-paths = [ "../../bin/std" ] +import-paths = [ "../../lib/src" ] +root = "src/echo.gsc" +mode = "application" +output = "bin" +archive = "archive" +print_ir = true diff --git a/tests/stdlib/src/echo.gsc b/tests/stdlib/src/echo.gsc new file mode 100644 index 0000000..8b0095b --- /dev/null +++ b/tests/stdlib/src/echo.gsc @@ -0,0 +1,27 @@ + +import "std" + +cstr: EOL = "\n" + +fun main(out u32: e) { + + handle: stdin = nullHandle + getStdinHandle(stdin) + + handle: stdout = nullHandle + getStdoutHandle(stdout) + + ref u8: buffer = 0 as ref u8 + heapAlloc(256)(buffer) + + u32: bytesRead = 0 as u32 + readBytes(stdin, buffer, 8)(bytesRead) + + u32: bytesWritten = 0 as u32 + writeBytes(stdout, buffer, bytesRead)(bytesWritten) + writeBytes(stdout, EOL, 1)(bytesWritten) + + heapFree(buffer) + + e = 0 as u32 +}