Skip to content

Commit

Permalink
Tutorial_2_ListsAndCollections.n works
Browse files Browse the repository at this point in the history
  • Loading branch information
ionoy committed Jul 25, 2012
1 parent 7448ee5 commit aad8127
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ namespace MVCTest
{
public Name : string { get; set; }
public Meal : Meal { get; set; }
/*public FormattedPrice : string
public FormattedPrice : string
{
get { if(Meal != null) Meal.Price.ToString() else "None" }
}*/
}
}

[Record]
Expand All @@ -35,7 +35,7 @@ namespace MVCTest

public AvailableMeals : List[Meal] { get; set; }
public Seats : List[SeatReservation] { get; set; }
/*public TotalSurcharge : decimal
public TotalSurcharge : decimal
{
get {
mutable total = 0m;
Expand All @@ -44,7 +44,7 @@ namespace MVCTest
}
total
}
}*/
}

public this()
{
Expand All @@ -53,15 +53,15 @@ namespace MVCTest
SeatReservation("Roger", AvailableMeals[1])].ToList();
}

/*public AddSeat() : void
public AddSeat() : void
{
Seats.Add(SeatReservation("", AvailableMeals[0]))
}

public RemoveSeat(seat : SeatReservation) : bool
{
Seats.Remove(seat)
}*/
}
}

public partial module Views
Expand All @@ -72,7 +72,7 @@ namespace MVCTest
_ = viewModel;
xml <#
<div xmlns="">
<h2>Your seat reservations (<span data-bind="text: Seats().length"></span> )</h2>
<h2>Your seat reservations (<span data-bind="text: Seats().length"> </span>)</h2>

