Compare commits

..

3 Commits

Author SHA1 Message Date
Richard Thier
00e818e43b statemachine basic architecture 2024-09-19 11:36:04 +02:00
Richard Thier
356a866e8d comments and first version state machine (too complex?) 2024-09-18 16:13:49 +02:00
Richard Thier
3ea872d744 better architecture with unions 2024-09-18 14:49:30 +02:00

View File

@ -1,6 +1,10 @@
#ifndef SLC_H
#define SLC_H
#include<stdint.h> // uint8_t, uint32_t..
#include<string.h> // memcpy, strlen..
#include<stddef.h> // NULL
/** Possible word types */
enum SLC_WORDTYP {
/** Still in plain text */
@ -107,6 +111,12 @@ enum SLC_IO_OP {
};
typedef enum SLC_IO_OP SLC_IO_OP;
union symptr {
uint32_t *varp;
wordstart *worp;
};
typedef union symptr symptr;
/**
* Function-abstraction for a "symbol-table".
*
@ -123,8 +133,7 @@ typedef enum SLC_IO_OP SLC_IO_OP;
* @param ptr When adding a found word/variable to the symbol table, the key will point to this wordstart* or uint32_t*
* @returns The word/var definition stored for the key, or NULL when it is not stored yet or op is SET and there was an error.
*/
typedef void* (*sym)(SLC_SYM_OP op, char *key, void *ptr);
// TODO: union for this?
typedef symptr (*sym)(SLC_SYM_OP op, char *key, symptr ptr);
/**
* Function-abstraction for an integer "stack".
@ -161,6 +170,14 @@ typedef uint32_t (*stack)(SLC_STACK_OP op, uint32_t param);
*/
typedef uint32_t (*session)(SLC_SESSION_OP op, uint32_t i, uint32_t j);
union iores {
/** Either the cmd result or the handle pointer*/
const char *ptr;
/** The read character */
char c;
};
typedef union iores iores;
/**
* Function-abstraction for io connectors.
*
@ -180,14 +197,54 @@ typedef uint32_t (*session)(SLC_SESSION_OP op, uint32_t i, uint32_t j);
* @param param The name or temporary name or command or the handle pointer parameter depending on op.
* @returns A handle pointer or pointer to character to read / written or closed/unlocked handle (NULL on errors). Also cmd stdout.
*/
typedef const char* (*ioconn)(SLC_IO_OP op, const char *param);
// TODO: union for this?
typedef iores (*ioconn)(SLC_IO_OP op, const char *param);
/**
* Function-abstraction for reading the source code byte-by-byte.
*/
typedef uint8_t (*coderead)();
enum slc_state : uint32_t {
/** Before things */
SLC_START,
/** In a comment */
SLC_COMMENT,
/** In multi-line comment */
SLC_MULTILINE_COMMENT,
/** Name part of word-definition (after ':') - whitespace ends it */
SLC_DEF_NAME,
/** Variable-listing part of word-definition - endline, '(', '[' or '{' ends it */
SLC_DEF_VAR,
/** Raw body part of the word definition - these can contain local variable accesses + words, depth counted by vars */
SLC_DEF_BODY,
/** Name part of a word "call" (non-definition). Ends by whitespace, '@' (in case of variable) or various parentheses */
SLC_WORD_NAME,
/** Variable call (MYWORD@MYVAR) - we get to be here from SLC_WORD_NAME or from START */
SLC_WORD_VAR,
};
typedef enum slc_state slc_state;
static inline slc_state slc_comment_statechange_in(
slc_state current_state,
char c,
const char *singleline_comment,
const char *multiline_comment_opener,
int singleline_comment_len,
int multiline_comment_len,
int *comment_i,
int *multiline_i){
// FIXME: Implement
return current_state;
}
static inline slc_state slc_defname_statechange(
slc_state current_state,
char c,
const char *prefix) {
// FIXME: Implement
return current_state;
}
/**
* This function runs the main slc engine over a snippet of code.
*
@ -217,26 +274,80 @@ typedef uint8_t (*coderead)();
* @param session_storage Can allocate and use arbitrary memory with this.
* @param symbol_table The symbol table to use while processing.
* @param code_stack The code stack (return addresses) to use.
* @param nesting_stack The stack used for the state-machine of the nested words.
* @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 current) with further words.
* @param io_connector The engine uses this to open/close pipes/files and write/read them.
* @param singleline_comment Like "//" - the character string that makes the rest of the line being comment. Can be "" (no NULL).
* @param multiline_comment_opener The character string that starts a multiline comment. Like / and * for C. Can be "" (no NULL).
* @param multiline_comment_closer The character string that ends a multiline comment. Like * and / for C. Can be "" (no NULL).
* @param prefix The prefix added to the lookup of built-ins. Useful when you write a compiler with SLC. Defaults to "" (empty).
* @param ender The character string that ends a word definition. Defaults to ";".
* @param varprefix The character string that prefixes variable declarations. Defaults to "@".
*/
static inline void slc(
coderead code_src,
session session_storage,
sym symbol_table,
stack code_stack,
stack data_stack,
stack insert_stack,
ioconn io_connector,
const char *prefix,
const char *ender,
const char *varprefix
) {
// TODO
coderead code_src,
session session_storage,
sym symbol_table,
stack code_stack,
stack nesting_stack,
stack data_stack,
stack insert_stack,
ioconn io_connector,
const char *singleline_comment,
const char *multiline_comment_opener,
const char *multiline_comment_closer,
const char *prefix,
const char *ender,
const char *varprefix) {
slc_state state = SLC_START;
int singleline_comment_len = strlen(singleline_comment);
int multiline_comment_opener_len = strlen(multiline_comment_opener);
uint8_t c = 0;
while(((c = code_src()) != 0)) {
switch(state) {
case SLC_START:
int comment_i = 0;
int multiline_i = 0;
int def_name_i = 0;
/* state -> comment | multiline_comment */
state = slc_comment_statechange_in(
state,
c,
singleline_comment,
multiline_comment_opener,
singleline_comment_len,
multiline_comment_opener_len,
&comment_i,
&multiline_i);
/* state -> def_name */
if(state == SLC_START) {
state = slc_defname_statechange(
state,
c,
prefix);
}
if(state == SLC_START) {
}
break;
case SLC_COMMENT:
break;
case SLC_MULTILINE_COMMENT:
break;
case SLC_DEF_NAME:
break;
case SLC_DEF_VAR:
break;
case SLC_DEF_BODY:
break;
case SLC_WORD_NAME:
break;
case SLC_WORD_VAR:
break;
}
}
}
#endif /* SLC_H */