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