<table>
<thead><tr>
Expand All @@ -83,7 +83,7 @@ namespace MVCTest
<td><input data-bind="value: Name" /></td>
<td><select data-bind="options: $root.AvailableMeals, value: Meal, optionsText: 'MealName'"></select></td>
<td data-bind="text: FormattedPrice"></td>
<td><a href="#" data-bind="click: $root.RemoveSeat">Remove</a></td>
<td><a href="#" data-bind="click: function() { $root.RemoveSeat(this) }">Remove</a></td>
</tr>
</tbody>
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ namespace Nemerle.WUI.Reactive
| Assign(l, r) when allowedForAssignment.Any(al => al == r.GetType()) => a
| Assign(left, expr) => DelayAssignment(expr, left);
| DefValue(left, expr) => JsAST.Seq([JsAST.DefValue(left, JsAST.Void()), DelayAssignment(expr, JsAST.LocalRef(left))]);
| Array(xs) =>
JsAST.Array(xs.Flatten(x => match(x) {
| JsAST.Array(xs2) => xs2
| x => [x]
}))
| _ => a
}}, false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Nemerle.WUI.Reactive
module JsKnownMembers
{
public Convert(call : TExpr.Call, builder : TExpr -> JsAST) : JsAST
{
{
def result = match(call) {
| Call(MethodRef(obj = o, meth = meth), parms, _) => ConvertMethod(o.Type.TypeInfo, builder(o), meth, parms.Map(p => builder(p.expr)))
| Call(StaticRef(from = fromType, mem = meth) as left, parms, _) => ConvertMethod(fromType.TypeInfo, builder(left), meth, parms.Map(p => builder(p.expr)))
Expand All @@ -27,21 +27,22 @@ namespace Nemerle.WUI.Reactive

public ConvertMethod(type : TypeInfo, left : JsAST, member : IMember, parms : list[JsAST]) : JsAST
{
type.Manager.LookupTypeInfo
def typeName = if(type.SystemType != null) type.SystemType.FullName else type.FrameworkTypeName;
match(member.Name, parms) {
| ("op_Inequality", _) => JsAST.Call(JsAST.OpCode("!="), parms)
| ("ToString", h :: _) => JsAST.Call(JsAST.MethodRef(h, "toString"), [])
| ("ToString", []) => JsAST.Call(JsAST.MethodRef(left, "toString"), [])
| ("Concat", h :: t) when type.FullName == typeof(string).FullName => JsAST.Call(JsAST.MethodRef(h, "concat"), t)
| ("ToUpper", []) when type.FullName == typeof(string).FullName => JsAST.Call(JsAST.MethodRef(left, "toUpperCase"), [])
| ("Concat", h :: t) when typeName == typeof(string).FullName => JsAST.Call(JsAST.MethodRef(h, "concat"), t)
| ("ToUpper", []) when typeName == typeof(string).FullName => JsAST.Call(JsAST.MethodRef(left, "toUpperCase"), [])
| ("op_Implicit", h :: []) => h
| ("op_Implicit", h :: t) => JsAST.Seq(h :: t)
| ("op_Addition", parms) => JsAST.Call(JsAST.OpCode("+"), parms)
//List
| ("get_Item", h :: []) when type.SystemType.FullName == typeof(List[_]).FullName => JsAST.Call(JsAST.Indexer(left), [h])
| ("Add", h :: []) when type.SystemType.FullName == typeof(List[_]).FullName => JsAST.Call(JsAST.MethodRef(PropertyToField(left), "push"), [h])
| ("Remove", h :: []) when type.SystemType.FullName == typeof(List[_]).FullName => JsAST.Call(JsAST.MethodRef(PropertyToField(left), "remove"), [h])
| (".ctor", parms) when member.DeclaringType.FrameworkTypeName == typeof(Nemerle.Core.list[_]).FullName => JsAST.Array(parms)
| ("get_Item", h :: []) when typeName == typeof(List[_]).FullName => JsAST.Call(JsAST.Indexer(left), [h])
| ("Add", h :: []) when typeName == typeof(List[_]).FullName => JsAST.Call(JsAST.MethodRef(PropertyToField(left), "push"), [h])
| ("Remove", h :: []) when typeName == typeof(List[_]).FullName => JsAST.Call(JsAST.MethodRef(PropertyToField(left), "remove"), [h])
| ("ToList", h :: []) when typeName == typeof(Enumerable).FullName => h
| (".ctor", parms) when typeName == typeof(list[_].Cons).FullName => JsAST.Array(parms.Filter(p => !(p is JsAST.NotImpl()))) //filter out [] in the end
| (".ctor", parms) => JsAST.Call(JsAST.Constructor(type), parms)
| _ when member.Name.StartsWith("get_") => JsAST.PropertyGet(left, RemoveGetSet(member.Name))
| (_, h :: []) when member.Name.StartsWith("set_") => JsAST.PropertySet(left, RemoveGetSet(member.Name), h)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ this.$name = function(..$parms) {
def arrayTypes = [<[ ttype: Nemerle.Core.list[_] ]>, <[ ttype: System.Collections.Generic.List[_] ]>];
def isArray = arrayTypes.Any(t => prop.Getter.ReturnType.TryUnify(t));
def ctor = if (isArray) "observableArray([])" else "observable()";
$<#this.$(prop.Name) = ko.$ctor#>;
$<#this.$(prop.Name) = ko.$ctor;#>;
//def paramName = char.ToLower(prop.Name[0]) + prop.Name.Substring(1);
| prop is PropertyBuilder when prop.CanRead && !prop.CanWrite => convertMethod(member.DeclaringType, prop.Name, prop.GetGetter() :> MethodBuilder)
| _ => null
Expand Down Expand Up @@ -335,7 +335,7 @@ $<#

def translation = match(meth.Header.Body) {
| FunBody.Typed(typedBody) =>
assert2(false);
//assert2(false);
mutable jsAst = JsASTBuilder.Build(typedBody, false);
jsAst = JsASTOptimizer.LabelsToSwitch(jsAst);
def jsAst' = JsASTOptimizer.Optimize(jsAst);
Expand Down

0 comments on commit aad8127

Please sign in to comment.