blob: a68b219d68449d0b1a0121fa33f2190176f54445 [file] [log] [blame]
reed@android.comf56e2952009-08-29 21:30:25 +00001#ifndef Forth_DEFINED
2#define Forth_DEFINED
3
4#include "SkTypes.h"
5
6class ForthOutput {
7public:
8 virtual void show(const char output[]) = 0;
9};
10
11union FloatIntDual {
12 int32_t fInt;
13 float fFloat;
14};
15
16static inline int32_t f2i_bits(float x) {
17 FloatIntDual d;
18 d.fFloat = x;
19 return d.fInt;
20}
21
22static inline float i2f_bits(int32_t x) {
23 FloatIntDual d;
24 d.fInt = x;
25 return d.fFloat;
26}
27
28class ForthEngine {
29public:
30 ForthEngine(ForthOutput*);
31 ~ForthEngine();
32
33 int depth() const { return fStackStop - fStackCurr; }
34 void clearStack() { fStackCurr = fStackStop; }
35
36 void push(intptr_t value);
37 intptr_t top() const { return this->peek(0); }
38 intptr_t peek(size_t index) const;
39 void setTop(intptr_t value);
40 intptr_t pop();
41
42 void fpush(float value) { this->push(f2i_bits(value)); }
43 float fpeek(size_t i) const { return i2f_bits(this->fpeek(i)); }
44 float ftop() const { return i2f_bits(this->top()); }
45 void fsetTop(float value) { this->setTop(f2i_bits(value)); }
46 float fpop() { return i2f_bits(this->pop()); }
47
48 void sendOutput(const char text[]);
49
50private:
51 ForthOutput* fOutput;
52 intptr_t* fStackBase;
53 intptr_t* fStackCurr;
54 intptr_t* fStackStop;
55
56 void signal_error(const char msg[]) const {
57 SkDebugf("ForthEngine error: %s\n", msg);
58 }
59};
60
61struct ForthCallBlock {
62 const intptr_t* in_data;
63 size_t in_count;
64 intptr_t* out_data;
65 size_t out_count;
66 size_t out_depth;
67};
68
69class ForthWord {
70public:
71 virtual ~ForthWord() {}
72 virtual void exec(ForthEngine*) = 0;
73
74 // todo: return error state of the engine
75 void call(ForthCallBlock*);
76};
77
78class ForthEnv {
79public:
80 ForthEnv();
81 ~ForthEnv();
82
83
84 void addWord(const char name[], ForthWord*);
85
86 void parse(const char code[]);
87
88 ForthWord* findWord(const char name[]);
89
90 void run(ForthOutput* = NULL);
91
92private:
93 class Impl;
94 Impl* fImpl;
95};
96
97#endif
98