blob: 0985c6af71e44e181910f4d5b3afda5c06280889 [file] [log] [blame]
reed@android.come50025a2009-09-01 21:00:44 +00001
reed@android.comf56e2952009-08-29 21:30:25 +00002#ifndef Forth_DEFINED
3#define Forth_DEFINED
4
5#include "SkTypes.h"
6
7class ForthOutput {
8public:
9 virtual void show(const char output[]) = 0;
10};
11
12union FloatIntDual {
13 int32_t fInt;
14 float fFloat;
15};
16
17static inline int32_t f2i_bits(float x) {
18 FloatIntDual d;
19 d.fFloat = x;
20 return d.fInt;
21}
22
23static inline float i2f_bits(int32_t x) {
24 FloatIntDual d;
25 d.fInt = x;
26 return d.fFloat;
27}
28
29class ForthEngine {
30public:
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
51private:
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
62struct 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
70class ForthWord {
71public:
72 virtual ~ForthWord() {}
73 virtual void exec(ForthEngine*) = 0;
74
75 // todo: return error state of the engine
76 void call(ForthCallBlock*);
77};
78
79class ForthEnv {
80public:
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
93private:
94 class Impl;
95 Impl* fImpl;
96};
97
98#endif
99