1 /* Digital Mars DMDScript source code. 2 * Copyright (c) 2000-2002 by Chromium Communications 3 * D version Copyright (c) 2004-2010 by Digital Mars 4 * Distributed under the Boost Software License, Version 1.0. 5 * (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 * written by Walter Bright 7 * http://www.digitalmars.com 8 * 9 * D2 port by Dmitry Olshansky 10 * 11 * DMDScript is implemented in the D Programming Language, 12 * http://www.digitalmars.com/d/ 13 * 14 * For a C++ implementation of DMDScript, including COM support, see 15 * http://www.digitalmars.com/dscript/cppscript.html 16 */ 17 18 19 module dmdscript.symbol; 20 21 import std.stdio; 22 23 import dmdscript.script; 24 import dmdscript.identifier; 25 import dmdscript.scopex; 26 import dmdscript.statement; 27 import dmdscript.irstate; 28 import dmdscript.opcodes; 29 import dmdscript.errmsgs; 30 31 /****************************** Symbol ******************************/ 32 33 class Symbol 34 { 35 Identifier *ident; 36 37 this() 38 { 39 } 40 41 this(Identifier * ident) 42 { 43 this.ident = ident; 44 } 45 46 override bool opEquals(Object o) 47 { 48 Symbol s; 49 50 if(this == o) 51 return true; 52 s = cast(Symbol)o; 53 if(s && ident == s.ident) 54 return true; 55 return false; 56 } 57 58 override string toString() 59 { 60 return ident ? "__ident" : "__anonymous"; 61 } 62 63 void semantic(Scope *sc) 64 { 65 assert(0); 66 } 67 68 Symbol search(Identifier *ident) 69 { 70 assert(0); 71 //error(DTEXT("%s.%s is undefined"),toString(), ident.toString()); 72 } 73 74 void toBuffer(ref tchar[] buf) 75 { 76 buf ~= toString(); 77 } 78 } 79 80 81 /********************************* ScopeSymbol ****************************/ 82 83 // Symbol that generates a scope 84 85 class ScopeSymbol : Symbol 86 { 87 Symbol[] members; // all Symbol's in this scope 88 SymbolTable *symtab; // member[] sorted into table 89 90 this() 91 { 92 super(); 93 } 94 95 this(Identifier * id) 96 { 97 super(id); 98 } 99 100 override Symbol search(Identifier *ident) 101 { 102 Symbol s; 103 104 //writef("ScopeSymbol::search(%s, '%s')\n", toString(), ident.toString()); 105 // Look in symbols declared in this module 106 s = symtab ? symtab.lookup(ident) : null; 107 if(s) 108 writef("\ts = '%s.%s'\n", toString(), s.toString()); 109 return s; 110 } 111 } 112 113 114 /****************************** SymbolTable ******************************/ 115 116 // Table of Symbol's 117 118 struct SymbolTable 119 { 120 Symbol[Identifier *] members; 121 122 // Look up Identifier. Return Symbol if found, NULL if not. 123 Symbol lookup(Identifier *ident) 124 { 125 Symbol *ps; 126 127 ps = ident in members; 128 if(ps) 129 return *ps; 130 return null; 131 } 132 133 // Insert Symbol in table. Return NULL if already there. 134 Symbol insert(Symbol s) 135 { 136 Symbol *ps; 137 138 ps = s.ident in members; 139 if(ps) 140 return null; // already in table 141 members[s.ident] = s; 142 return s; 143 } 144 145 // Look for Symbol in table. If there, return it. 146 // If not, insert s and return that. 147 Symbol update(Symbol s) 148 { 149 members[s.ident] = s; 150 return s; 151 } 152 } 153 154 155 /****************************** FunctionSymbol ******************************/ 156 157 class FunctionSymbol : ScopeSymbol 158 { 159 Loc loc; 160 161 Identifier*[] parameters; // array of Identifier's 162 TopStatement[] topstatements; // array of TopStatement's 163 164 SymbolTable labtab; // symbol table for LabelSymbol's 165 166 IR *code; 167 uint nlocals; 168 169 this(Loc loc, Identifier * ident, Identifier *[] parameters, 170 TopStatement[] topstatements) 171 { 172 super(ident); 173 this.loc = loc; 174 this.parameters = parameters; 175 this.topstatements = topstatements; 176 } 177 178 override void semantic(Scope *sc) 179 { 180 } 181 } 182 183 184 /****************************** LabelSymbol ******************************/ 185 186 class LabelSymbol : Symbol 187 { Loc loc; 188 LabelStatement statement; 189 190 this(Loc loc, Identifier * ident, LabelStatement statement) 191 { 192 super(ident); 193 this.loc = loc; 194 this.statement = statement; 195 } } 196 197