spl

a Simple Programming Language
Log | Files | Refs

commit 6374c152817207b11bfcdddd8c1b8e816eee8553
parent 21a798437a371a9e65277e53be41f82a71b79716
Author: thing1 <thing1@seacrossedlovers.xyz>
Date:   Thu, 18 Dec 2025 22:46:36 +0000

started work on the runtime

Diffstat:
M.gitignore | 2++
A.maps.h.swp | 0
ATORL.md | 9+++++++++
Mparse.c | 23++++++++++++++++++++---
Art/Makefile | 16++++++++++++++++
Art/array.c | 46++++++++++++++++++++++++++++++++++++++++++++++
Art/array.h | 17+++++++++++++++++
Art/arraytest.c | 13+++++++++++++
Mtest.spl | 5+++++
9 files changed, 128 insertions(+), 3 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,2 +1,4 @@ *.o spl +rt/*.o +rt/*.test diff --git a/.maps.h.swp b/.maps.h.swp Binary files differ. diff --git a/TORL.md b/TORL.md @@ -0,0 +1,9 @@ +- [x] scoped vars +- [ ] arrays + - [ ] rt lib with dynamic array stuff + - [ ] ability to cast to an array of bytes +- [ ] strings (arrays of i8s) + - [ ] string lits in lexer + - [ ] making into arrays +- [ ] while loops + - [ ] '!' statement diff --git a/parse.c b/parse.c @@ -12,6 +12,7 @@ #define SAME(s1, s2) (strcmp(s1, s2) == 0) #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) +map **scope = &vars; int haserrored = 0; char *builtintypes[] = { @@ -276,7 +277,7 @@ parsesimple(tok *start, tok *end, tok *lowest, mctx *ctx) { e->type = mkbasictype("f64", ctx); break; case NAME: - if (!(v = (var *)lookupmap(vars, lowest->name))) { + if (!(v = (var *)lookupmap(*scope, lowest->name))) { parserErrtok("var is not declared", *start); return NULL; } @@ -424,7 +425,7 @@ parseassign(lex *l, tok name, mctx *ctx) { } shortdec: - if (lookupmap(vars, name.name)) { + if (lookupmap(*scope, name.name)) { parserErr("redeclaration of var", *l); RESTORE(l); return NULL; @@ -446,7 +447,7 @@ shortdec: v = alloczctx(ctx, sizeof(var)); v->name = le->name; v->type = le->type; - vars = addmap(vars, v, le->name, ctx); + *scope = addmap(*scope, v, le->name, ctx); return le; } @@ -572,11 +573,23 @@ parsereassign(lex *l, tok t, mctx *ctx) { return le; } +map * +mknewscope(map *prevscope, mctx *ctx) { + map *new = NULL; + if (!prevscope) + return NULL; + new = addmap(new, prevscope->data, prevscope->id, ctx); + if (prevscope->next) + new->next = mknewscope(prevscope->next, ctx); + return new; +} + lexpr * parselexpr(lex *l, mctx *ctx) { SAVE(l); lexpr *le; fn *f; + map *newscope; tok t = next(l), *ts = NULL; int tsc = 0; @@ -603,7 +616,11 @@ parselexpr(lex *l, mctx *ctx) { le = parsereassign(l, t, ctx); break; case FUNC: + newscope = mknewscope(vars, ctx); + scope = &newscope; + le = parsefunc(l, ctx); /* we can drop the FUNC token, as it doesnt hold any info */ + scope = &vars; if (!le) { RESTORE(l); haserrored = 1; diff --git a/rt/Makefile b/rt/Makefile @@ -0,0 +1,16 @@ +CFLAGS = -ggdb -pedantic #-fsanitize=address +CPPFLAGS = -D_POSIX_C_SOURCE -D_XOPEN_SOURCE=500 +LDFLAGS = #-fsanitize=address + +SRC = array.c +OBJ = ${SRC:.c=.o} + +*.test: ${OBJ} + cc arraytest.c array.o -o array.test + +.c.o: + ${CC} -c ${CFLAGS} $< + +clean: + rm -rf ${OBJ} *.test + diff --git a/rt/array.c b/rt/array.c @@ -0,0 +1,46 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <signal.h> +#include "array.h" + +array * +newarray(int cap, int size) { + cap = (!cap) ? 1 : cap; + array *a = malloc(sizeof(array)); + a->arr = malloc(cap); + a->cap = cap; + a->len = 0; + a->size = size; + return a; +} + +void * +indexarray(array *a, int index) { + if (a->cap < index * a->size) + kill(0, SIGSEGV); + return a->arr + (index * a->size); +} + +void +resizearray(array *a, int new) { + if (a->cap > new) + kill(0, SIGSEGV); + a->cap = new; + a->arr = realloc(a->arr, new); +} + +void +appendarray(array *a, void *item) { + while (a->len + a->size > a->cap) + resizearray(a, a->cap * 2); + memcpy(a->arr + a->len, item, a->size); +} + +void +destroyarray(array *a) { + if (a->arr) + free(a->arr); + free(a); +} diff --git a/rt/array.h b/rt/array.h @@ -0,0 +1,17 @@ +#ifndef __ARRAY_H_ +#define __ARRAY_H_ + +#include <stdint.h> + +typedef struct array { + uint64_t cap, len; /* cap in bytes, len in bytes */ + uint32_t size; /* size of elements in bytes */ + void *arr; +} array; + +array *newarray(int cap, int size); +void *indexarray(array *a, int index); +void resizearray(array *a, int new); +void appendarray(array *a, void *item); +void destroyarray(array *a); +#endif diff --git a/rt/arraytest.c b/rt/arraytest.c @@ -0,0 +1,13 @@ +#include <stdlib.h> +#include <stdio.h> +#include "array.h" + +int +main() { + array *arr = newarray(0, sizeof(int)); + int item = 69; + appendarray(arr, &item); + int new = *(int *)indexarray(arr, 0); + printf("%d\n", new); + destroyarray(arr); +} diff --git a/test.spl b/test.spl @@ -1,3 +1,8 @@ +func foo() i32 { + a i32 = 21; + return a; +} + func main() i32 { a i32 = i16(400); }