lli

A small emulated asm like lang
Log | Files | Refs

commit 3bdadaa9da7500a076605dcc21907ada32491ec0
parent b537a462e3ed70b59020246f5aa180ea1075fea8
Author: thing1 <thing1@seacrossedlovers.xyz>
Date:   Wed,  1 Oct 2025 12:52:55 +0100

fixed up the asmbler

Diffstat:
Mlli.c | 24------------------------
Mlli.h | 21++++++++-------------
Mlliasm.c | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
Mtest.ll | 5+----
Atest.rom | 0
5 files changed, 101 insertions(+), 48 deletions(-)

diff --git a/lli.c b/lli.c @@ -35,30 +35,6 @@ 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 @@ -2,14 +2,18 @@ #include <stdlib.h> #include <string.h> #include <stdbool.h> +#include <stdint.h> typedef enum ttype { ILIT, SLIT, - WRITE, + LABEL, + PAD, DEL, - COPY, - REPLACE, + DUP, + STORE, + LOAD, + JMP, } ttype; typedef struct tval { @@ -22,18 +26,9 @@ typedef enum dtype { STR, } dtype; -typedef struct rlestr { - size_t len; - char *data; -} rlestr; - typedef struct value { - dtype type; bool prot; - union { - int ival; - rlestr *sval; - }; + uint8_t val; } value; typedef struct stack { diff --git a/lliasm.c b/lliasm.c @@ -2,9 +2,20 @@ #include <stdio.h> #include <string.h> #include <ctype.h> +#include <unistd.h> #include "lli.h" +struct dict { + char *label; + int address; +} label_table[1024] = { +}; + +int label_count = 0; + +#define SAME(s1, s2) (strcmp(s1, s2) == 0) + char * drainfile(FILE *f) { char *contents; @@ -22,10 +33,10 @@ drainfile(FILE *f) { } char * -readword(char **in) { +readword(char *in) { char *input; if (!in) input = NULL; - else input = *in; + else input = in; char *tok = strtok(input, " \n\0"); while (tok && *tok && isblank(*tok)) tok++; @@ -35,16 +46,75 @@ readword(char **in) { tval * tokenize(char *word) { tval *val = malloc(sizeof(tval)); - if (isdigit(word)) { + if (isdigit(*word)) { val->type = ILIT; val->val = word; + return val; + } + else if (*word == '\"') { + val->type = SLIT; + val->val = word + 1; + return val; + } + else if (*word == '@') { + val->type = LABEL; + val->val = word + 1; + return val; } - + else if (*word == '|') { + val->type = PAD; + val->val = word + 1; + return val; + } + + + else if (SAME(word, "del")) + val->type = DEL; + else if (SAME(word, "dup")) + val->type = DUP; + else if (SAME(word, "load")) + val->type = LOAD; + else if (SAME(word, "store")) + val->type = STORE; + else if (SAME(word, "jmp")) + val->type = JMP; + val->val = word; + + return val; +} + +int +compile(int f, tval *val, int bytesdeep) { + int ilit, ZERO = 0; + struct dict label = {val->val, bytesdeep}; + switch (val->type) { + case ILIT: + ilit = atoi(val->val); + bytesdeep += write(f, &ilit, sizeof(int)); + break; + case SLIT: + bytesdeep += write(f, val->val, strlen(val->val)); + break; + case LABEL: + memcpy(&label_table[label_count], &label, sizeof(struct dict)); + label_count++; + break; + case PAD: + while (bytesdeep != atoi(val->val)) + bytesdeep += write(f, &ZERO, 1); + break; + default: + ilit = (int)val->type; + bytesdeep += write(f, &ilit, sizeof(int)); + break; + } + + return bytesdeep; } int main(int argc, char **argv) { - FILE *in; + FILE *in, *out; if (argc < 2) in = stdin; else { @@ -52,10 +122,25 @@ main(int argc, char **argv) { if (!in) errormsg("couldn't open file"); } + + out = fopen("test.rom", "wb"); + int o = fileno(out); char *input = drainfile(in); + char *word; + tval *val; + int bytesdeep; + + word = readword(input); + val = tokenize(word); + bytesdeep = compile(o, val, 0); + + while ((word = readword(NULL))) { + val = tokenize(word); + bytesdeep = compile(o, val, bytesdeep); + } - printf("%s\n", readword(&input)); - printf("%s\n", readword(NULL)); + fclose(out); + close(o); free(input); } diff --git a/test.ll b/test.ll @@ -1,4 +1 @@ - - -hello - world +|100 @str "hello 10 "world 0 diff --git a/test.rom b/test.rom Binary files differ.