Compare commits

...

5 Commits

Author SHA1 Message Date
Richard Thier
ef7a2bf333 engine progress 2024-09-16 20:12:23 +02:00
Richard Thier
a6f47915d3 abstracted out code reader - might need it to support includes better or do they become literally a pass? 2024-09-14 13:41:34 +02:00
Richard Thier
87164e7a65 better enums - more like our struct typedefs 2024-09-14 13:00:43 +02:00
Richard Thier
4107f42b53 fixed some compile errors 2024-09-14 12:59:14 +02:00
Richard Thier
0bd5940a47 added main.c and makefile 2024-09-14 12:54:29 +02:00
3 changed files with 120 additions and 14 deletions

View File

@ -1,20 +1,90 @@
#ifndef SLC_H #ifndef SLC_H
#define SLC_H #define SLC_H
/** A word starts right after this - a word is usually after this memory area */ /** Possible word types */
struct wordstart { enum SLC_WORDTYP {
/* : */ /** Still in plain text */
char colon; SLC_WORDTYP_TEXT = 0,
/* whitespace after ':', usually used to store the flags */ /** Native code, use get_word_storage_offset to get what to run (relative pointer or array of pointers) */
uint8_t flags; SLC_WORDTYP_NATIVE = 1,
/* char name[]; // The name of the word being defined. */ /** "Threaded code" (utf16-like word offsets) and encoded parentheses IN-PLACE inlined where text was before */
/* char vars[]; // The local variables of the word. */ SLC_WORDTYP_THREADED_INLINE = 2,
/* char data[]; // The "body" of the word - either text source or after processing the threaded code of it (see flags) */ /** "Threaded code" that did not fit in-place and is thus stored in session storage, word offset tells where */
SLC_WORDTYP_THREADED_SESSION = 2
}; };
typedef enum SLC_WORDTYP SLC_WORDTYP;
/** Gets the wordtyp from a flags field - see wordstart */
static inline SLC_WORDTYP get_word_type(uint8_t flags) {
return (SLC_WORDTYP)(flags >> 6);
}
/** Gets the 6-bit variable count (at most 64 vars possible per word) */
static inline uint8_t get_word_var_count(uint8_t flags) {
return (flags && 0x3F); /* 0011 1111 */
}
/** Gets the 24bit storage offset of the word: Natives contain pointers in an offseted array */
static inline uint32_t get_word_storage_offset(uint8_t high_offset, uint16_t low_offset) {
return ((uint32_t)high_offset << 16) + low_offset;
}
/**
* A word definition starts right after this. After processing it, we inline overwrite random parts of it in memory...
*
* Examples:
*
* #: just_code
* #dup
* #inc
* #swap
* #
*
* : with_vars @a; @b; @c;
* @a
* inc
* @a(.)
* ;
*
* #builtin: to_prefix
* #swap
* #dup
*
* TODO: How to do this builtin to be properly changed inline? If it can be inline threaded-coded, then its fine, but is spec-case
* ^^The above always needs starting '#builtin' at the definition and inside. That is exchanged to real prefix...
* This is used for implementation implementing built-ins with FORTH-like code instead of native (saves native interpret. space)
*
* #: structural (
* #parse_num
* #dup
* #inc
* #swap
* #print
* #print
* ) [ #parse_num #print ] { #parse_num #print }
* #
*/
struct wordstart {
/** The ':' char - after processing it stores the flags */
uint8_t flags;
/** whitespace after ':' and first char of name - after processing contains the high-offset */
uint16_t high_offset;
/** Either remaining parts of the name - or the leading tab/space for starting variables (or newline if there's none) */
uint8_t low_offset;
/* char ..name[]; // The REMAINS of name of the word being defined. Can be empty! */
/* char vars[]; // The local (at least 1-letter) variables of the word. Can be empty - min 4x8bit per a var, like: " @a;" */
/* char newline; // there is always a newline at this point! XXX: "@a; ", "@b;\n" is how we store vars (*/
/* char data[]; // The "body" of the word - either text source or inline threaded code (at least 1 character) */
/* char ender[]; // The ender-string that ends the word - always at least 1 character! */
};
typedef struct wordstart wordstart;
enum SLC_SYM_OP { SLC_SYM_SET, SLC_SYM_GET, SLC_SYM_ERASE }; enum SLC_SYM_OP { SLC_SYM_SET, SLC_SYM_GET, SLC_SYM_ERASE };
typedef enum SLC_SYM_OP SLC_SYM_OP;
enum SLC_STACK_OP { SLC_STACK_PUSH, SLC_STACK_POP, SLC_STACK_AT, SLC_STACK_COUNT, SLC_STACK_ERASE }; enum SLC_STACK_OP { SLC_STACK_PUSH, SLC_STACK_POP, SLC_STACK_AT, SLC_STACK_COUNT, SLC_STACK_ERASE };
typedef enum SLC_STACK_OP SLC_STACK_OP;
enum SLC_SESSION_OP { SLC_SESSION_ALLOC, SLC_SESSION_ERASE, SLC_SESSION_GET }; enum SLC_SESSION_OP { SLC_SESSION_ALLOC, SLC_SESSION_ERASE, SLC_SESSION_GET };
typedef enum SLC_SESSION_OP SLC_SESSION_OP;
enum SLC_IO_OP { enum SLC_IO_OP {
SLC_IO_OPEN, SLC_IO_OPEN,
SLC_IO_OPEN_TMP, SLC_IO_OPEN_TMP,
@ -26,6 +96,12 @@ enum SLC_IO_OP {
SLC_IO_UNLOCK, SLC_IO_UNLOCK,
SLC_IO_CMD, SLC_IO_CMD,
}; };
typedef enum SLC_IO_OP SLC_IO_OP;
enum SLC_CODE_OP {
SLC_CODE_COUNT,
SLC_CODE_READ,
};
typedef enum SLC_CODE_OP SLC_CODE_OP;
/** /**
* Function-abstraction for a "symbol-table". * Function-abstraction for a "symbol-table".
@ -95,19 +171,35 @@ typedef uint32_t (*session)(SLC_SESSION_OP op, uint32_t i);
*/ */
typedef const char* (*ioconn)(SLC_IO_OP op, const char *param); typedef const char* (*ioconn)(SLC_IO_OP op, const char *param);
/**
* Function-abstraction for reading the source code.
*
* Operations:
*
* SLC_CODE_COUNT To get how much bytes are readable (i is unused). Returns size as uint32_t (full used)
* SLC_CODE_READ To read bytes of the source code at location i. Returns the byte char as uint32_t (low byte)
*
* @param op Defines which operation the caller wants.
* @param i In case of READ, the index of the data.
*/
typedef uint32_t (*coderead)(SLC_CODE_OP op, uint32_t i);
/** /**
* This function runs the main slc engine over a snippet of code. * This function runs the main slc engine over a snippet of code.
* *
* @param code The input source code to interpret / run. * @param code_src The input source code to interpret / run.
* @param symbol_table The symbol table to use * @param symbol_table The symbol table to use
* @param code_stack The code stack (return addresses) to use * @param code_stack The code stack (return addresses) to use
* @param data_stack The data stack (forth-like stack) to use * @param data_stack The data stack (forth-like stack) to use
* @param insert_stack Used for temporarily expanding the input stream (one word level above) with words * @param insert_stack Used for temporarily expanding the input stream (one word level above) with words
* @param session_storage Can allocate and use arbitrary memory with this. * @param session_storage Can allocate and use arbitrary memory with this.
* @param io_connector The engine uses this to open/close pipes/files and write/read them. * @param io_connector The engine uses this to open/close pipes/files and write/read them.
* @param prefix The prefix added to the lookup of built-ins.
* @param ender The character string that ends a word definition.
* @param varprefix The character string that prefixes variable declarations.
*/ */
void slc( static inline void slc(
const char *code, coderead code_src,
sym symbol_table, sym symbol_table,
stack code_stack, stack code_stack,
stack data_stack, stack data_stack,
@ -116,9 +208,9 @@ void slc(
ioconn io_connector, ioconn io_connector,
const char *prefix, const char *prefix,
const char *ender, const char *ender,
const char *varprefix, const char *varprefix
) { ) {
// TODO // TODO
} }
#ENDIF /* SLC_H */ #endif /* SLC_H */

10
main.c Normal file
View File

@ -0,0 +1,10 @@
#include <stdio.h>
#include "hamt.h"
#include "turbolist/turbolist.h"
#include "engine/slc.h"
int main(int argc, const char **argv) {
// TODO: Implement CLI frontend...
puts("TODO: Implement CLI frontend...");
return 0;
}

4
makefile Normal file
View File

@ -0,0 +1,4 @@
debug:
gcc -g hamt.c main.c -o main
release:
gcc -O2 hamt.c main.c -o main