-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
105 lines (82 loc) · 1.86 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#if defined(_WIN32) | defined(_WIN64)
# include <Windows.h>
#endif
#include "firth.h"
#include "firth_float.h"
//
static struct FirthState *pFirth = NULL;
//
static void myPrint(char *s)
{
fputs(s, stdout);
}
// examples of calling Firth from C
static void callFirth(FirthState *pFirth)
{
// exec_word is a set of convenience functions to push
// 0, 1, 2, or 3 parameters on stack and execute a word
fth_exec_word(pFirth, "words");
fth_exec_word2(pFirth, "+", 1, 2);
// parse, compile and execute a linen of text
fth_parse_string(pFirth, ": star 42 emit ;");
}
//
void banner(FirthState *pFirth)
{
pFirth->firth_print("Welcome to C-Firth! Copyright 2022 by Mark Seminatore\n");
pFirth->firth_print("See LICENSE file for usage rights and obligations.\n");
pFirth->firth_print("Type 'bye' to quit.\n");
}
//
int setupConsole()
{
#if defined(_WIN32) | defined(_WIN64)
// Set output mode to handle virtual terminal sequences
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE)
{
return GetLastError();
}
DWORD dwMode = 0;
if (!GetConsoleMode(hOut, &dwMode))
{
return GetLastError();
}
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (!SetConsoleMode(hOut, dwMode))
{
return GetLastError();
}
#endif
return 0;
}
//
int main(int argc, char *argv[])
{
setupConsole();
// create a new Firth state object
pFirth = fth_create_state();
// use our output function
fth_set_output_function(pFirth, myPrint);
banner(pFirth);
// if a file is given load it
// TODO - handle multiple files?
if (argc > 1)
{
fth_load_file(pFirth, argv[1]);
// process file until done
while (pFirth->BLK != stdin)
fth_update(pFirth);
return 0;
}
// REPL loop
while (!pFirth->halted)
{
fth_update(pFirth);
}
// we're done, cleanup and quit
fth_delete_state(pFirth);
return 0;
}