Back to index

texmacs  1.0.7.15
to_scheme.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : to_scheme.cpp
00004 * DESCRIPTION: conversion of TeXmacs trees to scheme expressions
00005 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "convert.hpp"
00013 #include "drd_std.hpp"
00014 
00015 /******************************************************************************
00016 * Handling escape characters
00017 ******************************************************************************/
00018 
00019 string
00020 slash (string s) {
00021   int i, n= N(s);
00022   string r;
00023   for (i=0; i<n; i++)
00024     switch (s[i]) {
00025     case '(':
00026     case ')':
00027     case ' ': 
00028     case '\'':
00029       if ((n<2) || (s[0]!='\042') || (s[n-1]!='\042')) r << "\\";
00030       r << s[i];
00031       break;
00032     case '\\':
00033       r << '\\' << s[i];
00034       break;
00035     case '\042':
00036       if (((i==0) && (s[n-1]=='\042')) ||
00037          ((i==(n-1)) && (s[0]=='\042')))
00038        r << s[i];
00039       else r << "\\" << s[i];
00040       break;
00041     case ((char) 0):
00042       r << "\\0";
00043       break;
00044     case '\t':
00045       r << "\\t";
00046       break;
00047     case '\n':
00048       r << "\\n";
00049       break;
00050     default:
00051       r << s[i];
00052     }
00053   return r;
00054 }
00055 
00056 /******************************************************************************
00057 * Converting scheme trees to strings
00058 ******************************************************************************/
00059 
00060 static void
00061 scheme_tree_to_string (string& out, scheme_tree p) {
00062   if (!is_tuple (p)) {
00063     string s= p->label;
00064     if (is_quoted (s)) out << scm_quote (raw_unquote (s));
00065     else out << slash (s);
00066   }
00067   else {
00068     if (is_tuple (p, "\'", 1)) {
00069       out << "\'";
00070       scheme_tree_to_string (out, p[1]);
00071     }
00072     else {
00073       int i, n= N(p);
00074       out << "(";
00075       for (i=0; i<n; i++) {
00076        if (i>0) out << " ";
00077        scheme_tree_to_string (out, p[i]);
00078       }
00079       out << ")";
00080     }
00081   }
00082 }
00083 
00084 string
00085 scheme_tree_to_string (scheme_tree p) {
00086   string out;
00087   scheme_tree_to_string (out, p);
00088   return out;
00089 }
00090 
00091 string
00092 scheme_tree_to_block (scheme_tree p) {
00093   string out;
00094   int i, n= N(p);
00095   for (i=0; i<n; i++)
00096     out << scheme_tree_to_string (p[i]) << "\n";
00097   return out;
00098 }
00099 
00100 /******************************************************************************
00101 * Conversion from trees to scheme trees
00102 ******************************************************************************/
00103 
00104 scheme_tree
00105 tree_to_scheme_tree (tree t) {
00106   if (is_atomic (t)) return scm_quote (t->label);
00107   else if (is_func (t, EXPAND) && is_atomic (t[0])) {
00108     int i, n= N(t);
00109     tree u (TUPLE, n);
00110     u[0]= copy (t[0]);
00111     for (i=1; i<n; i++)
00112       u[i]= tree_to_scheme_tree (t[i]);
00113     return u;    
00114   }
00115   else {
00116     int i, n= N(t);
00117     tree u (TUPLE, n+1);
00118     u[0]= copy (as_string (L(t)));
00119     for (i=0; i<n; i++)
00120       u[i+1]= tree_to_scheme_tree (t[i]);
00121     return u;
00122   }
00123 }
00124 
00125 /******************************************************************************
00126 * Conversion of trees to scheme strings
00127 ******************************************************************************/
00128 
00129 string
00130 tree_to_scheme (tree t) {
00131   return scheme_tree_to_string (tree_to_scheme_tree (t));
00132 }