Added some crypto 100% implemented in Fun. (0.11.0)
This commit is contained in:
parent
5d3cfd9f65
commit
12a5646d56
25 changed files with 1926 additions and 12 deletions
|
|
@ -59,7 +59,8 @@ case OP_INDEX_GET: {
|
|||
free_value(idx);
|
||||
push_value(vm, out);
|
||||
} else {
|
||||
fprintf(stderr, "Runtime type error: INDEX_GET expects array or map\n");
|
||||
fprintf(stderr, "Runtime type error: INDEX_GET expects array or map (got container=%s, index=%s)\n",
|
||||
value_type_name(container.type), value_type_name(idx.type));
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
|
|
|
|||
27
src/vm/bitwise/band.c
Normal file
27
src/vm/bitwise/band.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the ISC license.
|
||||
* https://opensource.org/license/isc-license-txt
|
||||
*
|
||||
* Added: 2025-09-29
|
||||
*/
|
||||
|
||||
/**
|
||||
* OP_BAND: bitwise AND (uint32 32-bit)
|
||||
* pops: b, a
|
||||
* pushes: (uint32_t)(a & b)
|
||||
*/
|
||||
case OP_BAND: {
|
||||
Value vb = pop_value(vm);
|
||||
Value va = pop_value(vm);
|
||||
uint32_t a = (va.type == VAL_INT) ? (uint32_t)va.i : 0u;
|
||||
uint32_t b = (vb.type == VAL_INT) ? (uint32_t)vb.i : 0u;
|
||||
uint32_t r = a & b;
|
||||
free_value(vb);
|
||||
free_value(va);
|
||||
push_value(vm, make_int((int64_t)(uint64_t)r));
|
||||
break;
|
||||
}
|
||||
24
src/vm/bitwise/bnot.c
Normal file
24
src/vm/bitwise/bnot.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the ISC license.
|
||||
* https://opensource.org/license/isc-license-txt
|
||||
*
|
||||
* Added: 2025-09-29
|
||||
*/
|
||||
|
||||
/**
|
||||
* OP_BNOT: bitwise NOT (uint32 32-bit)
|
||||
* pops: a
|
||||
* pushes: (uint32_t)(~a)
|
||||
*/
|
||||
case OP_BNOT: {
|
||||
Value va = pop_value(vm);
|
||||
uint32_t a = (va.type == VAL_INT) ? (uint32_t)va.i : 0u;
|
||||
uint32_t r = ~a;
|
||||
free_value(va);
|
||||
push_value(vm, make_int((int64_t)(uint64_t)r));
|
||||
break;
|
||||
}
|
||||
27
src/vm/bitwise/bor.c
Normal file
27
src/vm/bitwise/bor.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the ISC license.
|
||||
* https://opensource.org/license/isc-license-txt
|
||||
*
|
||||
* Added: 2025-09-29
|
||||
*/
|
||||
|
||||
/**
|
||||
* OP_BOR: bitwise OR (uint32 32-bit)
|
||||
* pops: b, a
|
||||
* pushes: (uint32_t)(a | b)
|
||||
*/
|
||||
case OP_BOR: {
|
||||
Value vb = pop_value(vm);
|
||||
Value va = pop_value(vm);
|
||||
uint32_t a = (va.type == VAL_INT) ? (uint32_t)va.i : 0u;
|
||||
uint32_t b = (vb.type == VAL_INT) ? (uint32_t)vb.i : 0u;
|
||||
uint32_t r = a | b;
|
||||
free_value(vb);
|
||||
free_value(va);
|
||||
push_value(vm, make_int((int64_t)(uint64_t)r));
|
||||
break;
|
||||
}
|
||||
27
src/vm/bitwise/bxor.c
Normal file
27
src/vm/bitwise/bxor.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the ISC license.
|
||||
* https://opensource.org/license/isc-license-txt
|
||||
*
|
||||
* Added: 2025-09-29
|
||||
*/
|
||||
|
||||
/**
|
||||
* OP_BXOR: bitwise XOR (uint32 32-bit)
|
||||
* pops: b, a
|
||||
* pushes: (uint32_t)(a ^ b)
|
||||
*/
|
||||
case OP_BXOR: {
|
||||
Value vb = pop_value(vm);
|
||||
Value va = pop_value(vm);
|
||||
uint32_t a = (va.type == VAL_INT) ? (uint32_t)va.i : 0u;
|
||||
uint32_t b = (vb.type == VAL_INT) ? (uint32_t)vb.i : 0u;
|
||||
uint32_t r = a ^ b;
|
||||
free_value(vb);
|
||||
free_value(va);
|
||||
push_value(vm, make_int((int64_t)(uint64_t)r));
|
||||
break;
|
||||
}
|
||||
28
src/vm/bitwise/rol.c
Normal file
28
src/vm/bitwise/rol.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the ISC license.
|
||||
* https://opensource.org/license/isc-license-txt
|
||||
*
|
||||
* Added: 2025-09-29
|
||||
*/
|
||||
|
||||
/**
|
||||
* OP_ROTL: rotate left (uint32)
|
||||
* pops: s, a
|
||||
* pushes: rotl32(a, s)
|
||||
*/
|
||||
case OP_ROTL: {
|
||||
Value vs = pop_value(vm);
|
||||
Value va = pop_value(vm);
|
||||
uint32_t a = (va.type == VAL_INT) ? (uint32_t)va.i : 0u;
|
||||
uint32_t s = (vs.type == VAL_INT) ? (uint32_t)vs.i : 0u;
|
||||
s &= 31u;
|
||||
uint32_t r = (s == 0u) ? a : ((a << s) | (a >> (32u - s)));
|
||||
free_value(vs);
|
||||
free_value(va);
|
||||
push_value(vm, make_int((int64_t)(uint64_t)r));
|
||||
break;
|
||||
}
|
||||
28
src/vm/bitwise/ror.c
Normal file
28
src/vm/bitwise/ror.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the ISC license.
|
||||
* https://opensource.org/license/isc-license-txt
|
||||
*
|
||||
* Added: 2025-09-29
|
||||
*/
|
||||
|
||||
/**
|
||||
* OP_ROTR: rotate right (uint32)
|
||||
* pops: s, a
|
||||
* pushes: rotr32(a, s)
|
||||
*/
|
||||
case OP_ROTR: {
|
||||
Value vs = pop_value(vm);
|
||||
Value va = pop_value(vm);
|
||||
uint32_t a = (va.type == VAL_INT) ? (uint32_t)va.i : 0u;
|
||||
uint32_t s = (vs.type == VAL_INT) ? (uint32_t)vs.i : 0u;
|
||||
s &= 31u;
|
||||
uint32_t r = (s == 0u) ? a : ((a >> s) | (a << (32u - s)));
|
||||
free_value(vs);
|
||||
free_value(va);
|
||||
push_value(vm, make_int((int64_t)(uint64_t)r));
|
||||
break;
|
||||
}
|
||||
28
src/vm/bitwise/shl.c
Normal file
28
src/vm/bitwise/shl.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the ISC license.
|
||||
* https://opensource.org/license/isc-license-txt
|
||||
*
|
||||
* Added: 2025-09-29
|
||||
*/
|
||||
|
||||
/**
|
||||
* OP_SHL: logical left shift (uint32)
|
||||
* pops: s, a
|
||||
* pushes: (uint32_t)(a << (s&31))
|
||||
*/
|
||||
case OP_SHL: {
|
||||
Value vs = pop_value(vm);
|
||||
Value va = pop_value(vm);
|
||||
uint32_t a = (va.type == VAL_INT) ? (uint32_t)va.i : 0u;
|
||||
uint32_t s = (vs.type == VAL_INT) ? (uint32_t)vs.i : 0u;
|
||||
s &= 31u;
|
||||
uint32_t r = (s == 0u) ? a : (a << s);
|
||||
free_value(vs);
|
||||
free_value(va);
|
||||
push_value(vm, make_int((int64_t)(uint64_t)r));
|
||||
break;
|
||||
}
|
||||
28
src/vm/bitwise/shr.c
Normal file
28
src/vm/bitwise/shr.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the ISC license.
|
||||
* https://opensource.org/license/isc-license-txt
|
||||
*
|
||||
* Added: 2025-09-29
|
||||
*/
|
||||
|
||||
/**
|
||||
* OP_SHR: logical right shift (uint32)
|
||||
* pops: s, a
|
||||
* pushes: (uint32_t)(a >> (s&31)) using logical shift
|
||||
*/
|
||||
case OP_SHR: {
|
||||
Value vs = pop_value(vm);
|
||||
Value va = pop_value(vm);
|
||||
uint32_t a = (va.type == VAL_INT) ? (uint32_t)va.i : 0u;
|
||||
uint32_t s = (vs.type == VAL_INT) ? (uint32_t)vs.i : 0u;
|
||||
s &= 31u;
|
||||
uint32_t r = (s == 0u) ? a : (a >> s);
|
||||
free_value(vs);
|
||||
free_value(va);
|
||||
push_value(vm, make_int((int64_t)(uint64_t)r));
|
||||
break;
|
||||
}
|
||||
|
|
@ -40,8 +40,8 @@ case OP_LEN: {
|
|||
len = array_length(&a);
|
||||
if (len < 0) len = 0;
|
||||
} else {
|
||||
fprintf(stderr, "Runtime type error: LEN expects array or string\n");
|
||||
exit(1);
|
||||
/* Be lenient: for non-array/non-string, treat length as 0 */
|
||||
push_value(vm, make_int(0));
|
||||
}
|
||||
free_value(a);
|
||||
push_value(vm, make_int(len));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue