Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Commit 937848b

Browse files
committed
Update compiler.ts
1 parent 49397b3 commit 937848b

1 file changed

Lines changed: 182 additions & 3 deletions

File tree

src/core/expressions/compiler.ts

Lines changed: 182 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class Expression
2121
return new ParameterExpression(name);
2222
}
2323

24+
static Condition(test: Expression, ifTrue: Expression, ifFalse: Expression) : ConditionalExpression
25+
{
26+
return new ConditionalExpression(test, ifTrue, ifFalse);
27+
}
28+
2429
static Add(left: Expression, right: Expression) : BinaryExpression
2530
{
2631
return new BinaryExpression(ExpressionType.Add, left, right);
@@ -100,6 +105,36 @@ class Expression
100105
{
101106
return new BinaryExpression(ExpressionType.GreaterThanOrEqual, left, right);
102107
}
108+
109+
static LeftShift(left: Expression, right: Expression) : BinaryExpression
110+
{
111+
return new BinaryExpression(ExpressionType.LeftShift, left, right);
112+
}
113+
114+
static RightShift(left: Expression, right: Expression) : BinaryExpression
115+
{
116+
return new BinaryExpression(ExpressionType.RightShift, left, right);
117+
}
118+
119+
static Not(operand: Expression) : UnaryExpression
120+
{
121+
return new UnaryExpression(ExpressionType.Not, operand);
122+
}
123+
124+
static UnaryPlus(operand: Expression) : UnaryExpression
125+
{
126+
return new UnaryExpression(ExpressionType.UnaryPlus, operand);
127+
}
128+
129+
static Negate(operand: Expression) : UnaryExpression
130+
{
131+
return new UnaryExpression(ExpressionType.Negate, operand);
132+
}
133+
134+
static OnesComplement(operand: Expression) : UnaryExpression
135+
{
136+
return new UnaryExpression(ExpressionType.OnesComplement, operand);
137+
}
103138

104139
static Lambda<T extends Function>(body: Expression, ... parameters: ParameterExpression[]) : LambdaExpression<T>
105140
{
@@ -133,6 +168,16 @@ class ExpressionVisitor
133168
{
134169
return node.Update(this.Visit(node.left), this.Visit(node.right));
135170
}
171+
172+
VisitUnary(node: UnaryExpression) : Expression
173+
{
174+
return node.Update(this.Visit(node.operand));
175+
}
176+
177+
VisitConditional(node: ConditionalExpression) : Expression
178+
{
179+
return node.Update(this.Visit(node.test), this.Visit(node.ifTrue), this.Visit(node.ifFalse));
180+
}
136181

137182
VisitLambda<T extends Function>(node: LambdaExpression<T>) : Expression
138183
{
@@ -201,6 +246,37 @@ class ParameterExpression extends Expression
201246
}
202247
}
203248

249+
class UnaryExpression extends Expression
250+
{
251+
_operand: Expression;
252+
253+
constructor(nodeType: ExpressionType, operand: Expression)
254+
{
255+
super(nodeType);
256+
this._operand = operand;
257+
}
258+
259+
get operand(): Expression
260+
{
261+
return this._operand;
262+
}
263+
264+
Accept(visitor: ExpressionVisitor) : Expression
265+
{
266+
return visitor.VisitUnary(this);
267+
}
268+
269+
Update(operand: Expression) : UnaryExpression
270+
{
271+
if (operand !== this._operand)
272+
{
273+
return new UnaryExpression(this.nodeType, operand);
274+
}
275+
276+
return this;
277+
}
278+
}
279+
204280
class BinaryExpression extends Expression
205281
{
206282
_left: Expression;
@@ -239,6 +315,51 @@ class BinaryExpression extends Expression
239315
}
240316
}
241317

