Skip to content

Commit

Permalink
[ pack, wip ] code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-hoeck committed Dec 1, 2023
1 parent a92c860 commit 2ce72af
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ build/
lib/
support/idris2-uv.o
support/libuv-idris
codegen/error_gen
3 changes: 3 additions & 0 deletions cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

git restore src/System/UV/Data/Error.idr
9 changes: 9 additions & 0 deletions codegen/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
LDFLAGS = -luv
CPPFLAGS =

CC_VERSION = $(shell $(CC) --version)


.PHONY: error_gen
error_gen:
$(CC) $(LDFLAGS) -o error_gen error_gen.c
39 changes: 39 additions & 0 deletions codegen/error_gen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <stdio.h>
#include <uv.h>

int main(void) {
printf("module System.UV.Data.Error\n\n");

printf("import Derive.Prelude\n\n");

printf("%%language ElabReflection\n");
printf("%%default total\n\n");

printf("public export\n");
printf("data UVError : Type where\n");
#define XX(code, _) printf(" %s : UVError\n", uv_err_name(UV_ ## code));
UV_ERRNO_MAP(XX)
#undef XX

printf("\n%%runElab derive \"UVError\" [Show,Eq]\n");

printf("\nexport\n");
printf("toCode : UVError -> Int32\n");
#define XX(code, _) printf("toCode %s = (%d)\n", uv_err_name(UV_ ## code), UV_ ## code);
UV_ERRNO_MAP(XX)
#undef XX

printf("\nexport\n");
printf("fromCode : Int32 -> UVError\n");
#define XX(code, _) printf("fromCode (%d) = %s\n", UV_ ## code, uv_err_name(UV_ ## code));
UV_ERRNO_MAP(XX)
#undef XX
printf("fromCode _ = UNKNOWN\n");

printf("\n%%foreign \"C:uv_strerror,libuv-idris\"\n");
printf("uv_strerror : Int32 -> String\n");

printf("\nexport %%inline\n");
printf("errorMsg : UVError -> String\n");
printf("errorMsg = uv_strerror . toCode\n");
}
4 changes: 4 additions & 0 deletions gencode.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

make -C codegen error_gen
codegen/error_gen > src/System/UV/Data/Error.idr
18 changes: 18 additions & 0 deletions src/System/UV/Data/Error.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module System.UV.Data.Error

import Derive.Prelude

%language ElabReflection
%default total

public export
data UVError : Type where
EOF : UVError

%runElab derive "UVError" [Show,Eq]

export
toCode : UVError -> Int32

export
fromCode : Int32 -> UVError
4 changes: 3 additions & 1 deletion uv.ipkg
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ modules = System.UV

sourcedir = "src"

prebuild = "make -C support"
prebuild = "make -C support; ./gencode.sh"

postbuild = "./cleanup.sh"

preinstall = "make -C support install"

0 comments on commit 2ce72af

Please sign in to comment.