move to folders, more cleaning and fixes
This commit is contained in:
+8
-5
@@ -4,9 +4,12 @@ project(forth C)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
|
||||
add_executable(forth
|
||||
main.c
|
||||
fh_builtins.c
|
||||
fh_runtime.c
|
||||
fh_stack.c
|
||||
fh_mem.c
|
||||
src/main.c
|
||||
src/fh_builtins.c
|
||||
src/fh_runtime.c
|
||||
src/fh_stack.c
|
||||
src/fh_mem.c
|
||||
)
|
||||
|
||||
target_include_directories(forth PRIVATE include)
|
||||
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
/**
|
||||
* TODO file description
|
||||
*
|
||||
* Created on 2021/11/13.
|
||||
*/
|
||||
|
||||
#ifndef FORTH_H
|
||||
#define FORTH_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/* for printing */
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "fh_config.h"
|
||||
#include "fh_error.h"
|
||||
|
||||
struct fh_thread_s;
|
||||
struct fh_word_s;
|
||||
struct fh_instruction_s;
|
||||
|
||||
/** Word handler typedef */
|
||||
typedef enum fh_error (*word_exec_t)(struct fh_thread_s *fh);
|
||||
|
||||
/** Forth runtime global state */
|
||||
struct fh_global_s {
|
||||
/** Verbose logging enabled */
|
||||
bool verbose;
|
||||
/** Interactive mode (i.e. not started with a file argument) */
|
||||
bool interactive;
|
||||
};
|
||||
|
||||
extern struct fh_global_s fh_globals;
|
||||
|
||||
/* logging */
|
||||
#define LOG(format, ...) do { if(fh_globals.verbose) { fprintf(stderr, format "\n", ##__VA_ARGS__); } } while (0)
|
||||
#define LOGI(format, ...) fprintf(stderr, "\x1b[32m" format "\x1b[m\n", ##__VA_ARGS__)
|
||||
#define LOGE(format, ...) fprintf(stderr, "\x1b[31;1m" format "\x1b[m\n", ##__VA_ARGS__)
|
||||
/* Forth standard output. XXX should be stdout, but then colors get mangled if logging is used */
|
||||
#define FHPRINT(format, ...) fprintf(stderr, "\x1b[33;1m" format "\x1b[m", ##__VA_ARGS__)
|
||||
#define FHPRINT_SVC(format, ...) fprintf(stderr, "" format "", ##__VA_ARGS__)
|
||||
|
||||
enum fh_error fh_init_thread(struct fh_thread_s *fh);
|
||||
enum fh_error fh_process_line(struct fh_thread_s *fh, char *linebuf);
|
||||
|
||||
#endif //FORTH_H
|
||||
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Global state
|
||||
*
|
||||
* Created on 2021/11/13.
|
||||
*/
|
||||
|
||||
#ifndef FORTH_FH_GLOBALS_H
|
||||
#define FORTH_FH_GLOBALS_H
|
||||
|
||||
/** Forth runtime global state */
|
||||
struct fh_global_s {
|
||||
/** Verbose logging enabled */
|
||||
bool verbose;
|
||||
/** Interactive mode (i.e. not started with a file argument) */
|
||||
bool interactive;
|
||||
};
|
||||
|
||||
extern struct fh_global_s fh_globals;
|
||||
|
||||
#endif //FORTH_FH_GLOBALS_H
|
||||
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Forth printing
|
||||
*
|
||||
* Created on 2021/11/13.
|
||||
*/
|
||||
|
||||
#ifndef FORTH_FH_PRINT_H
|
||||
#define FORTH_FH_PRINT_H
|
||||
|
||||
/* for printing */
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "fh_globals.h"
|
||||
|
||||
/* logging */
|
||||
#define LOG(format, ...) do { if(fh_globals.verbose) { fprintf(stderr, format "\n", ##__VA_ARGS__); } } while (0)
|
||||
#define LOGI(format, ...) fprintf(stderr, "\x1b[32m" format "\x1b[m\n", ##__VA_ARGS__)
|
||||
#define LOGE(format, ...) fprintf(stderr, "\x1b[31;1m" format "\x1b[m\n", ##__VA_ARGS__)
|
||||
|
||||
/* Forth standard output. XXX should be stdout, but then colors get mangled if logging is used */
|
||||
#define FHPRINT(format, ...) fprintf(stderr, "\x1b[33;1m" format "\x1b[m", ##__VA_ARGS__)
|
||||
#define FHPRINT_SVC(format, ...) fprintf(stderr, "" format "", ##__VA_ARGS__)
|
||||
|
||||
#endif //FORTH_FH_PRINT_H
|
||||
@@ -9,6 +9,17 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "fh_config.h"
|
||||
|
||||
struct fh_word_s;
|
||||
struct fh_instruction_s;
|
||||
struct fh_thread_s;
|
||||
|
||||
/** Word handler typedef */
|
||||
typedef enum fh_error (*word_exec_t)(struct fh_thread_s *fh);
|
||||
|
||||
/** Bytecode instruction type marker */
|
||||
enum fb_instruction_kind {
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Forth internal stack operations
|
||||
* Forth stack operations
|
||||
*
|
||||
* Created on 2021/11/13.
|
||||
*/
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Forth main entry-point
|
||||
*
|
||||
* Created on 2021/11/13.
|
||||
*/
|
||||
|
||||
#ifndef FORTH_H
|
||||
#define FORTH_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "fh_config.h"
|
||||
#include "fh_error.h"
|
||||
|
||||
struct fh_thread_s;
|
||||
|
||||
enum fh_error fh_init(struct fh_thread_s *fh);
|
||||
enum fh_error fh_process_line(struct fh_thread_s *fh, const char *linebuf);
|
||||
|
||||
#endif //FORTH_H
|
||||
@@ -1,6 +1,9 @@
|
||||
#include <string.h>
|
||||
#include "forth.h"
|
||||
#include <stdbool.h>
|
||||
#include "fh_runtime.h"
|
||||
#include "fh_config.h"
|
||||
#include "fh_error.h"
|
||||
#include "fh_print.h"
|
||||
#include "fh_builtins.h"
|
||||
#include "fh_stack.h"
|
||||
#include "fh_mem.h"
|
||||
@@ -1,11 +1,14 @@
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "forth.h"
|
||||
#include "fh_runtime.h"
|
||||
#include "fh_builtins.h"
|
||||
#include "fh_stack.h"
|
||||
#include "fh_mem.h"
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <ctype.h>
|
||||
#include "fh_globals.h"
|
||||
#include "fh_print.h"
|
||||
|
||||
struct fh_global_s fh_globals = {};
|
||||
|
||||
@@ -168,7 +171,7 @@ enum fh_error w_user_word(struct fh_thread_s *fh)
|
||||
|
||||
|
||||
/** Initialize a runtime */
|
||||
enum fh_error fh_init_thread(struct fh_thread_s *fh)
|
||||
enum fh_error fh_init(struct fh_thread_s *fh)
|
||||
{
|
||||
enum fh_error rv;
|
||||
|
||||
@@ -184,7 +187,7 @@ enum fh_error fh_init_thread(struct fh_thread_s *fh)
|
||||
/** Process a quoted string read from input */
|
||||
static enum fh_error fh_handle_quoted_string(
|
||||
struct fh_thread_s *fh,
|
||||
char *start,
|
||||
const char *start,
|
||||
size_t len
|
||||
)
|
||||
{
|
||||
@@ -229,7 +232,7 @@ static enum fh_error fh_handle_quoted_string(
|
||||
/** Process a word read from input */
|
||||
static enum fh_error fh_handle_word(
|
||||
struct fh_thread_s *fh,
|
||||
char *start,
|
||||
const char *start,
|
||||
size_t len
|
||||
)
|
||||
{
|
||||
@@ -291,10 +294,10 @@ static inline bool isnl(char c)
|
||||
}
|
||||
|
||||
/** Process a line read from input */
|
||||
enum fh_error fh_process_line(struct fh_thread_s *fh, char *linebuf)
|
||||
enum fh_error fh_process_line(struct fh_thread_s *fh, const char *linebuf)
|
||||
{
|
||||
enum fh_error rv;
|
||||
char *rp = linebuf;
|
||||
const char *rp = linebuf;
|
||||
char c;
|
||||
|
||||
if (!fh_globals.interactive) {
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "forth.h"
|
||||
#include "fh_error.h"
|
||||
#include "fh_config.h"
|
||||
#include "fh_runtime.h"
|
||||
#include "fh_stack.h"
|
||||
#include "fh_print.h"
|
||||
|
||||
/** Pop from data stack */
|
||||
enum fh_error ds_pop(struct fh_thread_s *fh, uint32_t *out)
|
||||
+2
-4
@@ -1,20 +1,18 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "forth.h"
|
||||
#include "fh_runtime.h"
|
||||
#include "fh_print.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
enum fh_error rv;
|
||||
struct fh_thread_s fh;
|
||||
rv = fh_init_thread(&fh);
|
||||
rv = fh_init(&fh);
|
||||
if (rv != FH_OK) {
|
||||
LOGE("Error in forth init: %s", fherr_name(rv));
|
||||
return 1;
|
||||
Reference in New Issue
Block a user