public void AddEncodeFun(Type type) { var funName = EncodeFunName(type); if (FunDefs.ContainsKey(funName)) { return; } var f = new FunDef(funName, $"{funName} : {type.Name} -> {Elm.Types.ENCODER_VALUE}"); f.Params.Add("v"); if (type.IsEnum) { var encodeApp = new FunApp(new Id(Elm.Encoders.STRING)) { Rands = { new FunApp(new Id(ToStringFunName(type))) { Rands ={ new Id("v") } } } }; f.Body = encodeApp; } else { var fieldTups = new List <ElmAstNode>(); foreach (var prop in type.GetProperties(PROP_FLAGS)) { var jsonAttrs = prop.GetCustomAttributes(typeof(JsonPropertyAttribute)); if (jsonAttrs.FirstOrDefault() is JsonPropertyAttribute jsonAttr) { var jsonFieldName = jsonAttr.PropertyName ?? SnakeCase(prop.Name); var keyExp = new StringLiteral(jsonFieldName); var valExp = new FunApp(new Id(EncodeFunName(prop.PropertyType))) { Rands = { new Id($"v.{ElmRecordFieldName(prop)}") } }; fieldTups.Add(new TupleExp(keyExp, valExp)); } } f.Body = new FunApp( new Id(Elm.Encoders.OBJECT), new ListExp(fieldTups.ToArray()) ); } FunDefs.Add(funName, f); }
private bool DoParseModelLine(string[] words) { if (words[words.Length - 2] != "->") { return(false); } if (currentFun != null) { if (words[0] == "else") { return(true); } FunApp fapp = new FunApp(); fapp.Args = new Partition[words.Length - 2]; for (int i = 0; i < fapp.Args.Length; ++i) { fapp.Args[i] = model.PartitionByName(words[i]); } fapp.Value = model.PartitionByName(words[words.Length - 1]); fapp.Fun = currentFun; currentFun.Apps.Add(fapp); fapp.Value.Values.Add(fapp); return(true); } else if (words.Length == 3) { FunSymbol fs = model.FunSymbolByName(words[0]); if (words[2] == "{") { currentFun = fs; } else { FunApp fapp = new FunApp(); fapp.Args = new Partition[0]; fapp.Fun = fs; fapp.Value = model.PartitionByName(words[words.Length - 1]); fs.Apps.Add(fapp); fapp.Value.Values.Add(fapp); } return(true); } else { return(false); } }