318+
class ConditionalExpression extends Expression
319+
{
320+
_test: Expression;
321+
_ifTrue: Expression;
322+
_ifFalse: Expression;
323+
324+
constructor(test: Expression, ifTrue: Expression, ifFalse: Expression)
325+
{
326+
super(ExpressionType.Condition);
327+
this._test = test;
328+
this._ifTrue = ifTrue;
329+
this._ifFalse = ifFalse;
330+
}
331+
332+
get test(): Expression
333+
{
334+
return this._test;
335+
}
336+
337+
get ifTrue(): Expression
338+
{
339+
return this._ifTrue;
340+
}
341+
342+
get ifFalse(): Expression
343+
{
344+
return this._ifTrue;
345+
}
346+
347+
Accept(visitor: ExpressionVisitor) : Expression
348+
{
349+
return visitor.VisitConditional(this);
350+
}
351+
352+
Update(test: Expression, ifTrue: Expression, ifFalse: Expression) : ConditionalExpression
353+
{
354+
if (test !== this._test || ifTrue !== this._ifTrue || ifFalse !== this._ifFalse)
355+
{
356+
return new ConditionalExpression(test, ifTrue, ifFalse);
357+
}
358+
359+
return this;
360+
}
361+
}
362+
242363
class LambdaExpression<T extends Function> extends Expression
243364
{
244365
_body: Expression;
@@ -366,6 +487,35 @@ class LambdaCompiler extends ExpressionVisitor
366487
return node;
367488
}
368489

490+
VisitUnary(node: UnaryExpression) : Expression
491+
{
492+
this.Visit(node.operand);
493+
494+
var o = this._stack.pop();
495+
var i = "";
496+
497+
switch (node.nodeType)
498+
{
499+
case ExpressionType.Negate:
500+
i = "-";
501+
break;
502+
case ExpressionType.UnaryPlus:
503+
i = "+";
504+
break;
505+
case ExpressionType.Not:
506+
i = "!";
507+
break;
508+
case ExpressionType.OnesComplement:
509+
i = "~";
510+
break;
511+
}
512+
513+
var res = i + "" + o;
514+
this._stack.push(res);
515+
516+
return node;
517+
}
518+
369519
VisitBinary(node: BinaryExpression) : Expression
370520
{
371521
this.Visit(node.left);
@@ -425,13 +575,36 @@ class LambdaCompiler extends ExpressionVisitor
425575
case ExpressionType.GreaterThanOrEqual:
426576
i = ">=";
427577
break;
578+
case ExpressionType.LeftShift:
579+
i = "<<";
580+
break;
581+
case ExpressionType.RightShift:
582+
i = ">>";
583+
break;
428584
}
429585

430586
var res = "(" + l + " " + i + " " + r + ")";
431587
this._stack.push(res);
432588

433589
return node;
434590
}
591+
592+
VisitConditional(node: ConditionalExpression) : Expression
593+
{
594+
this.Visit(node.test);
595+
this.Visit(node.ifTrue);
596+
this.Visit(node.ifFalse);
597+
598+
var f = this._stack.pop();
599+
var t = this._stack.pop();
600+
var c = this._stack.pop();
601+
602+
var res = "(" + c + " ? " + t + " : " + f + ")";
603+
604+
this._stack.push(res);
605+
606+
return node;
607+
}
435608

436609
VisitParameter(node: ParameterExpression) : Expression
437610
{
@@ -558,7 +731,14 @@ enum ExpressionType
558731
LessThanOrEqual,
559732
GreaterThan,
560733
GreaterThanOrEqual,
734+
LeftShift,
735+
RightShift,
561736
Invoke,
737+
Not,
738+
Negate,
739+
UnaryPlus,
740+
OnesComplement,
741+
Condition,
562742
}
563743

564744
class Binder extends ExpressionVisitor
@@ -611,7 +791,7 @@ var f =
611791
Expression.Invoke(
612792
Expression.Parameter("rx://operators/map"),
613793
Expression.Invoke(
614-
Expression.Parameter("rx://operators/where"),
794+
Expression.Parameter("rx://operators/filter"),
615795
Expression.Parameter("my://xs"),
616796
Expression.Lambda<(number) => boolean>(
617797
Expression.Equal(
@@ -637,12 +817,11 @@ var fvs = new FreeVariableScanner();
637817
fvs.Visit(f);
638818

639819
var unbound = fvs.result;
640-
alert(unbound.join(", "));
641820

642821
var resources =
643822
{
644823
"my://xs": [1, 2, 3, 4, 5],
645-
"rx://operators/where": function(xs : any[], f: (any) => boolean) { return xs.filter(f); },
824+
"rx://operators/filter": function(xs : any[], f: (any) => boolean) { return xs.filter(f); },
646825
"rx://operators/map": function(xs : any[], f: (any) => any) { return xs.map(f); },
647826
};
648827

0 commit comments

Comments
 (0)