function sheaf_hook_imperative($s) { $s = sheaf::r(trim($s), "\r", ""); // Render as HTML. $s = sheaf::r($s, "\n", "<br/>"); return $s; }
function sheaf_hook_machine($s) { $s = sheaf::r(trim($s), "\r", ""); // Build the simulator link. $instsURI = $s; $instsURI = sheaf::r($instsURI, "\n", "~"); $instsURI = sheaf::r($instsURI, " ", "_"); $instsURI = sheaf::r($instsURI, "#", "%23"); $link = 'onclick="window.open(\'machine.php?instructions=' . $instsURI . '\');"'; $s = sheaf::r($s, " ", " "); // Highlight keywords and built-in functions. $keywords = array('label', 'goto', 'branch', 'jump', 'set', 'copy', 'add'); $builtins = array('#increment#', '#decrement#', '#copy#'); foreach ($keywords as $t) { $s = sheaf::r($s, $t, '<span class="keyword">' . $t . '</span>'); } foreach ($builtins as $t) { $s = sheaf::r($s, $t, '<span class="builtin">' . $t . '</span>'); } // Render as HTML. $s = sheaf::r($s, "\n", "<br/>"); $s = '<div class="button"><button ' . $link . '>simulator</button></div><div class="code" style="margin-top:0px; border-top:0px;"><div class="source"><br/>' . $s . '<br/><br/></div></div>'; return $s; }
function sheaf_hook_JavaScript($s) { $s = sheaf::r($s, "\r", ""); $inp = $s; $s = ""; $flag = array('line' => 0, 'delimited' => 0, 'literal' => 'none', 'escaped' => 0); $lineComment = 'false'; for ($i = 0; $i < strlen($inp); $i++) { $c = $inp[$i]; // Handle single-line newline-terminated comments. if ($c === '#' && $flag['line'] == 0 && $flag['delimited'] == 0 && $flag['literal'] == 'none') { $s .= '<span class="comment">'; $s .= $c; $flag['line'] = 1; } else { if ($c == "\n" && $flag['line'] == 1) { $s .= '</span>'; $s .= $c; $flag['line'] = 0; } else { if ($c == '"') { if ($flag['line'] == 0 && $flag['delimited'] == 0 && $flag['literal'] == 'none') { $s .= '<span class="literal">'; $s .= $c; $flag['literal'] = '"'; } else { if ($flag['literal'] == '"') { $s .= $c; $s .= '</span>'; $flag['literal'] = 'none'; } } } else { if ($c == "'") { if ($flag['line'] == 0 && $flag['delimited'] == 0 && $flag['literal'] == 'none') { $s .= '<span class="literal">'; $s .= $c; $flag['literal'] = "'"; } else { if ($flag['literal'] == "'") { $s .= $c; $s .= '</span>'; $flag['literal'] = 'none'; } } } else { if ($c === " ") { $s .= " "; } else { if ($c === "@") { // Handle built in commands. $handled = false; $commands = array('function', 'return', 'for', 'while', 'break', 'continue', 'if', 'else'); $builtins = array('exit', 'print', 'len', 'range', 'max', 'min', 'pow', 'sum', 'int', 'type', 'str', 'dict', 'list', 'tuple', 'None', 'True', 'False'); foreach ($builtins as $prefix) { if (sheaf::startsWith(substr($inp, $i), '@' . $prefix)) { $s .= '<span class="builtin">' . $prefix . '</span>'; $i += strlen('@' . $prefix) - 1; $handled = true; break; } } foreach ($commands as $prefix) { if (sheaf::startsWith(substr($inp, $i), '@' . $prefix)) { $s .= '<span class="keyword">' . $prefix . '</span>'; $i += strlen('@' . $prefix) - 1; $handled = true; break; } } if (!$handled) { $s .= $c; } } else { $s .= $c; } } } } } } } $s = sheaf::r($s, ">>>", '<span style="color:#ABABAB;">>>></span>'); $s = sheaf::r($s, "\n", "<br/>"); return $s; }
private static function do_xml_parse($startF, $datF, $endF, $xml) { $xml_parser = sheaf::mk_xml_parser($startF, $datF, $endF); if (!xml_parse($xml_parser, $xml)) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } xml_parser_free($xml_parser); }
function sheaf_hook_Haskell($s) { $s = sheaf::r($s, "\r", ""); $inp = $s; $s = ""; $flag = array('line' => 0, 'delimited' => 0, 'literal' => 'none', 'escaped' => 0); $lineComment = 'false'; for ($i = 0; $i < strlen($inp); $i++) { $c = $inp[$i]; // Handle single-line newline-terminated comments. if (($c === '#' || $c === "-" && $i < strlen($inp) - 1 && $inp[$i + 1] == "-") && $flag['line'] == 0 && $flag['delimited'] == 0 && $flag['literal'] == 'none') { $s .= '<span class="comment">'; $s .= $c; $flag['line'] = 1; } else { if ($c == "\n" && $flag['line'] == 1) { $s .= '</span>'; $s .= $c; $flag['line'] = 0; } else { if ($c == '"') { if ($flag['line'] == 0 && $flag['delimited'] == 0 && $flag['literal'] == 'none') { $s .= '<span class="literal">'; $s .= $c; $flag['literal'] = '"'; } else { if ($flag['literal'] == '"') { $s .= $c; $s .= '</span>'; $flag['literal'] = 'none'; } else { $s .= $c; } } } else { if ($c == "'" && strlen($inp) > $i + 2 && $inp[$i + 2] == "'") { if ($flag['line'] == 0 && $flag['delimited'] == 0 && $flag['literal'] == 'none') { $s .= '<span class="literal">' . $inp[$i + 1] . '</span>'; $i += 2; } } else { if ($c === " ") { $s .= " "; } else { if ($c === "@") { // Handle built in commands. $handled = false; $commands = array('module', 'where', 'import', 'data', 'deriving', 'type', 'class', 'instance', 'if', 'then', 'else', 'let', 'in'); $builtins = array(); foreach ($builtins as $prefix) { if (sheaf::startsWith(substr($inp, $i), '@' . $prefix)) { $s .= '<span class="builtin">' . $prefix . '</span>'; $i += strlen('@' . $prefix) - 1; $handled = true; break; } } foreach ($commands as $prefix) { if (sheaf::startsWith(substr($inp, $i), '@' . $prefix)) { $s .= '<span class="keyword">' . $prefix . '</span>'; $i += strlen('@' . $prefix) - 1; $handled = true; break; } } if (!$handled) { $s .= $c; } } else { $s .= $c; } } } } } } } $s = sheaf::r($s, "*>", '<span style="color:#ABABAB;">*></span>'); $s = sheaf::r($s, "\n", "<br/>"); return $s; }
function sheaf_hook_math($s) { $s = sheaf::r($s, '#[', '</td><td><table cellpadding="0" cellspacing="0" style="display:inline;"><tr><td class="html_frac_lft"> </td><td><table cellpadding="0" cellspacing="0" style="font-size:12px;"><tr><td style="white-space:nowrap;">'); $s = sheaf::r($s, '#;', '</td></tr><tr><td style="white-space:nowrap;">'); $s = sheaf::r($s, '#,', '</td><td style="padding-left:8px; white-space:nowrap;">'); $s = sheaf::r($s, '#]', '</td></tr></table></td><td class="html_frac_rgt"> </td></tr></table></td><td>'); $s = sheaf::r($s, '@(', '</td><td><table cellpadding="0" cellspacing="0" style="display:inline;"><tr><td class="html_frac_lft"> </td><td><table cellpadding="0" cellspacing="0" style="font-size:12px;"><tr><td style="white-space:nowrap;">'); $s = sheaf::r($s, '@;', '</td></tr><tr><td style="border-top:1px solid #000000; white-space:nowrap;">'); $s = sheaf::r($s, '@)', '</td></tr></table></td><td class="html_frac_rgt"> </td></tr></table></td><td>'); $s = sheaf::r($s, '#(', '<span style="font-size:24px;">(</span></td><td><table cellpadding="0" cellspacing="0" style="display:inline;"><tr><td class="html_frac_lft"> </td><td><table cellpadding="0" cellspacing="0" style="font-size:12px;"><tr><td style="white-space:nowrap;">'); $s = sheaf::r($s, '#;', '</td></tr><tr><td style="white-space:nowrap;">'); $s = sheaf::r($s, '#)', '</td></tr></table></td><td class="html_frac_rgt"> </td></tr></table></td><td><span style="font-size:24px;">)</span>'); $s = sheaf::r($s, '%{^', '<span style="text-decoration:overline;">'); $s = sheaf::r($s, '}%', '</span>'); $s = sheaf::r($s, '^{\\phi(%m)-1}', '<sup>\\phi(%m)-1</sup>'); $s = sheaf::r($s, '^{\\phi(%m)}', '<sup>\\phi(%m)</sup>'); $s = sheaf::r($s, '^{\\phi(%m) \\cdot %k}', '<sup>\\phi(%m) \\cdot %k</sup>'); $s = sheaf::r($s, '^{\\varphi(%n)}', '<sup>\\varphi(%n)</sup>'); $s = sheaf::r($s, '^{%k}', '<sup><i>k</i></sup>'); $s = sheaf::r($s, '^{%d}', '<sup><i>d</i></sup>'); $s = sheaf::r($s, '^{%i}', '<sup><i>i</i></sup>'); $s = sheaf::r($s, '^{%a \\cdot %b}', '<sup>%a \\cdot %b</sup>'); $s = sheaf::r($s, '^{%a}', '<sup><i>a</i></sup>'); $s = sheaf::r($s, '^{%b}', '<sup><i>b</i></sup>'); $s = sheaf::r($s, '^{%d}', '<sup><i>d</i></sup>'); $s = sheaf::r($s, '^{%e}', '<sup><i>e</i></sup>'); $s = sheaf::r($s, '^{%n}', '<sup><i>n</i></sup>'); $s = sheaf::r($s, '^{%m}', '<sup><i>m</i></sup>'); $s = sheaf::r($s, '^{%r}', '<sup><i>r</i></sup>'); $s = sheaf::r($s, '^{%c}', '<sup><i>c</i></sup>'); $s = sheaf::r($s, '^{%y}', '<sup><i>y</i></sup>'); $s = sheaf::r($s, '^{1/2}', '<sup>1/2</sup>'); $s = sheaf::r($s, '^{%n/2}', '<sup>%n/2</sup>'); $s = sheaf::r($s, '^{%p-1}', '<sup>%p-1</sup>'); $s = sheaf::r($s, '^{%p-2}', '<sup>%p-2</sup>'); $s = sheaf::r($s, '^{%n-1}', '<sup>%n-1</sup>'); $s = sheaf::r($s, '^{%m-1}', '<sup>%m-1</sup>'); $s = sheaf::r($s, '^{%k-1}', '<sup>%k-1</sup>'); $s = sheaf::r($s, '^{%d+1}', '<sup><i>d</i>+1</sup>'); $s = sheaf::r($s, '^{%k+1}', '<sup><i>k</i>+1</sup>'); $s = sheaf::r($s, '^{%z}', '<sup><i>z</i></sup>'); $s = sheaf::r($s, '^{%z_2}', '<sup><i>z</i><sub>2</sub></sup>'); $s = sheaf::r($s, '_0', '<sub>0</sub>'); $s = sheaf::r($s, '_1', '<sub>1</sub>'); $s = sheaf::r($s, '_2', '<sub>2</sub>'); $s = sheaf::r($s, '_3', '<sub>3</sub>'); $s = sheaf::r($s, '_4', '<sub>4</sub>'); $s = sheaf::r($s, '_5', '<sub>5</sub>'); $s = sheaf::r($s, '_6', '<sub>6</sub>'); $s = sheaf::r($s, '_7', '<sub>7</sub>'); $s = sheaf::r($s, '_8', '<sub>8</sub>'); $s = sheaf::r($s, '_{10}', '<sub>10</sub>'); $s = sheaf::r($s, '^0', '<sup>0</sup>'); $s = sheaf::r($s, '^1', '<sup>1</sup>'); $s = sheaf::r($s, '^2', '<sup>2</sup>'); $s = sheaf::r($s, '^3', '<sup>3</sup>'); $s = sheaf::r($s, '^4', '<sup>4</sup>'); $s = sheaf::r($s, '^5', '<sup>5</sup>'); $s = sheaf::r($s, '^6', '<sup>6</sup>'); $s = sheaf::r($s, '^7', '<sup>7</sup>'); $s = sheaf::r($s, '^8', '<sup>8</sup>'); $s = sheaf::r($s, '^9', '<sup>9</sup>'); $s = sheaf::r($s, '^{-1}', '<sup>-1</sup>'); $s = sheaf::r($s, '^{11}', '<sup>11</sup>'); $s = sheaf::r($s, '^{16}', '<sup>16</sup>'); $s = sheaf::r($s, '^{8*32}', '<sup>8 \\cdot 32</sup>'); $s = sheaf::r($s, '^{256}', '<sup>256</sup>'); $s = sheaf::r($s, '^{21}', '<sup>21</sup>'); $s = sheaf::r($s, '_{32}', '<sub>32</sub>'); $s = sheaf::r($s, '_{50}', '<sub>50</sub>'); $s = sheaf::r($s, '_{51}', '<sub>51</sub>'); $s = sheaf::r($s, '_{100}', '<sub>100</sub>'); $s = sheaf::r($s, '_{%i}', '<sub><i>i</i></sub>'); $s = sheaf::r($s, '_{%m}', '<sub><i>m</i></sub>'); $s = sheaf::r($s, '_{%n}', '<sub><i>n</i></sub>'); $s = sheaf::r($s, '_{%r}', '<sub><i>r</i></sub>'); $s = sheaf::r($s, '_{%N}', '<sub><i>N</i></sub>'); $s = sheaf::r($s, '_{%i-1}', '<sub><i>i</i>-1</sub>'); $s = sheaf::r($s, '_{%n-1}', '<sub><i>n</i>-1</sub>'); $s = sheaf::r($s, '_{%k-1}', '<sub><i>k</i>-1</sub>'); $s = sheaf::r($s, '_{%k}', '<sub><i>k</i></sub>'); $s = sheaf::r($s, '_{%g}', '<sub><i>g</i></sub>'); $s = sheaf::r($s, '_{%j}', '<sub><i>j</i></sub>'); $s = sheaf::r($s, '\\lfloor', '⌊'); $s = sheaf::r($s, '\\rfloor', '⌋'); $s = sheaf::r($s, '\\lceil', '⌈'); $s = sheaf::r($s, '\\rceil', '⌉'); $s = sheaf::r($s, '\\emptyset', '∅'); $s = sheaf::r($s, '\\forall', '∀'); $s = sheaf::r($s, '\\exists', '∃'); $s = sheaf::r($s, '\\gcd', 'gcd'); $s = sheaf::r($s, '\\max', 'max'); $s = sheaf::r($s, '\\min', 'min'); $s = sheaf::r($s, '\\dom', 'dom'); $s = sheaf::r($s, '\\log', 'log'); $s = sheaf::r($s, '\\ln', 'ln'); $s = sheaf::r($s, '%-', '−'); $s = sheaf::r($s, '\\sqrt', '√'); $s = sheaf::r($s, '\\phi', 'φ'); $s = sheaf::r($s, '\\varphi', 'φ'); $s = sheaf::r($s, '\\lambda', 'λ'); $s = sheaf::r($s, '\\tau', 'τ'); $s = sheaf::r($s, '\\pm', '±'); $s = sheaf::r($s, '\\Gamma', 'Γ'); $s = sheaf::r($s, '\\vdash', '⊢'); $s = sheaf::r($s, '\\langle', '⟨'); $s = sheaf::r($s, '\\rangle', '⟩'); $s = sheaf::r($s, '\\0', '<b>0</b>'); $s = sheaf::r($s, '\\1', '<b>1</b>'); $s = sheaf::r($s, '\\top', '⊤'); $s = sheaf::r($s, '\\bot', '⊥'); $s = sheaf::r($s, '%[', '<i style="text-decoration:underline;">'); $s = sheaf::r($s, ']%', '</i>'); $ops = array('=' => '=', '>' => '>', '<' => '<', '\\gt' => '>', '\\lt' => '<', ':=' => ':=', '::=' => '::=', 'iff' => 'iff', '\\models' => '⊨', '\\rightarrow' => '<span style="font-size:12px;">→</span>', '\\leftarrow' => '<span style="font-size:12px;">←</span>', '\\downarrow' => '<span style="font-size:12px;">↓</span>', '\\uparrow' => '<span style="font-size:12px;">↑</span>', '\\Leftrightarrow' => '<span style="font-size:16px;">⇔</span>', '\\Rightarrow' => '<span style="font-size:16px;">⇒</span>', '|' => '|', '\\nmid' => '∤', '\\vdots' => '⋮', '\\leq' => '≤', '\\geq' => '≥', '\\neq' => '≠', '\\not\\in' => '∉', '\\in' => '<span style="font-size:12px;">∈</span>', '\\subset' => '⊂', '\\cup' => '∪', '\\cap' => '∩', '\\times' => '×', '\\Downarrow' => '⇓', '\\Sigma' => 'Σ', '\\sigma' => 'σ', '\\uplus' => '⊎', '\\oplus' => '⊕', '\\otimes' => '⊗', '\\mapsto' => '↦', '\\neg' => '¬', '\\wedge' => '∧', '\\vee' => '∨', '\\mod' => 'mod', '\\log' => 'log', '\\cdot' => '⋅', '\\not\\equiv' => '≢', '\\equiv' => '≡', '\\cong' => '≅', '\\approx' => '≈', '\\sim' => '∼', '\\varepsilon' => 'ε', '\\circ' => '<span style="font-size:10px;">o</span>', '%~' => ' '); foreach (str_split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') as $v) { $s = str_replace('%' . $v, '<i>' . $v . '</i>', $s); } foreach (str_split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') as $v) { $s = str_replace('@' . $v, '<b>' . $v . '</b>', $s); } $s = str_replace("\\begin{eqnarray}\n", "\\begin{eqnarray}", $s); $s = str_replace('\\begin{eqnarray}', '<table style="padding-left:20px; margin:4px 0px 4px 0px;"><tr><td style="text-align:right; white-space:nowrap;"><table style="width:100%;"><tr><td style="text-align:right;">', $s); foreach ($ops as $rel => $relH) { $s = str_replace('& ' . $rel . ' &', '<td></tr></table></td><td style="text-align:center;"> ' . $relH . ' </td><td><table style="white-space:nowrap;"><tr><td style="white-space:nowrap;">', $s); } $s = str_replace('\\\\', '</td></tr></table></td></tr><tr><td style="text-align:right;"><table style="width:100%;"><tr><td style="text-align:right;">', $s); $s = str_replace('%%', '</td></tr></table></td></tr><tr><td style="text-align:right;"><table style="width:100%;"><tr><td style="text-align:right;">', $s); $s = str_replace('\\end{eqnarray}', '</td></tr></table></td></tr></table>', $s); foreach ($ops as $str => $html) { $s = str_replace($str, $html, $s); } $s = sheaf::r($s, '\\Z', 'ℤ'); $s = sheaf::r($s, '\\N', 'ℕ'); $s = sheaf::r($s, '\\R', 'ℝ'); $s = sheaf::r($s, '\\U', '<b><i>U</i></b>'); $s = sheaf::r($s, '\\D', '<b><i>D</i></b>'); $s = sheaf::r($s, '\\powerset', '℘'); $s = sheaf::r($s, '\\Pr', 'Pr'); return $s; }