added echo test

This commit is contained in:
Sven Vogel 2024-07-21 11:49:38 +02:00
parent 20b6d269d8
commit d35442f092
8 changed files with 60 additions and 14 deletions

View File

@ -17,7 +17,7 @@ cmake_minimum_required(VERSION 3.15...3.25)
# test.c # test.c
project(gemstone project(gemstone
VERSION 0.1.0 VERSION 0.2.6
DESCRIPTION "programming language compiler" DESCRIPTION "programming language compiler"
LANGUAGES C) LANGUAGES C)

View File

@ -14,6 +14,10 @@ include "def.gsc"
# which can lead to errors and undefined behavior # which can lead to errors and undefined behavior
type ptr: handle 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 # Returns a handle to this processes standard input I/O handle
# -- Implementation note # -- 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) # On Linux this will return 0 as is it convention under UNIX (see: https://www.man7.org/linux/man-pages/man3/stdin.3.html)

View File

@ -10,14 +10,14 @@ include "def.gsc"
# Allocate `len` bytes of heap memory # Allocate `len` bytes of heap memory
# Returns a pointer to the memory as `ptr` # 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 # Rellocate `len` bytes of heap memory
# Returns a pointer to the memory as `ptr` # 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 # 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` # Copy `len` bytes from `dst` into `src`
fun copy(in ref u8: dst, in ref u8: src, in u32: len) fun copy(in ref u8: dst, in ref u8: src, in u32: len)

View File

@ -4,11 +4,11 @@
#include <def/api.h> #include <def/api.h>
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); void copy(u8* dst, u8* src, u32 len);

View File

@ -8,17 +8,17 @@
#define HEAP_API_GLOBAL_FLAGS HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS #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(); HANDLE heap = GetProcessHeap();
*ptr = HeapAlloc(heap, HEAP_API_GLOBAL_FLAGS, len); *ptr = HeapAlloc(heap, HEAP_API_GLOBAL_FLAGS, len);
} }
void heap_realloc(u32 len, u8** ptr) { void heapRealloc(u32 len, u8** ptr) {
HANDLE heap = GetProcessHeap(); HANDLE heap = GetProcessHeap();
*ptr = HeapReAlloc(heap, HEAP_API_GLOBAL_FLAGS, *ptr, len); *ptr = HeapReAlloc(heap, HEAP_API_GLOBAL_FLAGS, *ptr, len);
} }
void heap_free(u8* ptr) { void heapFree(u8* ptr) {
HANDLE heap = GetProcessHeap(); HANDLE heap = GetProcessHeap();
HeapFree(heap, ptr); HeapFree(heap, ptr);
} }
@ -27,15 +27,15 @@ void heap_free(u8* ptr) {
#include <malloc.h> #include <malloc.h>
void heap_alloc(u32 len, u8** ptr) { void heapAlloc(u32 len, u8** ptr) {
*ptr = malloc(len); *ptr = malloc(len);
} }
void heap_realloc(u32 len, u8** ptr) { void heapRealloc(u32 len, u8** ptr) {
*ptr = realloc(*ptr, len); *ptr = realloc(*ptr, len);
} }
void heap_free(u8* ptr) { void heapFree(u8* ptr) {
free(ptr); free(ptr);
} }

View File

@ -42,7 +42,7 @@ static BackendError get_const_composite_value(CompositeType composite,
BackendError impl_reference_const(LLVMBackendCompileUnit* unit, TypeValue* value, LLVMValueRef* llvm_value) { BackendError impl_reference_const(LLVMBackendCompileUnit* unit, TypeValue* value, LLVMValueRef* llvm_value) {
BackendError err = SUCCESS; BackendError err = SUCCESS;
if (value->type->kind == TypeKindReference && compareTypes(value->type, (Type*) &StringLiteralType)) { if (compareTypes(value->type, (Type*) &StringLiteralType)) {
// is string literal // is string literal
LLVMValueRef string_value = LLVMConstString(value->value, strlen(value->value), false); LLVMValueRef string_value = LLVMConstString(value->value, strlen(value->value), false);

15
tests/stdlib/build.toml Normal file
View File

@ -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 <sven.vogel123@web.de>" ]
[target.echo]
link-paths = [ "../../bin/std" ]
import-paths = [ "../../lib/src" ]
root = "src/echo.gsc"
mode = "application"
output = "bin"
archive = "archive"
print_ir = true

27
tests/stdlib/src/echo.gsc Normal file
View File

@ -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
}