Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Nemerle;
- using Nemerle.Collections;
- using Nemerle.Compiler;
- using Nemerle.Compiler.Parsetree;
- using Nemerle.Compiler.Typedtree;
- using Nemerle.Text;
- using Nemerle.Utility;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace NemerleExperiments {
- public module UtilM {
- public Expr2Parm (x : PExpr) : PParameter {
- mutable tyLoc = Location.Default;
- mutable nameLoc = x.Location;
- def param = Util.locate(x.Location, match (x) {
- | <[ _ ]> => <[ parameter: $(Util.tmpname ("wildcard") : dyn) ]>
- | <[ $(nm : name) ]> => nameLoc = nm.Location;
- <[ parameter: $(nm : name) ]>
- | <[ _ : $ty ]> as tyEnf => tyLoc = ty.Location;
- nameLoc = tyEnf.expr.Location;
- def nm = Util.tmpname ("wildcard");
- <[ parameter: $(nm : dyn) : $ty ]>
- | <[ $(nm : name) : $ty ]> => tyLoc = ty.Location;
- nameLoc = nm.Location;
- <[ parameter: $(nm : name) : $ty ]>
- | PExpr.Tuple(_args) as pattern => def ty = PExpr.Wildcard (tyLoc);
- def name = Splicable.Name (pattern.Location, Macros.NewSymbol("pat"));
- nameLoc = tyLoc;
- PParameter (nameLoc, name, ty, AttributesAndModifiers(0, []), pattern)
- | _ => Message.Error (x.Location, $"unsupported syntax for parameter of 'parms => body' lambda expression: $x");
- <[ parameter: $(Util.tmpname ("wildcard") : dyn) ]>
- });
- param.Location = x.Location;
- param.name.Location = nameLoc;
- param.Type.Location = tyLoc;
- param
- }
- public Expr2Parms(parmsExpr : PExpr) : list[PParameter] {
- | PExpr.Tuple as tuple when tuple.argsCount == 1 => [Expr2Parm (parmsExpr)]
- | <[ () ]> => []
- | <[ (..$parmsList) ]> => parmsList.Map (Expr2Parm)
- | _ => [Expr2Parm (parmsExpr)]
- }
- public Expr2List(expr: PExpr) : list[PExpr] {
- | <[($p)]> => [p]
- | <[(..$ps)]> => ps
- }
- public Expr2FunCall(expr: PExpr) : Name * list[PExpr] {
- | <[ $(n : name)(..$ps)]> => (n, ps);
- | _ => Message.FatalError(expr.Location,
- "Expected function call, like f(x)");
- }
- public GetName : PExpr -> Name = var => match(var) {
- | <[ $(n: name) ]> => n;
- | _ => Message.FatalError("Name exptected, got $var");
- }
- public WithType(typer: Typer, expr: PExpr, transform : TExpr -> PExpr, errMsg : string="") : PExpr{
- def errText = if(!string.IsNullOrEmpty(errMsg)) errMsg
- else $"Can not infer the type of $expr";
- def type = typer.TypeExpr(expr);
- typer.DelayMacro(lt => match(type.Type.Hint) {
- | None when lt => Message.FatalError(expr.Location, errText)
- | None => None();
- | Some => type |> transform |> Some
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment