move to folders, more cleaning and fixes

master
Ondřej Hruška 3 years ago
parent 68ddeb3e17
commit cf1a0cd99b
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 13
      CMakeLists.txt
  2. 2
      build.sh
  3. BIN
      forth
  4. 48
      forth.h
  5. 0
      include/fh_builtins.h
  6. 0
      include/fh_config.h
  7. 0
      include/fh_error.h
  8. 20
      include/fh_globals.h
  9. 0
      include/fh_mem.h
  10. 24
      include/fh_print.h
  11. 11
      include/fh_runtime.h
  12. 2
      include/fh_stack.h
  13. 21
      include/forth.h
  14. 5
      src/fh_builtins.c
  15. 0
      src/fh_mem.c
  16. 19
      src/fh_runtime.c
  17. 4
      src/fh_stack.c
  18. 6
      src/main.c

@ -4,9 +4,12 @@ project(forth C)
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)
add_executable(forth add_executable(forth
main.c src/main.c
fh_builtins.c src/fh_builtins.c
fh_runtime.c src/fh_runtime.c
fh_stack.c src/fh_stack.c
fh_mem.c src/fh_mem.c
) )
target_include_directories(forth PRIVATE include)

@ -1,3 +1,3 @@
#!/bin/bash #!/bin/bash
cc *.c -o forth cc -I include src/*.c -o forth

BIN
forth

Binary file not shown.

@ -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 <stdint.h>
#include <stddef.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 */ /** Bytecode instruction type marker */
enum fb_instruction_kind { enum fb_instruction_kind {

@ -1,5 +1,5 @@
/** /**
* Forth internal stack operations * Forth stack operations
* *
* Created on 2021/11/13. * 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 <string.h>
#include "forth.h" #include <stdbool.h>
#include "fh_runtime.h" #include "fh_runtime.h"
#include "fh_config.h"
#include "fh_error.h"
#include "fh_print.h"
#include "fh_builtins.h" #include "fh_builtins.h"
#include "fh_stack.h" #include "fh_stack.h"
#include "fh_mem.h" #include "fh_mem.h"

@ -1,11 +1,14 @@
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "forth.h" #include "forth.h"
#include "fh_runtime.h" #include "fh_runtime.h"
#include "fh_builtins.h" #include "fh_builtins.h"
#include "fh_stack.h" #include "fh_stack.h"
#include "fh_mem.h" #include "fh_mem.h"
#include <string.h> #include "fh_globals.h"
#include <errno.h> #include "fh_print.h"
#include <ctype.h>
struct fh_global_s fh_globals = {}; struct fh_global_s fh_globals = {};
@ -168,7 +171,7 @@ enum fh_error w_user_word(struct fh_thread_s *fh)
/** Initialize a runtime */ /** 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; 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 */ /** Process a quoted string read from input */
static enum fh_error fh_handle_quoted_string( static enum fh_error fh_handle_quoted_string(
struct fh_thread_s *fh, struct fh_thread_s *fh,
char *start, const char *start,
size_t len size_t len
) )
{ {
@ -229,7 +232,7 @@ static enum fh_error fh_handle_quoted_string(
/** Process a word read from input */ /** Process a word read from input */
static enum fh_error fh_handle_word( static enum fh_error fh_handle_word(
struct fh_thread_s *fh, struct fh_thread_s *fh,
char *start, const char *start,
size_t len size_t len
) )
{ {
@ -291,10 +294,10 @@ static inline bool isnl(char c)
} }
/** Process a line read from input */ /** 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; enum fh_error rv;
char *rp = linebuf; const char *rp = linebuf;
char c; char c;
if (!fh_globals.interactive) { 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_runtime.h"
#include "fh_stack.h" #include "fh_stack.h"
#include "fh_print.h"
/** Pop from data stack */ /** Pop from data stack */
enum fh_error ds_pop(struct fh_thread_s *fh, uint32_t *out) enum fh_error ds_pop(struct fh_thread_s *fh, uint32_t *out)

@ -1,20 +1,18 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <ctype.h> #include <ctype.h>
#include "forth.h" #include "forth.h"
#include "fh_runtime.h" #include "fh_runtime.h"
#include "fh_print.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
enum fh_error rv; enum fh_error rv;
struct fh_thread_s fh; struct fh_thread_s fh;
rv = fh_init_thread(&fh); rv = fh_init(&fh);
if (rv != FH_OK) { if (rv != FH_OK) {
LOGE("Error in forth init: %s", fherr_name(rv)); LOGE("Error in forth init: %s", fherr_name(rv));
return 1; return 1;
Loading…
Cancel
Save