lli

A small emulated asm like lang
Log | Files | Refs

commit 6434dc17f5cdc917b6a4b25592dc8c2a4796e517
Author: thing1 <l.standen@posteo.com>
Date:   Tue, 30 Sep 2025 10:36:04 +0100

init commit

Diffstat:
A.gitignore | 2++
AMakefile | 11+++++++++++
Alli.c | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Alli.h | 48++++++++++++++++++++++++++++++++++++++++++++++++
Alliasm.c | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aspec.md | 11+++++++++++
Atest.ll | 4++++
7 files changed, 202 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +lli +lliasm diff --git a/Makefile b/Makefile @@ -0,0 +1,11 @@ +CFLAGS=-ggdb + +all: lli lliasm + +lli: lli.c + cc lli.c -o lli ${CFLAGS} +lliasm: lliasm.c + cc lliasm.c -o lliasm ${CFLAGS} + +clean: + rm -rf lli lliasm diff --git a/lli.c b/lli.c @@ -0,0 +1,65 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "lli.h" + +void +push(stack *s, value *v) { + if (((s->sptr + sizeof(value)) - s->arr) > s->cap) + errormsg("Stack overflow"); + + s->sptr = v; + s->sptr += sizeof(value); +} + +value * +pop(stack *s) { + if (s->sptr == s->arr) + errormsg("Stack underflow"); + + s->sptr -= sizeof(value); + return s->sptr; +} + +value * +peak(stack *s) { + if (s->sptr == s->arr) + errormsg("Stack underflow"); + + return s->sptr; +} + +inline int +depth(stack *s) { + return (s->sptr - s->arr) / sizeof(value); +} + +rlestr * +mkstr(char *s) { + rlestr *str = malloc(sizeof(rlestr)); + str->len = strlen(s); + memcpy(str->data, s, str->len); + return str; +} + +value * +mkstrv(char *s) { + value *v = malloc(sizeof(value)); + v->type = STR; + v->sval = mkstr(s); + return v; +} + +value * +mkintv(int i) { + value *v = malloc(sizeof(value)); + v->type = INT; + v->ival = i; + return v; +} + +int +main() { + +} diff --git a/lli.h b/lli.h @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> + +typedef enum ttype { + ILIT, + SLIT, + WRITE, + DEL, + COPY, + REPLACE, +} ttype; + +typedef struct tval { + ttype type; + char *val; +} tval; + +typedef enum dtype { + INT, + STR, +} dtype; + +typedef struct rlestr { + size_t len; + char *data; +} rlestr; + +typedef struct value { + dtype type; + bool prot; + union { + int ival; + rlestr *sval; + }; +} value; + +typedef struct stack { + int cap; /* in bytes */ + value *arr, *sptr; +} stack; + +void +errormsg(const char *msg) { + fprintf(stderr, "error: %s\n", msg); + exit(1); +} diff --git a/lliasm.c b/lliasm.c @@ -0,0 +1,61 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <ctype.h> + +#include "lli.h" + +char * +drainfile(FILE *f) { + char *contents; + + fseek(f, 0, SEEK_END); + int len = ftell(f); + rewind(f); + + contents = malloc(len + 1); + contents[len] = 0; + + fread(contents, 1, len, f); + + return contents; +} + +char * +readword(char **in) { + char *input; + if (!in) input = NULL; + else input = *in; + + char *tok = strtok(input, " \n\0"); + while (tok && *tok && isblank(*tok)) tok++; + return tok; +} + +tavl * +tokenize(char *word) { + tval *val = malloc(sizeof(tval)); + if (isdigit(word)) { + tval->type = ILIT; + tval->val = word; + } + +} + +int +main(int argc, char **argv) { + FILE *in; + if (argc < 2) + in = stdin; + else { + in = fopen(argv[1], "r"); + if (!in) + errormsg("couldn't open file"); + } + char *input = drainfile(in); + + printf("%s\n", readword(&input)); + printf("%s\n", readword(NULL)); + + free(input); +} diff --git a/spec.md b/spec.md @@ -0,0 +1,11 @@ +SPEC +==== + +| Keyword | Stack pops | Stack pushes | Function | +|---------|------------|--------------|--------------------------------------------------------| +| write | ANY | | writes the top value of stack to stdout | +| del | ANY | | pops the top value and does nothing | +| copy | INT | ANY | copys the value from val1 deep in the stack to the top | +| replace | INT, ANY | ANY | swaps the value from val1 deep in the stack with val2 | + +- labels start with @ diff --git a/test.ll b/test.ll @@ -0,0 +1,4 @@ + + +hello + world