This commit is contained in:
attilavs2 2025-03-04 08:56:33 +01:00
parent 142c01e443
commit 5f8cf91f8d
11 changed files with 291 additions and 10 deletions

View file

@ -29,10 +29,10 @@ windef:
$(eval LDFLAGS = $(LDFLAGS_WIN)) $(eval LDFLAGS = $(LDFLAGS_WIN))
builddir: builddir:
- mkdir $(BUILD_DIR) @- mkdir $(BUILD_DIR)
- mkdir build-tmp @- mkdir build-tmp
- rm -rf $(wildcard build-tmp/*) @- rm -rf $(wildcard build-tmp/*)
- mv $(wildcard $(BUILD_DIR)/*.o) build-tmp/ @- mv $(wildcard $(BUILD_DIR)/*.o) build-tmp/
build-tmp/%.o : $(SRC_DIR)/%.c build-tmp/%.o : $(SRC_DIR)/%.c
${CC} -c $< -o $@ ${CFLAGS} ${CC} -c $< -o $@ ${CFLAGS}
@ -41,8 +41,8 @@ build: $(OBJS)
${CC} ${CFLAGS} -o ${OUTPUT} $(OBJS) ${LDFLAGS} ${CC} ${CFLAGS} -o ${OUTPUT} $(OBJS) ${LDFLAGS}
builddir2: builddir2:
mv $(OBJS) $(BUILD_DIR) @mv $(OBJS) $(BUILD_DIR)
rm -rf build-tmp @rm -rf build-tmp
win: | windef all win: | windef all

View file

@ -0,0 +1,2 @@
(import console)
(write "Hello world")

View file

@ -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)
)
)

16
spec.md
View file

@ -53,6 +53,7 @@ In this document :
Optional argument Optional argument
- `|` : - `|` :
Or Or
## Core types ## Core types
- `null`: - `null`:
@ -158,11 +159,26 @@ where they may be strings), and of the same type
- `(pop <vec> <arg>)`: - `(pop <vec> <arg>)`:
Deletes item at pos arg from vec, and returns it Deletes item at pos arg from vec, and returns it
## utils functions (`(import utils)`)
- `(rand-seed <arg>)` :
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)`) ## console functions (`(import console)`)
- `(input)`: - `(input)`:
Will try to get input from the user, returns it as a string Will try to get input from the user, returns it as a string
- `(write <var>)`: - `(write <var>)`:
Will try to convert var to a string then print it to the console Will try to convert var to a string then print it to the console
- `(newline)`: - `(newline)`:
Will print a newline to the console (newline characters aren't escaped with Will print a newline to the console (newline characters aren't escaped with
write) write)

View file

@ -32,15 +32,42 @@ enum OpTypes {
OP_iter = 24, OP_iter = 24,
OP_not = 25, OP_not = 25,
OP_and = 26, 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 { typedef struct {
u8 type; // OpTypes u8 type; // OpTypes
u8 o1; u8 o[3];
u8 o2;
u8 o3;
} PACKED Opcode; } PACKED Opcode;

4
src/code_defs.h Normal file
View file

@ -0,0 +1,4 @@
#include "types.h"
#pragma once

6
src/config.h Normal file
View file

@ -0,0 +1,6 @@
#pragma once
// 0 : None (still features runtime debug)
// 1 : Some checks and prints
// 2 : All checks
#define DEBUG 2

111
src/fixed.h Normal file
View file

@ -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 <stdint.h>
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;
}

38
src/hash.c Normal file
View file

@ -0,0 +1,38 @@
#include <assert.h>
#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;
}

31
src/hash.h Normal file
View file

@ -0,0 +1,31 @@
#include <stdlib.h>
#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);

View file

@ -5,8 +5,24 @@
#include "types.h" #include "types.h"
#include "byte_defs.h" #include "byte_defs.h"
#include "hash.h"
int main(){ 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; return 0;
} }