added some ideas to ideas file, added garbage collector dependency

This commit is contained in:
Rachel Lambda Samuelsson 2021-12-13 15:47:21 +01:00
parent 43f4a68565
commit 8739857dc4
7 changed files with 91 additions and 26 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.o
secd
secd_debug

View File

@ -1,11 +1,19 @@
.POSIX:
.PHONY: all clean
.PHONY: all debug run clean
DEPS=src/main.o src/util.o
LIBS=-lgc
DBGFLAGS=-Og -g
DBGBIN=secd_debug
all: secd
secd: $(DEPS)
$(CC) $(CFLAGS) $(DEPS) -o secd
$(CC) $(LIBS) $(CFLAGS) $(DEPS) -o secd
$(DBGBIN): $(DEPS)
$(CC) $(LIBS) $(DBGFLAGS) -DENABLE_DBG_H -o secd_debug
debug: $(DBGBIN)
run: secd
@./secd

View File

@ -1,3 +1,7 @@
# SECD
This is a (wip) implementation of a SECD machine written in C. For more information on what a SECD machine is, and what the goals of this project is, read [my blogpost on secd machines](https://depsterr.com/blog/2021/secd1.html).
## DEPENDS
* Bohem-Demer-Weiser garbage collector [web](https://www.hboehm.info/gc/) [git](https://github.com/ivmai/bdwgc)

49
doc/ideas.md Normal file
View File

@ -0,0 +1,49 @@
# Goals
* Be an efficent compilation target for typed lambda calculi
* Be substantially easier to translate to than machine code.
* Run cross-platform on as many operating systems and architectures as possible. (bare minimum 32-bit).
* Allow for code completely independent of the host machine.
* Leave memory managment out of the question for the compiler writer.
* Provide significant IO functionality.
* Have methods for representing algebraic data types built into the machine.
* Provide efficient native methods for manipulating commonly used types
* Strings (don't represent as a list of characters, god)
* Characters
* Integers
* Floating point numbers
* Raw bytes
* Lists (cons linked list), efficient map and foldr builtin would be really nice
* Arrays (constant lookup continous memory)
# Non-Goals
* Safely error out or leave catchable exceptions when instruction is called with wrongful operands.
* Dynamic linking, creating object files with polymorphic types.
* Lazy evaluation.
# Nicities (plausible)
* Provide a compiler from a simply typed lambda calculus, coupled with a small standard library which would
confirm type safety of all used instructions. Maybe go even further? System F?
* Maybe some vector operations? You could reasonably operate on chunks of the stack.
# Nicities (dreaming)
* Provide more complete IO functionality, such as networking, etc. (this is not likely to happen)
* Provide some sort of FFI (this is not likely to happen)
* It's possible one might be able to JIT compile some stuff?
# Ideas

View File

@ -1,5 +1,23 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <gc.h>
#include "dbg.h"
#include "util.h"
int main(int argc, char** argv) {
GC_INIT();
if (argc < 2) {
die("Not enough args");
}
size_t size;
uint8_t* buf;
buf = readFile(argv[1], &size);
fwrite(buf, 1, size, stdout);
}

View File

@ -13,7 +13,7 @@
// print message to stderr and then exit
void die(const char* s) {
fprintf(stderr, "FATAL ERROR: %s.\n", s);
fprintf(stderr, "ERROR: %s.\n", s);
exit(1);
}
@ -23,27 +23,6 @@ void diePerror(const char* s) {
exit(1);
}
// malloc which dies on failure
void* xmalloc(size_t bytes) {
void* p = malloc(bytes);
GUARD(p);
return p;
}
// realloc which dies on failure
void* xrealloc(void* p, size_t bytes) {
p = realloc(p, bytes);
GUARD(p);
return p;
}
// free which is garantueed null pointer safe
void xfree(void* p) {
if (p) {
free(p);
}
}
// read the file at the given path and return a byte buffer
// pointing to the contents of the file, the length of which
// is stored in rsize
@ -75,7 +54,8 @@ uint8_t* readFile(char* path, size_t* rsize) {
rewind(fp);
buf = xmalloc(size);
buf = malloc(size);
GUARD(buf);
GUARD(fread(buf, 1, size, fp) == size);
@ -92,6 +72,6 @@ void freeFile(uint8_t* buf, size_t size) {
#ifdef POSIX
SGUARD(munmap(buf, size));
#else
xfree(buf);
free(buf);
#endif
}

View File

@ -12,4 +12,9 @@
#define WINDOWS
#endif
void die(const char* s);
void diePerror(const char* s);
uint8_t* readFile(char* path, size_t* size);
void freeFile(uint8_t* buf, size_t size);
#endif