Overview
SHA1 Hash: | 3f6f41b5583ea639ed08f7cef0f6f0beb7c555d4 |
---|---|
Date: | 2010-11-20 21:57:15 |
User: | kinaba |
Comment: | ast - table conversion (NOT AT ALL TESTED) |
Timelines: | family | ancestors | descendants | both | trunk |
Downloads: | Tarball | ZIP archive |
Other Links: | files | file ages | manifest |
Tags And Properties
- branch=trunk inherited from [f65680e1d2]
- sym-trunk inherited from [f65680e1d2]
Changes
Modified main.d from [b6f89f38f0a1626d] to [d5ef45952651bda8].
47 47 { 48 48 writef(">> ", lineno); 49 49 string line = readln(); 50 50 if( line.startsWith("exit") || line.startsWith("quit") ) 51 51 return false; 52 52 try { 53 53 if( tryRun(line) ) 54 + { 55 + // for debugging. 56 + //try { 57 + // writeln(tableToAST("@v", cast(Table)lastVal)); 58 + //} catch(Throwable e) { 59 + // writeln(e); 60 + //} 54 61 writeln(lastVal); 62 + } 55 63 } catch(Throwable e) { 56 64 writeln(e); 57 65 } 58 66 return true; 59 67 } 60 68 } 61 69
Modified polemy/eval.d from [6db0ce492da06d00] to [4f24d3bd11889ec1].
177 177 ctxNeo.set(p.name, lay, args[i]); 178 178 auto v = eval(e.funbody, ctxNeo, true, lay); 179 179 // auto memoization 180 180 if( lay != "@v" ) 181 181 memo[lay][args] = v; 182 182 return v; 183 183 }); 184 + }, 185 + delegate Value (AST e) 186 + { 187 + throw genex!RuntimeException(e.pos, sprintf!"Unknown Kind of Expression %s"(typeid(e))); 188 + } 189 + ); 190 +} 191 + 192 +// [TODO] Optimization 193 +Value macroEval(AST e, Table ctx, bool AlwaysMacro) 194 +{ 195 + Layer theLayer = "@v"; 196 + 197 + Table pos = new Table; 198 + pos.set("filename", theLayer, new StrValue(e.pos.filename)); 199 + pos.set("lineno", theLayer, new IntValue(BigInt(e.pos.lineno))); 200 + pos.set("column", theLayer, new IntValue(BigInt(e.pos.column))); 201 + return e.match( 202 + (StrLiteral e) 203 + { 204 + Table t = new Table; 205 + t.set("pos", theLayer, pos); 206 + t.set("is", theLayer, new StrValue("str")); 207 + t.set("data", theLayer, new StrValue(e.data)); 208 + return t; 209 + }, 210 + (IntLiteral e) 211 + { 212 + Table t = new Table; 213 + t.set("pos", theLayer, pos); 214 + t.set("is", theLayer, new StrValue("int")); 215 + t.set("data", theLayer, new IntValue(e.data)); 216 + return t; 217 + }, 218 + (VarExpression e) 219 + { 220 + Table t = new Table; 221 + t.set("pos", theLayer, pos); 222 + t.set("is", theLayer, new StrValue("var")); 223 + t.set("name", theLayer, new StrValue(e.var)); 224 + return t; 225 + }, 226 + (LayeredExpression e) 227 + { 228 + if( AlwaysMacro ) 229 + { 230 + Table t = new Table; 231 + t.set("pos", theLayer, pos); 232 + t.set("is", theLayer, new StrValue("lay")); 233 + t.set("expr", theLayer, macroEval(e.expr,ctx,AlwaysMacro)); 234 + return cast(Value)t; 235 + } 236 + else 237 + { 238 + return eval(e.expr, ctx, true, e.lay); 239 + } 240 + }, 241 + (LetExpression e) 242 + { 243 + Table t = new Table; 244 + t.set("pos", theLayer, pos); 245 + t.set("is", theLayer, new StrValue("let")); 246 + t.set("name", theLayer, new StrValue(e.var)); 247 + t.set("init", theLayer, macroEval(e.init,ctx,AlwaysMacro)); 248 + t.set("expr", theLayer, macroEval(e.expr,ctx,AlwaysMacro)); 249 + return t; 250 + }, 251 + (FuncallExpression e) 252 + { 253 + // [TODO] @macro invokation!!!! 254 + // if e.fun is varname and its ctx[@macro] is set, switch to usual eval 255 + Table t = new Table; 256 + t.set("pos", theLayer, pos); 257 + t.set("is", theLayer, new StrValue("app")); 258 + t.set("fun", theLayer, macroEval(e.fun,ctx,AlwaysMacro)); 259 + Table args = new Table; 260 + foreach_reverse(a; e.args) { 261 + Table cons = new Table; 262 + cons.set("car",theLayer,macroEval(a,ctx,AlwaysMacro)); 263 + cons.set("cdr",theLayer,args); 264 + args = cons; 265 + } 266 + t.set("arg", theLayer, args); 267 + return t; 268 + }, 269 + (FunLiteral e) 270 + { 271 + Table t = new Table; 272 + t.set("pos", theLayer, pos); 273 + t.set("is", theLayer, new StrValue("fun")); 274 + t.set("body", theLayer, macroEval(e.funbody,ctx,AlwaysMacro)); 275 + Table param = new Table; 276 + foreach_reverse(p; e.params) 277 + { 278 + Table cons = new Table; 279 + Table kv = new Table; 280 + kv.set("name", theLayer, new StrValue(p.name)); 281 + foreach_reverse(lay; p.layers) 282 + { 283 + Table cons2 = new Table; 284 + cons2.set("car", theLayer, new StrValue(lay)); 285 + cons2.set("cdr", theLayer, kv); 286 + kv = cons2; 287 + } 288 + cons.set("car", theLayer, kv); 289 + cons.set("cdr", theLayer, param); 290 + param = cons; 291 + } 292 + t.set("param", theLayer, param); 293 + return t; 184 294 }, 185 295 delegate Value (AST e) 186 296 { 187 297 throw genex!RuntimeException(e.pos, sprintf!"Unknown Kind of Expression %s"(typeid(e))); 188 298 } 189 299 ); 190 300 }
Modified polemy/lex.d from [f9cdaf211a0e9fb0] to [514757ca55e253b4].
10 10 import std.ctype : isspace, isalnum; 11 11 12 12 /*mixin*/ 13 13 template ExceptionWithPosition() 14 14 { 15 15 const LexPosition pos; 16 16 this( const LexPosition pos, string msg, string file=null, size_t line=0, Throwable next=null ) 17 - { super(sprintf!"[%s] %s"(pos, msg), file, line, next); this.pos = pos; } 17 + { 18 + if(pos is null) 19 + super(sprintf!"[??] %s"(msg), file, line, next); 20 + else 21 + super(sprintf!"[%s] %s"(pos, msg), file, line, next); 22 + this.pos = pos; 23 + } 18 24 } 19 25 20 26 /// Thrown when encountered an EOF in the middle of a lexical token 21 27 22 28 class UnexpectedEOF : Exception 23 29 { 24 30 mixin ExceptionWithPosition;
Modified polemy/value.d from [a4c75e644b34fb64] to [10fa9090c50cdaf9].
3 3 * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 4 4 * 5 5 * Runtime data structures for Polemy programming language. 6 6 */ 7 7 module polemy.value; 8 8 import polemy._common; 9 9 import polemy.lex; 10 +import polemy.ast; 10 11 11 12 /// Raised when something went wrong in runtime 12 13 13 14 class RuntimeException : Exception 14 15 { 15 16 mixin ExceptionWithPosition; 16 17 } ................................................................................ 129 130 throw genex!RuntimeException(pos, sprintf!"variable %s is not set in layer %s"(i,lay)); 130 131 return data[i][lay]; 131 132 } 132 133 if( prototype is null ) 133 134 throw new RuntimeException(pos, sprintf!"variable %s not found"(i)); 134 135 return prototype.get(i, lay, pos); 135 136 } 137 + 138 + T access(T,S...)( Layer lay, string path, S rest ) 139 + { 140 + static if( rest.length == 0 ) 141 + { 142 + if( this.has(path, lay) ) 143 + return cast(T) this.get(path, lay); 144 + } 145 + else 146 + { 147 + if(auto next = this.access!Table(lay,path)) 148 + return next.access!T(lay,rest); 149 + } 150 + return null; 151 + } 136 152 137 153 private: 138 154 Table prototype; 139 155 Kind kind; 140 156 Value[Layer][string] data; 141 157 142 158 bool setIfExist(string i, Layer lay, Value v) ................................................................................ 186 202 assert_eq( c013.get("z", "@v"), new IntValue(BigInt(0)) ); 187 203 assert_eq( c012.get("z", "@v"), new IntValue(BigInt(555)) ); 188 204 assert_eq( c01.get("z", "@v"), new IntValue(BigInt(0)) ); 189 205 assert_eq( c0.get("z", "@v"), new IntValue(BigInt(0)) ); 190 206 191 207 // [TODO] define the semantics and test @layers 192 208 } 209 + 210 +immutable(LexPosition) extractPos( Table t ) 211 +{ 212 + Layer theLayer = "@v"; 213 + if(auto tt = t.access!Table(theLayer, "pos")) 214 + { 215 + auto fn = tt.access!StrValue(theLayer, "filename"); 216 + auto ln = tt.access!IntValue(theLayer, "lineno"); 217 + auto cl = tt.access!IntValue(theLayer, "column"); 218 + if(fn !is null && ln !is null && cl !is null) 219 + return new immutable(LexPosition)(fn.data,cast(int)ln.data.toInt,cast(int)cl.data.toInt); 220 + } 221 + return null; 222 +} 223 + 224 +Value[] tableAsConsList( Layer theLayer, Table t ) 225 +{ 226 + Value[] result; 227 + while(t) 228 + if(auto v = t.access!Value(theLayer, "car")) 229 + { 230 + result ~= v; 231 + t = t.access!Table(theLayer, "cdr"); 232 + } 233 + else 234 + break; 235 + return result; 236 +} 237 + 238 +AST[] tableToASTList( Layer theLayer, Table t ) 239 +{ 240 + AST[] result; 241 + foreach(v; tableAsConsList(theLayer, t)) 242 + if(auto t = cast(Table)v) 243 + result ~= tableToAST(theLayer,t); 244 + else 245 + throw genex!RuntimeException(cast(LexPosition)null, "Invalid AST (non-table in cons-list)"); 246 + return result; 247 +} 248 + 249 +AST tableToAST( Layer theLayer, Table t ) 250 +{ 251 + auto nodeType = t.access!StrValue(theLayer, "is"); 252 + if( nodeType is null ) 253 + throw genex!RuntimeException(cast(LexPosition)null, "Invalid AST {is:(not string)}"); 254 + auto pos = extractPos(t); 255 + switch(nodeType.data) 256 + { 257 + case "int": 258 + if(auto v = t.access!IntValue(theLayer, "data")) 259 + return new IntLiteral(pos, v.data); 260 + throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST {is:"int", data:(not int)}`); 261 + case "str": 262 + if(auto v = t.access!StrValue(theLayer, "data")) 263 + return new StrLiteral(pos, v.data); 264 + throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST {is:"str", data:(not string)}`); 265 + case "var": 266 + if(auto v = t.access!StrValue(theLayer, "name")) 267 + return new VarExpression(pos, v.data); 268 + throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST {is:"var", name:(not string)}`); 269 + case "lay": 270 + if(auto v = t.access!StrValue(theLayer, "layer")) 271 + if(auto e = t.access!Table(theLayer, "expr")) 272 + return new LayeredExpression(pos, v.data, tableToAST(theLayer,e)); 273 + else 274 + throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST {is:"lay", expr:(not table)}`); 275 + throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST {is:"lay", layer:(not string)}`); 276 + case "let": 277 + if(auto n = t.access!StrValue(theLayer, "name")) 278 + if(auto e = t.access!Table(theLayer, "init")) 279 + if(auto b = t.access!Table(theLayer, "expr")) 280 + { 281 + string nn = n.data; 282 + auto ee = tableToAST(theLayer, e); 283 + auto bb = tableToAST(theLayer, b); 284 + Layer lay=""; 285 + if(auto l = t.access!StrValue(theLayer, "layer")) 286 + lay = l.data; 287 + return new LetExpression(pos, nn, lay, ee, bb); 288 + } 289 + throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST {is:"let", name:"???", init:"???", expr:"???"}`); 290 + case "app": 291 + if(auto f = t.access!Table(theLayer, "fun")) 292 + if(auto a = t.access!Table(theLayer, "arg")) 293 + return new FuncallExpression(pos, tableToAST(theLayer,f), tableToASTList(theLayer,a)); 294 + throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST {is:"app", fun:???, arg:???}`); 295 + case "fun": 296 + if(auto p = t.access!Table(theLayer, "param")) 297 + if(auto b = t.access!Table(theLayer, "body")) 298 + { 299 + Parameter[] ps; 300 + foreach(v; tableAsConsList(theLayer, p)) 301 + { 302 + if(auto tt = cast(Table)v) 303 + if(auto ss = tt.access!StrValue(theLayer, "name")) 304 + if(auto ll = tt.access!Table(theLayer, "layer")) 305 + { 306 + Layer[] ls; 307 + foreach(lll; tableAsConsList(theLayer, ll)) 308 + if(auto l = cast(StrValue)lll) 309 + ls ~= l.data; 310 + else 311 + throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST {bad fun params}`); 312 + ps ~= new Parameter(ss.data, ls); 313 + continue; 314 + } 315 + else 316 + { 317 + Layer[] emp; 318 + ps ~= new Parameter(ss.data, emp); 319 + } 320 + throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST {bad fun params}`); 321 + } 322 + auto bb = tableToAST(theLayer, b); 323 + return new FunLiteral(pos,ps,bb); 324 + } 325 + throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST {is:"fun", param:???, body:???}`); 326 + default: 327 + throw genex!RuntimeException(cast(LexPosition)null, sprintf!`Invalid AST {is: "%s"} unknown`(nodeType.data)); 328 + } 329 +}