-
Notifications
You must be signed in to change notification settings - Fork 0
/
porth.porth
83 lines (67 loc) · 1.61 KB
/
porth.porth
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
include "windows.porth"
include "std.porth"
// max sizes
const TK_SIZE 50 end
const MAX_FILE_SIZE 12 1000 * end
// operand struct
const Op.type sizeof(int) offset end
// const op.token sizeof(ptr) offset end
const Op.operand sizeof(int) offset end
const sizeof(Op) reset end
// ops enum
const OP_PUSH_INT 1 offset end
const OP_DUMP 1 offset end
const NO_OF_OPS reset end
// token struct
const Token.type sizeof(int) offset end
const Token.text TK_SIZE offset end
const sizeof(Token) reset end
// token enum
const TK_WORD 1 offset end
const TK_INT 1 offset end
const NO_OF_TK_TYPS reset end
// Arrays
memory program sizeof(Op) 1000 * end
memory program_len sizeof(int) end
memory file_text MAX_FILE_SIZE end
proc parse_tok
ptr // text ptr
ptr // buffer
--
ptr // new text ptr
in
let tok buf in
0 while tok over ptr+ @char ' ' != do
1 +
end
dup tok buf memcpy
tok swap ptr+
end
end
proc seek_till_space ptr -- ptr in
let text in
0 while text over ptr+ @char dup ' ' = swap '\0' != land do
1 +
end
text swap ptr+
end
end
proc lex_file ptr in
memory tok_text TK_SIZE end
OPEN_EXISTING swap fopen
let fh in
MAX_FILE_SIZE file_text fh fread drop
// file_text stdout
file_text while dup @char '\0' != do
tok_text parse_tok
tok_text stdout
TK_SIZE tok_text free
"\n" stdout
seek_till_space
// dup stdout
// "\n" stdout
end drop
fh fclose
end
end
"math.porth" lex_file