At this point...
...I'm just relearning C semantics. (And they are garbage, as is widely known.) I don't think there's much point to this (at the moment) because I don't want to relearn C (at the moment) and Nim is available (at the moment.) Really, I'm trying to do away with the entire relationship of C et. al. to the underlying machine. (For instance, Nim gives you a much nicer relationship, without the vast distance that, say, Python imposes.) I should really look at other compiled languages, like Ocaml or Julia.
This commit is contained in:
parent
ae864909ee
commit
8cb04fc72d
Binary file not shown.
|
|
@ -1,31 +1,57 @@
|
||||||
#include <gc.h>
|
#include <gc.h>
|
||||||
#include <gmp.h>
|
#include <gmp.h>
|
||||||
|
|
||||||
|
|
||||||
|
enum JoyTypeType {
|
||||||
|
joySymbol,
|
||||||
|
joyTrue,
|
||||||
|
joyFalse,
|
||||||
|
joyInt,
|
||||||
|
joyList
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct list_node* JoyList;
|
||||||
|
|
||||||
|
typedef struct JoyType {
|
||||||
|
enum JoyTypeType kind;
|
||||||
|
union {
|
||||||
|
int b; // bool
|
||||||
|
mpz_t i;
|
||||||
|
JoyList el;
|
||||||
|
};
|
||||||
|
} name ;
|
||||||
|
|
||||||
|
typedef struct list_node {
|
||||||
|
struct JoyType head;
|
||||||
|
struct list_node* tail;
|
||||||
|
} *JoyList;
|
||||||
|
|
||||||
|
|
||||||
// Example S-exprs
|
// Example S-exprs
|
||||||
// https://www.hboehm.info/gc/04tutorial.pdf
|
// https://www.hboehm.info/gc/04tutorial.pdf
|
||||||
|
/**/
|
||||||
typedef union se
|
/*typedef union se*/
|
||||||
{
|
/*{*/
|
||||||
struct cons * cp;
|
/* struct cons * cp;*/
|
||||||
mpz_t i;
|
/* mpz_t i;*/
|
||||||
} sexpr;
|
/*} sexpr;*/
|
||||||
|
/**/
|
||||||
struct cons
|
/*struct cons*/
|
||||||
{
|
/*{*/
|
||||||
union se head;
|
/* union se head;*/
|
||||||
union se tail;
|
/* union se tail;*/
|
||||||
};
|
/*};*/
|
||||||
|
/**/
|
||||||
#define car(s) (s).cp->head
|
/*#define car(s) (s).cp->head*/
|
||||||
#define cdr(s) (s).cp->tail
|
/*#define cdr(s) (s).cp->tail*/
|
||||||
#define from_i(z) ({sexpr tmp; tmp.i=z; tmp;})
|
/*#define from_i(z) ({sexpr tmp; tmp.i=z; tmp;})*/
|
||||||
#define to_i(s) (s).i
|
/*#define to_i(s) (s).i*/
|
||||||
|
/**/
|
||||||
sexpr cons(sexpr a, sexpr b) {
|
/*sexpr cons(sexpr a, sexpr b) {*/
|
||||||
sexpr tmp = {GC_MALLOC(sizeof(struct cons))};
|
/* sexpr tmp = {GC_MALLOC(sizeof(struct cons))};*/
|
||||||
car(tmp) = a; cdr(tmp) = b;
|
/* car(tmp) = a; cdr(tmp) = b;*/
|
||||||
return (tmp);
|
/* return (tmp);*/
|
||||||
};
|
/*};*/
|
||||||
|
|
||||||
void* reallocate_function (void *ptr, size_t old_size, size_t new_size) {
|
void* reallocate_function (void *ptr, size_t old_size, size_t new_size) {
|
||||||
return GC_realloc(ptr, new_size);
|
return GC_realloc(ptr, new_size);
|
||||||
|
|
@ -40,12 +66,12 @@ void my_callback(GC_PTR void_obj, GC_PTR void_environment) {
|
||||||
mpz_clear(*obj);
|
mpz_clear(*obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
sexpr new_int(void) {
|
/*sexpr new_int(void) {*/
|
||||||
sexpr tmp = {GC_MALLOC(sizeof(struct cons))};
|
/* sexpr node = {GC_MALLOC(sizeof(struct cons))};*/
|
||||||
mpz_init(tmp.i);
|
/* mpz_init(node.i);*/
|
||||||
GC_register_finalizer(tmp.i, my_callback, NULL, NULL, NULL);
|
/* GC_register_finalizer(node.i, my_callback, NULL, NULL, NULL);*/
|
||||||
return (tmp);
|
/* return (node);*/
|
||||||
}
|
/*}*/
|
||||||
|
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
|
|
@ -60,10 +86,10 @@ int main(void)
|
||||||
mpz_init_set_str (pi, "25d0c79fe247f31777d922627a74624", 16);
|
mpz_init_set_str (pi, "25d0c79fe247f31777d922627a74624", 16);
|
||||||
gmp_printf ("%Zd = %Zx\n", pi, pi);
|
gmp_printf ("%Zd = %Zx\n", pi, pi);
|
||||||
GC_register_finalizer(pi, my_callback, NULL, NULL, NULL);
|
GC_register_finalizer(pi, my_callback, NULL, NULL, NULL);
|
||||||
|
|
||||||
sexpr i = new_int();
|
/*sexpr i = new_int();*/
|
||||||
mpz_add(i.i, pi, pi);
|
/*mpz_add(i.i, pi, pi);*/
|
||||||
gmp_printf ("%Zd\n", i.i);
|
/*gmp_printf ("%Zd\n", i.i);*/
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
//return to_i(car(cons(from_i(0),from_i(1))));
|
//return to_i(car(cons(from_i(0),from_i(1))));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue