Changeset 115
- Timestamp:
- 09/18/07 02:41:33 (1 year ago)
- Files:
-
- trunk/blade/Blade.d (modified) (1 diff)
- trunk/blade/SyntaxTree.d (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/blade/Blade.d
r112 r115 52 52 53 53 public import SyntaxTree : mixin_SymbolTable, AbstractSyntaxTree, syntaxtreeof, AST, Symbol; 54 private import BladeUtil ;54 private import BladeUtil : wrapInQuotes; 55 55 private import BladeRank; 56 56 public import CodegenX86 : generateCodeForAsmX87, generateCodeForSSE2, isSSE2AsmPossible; trunk/blade/SyntaxTree.d
r114 r115 38 38 */ 39 39 module SyntaxTree; 40 import BladeUtil ;40 import BladeUtil : wrapInQuotes; 41 41 42 42 public: … … 45 45 * Returns a string, which, when mixed in, lexes, parses, and semantically 46 46 * analyses the given expression, then creates a struct literal of type 47 * AbstractSyntaxTree, containing a symbol table, and a syntax tree in a 48 * very simple placeholder format, which refers to the elements in the symbol table. 47 * AbstractSyntaxTree, containing a symbol table and a syntax tree. 49 48 * 50 49 * Syntax trees are represented as placeholder expressions, with associativity 51 * and precedence indicated by parentheses. 50 * and precedence indicated by parentheses. The placeholders are indices into 51 * the symbol table. 52 52 * 53 53 … … 81 81 struct AbstractSyntaxTree { 82 82 char [] expression; /// syntax tree in Placeholder format, eg A+=(B*C) 83 Symbol[] symbolTable; /// T he types and values of A,B,C,...83 Symbol[] symbolTable; /// Textual form of the types and values of A,B,C,... 84 84 } 85 85 … … 88 88 // ==== LEXER ==== 89 89 90 /// Return true if text is a D keyword 90 /** Return true if text is a D keyword 91 */ 91 92 bool isKeyword(char [] text) { 92 93 char [][] keywords = [ "abstract", "alias", "align", "asm", "assert", … … 136 137 for(int i=0; i<expr.length; ++i) { 137 138 char c = expr[i]; 138 if (quote!=0) { // If inside a string literal, wait until it ends 139 // Deal with string literals 140 if (quote!=0) { 139 141 symbol~=c; 140 142 if (c==quote && (symbol.length<1 || symbol[$-1]!='\\')) quote=0; … … 177 179 code ~= "."; 178 180 } 179 code ~= c; 181 if (c!='/' || i >= expr.length-1) code ~= c; 182 else { 183 // Remove comments 184 if (expr[i..i+2]=="//") { 185 for ( ; i<expr.length && expr[i]!='\n' ; ++i) {} 186 } else if (expr[i..i+2]=="/*") { 187 i+=3; 188 for ( ; i<expr.length && expr[i-1..i+1] != "*/"; ++i) {} 189 if ( i >= expr.length-1 ) break; 190 } else if (expr[i..i+2]=="/+") { 191 i++; 192 for (int nest = 1; nest>0 && i < expr.length-1; ++i) { 193 if (expr[i..i+2] == "+/") { --nest; ++i; } 194 else if (expr[i..i+2] == "/+") { ++nest; ++i; } 195 } 196 --i; 197 if (i >= expr.length-1) break; 198 } else code~=c; 199 } 180 200 } 181 201 } … … 183 203 expr = code; 184 204 return symbols; 205 } 206 207 unittest { 208 char [] expr = "xyzzy+ y/+ comment/++ /++//++/+/ +/*2"; 209 char [][] symbols = replaceSymbolsWithPlaceholders(expr); 210 assert(expr == "A+ B*C"); 185 211 } 186 212
