stdray

Untitled

Feb 27th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. using Nemerle;
  2. using Nemerle.Collections;
  3. using Nemerle.Compiler;
  4. using Nemerle.Compiler.Parsetree;
  5. using Nemerle.Compiler.Typedtree;
  6. using Nemerle.Text;
  7. using Nemerle.Utility;
  8.  
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12.  
  13. namespace NemerleExperiments {
  14.    
  15.     public module UtilM {
  16.          public Expr2Parm (x : PExpr) : PParameter {
  17.             mutable tyLoc = Location.Default;
  18.             mutable nameLoc = x.Location;
  19.             def param = Util.locate(x.Location, match (x) {
  20.                 | <[ _ ]>                       => <[ parameter: $(Util.tmpname ("wildcard") : dyn) ]>
  21.                 | <[ $(nm : name) ]>            => nameLoc = nm.Location;
  22.                                                    <[ parameter: $(nm : name) ]>
  23.                 | <[ _ : $ty ]> as tyEnf        => tyLoc = ty.Location;
  24.                                                    nameLoc = tyEnf.expr.Location;
  25.                                                    def nm = Util.tmpname ("wildcard");
  26.                                                    <[ parameter: $(nm : dyn) : $ty ]>
  27.                 | <[ $(nm : name) : $ty ]>      => tyLoc = ty.Location;
  28.                                                    nameLoc = nm.Location;
  29.                                                    <[ parameter: $(nm : name) : $ty ]>
  30.                 | PExpr.Tuple(_args) as pattern => def ty = PExpr.Wildcard (tyLoc);
  31.                                                    def name = Splicable.Name (pattern.Location, Macros.NewSymbol("pat"));
  32.                                                    nameLoc = tyLoc;
  33.                                                    PParameter (nameLoc, name, ty, AttributesAndModifiers(0, []), pattern)
  34.                 | _                             => Message.Error (x.Location, $"unsupported syntax for parameter of 'parms => body' lambda expression: $x");
  35.                                                    <[ parameter: $(Util.tmpname ("wildcard") : dyn) ]>
  36.             });
  37.             param.Location = x.Location;
  38.             param.name.Location = nameLoc;
  39.             param.Type.Location = tyLoc;
  40.             param
  41.         }
  42.         public Expr2Parms(parmsExpr : PExpr) : list[PParameter] {
  43.             | PExpr.Tuple as tuple when tuple.argsCount == 1 => [Expr2Parm (parmsExpr)]
  44.             | <[ () ]> => []
  45.             | <[ (..$parmsList) ]> => parmsList.Map (Expr2Parm)
  46.             | _ => [Expr2Parm (parmsExpr)]
  47.         }
  48.         public Expr2List(expr: PExpr) : list[PExpr] {
  49.             | <[($p)]>    => [p]  
  50.             | <[(..$ps)]> => ps
  51.         }
  52.         public Expr2FunCall(expr: PExpr) : Name * list[PExpr] {
  53.             | <[ $(n : name)(..$ps)]> => (n, ps);
  54.             | _                       => Message.FatalError(expr.Location,
  55.                                                             "Expected function call, like f(x)");
  56.         }
  57.         public GetName : PExpr -> Name = var => match(var) {
  58.             | <[ $(n: name) ]> => n;
  59.             | _                => Message.FatalError("Name exptected, got $var");
  60.         }
  61.         public WithType(typer: Typer, expr: PExpr, transform : TExpr -> PExpr, errMsg : string="") : PExpr{
  62.             def errText = if(!string.IsNullOrEmpty(errMsg)) errMsg
  63.                           else $"Can not infer the type of $expr";
  64.             def type = typer.TypeExpr(expr);
  65.             typer.DelayMacro(lt => match(type.Type.Hint) {
  66.                 | None when lt => Message.FatalError(expr.Location, errText)
  67.                 | None         => None();
  68.                 | Some         => type |> transform |> Some
  69.             });
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment