tree evaluate_eval_args (tree t) { #ifdef CLASSICAL_MACRO_EXPANSION if (macro_top_level (std_env) || !is_atomic (t[0])) return evaluate_error ("undefined", t[0]); basic_environment local= macro_arguments (std_env); int key= make_tree_label (t[0]->label); if (!local->contains (key)) return evaluate_error ("undefined", t[0]); tree u= local [key]; #else tree u= t[0]; #endif if (is_atomic (u)) return evaluate_error ("bad eval-args"); #ifdef CLASSICAL_MACRO_EXPANSION macro_up (std_env); #endif int i, n= N(u); tree r (u, n); for (i=0; i<n; i++) r[i]= evaluate (u[i]); #ifdef CLASSICAL_MACRO_EXPANSION macro_redown (std_env, local); #endif return r; }
tree xml_html_parser::finalize_space (tree t) { if (is_atomic (t) || (!is_tuple (t, "tag"))) return t; else { int i, n= N(t); tree r= tuple (t[0], t[1]); int first= -1, last= -1; for (i=2; i<n; i++) if (!is_tuple (t[i], "attr")) { first= i; break; } if (!is_tuple (t[n-1], "attr")) last= n-1; (void) first; (void) last; for (i=2; i<n; i++) { if (is_atomic (t[i])) { if (finalize_preserve_space (t[1]->label)) r << t[i]; else { string s= finalize_space (t[i]->label, i==2, i==(n-1)); if (s != "") r << s; } } else if (is_tuple (t[i], "tag")) r << finalize_space (t[i]); else r << t[i]; } return r; } }
static int is_simple(EXPRESSION *expr) { if (is_atomic(expr)) return 1; if (is_binary_op(expr)) { return is_atomic(tree_get_child(expr, 0)) && is_atomic(tree_get_child(expr, 1)); } if (tree_is_type(expr, EXPR_CALL)) { return is_atomic(tree_get_child(expr, 1)); } if (tree_is_type(expr, EXPR_TUPLE)) { int i; for (i = 0; i < tree_num_children(expr); i++) if (!is_simple(tree_get_child(expr, i))) return 0; return 1; } return 0; }
tree drd_info_rep::get_syntax (tree t, path p) { if (is_atomic (t)) { string s= t->label; if (N(s) == 1 || !existing_tree_label (s)) return UNINIT; return get_syntax (as_tree_label (s)); } else if (is_func (t, VALUE, 1) && is_atomic (t[0])) { string s= t[0]->label; if (!existing_tree_label (s)) return UNINIT; return get_syntax (as_tree_label (s)); } else if (L(t) < START_EXTENSIONS) return UNINIT; else { tree fun= the_drd->get_syntax (L(t)); if (fun == UNINIT) return UNINIT; else if (N(t) == 0 && !is_func (fun, MACRO)) return fun; else if (!is_func (fun, MACRO)) return UNINIT; else { int i, n= N(fun)-1; hashmap<tree,tree> tab (UNINIT); for (i=0; i<n; i++) { tree var= tree (ARG, fun[i]); tree val= ""; if (i < N(t)) { if (p == path (-1)) val= t[i]; else val= tree (QUASI, t[i], (tree) (p * i)); } tab (var)= val; } return replace (fun[n], tab); } } }
static tree get_with_text (tree t) { int i, n=N(t), k=(n-1)/2; if ((n&1)!=1) return ""; if (is_func (t[n-1], GRAPHICS)) return ""; tree s= concat (); for (i=0; i<k; i++) if (is_atomic (t[2*i]) && (t[2*i]!="") && is_atomic (t[2*i+1])) { if (i>0) s << " "; string var= t[2*i]->label; if ((var!=MODE) && (var!=COLOR) && (var!=PAR_MODE) && (var!=LANGUAGE) && (var!=FONT) && (var!=FONT_FAMILY) && (var!=FONT_SHAPE) && (var!=FONT_SERIES) && (var!=MATH_LANGUAGE) && (var!=MATH_FONT) && (var!=MATH_FONT_FAMILY) && (var!=MATH_FONT_SHAPE) && (var!=MATH_FONT_SERIES) && (var!=PROG_LANGUAGE) && (var!=PROG_FONT) && (var!=PROG_FONT_FAMILY) && (var!=PROG_FONT_SHAPE) && (var!=PROG_FONT_SERIES) && (var!=PROG_SESSION)) s << (var * "=" * t[2*i+1]->label); else s << t[2*i+1]->label; } return s; }
tree evaluate_merge (tree t) { int i, n= N(t); if (n == 0) return ""; tree acc= evaluate (t[0]); if (is_concat (acc)) acc= tree_as_string (acc); for (i=1; i<n; i++) { tree add= evaluate (t[i]); if (is_atomic (acc) && (is_atomic (add) || is_concat (add))) acc= acc->label * tree_as_string (add); else if (is_tuple (acc) && is_tuple (add)) acc= acc * add; else if (is_func (acc, MACRO) && is_func (add, MACRO) && (N(acc) == N(add)) && (acc (0, N(acc)-1) == add (0, N(add)-1))) { tree r = copy (acc); tree u1= copy (acc[N(acc)-1]); tree u2= copy (add[N(add)-1]); tree u (CONCAT, u1, u2); if (u1 == "") u= u2; else if (u2 == "") u= u1; else if (is_atomic (u1) && is_atomic (u2)) u= u1->label * u2->label; r[N(r)-1]= u; acc= r; } else return evaluate_error ("bad merge"); } return acc; }
db_atoms database_rep::query (tree ql, db_time t, int limit) { //cout << "query " << ql << ", " << t << ", " << limit << LF; ql= normalize_query (ql); //cout << "normalized query " << ql << ", " << t << ", " << limit << LF; db_atoms ids= ansatz (ql, t); //cout << "ansatz ids= " << ids << LF; bool sort_flag= false; if (is_tuple (ql)) for (int i=0; i<N(ql); i++) sort_flag= sort_flag || is_tuple (ql[i], "order", 2); ids= filter (ids, ql, t, max (limit, sort_flag? 1000: 0)); //cout << "filtered ids= " << ids << LF; for (int i=0; i<N(ql); i++) { if (is_tuple (ql[i], "modified", 2) && is_atomic (ql[i][1]) && is_atomic (ql[i][2]) && is_quoted (ql[i][1]->label) && is_quoted (ql[i][2]->label)) { string t1= scm_unquote (ql[i][1]->label); string t2= scm_unquote (ql[i][2]->label); if (is_int (t1) && is_int (t2)) ids= filter_modified (ids, (db_time) as_long_int (t1), (db_time) as_long_int (t2)); } } //cout << "filtered on modified ids= " << ids << LF; ids= sort_results (ids, ql, t); //cout << "sorted ids= " << ids << LF; if (N(ids) > limit) ids= range (ids, 0, limit); return ids; }
bool join (modification& m1, modification m2, tree t) { if (m1->k == MOD_INSERT && m2->k == MOD_INSERT && is_atomic (m1->t) && root (m1) == root (m2) && (index (m2) == index (m1) || index (m2) == index (m1) + N (m1->t->label))) { string s= m1->t->label * m2->t->label; if (index (m2) == index (m1)) s= m2->t->label * m1->t->label; m1= mod_insert (root (m1), index (m1), tree (s)); return true; } if (m1->k == MOD_REMOVE && m2->k == MOD_REMOVE && is_atomic (subtree (t, root (m1))) && root (m1) == root (m2) && (index (m1) == index (m2) || index (m1) == index (m2) + argument (m2))) { m1= mod_remove (root (m2), index (m2), argument (m1) + argument (m2)); return true; } return false; }
void dictionary_rep::load (url u) { if (is_none (u)) return; if (is_or (u)) { load (u[1]); load (u[2]); return; } string s; if (load_string (u, s, false)) return; tree t= block_to_scheme_tree (s); if (!is_tuple (t)) return; int i, n= N(t); for (i=0; i<n; i++) if (is_func (t[i], TUPLE, 2) && is_atomic (t[i][0]) && is_atomic (t[i][1])) { string l= t[i][0]->label; if (is_quoted (l)) l= scm_unquote (l); string r= t[i][1]->label; if (is_quoted (r)) r= scm_unquote (r); if (to == "chinese" || to == "japanese" || to == "german" || to == "korean" || to == "taiwanese" || to == "russian" || to == "ukrainian" || to == "bulgarian") r= utf8_to_cork (r); table (l)= r; } }
tree tree_translate (tree t, string from, string to) { //cout << "Translating " << t << " from " << from << " into " << to << "\n"; if (is_atomic (t)) return translate (t->label, from, to); else if (is_compound (t, "replace")) { if (!is_atomic (t[0])) { //cout << "tree_translate() ERROR: first child should be a string\n"; return t; } t[0]->label= translate_as_is (t[0]->label, from, to); return translate_replace (t, from, to); } else if (is_compound (t, "verbatim", 1)) return t[0]; else if (is_compound (t, "localize", 1)) return tree_translate (t[0], "english", out_lan); else if (is_compound (t, "render-key", 1)) return compound ("render-key", tree_translate (t[0], from, to)); else { tree r (t, N(t)); for (int i=0; i<N(t); i++) if (!the_drd->is_accessible_child (t, i)) r[i]= t[i]; else r[i]= tree_translate (t[i], from, to); return r; } }
tree translate_replace (tree t, string from, string to, int n=1) { if (N(t) < 2) return t[0]; string s= t[0]->label; string arg= "%" * as_string (n); if (is_atomic (t[1])) { s= replace (s, arg, translate (t[1]->label, from, to)); return translate_replace (concat (s) * t(2, N(t)), from, to, n+1); } else { int l= search_forwards (arg, s); if (l < 0) return t; int r= l + N(arg); tree r1= tree_translate (t[1], from, to); tree r2= translate_replace (tuple (s (r, N(s))) * t(2, N(t)), from, to, n+1); s= s(0, l); if (is_atomic (r1)) { if (is_atomic (r2)) return s * r1->label * r2->label; else return concat (s * r1->label, r2); } return concat (s, r1, r2); } }
bool drd_info_rep::is_accessible_child (tree t, int i) { //cout << "l= " << as_string (L(t)) << "\n"; tag_info ti= info[L(t)]; int index= ti->get_index (i, N(t)); if (is_func (t, EXTERN) && N(t)>0 && is_atomic (t[0])) { ti= info[make_tree_label ("extern:" * t[0]->label)]; index= ti->get_index (i-1, N(t)); } if ((index<0) || (index>=N(ti->ci))) { if (get_access_mode () == DRD_ACCESS_SOURCE) return !is_atomic (t) && i >= 0 && i < N(t); else return false; } switch (get_access_mode ()) { case DRD_ACCESS_NORMAL: return ti->ci[index].accessible == ACCESSIBLE_ALWAYS; case DRD_ACCESS_HIDDEN: return ti->ci[index].accessible == ACCESSIBLE_ALWAYS || ti->ci[index].accessible == ACCESSIBLE_HIDDEN; case DRD_ACCESS_SOURCE: return true; } return true; // NOT REACHED }
void bridge_compound_rep::my_typeset (int desired_status) { int d; tree f; if (L(st) == COMPOUND) { d= 1; f= st[0]; if (is_compound (f)) f= env->exec (f); if (is_atomic (f)) { string var= f->label; if (env->provides (var)) f= env->read (var); else f= tree (ERROR, st); } } else { string var= as_string (L(st)); if (env->provides (var)) f= env->read (var); else f= tree (ERROR, st); d= 0; } if (is_applicable (f)) { int i, n=N(f)-1, m=N(st)-d; env->macro_arg= list<hashmap<string,tree> > ( hashmap<string,tree> (UNINIT), env->macro_arg); env->macro_src= list<hashmap<string,path> > ( hashmap<string,path> (path (DECORATION)), env->macro_src); if (L(f) == XMACRO) { if (is_atomic (f[0])) { string var= f[0]->label; env->macro_arg->item (var)= st; env->macro_src->item (var)= ip; } } else for (i=0; i<n; i++) if (is_atomic (f[i])) { string var= f[i]->label; env->macro_arg->item (var)= i<m? st[i+d]: attach_dip (tree (UNINIT), decorate_right (ip)); env->macro_src->item (var)= i<m? descend (ip,i+d): decorate_right(ip); } initialize (f[n], d, f); // /*IF_NON_CHILD_ENFORCING(st)*/ ttt->insert_marker (st, ip); if (!the_drd->is_child_enforcing (st)) ttt->insert_marker (st, ip); body->typeset (desired_status); env->macro_arg= env->macro_arg->next; env->macro_src= env->macro_src->next; } else { initialize (f, d, f); ///*IF_NON_CHILD_ENFORCING(st)*/ ttt->insert_marker (st, ip); if (!the_drd->is_child_enforcing (st)) ttt->insert_marker (st, ip); body->typeset (desired_status); } }
tree evaluate_unequal (tree t) { if (N(t)!=2) return evaluate_error ("bad unequal"); tree t1= evaluate (t[0]); tree t2= evaluate (t[1]); if (is_atomic (t1) && is_atomic (t2) && is_length (t1->label) && is_length (t2->label)) return as_string_bool (as_length (t1) != as_length (t2)); return as_string_bool (t1 != t2); }
static string get_prompt (scheme_tree p, int i) { if (is_atomic (p[i]) && is_quoted (p[i]->label)) return translate (scm_unquote (p[i]->label)); else if (is_tuple (p[i]) && N(p[i])>0) { if (is_atomic (p[i][0]) && is_quoted (p[i][0]->label)) return translate (scm_unquote (p[i][0]->label)); return translate (scheme_tree_to_tree (p[i][0])); } return translate ("Input:"); }
static tree simplify_execed (tree t) { if (is_atomic (t)) return t; int i, n= N(t); tree r (t, n); for (i=0; i<n; i++) r[i]= simplify_execed (t[i]); if (is_func (r, QUOTE, 1) && is_atomic (r[0])) return r[0]; else return r; }
list<tree> get_mirrors (tree ln, string id) { if (!is_compound (ln, "link", 4) || ln[0] != "mirror" || !is_compound (ln[2], "id", 1) || !is_atomic (ln[2][0]) || !is_compound (ln[3], "id", 1) || !is_atomic (ln[3][0])) return list<tree> (); if (ln[2][0] == id) return not_done (get_trees (ln[3][0]->label)); if (ln[3][0] == id) return not_done (get_trees (ln[2][0]->label)); return list<tree> (); }
player accelerate (player pl, tree kind) { if (kind == "reverse") return reverse_player (pl); if (kind == "fade-in") return fade_in_player (pl); if (kind == "fade-out") return fade_out_player (pl); if (kind == "faded") return faded_player (pl); if (kind == "bump") return bump_player (pl); if (is_atomic (kind) && starts (kind->label, "reverse-")) return reverse_player (accelerate (pl, kind->label (8, N(kind->label)))); if (is_tuple (kind, "fixed", 1) && is_atomic (kind[1])) return fixed_player (pl, as_double (kind[1])); return pl; }
static bool operator <= (url u1, url u2) { if (is_atomic (u1) && is_atomic (u2)) return u1->t->label <= u2->t->label; if (is_atomic (u1)) return true; if (is_atomic (u2)) return false; if (is_concat (u1) && is_concat (u2)) { if (u1[1] == u2[1]) return u1[2] <= u2[2]; else return u1[1] <= u2[1]; } if (is_concat (u1)) return true; if (is_concat (u2)) return false; return true; // does not matter for sorting }
static tree value_to_compound (tree t, hashmap<string,tree> h) { if (is_atomic (t)) return t; else if (is_func (t, VALUE, 1) && is_atomic (t[0]) && h->contains (t[0]->label)) return compound (t[0]->label); else { int i, n= N(t); tree r (t, n); for (i=0; i<n; i++) r[i]= value_to_compound (t[i], h); return r; } }
static tree make_small (tree br) { if (is_atomic (br)) return br; if (is_func (br, LEFT) || is_func (br, MID) || is_func (br, RIGHT) || is_func (br, BIG)) if (N(br) > 0 && is_atomic (br[0])) { string s= br[0]->label; if (s == ".") return "<nobracket>"; if (N(s) <= 1) return s; return "<" * s * ">"; } return "<nobracket>"; }
EXPRESSION *atomise_expression(MODULE *module, FUNCTION *func, BLOCK *block, EXPRESSION *expr, STATEMENT *before) { if (is_atomic(expr)) return expr; if (tree_is_type(expr, EXPR_TUPLE)) { EXPRESSION *new_temp = make_empty_tuple(CAST_TO_AST(expr)->source_line); int i; for (i = 0; i < tree_num_children(expr); i++) tree_add_child(new_temp, atomise_expression(module, func, block, tree_get_child(expr, i), before)); return new_temp; } EXPRESSION *new_temp = make_new_temp(module, func, expr->type, CAST_TO_AST(expr)->source_line); STATEMENT *new_assign = make_assignment(new_temp, expr, CAST_TO_AST(expr)->source_line); if (has_graph(func)) { GRAPH *graph = func->graph; add_vertex(graph, CAST_TO_NODE(new_assign)); inject_before(graph, CAST_TO_NODE(new_assign), CAST_TO_NODE(before), 0); } else tree_add_before(CAST_TO_NODE(block), CAST_TO_NODE(new_assign), CAST_TO_NODE(before)); return new_temp; }
tree xml_html_parser::finalize_sxml (tree t) { if (!is_tuple (t, "tag")) return ""; // sanity int i, n= N(t); tree tag = tuple (t[1]); if (t[1] == "<document>") tag= tuple ("*TOP*"); tree attrs = tuple ("@"); tree content = tuple (); for (i=2; i<n; i++) if (is_tuple (t[i], "attr")) { tree attr; if (N(t[i]) == 2) attr= tuple (t[i][1]); else attr= tuple (t[i][1]->label, simple_quote (t[i][2]->label)); attrs << attr; } else if (is_tuple (t[i], "tag")) content << finalize_sxml (t[i]); else if (is_atomic (t[i])) content << simple_quote (t[i]->label); else if (is_tuple (t[i], "pi")) content << tuple ("*PI*", t[i][1]->label, simple_quote (t[i][2]->label)); else if (is_tuple (t[i], "doctype")) // TODO: convert DTD declarations content << tuple ("*DOCTYPE*", simple_quote (t[i][1]->label)); else if (is_tuple (t[i], "cdata")) content << simple_quote (t[i][1]->label); if (N(attrs) > 1) tag << attrs; tag << A(content); return tag; }
static void adjust_right_script (tree t, path& o1) { while (is_concat (t) && o1->item > 0 && o1->next == path (0)) { tree st= t[o1->item]; if (is_func (st, RSUB) || is_func (st, RSUP) || is_func (st, RPRIME)) { tree pt= t[o1->item-1]; if (!is_atomic (pt)) o1= path (o1->item-1, start (pt)); else { string s= pt->label; int pos= N(s); while (pos > 0) { int prev= pos; tm_char_backwards (s, prev); if (pos == N(s)); else if (is_numeric (s (prev, N(s)))); else if (is_iso_alpha (s (prev, N(s)))); else break; pos= prev; } o1= path (o1->item-1, pos); } } else break; } }
translator load_virtual (string name) { if (translator::instances -> contains (name)) return translator (name); translator trl= tm_new<translator_rep> (name); string s, r; name= name * ".vfn"; if (DEBUG_STD) debug_fonts << "Loading " << name << "\n"; url u ("$TEXMACS_HOME_PATH/fonts/virtual:$TEXMACS_PATH/fonts/virtual", name); load_string (u, s, true); tree t= string_to_scheme_tree (s); ASSERT (is_tuple (t, "virtual-font"), "bad virtual font format"); int i, n= N(t); trl->virt_def= array<tree> (n); for (i=1; i<n; i++) if (is_func (t[i], TUPLE, 2) && is_atomic (t[i][0])) { string s= as_string (t[i][0]); if (N(s)>1) s= "<" * s * ">"; trl->dict (s)= i; trl->virt_def[i]= t[i][1]; // cout << s << "\t" << i << "\t" << t[i][1] << "\n"; } return trl; }
bool edit_interface_rep::set_hybrid_footer (tree st) { // WARNING: update edit_dynamic_rep::activate_hybrid when updating this if (is_atomic (st)) if (is_func (subtree (et, path_up (tp, 2)), HYBRID, 1)) { string msg; // macro argument string name= st->label; path mp= search_upwards (MACRO); if (!is_nil (mp)) { tree mt= subtree (et, mp); int i, n= N(mt)-1; for (i=0; i<n; i++) if (mt[i] == name) { set_message (concat (kbd ("return"), ": insert argument ", name), "hybrid command"); return true; } } // macro application tree f= get_env_value (name); if (drd->contains (name) && (f == UNINIT)) set_message (concat (kbd ("return"), ": insert primitive ", name), "hybrid command"); else if (is_func (f, MACRO) || is_func (f, XMACRO)) set_message (concat (kbd ("return"), ": insert macro ", name), "hybrid command"); else if (f != UNINIT) set_message (concat (kbd ("return"), ": insert value ", name), "hybrid command"); else return false; return true; } return false; }
static void adjust_left_script (tree t, path& o2) { while (is_concat (t) && o2->item + 1 < N(t) && o2->next == path (1)) { tree st= t[o2->item]; if (is_func (st, LSUB) || is_func (st, LSUP) || is_func (st, LPRIME)) { tree nt= t[o2->item+1]; if (!is_atomic (nt)) o2= path (o2->item+1, end (nt)); else { string s= nt->label; int pos= 0; while (pos < N(s)) { int next= pos; tm_char_forwards (s, next); if (pos == 0); else if (is_numeric (s (0, next))); else if (is_iso_alpha (s (0, next))); else break; pos= next; } o2= path (o2->item+1, pos); } } else break; } }
tree evaluate (tree t) { if (is_atomic (t)) return t; cout << "Evaluate " // << obtain_ip (t) << " " << "[" << (t.operator -> ()) << ", " << (std_env.operator -> ()) << "] " << t << INDENT << LF; memorizer mem= evaluate_memorizer (std_env, t); if (is_memorized (mem)) { cout << UNINDENT << "Memorized " << mem->get_tree () << LF; std_env= mem->get_environment (); return mem->get_tree (); } memorize_start (); tree r= evaluate_impl (t); decorate_ip (t, r); mem->set_tree (r); mem->set_environment (std_env); memorize_end (); cout << UNINDENT << "Computed " << mem->get_tree () // << " at " << obtain_ip (r); << LF; return mem->get_tree (); }
db_constraint database_rep::encode_constraint (tree q) { db_constraint r; if (!is_tuple (q)) return db_constraint (); if (N(q) <= 1 || !is_atomic (q[0])) return db_constraint (); string attr= q[0]->label; if (attr == "any") r << -1; else if (attr == "keywords") return encode_keywords_constraint (q); else if (attr == "order") { r << -2; return r; } else if (attr == "modified") { r << -2; return r; } else if (!is_quoted (q[0]->label)) return db_constraint (); else if (atom_encode->contains (scm_unquote (q[0]->label))) r << atom_encode [scm_unquote (q[0]->label)]; else return db_constraint (); for (int i=1; i<N(q); i++) if (atom_encode->contains (scm_unquote (q[i]->label))) r << atom_encode [scm_unquote (q[i]->label)]; return r; }
tree filter_spaces (tree t, bool &spaced) { if (is_space (t) && spaced) return concat(); if (is_space (t) && !spaced) { spaced= true; return t; } spaced= false; if (is_atomic (t)) return t; tree r (L(t)); int i, n=N(t); if (is_apply (t) || is_tuple (t)) { // then arity shouldn't vary for (i=0; i<n; i++) r << filter_spaces (t[i], spaced); return r; } for (i=0; i<n; i++) { if (t[i] == concat() || t[i] == "") continue; if (!is_space (t[i]) || !spaced) { r << filter_spaces (t[i], spaced); if (is_space (t[i])) spaced= true; else spaced= false; } } n= N(r); if (n>0 && is_space (r[n-1])) r[n-1]= concat(); return r; }