A minimalistic programming language written in C89.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

156 строки
4.4 KiB

#include "ink.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
size_t allocated = 0;
size_t deallocated = 0;
static void* ink_malloc(struct context* _, size_t sz) {
(void)(_);
allocated+=sz;
void* ptr = malloc(sz + sizeof(size_t));
*(size_t*)ptr = sz;
return (size_t*)ptr + 1;
}
static void ink_free(struct context* _, void* ptr) {
(void)(_);
deallocated += *((size_t*)ptr-1);
free(((size_t*)ptr)-1);
}
static void* ink_realloc(struct context* _, void* ptr, size_t sz) {
(void)(_);
char* new_ptr = ink_malloc(_, sz);
size_t old_sz = *(-1 + (size_t*)ptr);
memcpy(new_ptr, ptr, old_sz);
ink_free(_, ptr);
return new_ptr;
}
static int ink_putchar(struct context* _, int c) {
(void)(_);
return putchar(c);
}
int main(int argc, char** argv) {
struct context* ctx;
ctx = ink_make_context(ink_malloc, ink_realloc, ink_free, ink_putchar);
ink_std_library(ctx);
ink_compile(
ctx,
"fn encrypt do\n"
" 3 pluck array.size\n"
" # array add_key modulo_key index\n"
" loop:\n"
" 1 - dup 5 pluck\n"
" # array add_key modulo_key index index array\n"
" array.index \n"
" # array add_key modulo_key index v\n"
" 4 pluck +\n"
" # array add_key modulo_key index (v + add_key)\n"
" 3 pluck %\n"
" # array add_key modulo_key index ((v + add_key) % modulo_key)\n"
" 2 pluck\n"
" # array add_key modulo_key index ((v + add_key) % modulo_key) index\n"
" 6 pluck\n"
" # array add_key modulo_key index ((v + add_key) % modulo_key) index array\n"
" array.set\n"
" # array add_key modulo_key index\n"
" dup 0 != loop jump_if drop drop drop drop\n"
"end\n"
"fn string.dump do\n"
" dup array.size 0\n"
" # array end it\n"
" 91 print_utf8\n"
" 32 print_utf8\n"
" loop:\n"
" dup\n"
" # array end it it\n"
" 4 pluck\n"
" # array end it it array\n"
" array.index print_int\n"
" 32 print_utf8\n"
" 1 +\n"
" # array end it\n"
" 2 pluck 2 pluck > loop jump_if\n"
" # array end it\n"
" 93 print_utf8\n"
" drop drop drop\n"
"end\n"
"fn string.print do\n"
" dup array.size 0\n"
" # array end it\n"
" loop:\n"
" dup\n"
" # array end it it\n"
" 4 pluck\n"
" # array end it it array\n"
" array.index print_utf8\n"
" 1 +\n"
" # array end it\n"
" 2 pluck 2 pluck > loop jump_if\n"
" # array end it\n"
" drop drop drop\n"
"end"
);
ink_compile(
ctx,
"# Clones an array, creating a new array\n"
"#\n"
"# @param array The array to clone into a new array\n"
"# @return a new array that contains the same elements as the source array\n"
"#\n"
"# array -> new_array\n"
"fn array.clone do\n"
" array.new 2 pluck array.size 0\n"
" # array new_array end it\n"
" 2 pluck 2 pluck == l jump_if\n"
" # array new_array end it\n"
" loop:\n"
" dup 5 pluck\n"
" # array new_array end it it array\n"
" array.index 4 pluck\n"
" # array new_array end it v new_array\n"
" array.push\n"
" # array new_array end it\n"
" 1 +\n"
" 2 pluck 2 pluck > loop jump_if\n"
" l: drop drop swap drop\n"
" # new_array\n"
"end"
);
ink_compile(
ctx,
"[ 72 101 108 108 111 32 87 111 114 108 100 10 ] dup dup dup string.print\n"
"32 131 encrypt string.dump\n"
"[ 72 101 108 108 111 32 87 111 114 108 100 10 ] dup dup dup string.print\n"
"32 131 encrypt string.dump\n"
"[ 72 101 108 108 111 32 87 111 114 108 100 10 ] dup dup dup string.print\n"
"32 131 encrypt string.dump\n"
"[ 72 101 108 108 111 32 87 111 114 108 100 10 ] dup dup dup string.print\n"
"32 131 encrypt string.dump\n"
"[ 72 101 108 108 111 32 87 111 114 108 100 10 ] dup dup dup string.print\n"
"32 131 encrypt string.dump\n"
);
//ctx->routines[coro].panic = 0;
int increment = 16;
int counter = increment;
int c;
while(ink_can_run(ctx)) {
ink_step_everyone(ctx);
if(ctx->steps > counter) {
ink_gc(ctx);
counter += increment;
}
}
ink_destroy(ctx);
assert(allocated == deallocated);
//return ctx->routines[coro].panic != INK_ROUTINE_SUCCESS && ctx->routines[coro].panic != INK_ROUTINE_CAN_REUSE;
}