From 5f8cf91f8deb791b4c7d95809f0270bf7b19a556 Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Tue, 4 Mar 2025 08:56:33 +0100 Subject: [PATCH] feat: b --- Makefile | 12 ++-- examples/hello_world.flsp | 2 + examples/more_or_less.flsp | 30 ++++++++++ spec.md | 16 ++++++ src/byte_defs.h | 35 ++++++++++-- src/code_defs.h | 4 ++ src/config.h | 6 ++ src/fixed.h | 111 +++++++++++++++++++++++++++++++++++++ src/hash.c | 38 +++++++++++++ src/hash.h | 31 +++++++++++ src/main.c | 16 ++++++ 11 files changed, 291 insertions(+), 10 deletions(-) create mode 100644 examples/hello_world.flsp create mode 100644 examples/more_or_less.flsp create mode 100644 src/code_defs.h create mode 100644 src/config.h create mode 100644 src/fixed.h create mode 100644 src/hash.c create mode 100644 src/hash.h diff --git a/Makefile b/Makefile index 2692ad4..5fb9c72 100644 --- a/Makefile +++ b/Makefile @@ -29,10 +29,10 @@ windef: $(eval LDFLAGS = $(LDFLAGS_WIN)) builddir: - - mkdir $(BUILD_DIR) - - mkdir build-tmp - - rm -rf $(wildcard build-tmp/*) - - mv $(wildcard $(BUILD_DIR)/*.o) build-tmp/ + @- mkdir $(BUILD_DIR) + @- mkdir build-tmp + @- rm -rf $(wildcard build-tmp/*) + @- mv $(wildcard $(BUILD_DIR)/*.o) build-tmp/ build-tmp/%.o : $(SRC_DIR)/%.c ${CC} -c $< -o $@ ${CFLAGS} @@ -41,8 +41,8 @@ build: $(OBJS) ${CC} ${CFLAGS} -o ${OUTPUT} $(OBJS) ${LDFLAGS} builddir2: - mv $(OBJS) $(BUILD_DIR) - rm -rf build-tmp + @mv $(OBJS) $(BUILD_DIR) + @rm -rf build-tmp win: | windef all diff --git a/examples/hello_world.flsp b/examples/hello_world.flsp new file mode 100644 index 0000000..467e9f1 --- /dev/null +++ b/examples/hello_world.flsp @@ -0,0 +1,2 @@ +(import console) +(write "Hello world") diff --git a/examples/more_or_less.flsp b/examples/more_or_less.flsp new file mode 100644 index 0000000..07acaf8 --- /dev/null +++ b/examples/more_or_less.flsp @@ -0,0 +1,30 @@ +(import write utils) + +(let:int won 0) +(let:int number (% (random) 20)) + +(write "Guess a number between 0 and 20") +(newline) +(while (! won) + (write "Your guess ? ") + (let guess (:int (input))) + (if (! (is:null guess)) + (if (> guess number) + (write "The number is smaller") + (newline) + ) + (if (< guess number) + (write "The number is larger") + (newline) + ) + (if (= guess number) + (write "You won !") + (newline) + (won 1) + ) + ) + (if (! won) + (write "Sorry, try again.") + (newline) + ) +) diff --git a/spec.md b/spec.md index c394542..e22b441 100644 --- a/spec.md +++ b/spec.md @@ -53,6 +53,7 @@ In this document : Optional argument - `|` : Or + ## Core types - `null`: @@ -158,11 +159,26 @@ where they may be strings), and of the same type - `(pop )`: Deletes item at pos arg from vec, and returns it +## utils functions (`(import utils)`) + +- `(rand-seed )` : + Sets the seed of the random number generator of random to integer arg + By default is seeded with a random enough value, gotten at runtime + +- `(random)` : + Returns a random integer between -2³¹ and +2³¹-1 + +- `(time)` : + Returns the current number of seconds since 2010, as an integer + ## console functions (`(import console)`) + - `(input)`: Will try to get input from the user, returns it as a string + - `(write )`: Will try to convert var to a string then print it to the console + - `(newline)`: Will print a newline to the console (newline characters aren't escaped with write) diff --git a/src/byte_defs.h b/src/byte_defs.h index 2072f24..9e5965c 100644 --- a/src/byte_defs.h +++ b/src/byte_defs.h @@ -32,15 +32,42 @@ enum OpTypes { OP_iter = 24, OP_not = 25, OP_and = 26, - OP_or = 27 + OP_or = 27, + OP_vpop = 28, + OP_vpsh = 29 +}; + +enum ValTypes { + T_null = 0, + T_int = 1, + T_fix = 2, + T_float = 3, + T_str = 4, + T_fn = 5 +}; + +enum TypeFlags { + F_int = 0x1, + F_fix = 0x2, + F_float = 0x4, + F_str = 0x8, + F_fn = 0x10, + F_const = 0x20, + F_lab2 = 0x40, + F_lab3 = 0x80 }; +typedef struct { + + u16 types[3]; + u16 op_n; + +} OpDef; + typedef struct { u8 type; // OpTypes - u8 o1; - u8 o2; - u8 o3; + u8 o[3]; } PACKED Opcode; diff --git a/src/code_defs.h b/src/code_defs.h new file mode 100644 index 0000000..db8c0fb --- /dev/null +++ b/src/code_defs.h @@ -0,0 +1,4 @@ +#include "types.h" + +#pragma once + diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..ae86007 --- /dev/null +++ b/src/config.h @@ -0,0 +1,6 @@ +#pragma once + +// 0 : None (still features runtime debug) +// 1 : Some checks and prints +// 2 : All checks +#define DEBUG 2 diff --git a/src/fixed.h b/src/fixed.h new file mode 100644 index 0000000..381c10e --- /dev/null +++ b/src/fixed.h @@ -0,0 +1,111 @@ +//--- +// fixed: 16:16 fixed-point arithmetic +//--- +//vient de https://gitea.planet-casio.com/Slyvtt/OutRun/src/branch/master/src +// +#pragma once + +#include + +typedef int32_t fixed_t; +/* Standard arithmetic. */ + +typedef struct { + int32_t x,y; +} V2d; + +typedef struct { + int16_t x,y; +} V2d16; + +static inline fixed_t fmul(fixed_t left, fixed_t right) +{ + /* Generally optimized by the compiler to use dmuls.l and xtrct */ + int64_t p = (int64_t)left * (int64_t)right; + return (int32_t)(p >> 16); +} + +static inline fixed_t fdiv(fixed_t left, fixed_t right) +{ + if(!right) + return 0; + /* Pretty slow */ + int64_t d = (int64_t)left << 16; + return d / right; +} + +#define fix(x) ((int)((x) * 65536)) + +static inline fixed_t fixdouble(double constant) +{ + return (fixed_t)(constant * 65536); +} + +static inline fixed_t fixfloat(float constant) +{ + return (fixed_t)(constant * 65536); +} + +static inline fixed_t fdec(fixed_t f) +{ + return f & 0xffff; +} + +static inline int ffloor(fixed_t f) +{ + return f >> 16; +} + +static inline int fceil(fixed_t f) +{ + return (f + 0xffff) >> 16; +} + +static inline int fround(fixed_t f) +{ + return (f + 0x8000) >> 16; +} + +static inline float f2float(fixed_t f) +{ + return (float)f / 65536; +} + +static inline double f2double(fixed_t f) +{ + return (double)f / 65536; +} + +static inline fixed_t fsq(fixed_t x) +{ + return fmul(x, x); +} + +static inline fixed_t fease(fixed_t x) +{ + if(x <= fix(0.5)) { + return 2 * fmul(x, x); + } + else { + x = fix(1) - x; + return fix(1) - 2 * fmul(x, x); + } +} + +static inline fixed_t fixabs(fixed_t x){ + return x > 0 ? x:-x; +} + +static inline fixed_t fV2d_dotp(V2d u, V2d v){ + return fmul(u.x,v.x) + fmul(u.y,v.y); +} + +static inline uint16_t mul_color(uint16_t color, fixed_t multpl){ + int r = color & 31; + int g = (color>>5) & 63; + int b = (color>>11) & 31; + r *= multpl; + g *= multpl; + b *= multpl; + return (ffloor(b)<<11) | (ffloor(g)<<5) | r; +} diff --git a/src/hash.c b/src/hash.c new file mode 100644 index 0000000..fa2053f --- /dev/null +++ b/src/hash.c @@ -0,0 +1,38 @@ +#include + +#include "types.h" +#include "hash.h" +#include "config.h" + +int init_hashmap(HashMap *map){ + map->curr_len = 64; + map->buffer = malloc(sizeof(MapItem)*64); + map->bit_free = malloc(64/32); + map->load_factor = 0; + + if(!map->buffer || !map->bit_free) + return 1; + return 0; +} + +void free_hashmap(HashMap *map){ + free(map->buffer); + free(map->bit_free); +} + +// ~djb2 +u32 hash(i32 max, char *str){ + if(!str) + return HASH_NULL; + u32 hsh = 5281; + char ch = str[0]; + for(int i = 1; i < 32 && ch; i++){ + hsh = (hsh << 5) ^ ch; + ch = str[i]; + } + hsh %= max; + #if DEBUG == 2 + assert(hsh < HASH_NULL); + #endif + return hsh; +} diff --git a/src/hash.h b/src/hash.h new file mode 100644 index 0000000..292f1dd --- /dev/null +++ b/src/hash.h @@ -0,0 +1,31 @@ +#include + +#include "types.h" +#include "fixed.h" + +#pragma once + +#define HASH_NULL 0xFFFFFF + +typedef struct { + + u32 hash; // Hashs are internally 24bit for bytecode ops + char str[32]; + +} MapItem; + +typedef struct { + + MapItem *buffer; + u32 *bit_free; // Bit map to track usage + u32 curr_len; + fixed_t load_factor; + +} HashMap; + +int init_hashmap(HashMap *map); + +void free_hashmap(HashMap *map); + +// Max is max value of hash +u32 hash(i32 max, char *str); diff --git a/src/main.c b/src/main.c index 859bd1e..7ae1fc6 100644 --- a/src/main.c +++ b/src/main.c @@ -5,8 +5,24 @@ #include "types.h" #include "byte_defs.h" +#include "hash.h" int main(){ + HashMap map; + if(init_hashmap(&map)){ + printf("Failed to alloc hashmap"); + return 1; + } + + char *string = "Je mange du fromage"; + char *str2 = "69420"; + char *str3 = "a"; + char *str4 = "func_init_foo_thing"; + + printf("%d %d %d %d\n", hash(128, string), hash(128,str2), hash(128,str3), + hash(128,str4)); + + free_hashmap(&map); return 0; }