Sheep
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros
sheepc.h
Go to the documentation of this file.
1 #ifndef SHEEPC_H
2 #define SHEEPC_H
3 
5 
6 #ifdef __cplusplus
7 #include <cstring>
8 extern "C"
9 {
10 #else
11 #include <string.h>
12 #endif
13 
14 #include "sheepCommon.h"
15 
16 #define SHEEP_TRUE 1
17 #define SHEEP_FALSE 0
18 
19 #define SHEEP_VERSION_MAJOR 0
20 #define SHEEP_VERSION_MINOR 4
21 #define SHEEP_VERSION_REVISION 0
22 
23 #define SHEEP_VERBOSITY_SILENT 0
24 #define SHEEP_VERBOSITY_POLITE 1
25 #define SHEEP_VERBOSITY_ANNOYING 2
26 #define SHEEP_VERBOSITY_EXTREME 3
27 
28 typedef unsigned char byte;
29 
30 enum SHP_SymbolType
31 {
32  Void = 0,
33  Int = 1,
34  Float = 2,
35  String = 3
36 };
37 
38 struct SHP_Symbol
39 {
40  char* Name;
41  SHP_SymbolType Type;
42 
43  int InitialIntValue;
44  float InitialFloatValue;
45  int InitialStringIndexValue;
46 };
47 
49 {
50  char* Name;
51  char* Value;
52 };
53 
54 /*struct SHP_Import
55 {
56  char* Name;
57  SHP_SymbolType ReturnType;
58  int NumParameters;
59  SHP_SymbolType* ParameterTypes;
60 };*/
61 
63 {
64  char* Name;
65  byte Reserved1;
66  byte Reserved2;
67 
68  byte* Code;
69  int CodeLength;
70 };
71 
73 {
74  const char* Output;
75  int LineNumber;
76 };
77 
79 typedef struct {} SheepVM;
80 
82 typedef struct {} SheepVMContext;
83 
84 typedef struct {} SheepImportFunction;
85 
87 typedef struct {} SheepScript;
88 
90 SHP_DECLSPEC SheepVM* SHP_LIB_CALL SHP_CreateNewVM(int languageVersion);
91 
93 SHP_DECLSPEC void SHP_LIB_CALL SHP_DestroyVM(SheepVM* vm);
94 
97 SHP_DECLSPEC void SHP_LIB_CALL SHP_SetVMTag(SheepVM* vm, void* tag);
99 SHP_DECLSPEC void* SHP_LIB_CALL SHP_GetVMTag(SheepVM* vm);
100 
101 typedef struct SHP_Allocator
102 {
103  void* (SHP_CALLBACK *Allocator)(size_t);
104  void (SHP_CALLBACK *Deallocator)(void*);
105 } SHP_Allocator;
106 
107 /* Sets the allocator. This is optional, but if you want to set the allocator
108 then this MUST Be called before any calls to SHP_CreateNewVM(). */
109 SHP_DECLSPEC void SHP_LIB_CALL SHP_SetAllocator(SHP_Allocator* allocator);
110 
111 
112 typedef void (SHP_CALLBACK *SHP_MessageCallback)(int linenumber, const char* message);
113 SHP_DECLSPEC void SHP_LIB_CALL SHP_SetOutputCallback(SheepVM* vm, SHP_MessageCallback callback);
114 
115 SHP_DECLSPEC int SHP_LIB_CALL shp_PrepareScriptForExecution(SheepVM* vm, SheepScript* script, const char* function, SheepVMContext** context);
116 SHP_DECLSPEC int SHP_LIB_CALL shp_Execute(SheepVMContext* context);
117 SHP_DECLSPEC int SHP_LIB_CALL shp_GetNumVariables(SheepVMContext* context);
118 SHP_DECLSPEC int SHP_LIB_CALL shp_GetVariableName(SheepVMContext* context, int index, const char** name);
119 SHP_DECLSPEC int SHP_LIB_CALL shp_GetVariableI(SheepVMContext* context, int index, int* value);
120 SHP_DECLSPEC int SHP_LIB_CALL shp_GetVariableF(SheepVMContext* context, int index, float* value);
121 SHP_DECLSPEC int SHP_LIB_CALL shp_SetVariableI(SheepVMContext* context, int index, int value);
122 SHP_DECLSPEC int SHP_LIB_CALL shp_SetVariableF(SheepVMContext* context, int index, float value);
123 
124 typedef void (SHP_CALLBACK *SHP_ImportCallback)(SheepVM* vm);
125 SHP_DECLSPEC void SHP_LIB_CALL SHP_SetImportCallback(SheepVM* vm, const char* name, SHP_ImportCallback callback);
126 
127 SHP_DECLSPEC int SHP_LIB_CALL SHP_PopIntFromStack(SheepVMContext* vm, int* result);
128 SHP_DECLSPEC int SHP_LIB_CALL SHP_PopFloatFromStack(SheepVMContext* vm, float* result);
129 SHP_DECLSPEC int SHP_LIB_CALL SHP_PopStringFromStack(SheepVMContext* vm, const char** result);
130 
131 SHP_DECLSPEC int SHP_LIB_CALL SHP_PushIntOntoStack(SheepVMContext* vm, int i);
132 
133 SHP_DECLSPEC SheepVMContext* SHP_LIB_CALL SHP_GetCurrentContext(SheepVM* vm);
134 
135 /* these next few functions are just for debugging the Compiler and VM. They shouldn't
136 be used for anything else. */
137 SHP_DECLSPEC int SHP_LIB_CALL SHP_GetNumContexts(SheepVM* vm);
138 SHP_DECLSPEC int SHP_LIB_CALL SHP_GetCurrentContextStackSize(SheepVM* vm);
139 SHP_DECLSPEC void SHP_LIB_CALL SHP_SetVerbosity(SheepVM* vm, int verbosity);
140 SHP_DECLSPEC void SHP_LIB_CALL SHP_PrintMemoryUsage();
141 SHP_DECLSPEC void SHP_LIB_CALL SHP_PrintStackTrace(SheepVM* vm);
142 
143 /* You can read more about Waiting in Sheep Engine.doc, which is embedded in the GK3 barns.
144 
145 As far as this library is concerned, there's one simple rule you should remember:
146 
147 - ALL import functions return immediately
148 
149 This means that no functions are allowed to block. They MUST return control to the Sheep VM
150 immediately. Even if they aren't finished executing. In other words, any import function
151 that may take a few frames to execute are actually asynchronous. The Wait mechanism
152 is what allows the VM to wait on functions to finish before continuing with the script.
153 
154 Here's the typical usage of the wait system:
155 
156 1) VM encounters a BeginWait instruction. It sets an internal IsWaiting flag.
157  (this is the flag that SHP_IsInWaitSection() returns)
158 2) VM calls an import function. It is up to this import function to determine what to do next.
159  If the function can return immediately then everything works as normal. But if
160  this is an import function that may take a few frames to execute then the host
161  application must check if the VM is in a wait section (using SHP_IsInWaitSection()).
162  If so, the host application must somehow *remember that the action is still pending,*
163  and then return.
164 3) The VM encounters an EndWait instruction. This raises the EndWaitCallback (which
165  should be set using SHP_SetEndWaitCallback()). The host application should check
166  for any pending actions, and call SHP_Suspend() if there is anything still executing.
167  Later, once everything finishes, the host application can call SHP_Resume() and
168  the script can continue executing where it left off.
169 
170 NOTE: Though the VM itself doesn't really care, asynchronous import functions should
171  never need to return a value.
172 
173 ** THE WAITING STUFF ISN'T WORKING YET! DON'T USE IT! THE API WILL PROBABLY CHANGE! **
174 
175 */
176 
177 SHP_DECLSPEC int SHP_LIB_CALL SHP_IsInWaitSection(SheepVM* vm);
178 SHP_DECLSPEC int SHP_LIB_CALL SHP_Suspend(SheepVMContext* context);
179 typedef void (SHP_CALLBACK *SHP_EndWaitCallback)(SheepVM* vm, SheepVMContext* context);
180 SHP_DECLSPEC void SHP_LIB_CALL SHP_SetEndWaitCallback(SheepVM* vm, SHP_EndWaitCallback callback);
181 
182 
183 /* To get the disassembly of a sheep file, do this:
184 1) load the sheep file (you have to do that part yourself)
185 2) send the entire file contents to SHP_GetDisassembly()
186 3) call SHP_GetDisassemblyLength() to get the length of the disassembly text
187 4) allocate a string buffer big enough to hold the disassembly
188 5) call SHP_GetDisassemblyText() using the allocated string buffer
189 6) call SHP_FreeDisassembly() to clean up the disassembly info
190 (since you allocated the buffer yourself you can keep using it even after
191 the SHP_FreeDisassembly() call) */
192 typedef struct {} SheepDisassembly;
193 SHP_DECLSPEC SheepDisassembly* SHP_GetDisassembly(SheepScript* script);
194 SHP_DECLSPEC const char* SHP_GetDisassemblyText(const SheepDisassembly* disassembly);
195 SHP_DECLSPEC void SHP_FreeDisassembly(const SheepDisassembly* disassembly);
196 
197 typedef struct {} SheepCompiler;
198 SHP_DECLSPEC SheepCompiler* SHP_LIB_CALL shp_CreateNewCompiler(int languageVersion);
199 SHP_DECLSPEC void SHP_LIB_CALL shp_DestroyCompiler(SheepCompiler* compiler);
200 SHP_DECLSPEC int SHP_LIB_CALL shp_DefineImportFunction(SheepCompiler* compiler, const char* name, SHP_SymbolType returnType, SHP_SymbolType parameters[], int numParameters);
201 SHP_DECLSPEC int SHP_LIB_CALL shp_CompileScript(SheepCompiler* compiler, const char* script, SheepScript** result);
202 SHP_DECLSPEC int SHP_LIB_CALL shp_LoadScriptFromBytecode(const char* bytecode, int length, SheepScript** result);
203 SHP_DECLSPEC void SHP_LIB_CALL shp_ReleaseSheepScript(SheepScript* script);
204 
205 
206 #ifdef __cplusplus
207 }
208 #endif
209 
210 #endif // SHEEPC_H
Definition: sheepc.h:197
Definition: sheepc.h:101
Definition: sheepc.h:38
SHP_DECLSPEC SheepVM *SHP_LIB_CALL SHP_CreateNewVM(int languageVersion)
Creates a new virtual machine.
Definition: sheepc.h:48
Definition: sheepc.h:72
SHP_DECLSPEC void SHP_LIB_CALL SHP_SetVMTag(SheepVM *vm, void *tag)
SHP_DECLSPEC void *SHP_LIB_CALL SHP_GetVMTag(SheepVM *vm)
Gets the "tag" data.
Handle to a virtual machine object.
Definition: sheepc.h:79
Handle to a Sheep script object.
Definition: sheepc.h:87
Handle to a virtual machine context object.
Definition: sheepc.h:82
SHP_DECLSPEC void SHP_LIB_CALL SHP_DestroyVM(SheepVM *vm)
Destroys an existing virtual machine.
Definition: sheepc.h:192
Definition: sheepc.h:84
Definition: sheepc.h:62