// Transforms a sequence containing two-argument with statements // into a statement containing those statements in nested form Node withTransform (Node source) { Node o = token("--"); Metadata m = source.metadata; std::vector<Node> args; for (int i = source.args.size() - 1; i >= 0; i--) { Node a = source.args[i]; if (a.val == "with" && a.args.size() == 2) { std::vector<Node> flipargs; for (int j = args.size() - 1; j >= 0; j--) flipargs.push_back(args[i]); if (o.val != "--") flipargs.push_back(o); o = asn("with", a.args[0], a.args[1], asn("seq", flipargs, m), m); args = std::vector<Node>(); } else { args.push_back(a); } } std::vector<Node> flipargs; for (int j = args.size() - 1; j >= 0; j--) flipargs.push_back(args[j]); if (o.val != "--") flipargs.push_back(o); return asn("seq", flipargs, m); }
// Fills in the pattern with a dictionary mapping variable names to // nodes (these dicts are generated by match). Match and subst together // create a full pattern-matching engine. Node subst(Node pattern, std::map<std::string, Node> dict, std::string varflag, Metadata m) { // Swap out patterns at the token level if (pattern.metadata.ln == -1) pattern.metadata = m; if (pattern.type == TOKEN && pattern.val[0] == '$') { if (dict.count(pattern.val.substr(1))) { return dict[pattern.val.substr(1)]; } else { return token(varflag + pattern.val.substr(1), m); } } // Other tokens are untouched else if (pattern.type == TOKEN) { return pattern; } // Substitute recursively for ASTs else { std::vector<Node> args; for (unsigned i = 0; i < pattern.args.size(); i++) { args.push_back(subst(pattern.args[i], dict, varflag, m)); } return asn(pattern.val, args, m); } }
/* flatten * a || b * to * if a || b * t = true * else * t = false * ;; */ static Node * flattenlazy(Flattenctx *s, Node *n) { Node *r, *t, *u; Node *ltrue, *lfalse, *ldone; /* set up temps and labels */ r = temp(s, n); ltrue = genlbl(n->loc); lfalse = genlbl(n->loc); ldone = genlbl(n->loc); /* flatten the conditional */ flattencond(s, n, ltrue, lfalse); /* if true */ append(s, ltrue); u = mkexpr(n->loc, Olit, mkbool(n->loc, 1), NULL); u->expr.type = mktype(n->loc, Tybool); t = asn(r, u); append(s, t); jmp(s, ldone); /* if false */ append(s, lfalse); u = mkexpr(n->loc, Olit, mkbool(n->loc, 0), NULL); u->expr.type = mktype(n->loc, Tybool); t = asn(r, u); append(s, t); jmp(s, ldone); /* finish */ append(s, ldone); return r; }
preprocessResult processTypes (preprocessResult pr) { preprocessAux aux = pr.second; Node node = pr.first; if (node.type == TOKEN && aux.types.count(node.val)) node = asn(aux.types[node.val], node, node.metadata); else if (node.val == "untyped") return preprocessResult(node.args[0], aux); else if (node.val == "outer") return preprocessResult(node, aux); else { for (unsigned i = 0; i < node.args.size(); i++) { node.args[i] = processTypes(preprocessResult(node.args[i], aux)).first; } } return preprocessResult(node, aux); }
// Flatten nested sequence into flat sequence Node flattenSeq(Node inp) { std::vector<Node> o; if (inp.val == "seq" && inp.type == ASTNODE) { for (unsigned i = 0; i < inp.args.size(); i++) { if (inp.args[i].val == "seq" && inp.args[i].type == ASTNODE) o = extend(o, flattenSeq(inp.args[i]).args); else o.push_back(flattenSeq(inp.args[i])); } } else if (inp.type == ASTNODE) { for (unsigned i = 0; i < inp.args.size(); i++) { o.push_back(flattenSeq(inp.args[i])); } } else return inp; return asn(inp.val, o, inp.metadata); }
// RAMC, zu der der Ekliptikpunkt der Länge l die mund. Pos. m erhält double t_koch( double m, double ee, double l, double phi ) { int ostpunkt,i; double admc,t,t0,ascdiff,ra,d,a,_oa; m = frac(m,360.); // Wert auf 0-360 reduzieren ostpunkt = (fabs(ArcDiff(m,0))<90.); ekq( ee, l, 0., &ra, &d); // Hilfsfaktor a a = ostpunkt ? (m/90.) : (m/90.-2.); // Für Ostpunkte OA, für Westpunkte OD berechnen _oa = oa( ra, d, (ostpunkt ? phi : -phi) ); // Iterativ Formel (58) auflösen t0 = frac( (_oa - 90 - m), 360. ); for (i = 0; i<1000; i++) { admc = asn(t1(ee)*t1(phi)*s1(t0)); t = frac( (_oa - 90 - m - a*admc), 360. ); if (ArcDiff(t,t0)<PREC) break; t0 = frac(t,360.); } return frac(t,360.); }
static Node * visit(Flattenctx *s, Node *n) { size_t i; Node *r; for (i = 0; i < n->expr.nargs; i++) n->expr.args[i] = rval(s, n->expr.args[i]); if (opispure[exprop(n)]) { r = n; } else { if (exprtype(n)->type == Tyvoid) { r = mkexpr(n->loc, Olit, mkvoid(n->loc), NULL); r->expr.type = mktype(n->loc, Tyvoid); append(s, n); } else { r = temp(s, n); append(s, asn(r, n)); } } return r; }