Differences From Artifact [76bd90b6c3824480]:
- File
polemy/valueconv.d
- 2010-11-23 10:09:03 - part of checkin [36c517dfc4] on branch trunk - refactored d-value and polemy-value conversion (user: kinaba) [annotate]
To Artifact [2816a291e16d0986]:
- File
polemy/valueconv.d
- 2010-11-23 13:55:15 - part of checkin [2134cd44cc] on branch trunk - further clean-up for polemy2d (user: kinaba) [annotate]
10 import polemy.ast; 10 import polemy.ast;
11 import polemy.layer; 11 import polemy.layer;
12 import polemy.value; 12 import polemy.value;
13 import std.string; 13 import std.string;
14 14
15 LexPosition extractPos( Table t ) 15 LexPosition extractPos( Table t )
16 { 16 {
17 Layer theLayer = ValueLayer; <
18 if(auto tt = t.access!Table(theLayer, "pos")) | 17 if(auto tt = t.access!Table(ValueLayer, "pos"))
19 { 18 {
20 auto fn = tt.access!StrValue(theLayer, "filename"); | 19 auto fn = tt.access!StrValue(ValueLayer, "filename");
21 auto ln = tt.access!IntValue(theLayer, "lineno"); | 20 auto ln = tt.access!IntValue(ValueLayer, "lineno");
22 auto cl = tt.access!IntValue(theLayer, "column"); | 21 auto cl = tt.access!IntValue(ValueLayer, "column");
23 if(fn !is null && ln !is null && cl !is null) 22 if(fn !is null && ln !is null && cl !is null)
24 return new LexPosition(fn.data,cast(int)ln.data.toInt,ca 23 return new LexPosition(fn.data,cast(int)ln.data.toInt,ca
25 } 24 }
26 return null; 25 return null;
27 } 26 }
28 27
29 Value[] tableAsConsList( Layer theLayer, Table t ) | 28 /// Experimental!! Convert Polemy value to D Value
30 { <
31 Value[] result; <
32 while(t) <
33 if(auto v = t.access!Value(theLayer, "car")) <
34 { <
35 result ~= v; <
36 t = t.access!Table(theLayer, "cdr"); <
37 } <
38 else <
39 break; <
40 return result; <
41 } <
42 29
43 AST[] tableToASTList( Layer theLayer, Table t ) | 30 T polemy2d(T)(Value _v, LexPosition callpos=null)
44 { 31 {
45 AST[] result; | 32 static if(is(T==BigInt))
46 foreach(v; tableAsConsList(theLayer, t)) <
> 33 {
47 if(auto t = cast(Table)v) | 34 if(auto v = cast(IntValue)_v)
48 result ~= tableToAST(theLayer,t); | 35 return v.data;
> 36 }
49 else | 37 else
50 throw genex!RuntimeException(cast(LexPosition)null, "Inv <
> 38 static if(isIntegral!(T))
> 39 {
> 40 if(auto v = cast(IntValue)_v)
> 41 return cast(T) v.data.toLong();
> 42 }
> 43 else
> 44 static if(is(T==string))
> 45 {
> 46 if(auto v = cast(StrValue)_v)
> 47 return v.data;
> 48 }
> 49 else
> 50 static if(is(T S : S[]))
> 51 {
> 52 if(auto t = cast(Table)_v)
> 53 {
> 54 S[] result;
> 55 foreach(e; t.toList())
> 56 result ~= polemy2d!(S)(e, callpos);
51 return result; | 57 return result;
52 } | 58 }
> 59 }
> 60 else
> 61 static if(is(T == AST))
> 62 {
> 63 if(auto t = cast(Table)_v)
> 64 {
> 65 LexPosition pos = extractPos(t);
53 66
54 AST tableToAST( Layer theLayer, Value vvvv ) | 67 StrValue typ = cast(StrValue) t.access!StrValue(ValueLay
55 { <
56 Table t = cast(Table)vvvv; <
57 if( t is null ) | 68 if( typ is null )
58 throw genex!RuntimeException(cast(LexPosition)null, "Invalid AST | 69 throw genex!RuntimeException(text(`Invalid AST (
59 70
60 auto nodeType = t.access!StrValue(theLayer, "is"); | 71 foreach(AT; ListOfASTTypes)
> 72 if(typ.data == typeid(AT).name.split(".")[$-1].t
> 73 {
> 74 typeof(AT.tupleof) mems;
> 75 foreach(i,m; mems)
> 76 {
> 77 string name = AT.tupleof[i].stri
> 78 Value vm = t.access!Value(ValueL
61 if( nodeType is null ) | 79 if( vm is null )
62 throw genex!RuntimeException(cast(LexPosition)null, "Invalid AST | 80 throw genex!RuntimeExcep
63 auto pos = extractPos(t); <
64 switch(nodeType.data) <
> 81 text(`Invalid AS
> 82 mems[i] = polemy2d!(typeof(m))(v
> 83 }
> 84 return new AT(pos,mems);
> 85 }
> 86 throw genex!RuntimeException(callpos, text(`Invalid AST
> 87 }
> 88 throw genex!RuntimeException(callpos, text(`Invalid AST (not a t
> 89 }
> 90 else
> 91 static if(is(T == class))
65 { 92 {
66 case "int": | 93 if(auto t = cast(Table)_v)
67 if(auto v = t.access!IntValue(theLayer, "data")) <
68 return new Int(pos, v.data); <
69 throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST <
70 case "str": <
71 if(auto v = t.access!StrValue(theLayer, "data")) <
72 return new Str(pos, v.data); <
73 throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST <
74 case "var": <
75 if(auto v = t.access!StrValue(theLayer, "name")) <
76 return new Var(pos, v.data); <
77 throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST <
78 case "lay": <
79 if(auto v = t.access!StrValue(theLayer, "layer")) <
80 if(auto e = t.access!Table(theLayer, "expr")) <
81 return new Lay(pos, v.data, tableToAST(theLayer, <
82 else <
83 throw genex!RuntimeException(cast(LexPosition)nu <
84 throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST <
85 case "let": <
86 if(auto n = t.access!StrValue(theLayer, "name")) <
87 if(auto e = t.access!Table(theLayer, "init")) <
88 if(auto b = t.access!Table(theLayer, "expr")) <
89 { 94 {
90 string nn = n.data; | 95 typeof(T.tupleof) mems;
91 auto ee = tableToAST(theLayer, e); | 96 foreach(i,m; mems)
92 auto bb = tableToAST(theLayer, b); | 97 mems[i] = polemy2d!(typeof(m))(t.get(T.tupleof[i
93 Layer lay=""; <
94 if(auto l = t.access!StrValue(theLayer, "layer")) <
95 lay = l.data; <
96 return new Let(pos, nn, lay, ee, bb); | 98 return new T(mems);
97 } 99 }
98 throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST <
99 case "app": <
100 if(auto f = t.access!Table(theLayer, "fun")) <
101 if(auto a = t.access!Table(theLayer, "args")) <
102 return new App(pos, tableToAST(theLayer,f), tableToASTLi <
103 throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST <
104 case "fun": <
105 if(auto p = t.access!Table(theLayer, "params")) <
106 if(auto b = t.access!Table(theLayer, "funbody")) <
107 { <
108 Parameter[] ps; <
109 foreach(v; tableAsConsList(theLayer, p)) <
110 { <
111 if(auto tt = cast(Table)v) <
112 if(auto ss = tt.access!StrValue(theLayer, "name" <
113 if(auto ll = tt.access!Table(theLayer, "layers") <
114 { <
115 Layer[] ls; <
116 foreach(lll; tableAsConsList(theLayer, l <
117 if(auto l = cast(StrValue)lll) <
118 ls ~= l.data; <
119 else <
120 throw genex!RuntimeExcep <
121 ps ~= new Parameter(ss.data, ls); <
122 continue; <
123 } <
124 else <
125 { <
126 Layer[] emp; <
127 ps ~= new Parameter(ss.data, emp); <
128 continue; <
129 } <
130 throw genex!RuntimeException(cast(LexPosition)nu <
131 } <
132 auto bb = tableToAST(theLayer, b); <
133 return new Fun(pos,ps,bb); <
134 } <
135 throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST <
136 default: <
137 throw genex!RuntimeException(cast(LexPosition)null, sprintf!`Inv <
138 } 100 }
> 101 else
> 102 static assert(false, "unknown type <"~T.stringof~"> during polem
> 103 throw genex!RuntimeException(callpos, text("Cannot convert ",_v," to ",T
139 } 104 }
140 105
141 /// Cons of two pairs 106 /// Cons of two pairs
142 107
143 Table makeCons(Value a, Value d) 108 Table makeCons(Value a, Value d)
144 { 109 {
145 Table t = new Table; 110 Table t = new Table;
146 t.set("car", ValueLayer, a); 111 t.set("car", ValueLayer, a);
147 t.set("cdr", ValueLayer, d); 112 t.set("cdr", ValueLayer, d);
148 return t; 113 return t;
149 } 114 }
150 115
151 /// Experimental!!! Convert D value (except AST) to Polemy Value | 116 /// Experimental!! Convert D value (except AST) to Polemy Value
152 117
153 Value d2polemy(T)(T e) 118 Value d2polemy(T)(T e)
154 { 119 {
155 return ast2table(e, delegate Value(AST){ assert(false); }); 120 return ast2table(e, delegate Value(AST){ assert(false); });
156 } 121 }
157 122
158 /// Convert AST to Table so that it can be used in Polemy 123 /// Convert AST to Table so that it can be used in Polemy
................................................................................................................................................................................
180 { 145 {
181 assert( typeid(e) == typeid(T), text("abstracted: ", typeid(e), 146 assert( typeid(e) == typeid(T), text("abstracted: ", typeid(e),
182 auto t = new Table; 147 auto t = new Table;
183 t.set("pos", ValueLayer, ast2table(e.pos,rec)); 148 t.set("pos", ValueLayer, ast2table(e.pos,rec));
184 t.set("is" , ValueLayer, new StrValue(typeid(e).name.split(".")[ 149 t.set("is" , ValueLayer, new StrValue(typeid(e).name.split(".")[
185 foreach(i,m; e.tupleof) 150 foreach(i,m; e.tupleof)
186 static if(is(typeof(m) : AST)) 151 static if(is(typeof(m) : AST))
187 t.set(e.tupleof[i].stringof[2..$], ValueLayer, r | 152 t.set(e.tupleof[i].stringof.split(".")[$-1], Val
188 else 153 else
189 t.set(e.tupleof[i].stringof[2..$], ValueLayer, a | 154 t.set(e.tupleof[i].stringof.split(".")[$-1], Val
190 return t; 155 return t;
191 } 156 }
192 else 157 else
193 static if(is(T == class)) 158 static if(is(T == class))
194 { 159 {
195 auto t = new Table; 160 auto t = new Table;
196 foreach(i,m; e.tupleof) 161 foreach(i,m; e.tupleof)