1 /** 2 * Structures and functions for interpreting a parse tree. The interpreter 3 * traverses a parse tree in a depth-first manner, interpreting each node it 4 * reaches along the way. This is the last stage of the processing of a source 5 * code file. 6 * 7 * \file interpreter.h 8 * 9 * \author Justin J. Meza 10 * 11 * \date 2010-2012 12 */ 13 14 #ifndef __INTERPRETER_H__ 15 #define __INTERPRETER_H__ 16 17 #include <stdio.h> 18 #include <ctype.h> 19 #include <math.h> 20 21 #include "parser.h" 22 #include "unicode.h" 23 24 /** 25 * Retrieves a value's integer data. 26 */ 27 #define getInteger(value) (value->data.i) 28 29 /** 30 * Retrieves a value's decimal data. 31 */ 32 #define getFloat(value) (value->data.f) 33 34 /** 35 * Retrieves a value's string data. 36 */ 37 #define getString(value) (value->data.s) 38 39 /** 40 * Retrieves a value's function data. 41 */ 42 #define getFunction(value) (value->data.fn) 43 44 /** 45 * Retrieves a value's array data. 46 */ 47 #define getArray(value) (value->data.a) 48 49 /** 50 * Retrieves a value's blob data. 51 */ 52 #define getBlob(value) (value->data.b) 53 54 /** 55 * Represents a value type. 56 */ 57 typedef enum { 58 VT_INTEGER, /**< An integer value. */ 59 VT_FLOAT, /**< A decimal value. */ 60 VT_BOOLEAN, /**< A boolean value. */ 61 VT_STRING, /**< A string value. */ 62 VT_NIL, /**< Represents no value. */ 63 VT_FUNC, /**< A function. */ 64 VT_ARRAY, /**< An array. */ 65 VT_BLOB /**< A binary blob of data. */ 66 } ValueType; 67 68 /** 69 * Stores value data. 70 */ 71 typedef union { 72 long long i; /**< Integer data. */ 73 float f; /**< Decimal data. */ 74 char *s; /**< String data. */ 75 FuncDefStmtNode *fn; /**< Function data. */ 76 struct scopeobject *a; /**< Array data. */ 77 void *b; /**< Binary blob data. */ 78 } ValueData; 79 80 /** 81 * Increments a value's semaphore. 82 */ 83 #define V(value) (value->semaphore++) 84 85 /** 86 * Decrements a value's semaphore. 87 */ 88 #define P(value) (value->semaphore--) 89 90 /** 91 * Stores a value. 92 */ 93 typedef struct { 94 ValueType type; /**< The type of value stored. */ 95 ValueData data; /**< The value data. */ 96 unsigned short semaphore; /**< A semaphore for value usage. */ 97 } ValueObject; 98 99 /** 100 * Represents the return type. 101 */ 102 typedef enum { 103 RT_DEFAULT, /**< Code block completed successfully. */ 104 RT_BREAK, /**< Broke out of a loop or switch statement. */ 105 RT_RETURN /**< Returned from a function. */ 106 } ReturnType; 107 108 /** 109 * Stores return state. 110 */ 111 typedef struct returnobject { 112 ReturnType type; /**< The type of return encountered. */ 113 ValueObject *value; /**< The optional return value. */ 114 } ReturnObject; 115 116 /** 117 * Stores a set of variables hierarchically. 118 */ 119 typedef struct scopeobject { 120 struct scopeobject *parent; /**< The parent scope. */ 121 struct scopeobject *caller; /**< The caller scope (if in a function). */ 122 ValueObject *impvar; /**< The \ref impvar "implicit variable". */ 123 unsigned int numvals; /**< The number of values in the scope. */ 124 char **names; /**< The names of the values. */ 125 ValueObject **values; /**< The values in the scope. */ 126 } ScopeObject; 127 128 /** 129 * \name Utilities 130 * 131 * Functions for performing helper tasks. 132 */ 133 /**@{*/ 134 void printInterpreterError(const char *, IdentifierNode *, ScopeObject *); 135 char *copyString(char *); 136 unsigned int isDecString(const char *); 137 unsigned int isHexString(const char *); 138 char *resolveIdentifierName(IdentifierNode *, ScopeObject *); 139 int resolveTerminalSlot(ScopeObject *, ScopeObject *, IdentifierNode *, ScopeObject **, IdentifierNode **); 140 /**@}*/ 141 142 /** 143 * \name Value object modifiers 144 * 145 * Functions for creating, copying, and deleting value objects. 146 */ 147 /**@{*/ 148 ValueObject *createNilValueObject(void); 149 ValueObject *createBooleanValueObject(int); 150 ValueObject *createIntegerValueObject(long long); 151 ValueObject *createFloatValueObject(float); 152 ValueObject *createStringValueObject(char *); 153 ValueObject *createFunctionValueObject(FuncDefStmtNode *); 154 ValueObject *createArrayValueObject(ScopeObject *); 155 ValueObject *createBlobValueObject(void *); 156 ValueObject *copyValueObject(ValueObject *); 157 void deleteValueObject(ValueObject *); 158 /**@}*/ 159 160 /** 161 * \name Scope object modifiers 162 * 163 * Functions for manipulating scope objects and their data. 164 */ 165 /**@{*/ 166 ScopeObject *createScopeObject(ScopeObject *); 167 ScopeObject *createScopeObjectCaller(ScopeObject *, ScopeObject *); 168 void deleteScopeObject(ScopeObject *); 169 ValueObject *createScopeValue(ScopeObject *, ScopeObject *, IdentifierNode *); 170 ValueObject *updateScopeValue(ScopeObject *, ScopeObject *, IdentifierNode *, ValueObject *); 171 ValueObject *getScopeValue(ScopeObject *, ScopeObject *, IdentifierNode *); 172 ValueObject *getScopeValueLocal(ScopeObject *, ScopeObject *, IdentifierNode *); 173 ScopeObject *getScopeObject(ScopeObject *, ScopeObject *, IdentifierNode *); 174 ScopeObject *getScopeObjectLocal(ScopeObject *, ScopeObject *, IdentifierNode *); 175 void deleteScopeValue(ScopeObject *, ScopeObject *, IdentifierNode *); 176 /**@}*/ 177 178 /** 179 * \name Return object modifiers 180 * 181 * Functions for creating and deleting return objects. 182 */ 183 /**@{*/ 184 ReturnObject *createReturnObject(ReturnType, ValueObject *); 185 void deleteReturnObject(ReturnObject *); 186 /**@}*/ 187 188 /** 189 * \name Casts 190 * 191 * Functions for performing casts between different types of values. 192 */ 193 /**@{*/ 194 ValueObject *castBooleanImplicit(ValueObject *, ScopeObject *); 195 ValueObject *castIntegerImplicit(ValueObject *, ScopeObject *); 196 ValueObject *castFloatImplicit(ValueObject *, ScopeObject *); 197 ValueObject *castStringImplicit(ValueObject *, ScopeObject *); 198 ValueObject *castBooleanExplicit(ValueObject *, ScopeObject *); 199 ValueObject *castIntegerExplicit(ValueObject *, ScopeObject *); 200 ValueObject *castFloatExplicit(ValueObject *, ScopeObject *); 201 ValueObject *castStringExplicit(ValueObject *, ScopeObject *); 202 /**@}*/ 203 204 /** 205 * \name Node interpreters 206 * 207 * Functions for interpreting basic parse tree nodes. 208 */ 209 /**@{*/ 210 ValueObject *interpretExprNode(ExprNode *, ScopeObject *); 211 ReturnObject *interpretStmtNode(StmtNode *, ScopeObject *); 212 ReturnObject *interpretStmtNodeList(StmtNodeList *, ScopeObject *); 213 ReturnObject *interpretBlockNode(BlockNode *, ScopeObject *); 214 int interpretMainNodeScope(MainNode *, ScopeObject *); 215 /**@}*/ 216 217 /** 218 * \name Expression interpreters 219 * 220 * Functions for interpreting expression parse tree nodes. 221 */ 222 /**@{*/ 223 ValueObject *interpretImpVarExprNode(ExprNode *, ScopeObject *); 224 ValueObject *interpretCastExprNode(ExprNode *, ScopeObject *); 225 ValueObject *interpretFuncCallExprNode(ExprNode *, ScopeObject *); 226 ValueObject *interpretIdentifierExprNode(ExprNode *, ScopeObject *); 227 ValueObject *interpretConstantExprNode(ExprNode *, ScopeObject *); 228 ValueObject *interpretSystemCommandExprNode(ExprNode *, ScopeObject *); 229 /**@}*/ 230 231 /** 232 * \name Operation interpreters 233 * 234 * Functions for interpreting operation parse tree nodes. 235 */ 236 /**@{*/ 237 ValueObject *interpretNotOpExprNode(OpExprNode *, ScopeObject *); 238 ValueObject *interpretArithOpExprNode(OpExprNode *, ScopeObject *); 239 ValueObject *interpretBoolOpExprNode(OpExprNode *, ScopeObject *); 240 ValueObject *interpretEqualityOpExprNode(OpExprNode *, ScopeObject *); 241 ValueObject *interpretConcatOpExprNode(OpExprNode *, ScopeObject *); 242 ValueObject *interpretOpExprNode(ExprNode *, ScopeObject *); 243 /**@}*/ 244 245 /** 246 * \name Statement interpreters 247 * 248 * Functions for interpreting statement parse tree nodes. 249 */ 250 /**@{*/ 251 ReturnObject *interpretCastStmtNode(StmtNode *, ScopeObject *); 252 ReturnObject *interpretPrintStmtNode(StmtNode *, ScopeObject *); 253 ReturnObject *interpretInputStmtNode(StmtNode *, ScopeObject *); 254 ReturnObject *interpretAssignmentStmtNode(StmtNode *, ScopeObject *); 255 ReturnObject *interpretDeclarationStmtNode(StmtNode *, ScopeObject *); 256 ReturnObject *interpretIfThenElseStmtNode(StmtNode *, ScopeObject *); 257 ReturnObject *interpretSwitchStmtNode(StmtNode *, ScopeObject *); 258 ReturnObject *interpretBreakStmtNode(StmtNode *, ScopeObject *); 259 ReturnObject *interpretReturnStmtNode(StmtNode *, ScopeObject *); 260 ReturnObject *interpretLoopStmtNode(StmtNode *, ScopeObject *); 261 ReturnObject *interpretDeallocationStmtNode(StmtNode *, ScopeObject *); 262 ReturnObject *interpretFuncDefStmtNode(StmtNode *, ScopeObject *); 263 ReturnObject *interpretExprStmtNode(StmtNode *, ScopeObject *); 264 ReturnObject *interpretAltArrayDefStmtNode(StmtNode *, ScopeObject *); 265 ReturnObject *interpretBindingStmtNode(StmtNode *, ScopeObject *); 266 /* Forward declaration of binding.h function (to break circular dependence) */ 267 void loadLibrary(ScopeObject *, IdentifierNode *); 268 ReturnObject *interpretImportStmtNode(StmtNode *, ScopeObject *); 269 /**@}*/ 270 271 /** 272 * \name Arithmetic operations (integer-integer) 273 * 274 * Functions for performing integer-integer operations on values. 275 */ 276 /**@{*/ 277 ValueObject *opAddIntegerInteger(ValueObject *, ValueObject *); 278 ValueObject *opSubIntegerInteger(ValueObject *, ValueObject *); 279 ValueObject *opMultIntegerInteger(ValueObject *, ValueObject *); 280 ValueObject *opDivIntegerInteger(ValueObject *, ValueObject *); 281 ValueObject *opMaxIntegerInteger(ValueObject *, ValueObject *); 282 ValueObject *opMinIntegerInteger(ValueObject *, ValueObject *); 283 ValueObject *opModIntegerInteger(ValueObject *, ValueObject *); 284 /**@}*/ 285 286 /** 287 * \name Arithmetic operations (integer-float) 288 * 289 * Functions for performing integer-float operations on values. 290 */ 291 /**@{*/ 292 ValueObject *opAddIntegerFloat(ValueObject *, ValueObject *); 293 ValueObject *opSubIntegerFloat(ValueObject *, ValueObject *); 294 ValueObject *opMultIntegerFloat(ValueObject *, ValueObject *); 295 ValueObject *opDivIntegerFloat(ValueObject *, ValueObject *); 296 ValueObject *opMaxIntegerFloat(ValueObject *, ValueObject *); 297 ValueObject *opMinIntegerFloat(ValueObject *, ValueObject *); 298 ValueObject *opModIntegerFloat(ValueObject *, ValueObject *); 299 /**@}*/ 300 301 /** 302 * \name Arithmetic operations (float-integer) 303 * 304 * Functions for performing float-integer operations on values. 305 */ 306 /**@{*/ 307 ValueObject *opAddFloatInteger(ValueObject *, ValueObject *); 308 ValueObject *opSubFloatInteger(ValueObject *, ValueObject *); 309 ValueObject *opMultFloatInteger(ValueObject *, ValueObject *); 310 ValueObject *opDivFloatInteger(ValueObject *, ValueObject *); 311 ValueObject *opMaxFloatInteger(ValueObject *, ValueObject *); 312 ValueObject *opMinFloatInteger(ValueObject *, ValueObject *); 313 ValueObject *opModFloatInteger(ValueObject *, ValueObject *); 314 /**@}*/ 315 316 /** 317 * \name Arithmetic operations (float-float) 318 * 319 * Functions for performing float-float operations on values. 320 */ 321 /**@{*/ 322 ValueObject *opAddFloatFloat(ValueObject *, ValueObject *); 323 ValueObject *opSubFloatFloat(ValueObject *, ValueObject *); 324 ValueObject *opMultFloatFloat(ValueObject *, ValueObject *); 325 ValueObject *opDivFloatFloat(ValueObject *, ValueObject *); 326 ValueObject *opMaxFloatFloat(ValueObject *, ValueObject *); 327 ValueObject *opMinFloatFloat(ValueObject *, ValueObject *); 328 ValueObject *opModFloatFloat(ValueObject *, ValueObject *); 329 /**@}*/ 330 331 /** 332 * \name Equality operations (boolean-boolean) 333 * 334 * Functions for performing boolean-boolean operations on values. 335 */ 336 /**@{*/ 337 ValueObject *opEqBooleanBoolean(ValueObject *, ValueObject *); 338 ValueObject *opNeqBooleanBoolean(ValueObject *, ValueObject *); 339 /**@}*/ 340 341 /** 342 * \name Equality operations (integer-integer) 343 * 344 * Functions for performing integer-integer operations on values. 345 */ 346 /**@{*/ 347 ValueObject *opEqIntegerInteger(ValueObject *, ValueObject *); 348 ValueObject *opNeqIntegerInteger(ValueObject *, ValueObject *); 349 /**@}*/ 350 351 /** 352 * \name Equality operations (integer-float) 353 * 354 * Functions for performing integer-float operations on values. 355 */ 356 /**@{*/ 357 ValueObject *opEqIntegerFloat(ValueObject *, ValueObject *); 358 ValueObject *opNeqIntegerFloat(ValueObject *, ValueObject *); 359 /**@}*/ 360 361 /** 362 * \name Equality operations (float-integer) 363 * 364 * Functions for performing float-integer operations on values. 365 */ 366 /**@{*/ 367 ValueObject *opEqFloatInteger(ValueObject *, ValueObject *); 368 ValueObject *opNeqFloatInteger(ValueObject *, ValueObject *); 369 /**@}*/ 370 371 /** 372 * \name Equality operations (float-float) 373 * 374 * Functions for performing float-float operations on values. 375 */ 376 /**@{*/ 377 ValueObject *opEqFloatFloat(ValueObject *, ValueObject *); 378 ValueObject *opNeqFloatFloat(ValueObject *, ValueObject *); 379 /**@}*/ 380 381 /** 382 * \name Equality operations (string-string) 383 * 384 * Functions for performing string-string operations on values. 385 */ 386 /**@{*/ 387 ValueObject *opEqStringString(ValueObject *, ValueObject *); 388 ValueObject *opNeqStringString(ValueObject *, ValueObject *); 389 /**@}*/ 390 391 /** 392 * \name Equality operations (nil-nil) 393 * 394 * Functions for performing nil-nil operations on values. 395 */ 396 /**@{*/ 397 ValueObject *opEqNilNil(ValueObject *, ValueObject *); 398 ValueObject *opNeqNilNil(ValueObject *, ValueObject *); 399 /**@}*/ 400 401 #endif /* __INTERPRETER_H__ */ 402