| | 1 | #include "tads.h" |
| | 2 | #include "t3.h" |
| | 3 | #include "lookup.h" |
| | 4 | |
| | 5 | enum RED, GREEN, BLUE; |
| | 6 | |
| | 7 | class Item: object |
| | 8 | sdesc = "" |
| | 9 | ldesc = "" |
| | 10 | ; |
| | 11 | |
| | 12 | book: Item |
| | 13 | sdesc = "book" |
| | 14 | ldesc = "This is the book." |
| | 15 | ; |
| | 16 | |
| | 17 | box: Item |
| | 18 | sdesc = "box" |
| | 19 | ldesc = "This is the box." |
| | 20 | ; |
| | 21 | |
| | 22 | ball: Item |
| | 23 | sdesc = "ball" |
| | 24 | ldesc = "This is the ball." |
| | 25 | ; |
| | 26 | |
| | 27 | globals: PreinitObject |
| | 28 | execute() |
| | 29 | { |
| | 30 | symtab = t3GetGlobalSymbols(); |
| | 31 | } |
| | 32 | |
| | 33 | /* |
| | 34 | * the global symbol table object - stash it here so it remains |
| | 35 | * around at run-time |
| | 36 | */ |
| | 37 | symtab = nil |
| | 38 | ; |
| | 39 | |
| | 40 | main(args) |
| | 41 | { |
| | 42 | "Symbol table test\n"; |
| | 43 | |
| | 44 | /* check if t3GetGlobalSymbols() gives us anything */ |
| | 45 | if (t3GetGlobalSymbols() == nil) |
| | 46 | "No debug symbols available\n"; |
| | 47 | else |
| | 48 | "Debug symbols are available\n"; |
| | 49 | |
| | 50 | "book.sdesc: <<sendMessage('book', 'sdesc')>>\n"; |
| | 51 | "ball.ldesc: <<sendMessage('ball', 'ldesc')>>\n"; |
| | 52 | "sayVal(5): <<callFunc('sayVal', 5)>>\n"; |
| | 53 | } |
| | 54 | |
| | 55 | sayVal(val) |
| | 56 | { |
| | 57 | "This is sayVal: val = <<val>>\n"; |
| | 58 | } |
| | 59 | |
| | 60 | callFunc(funcName, [args]) |
| | 61 | { |
| | 62 | local func; |
| | 63 | |
| | 64 | /* look up the function */ |
| | 65 | func = globals.symtab[funcName]; |
| | 66 | if (func == nil) |
| | 67 | "no such function: '<<funcName>>'"; |
| | 68 | else if (dataType(func) != TypeFuncPtr) |
| | 69 | "not a function: '<<funcName>>'"; |
| | 70 | else |
| | 71 | { |
| | 72 | try |
| | 73 | { |
| | 74 | /* invoke the function with the given argument list */ |
| | 75 | (func)(args...); |
| | 76 | } |
| | 77 | catch (Exception exc) |
| | 78 | { |
| | 79 | "error calling <<funcName>>(): <<exc.displayException()>>"; |
| | 80 | } |
| | 81 | } |
| | 82 | } |
| | 83 | |
| | 84 | sendMessage(objName, propName, [args]) |
| | 85 | { |
| | 86 | local obj, prop; |
| | 87 | |
| | 88 | /* look up the object */ |
| | 89 | obj = globals.symtab[objName]; |
| | 90 | if (obj == nil) |
| | 91 | "no such object: '<<objName>>'"; |
| | 92 | else if (dataType(obj) != TypeObject) |
| | 93 | "not an object: '<<objName>>'"; |
| | 94 | else |
| | 95 | { |
| | 96 | /* look up the property */ |
| | 97 | prop = globals.symtab[propName]; |
| | 98 | if (prop == nil) |
| | 99 | "no such property: '<<propName>>'"; |
| | 100 | else if (dataType(prop) != TypeProp) |
| | 101 | "not a property: '<<propName>>'"; |
| | 102 | else |
| | 103 | { |
| | 104 | try |
| | 105 | { |
| | 106 | /* invoke the property with the given argument list */ |
| | 107 | obj.(prop)(args...); |
| | 108 | } |
| | 109 | catch (Exception exc) |
| | 110 | { |
| | 111 | "error calling <<objName>>.<<propName>>: |
| | 112 | <<exc.displayException()>>"; |
| | 113 | } |
| | 114 | } |
| | 115 | } |
| | 116 | } |
| | 117 | |