Fix: merge conflict
[myslice.git] / to-be-integrated / third-party / codemirror-3.15 / mode / ruby / index.html
1 <!doctype html>
2 <html>
3   <head>
4     <meta charset="utf-8">
5     <title>CodeMirror: Ruby mode</title>
6     <link rel="stylesheet" href="../../lib/codemirror.css">
7     <script src="../../lib/codemirror.js"></script>
8     <script src="../../addon/edit/matchbrackets.js"></script>
9     <script src="ruby.js"></script>
10     <style>
11       .CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}
12       .cm-s-default span.cm-arrow { color: red; }
13     </style>
14     <link rel="stylesheet" href="../../doc/docs.css">
15   </head>
16   <body>
17     <h1>CodeMirror: Ruby mode</h1>
18     <form><textarea id="code" name="code">
19 # Code from http://sandbox.mc.edu/~bennet/ruby/code/poly_rb.html
20 #
21 # This program evaluates polynomials.  It first asks for the coefficients
22 # of a polynomial, which must be entered on one line, highest-order first.
23 # It then requests values of x and will compute the value of the poly for
24 # each x.  It will repeatly ask for x values, unless you the user enters
25 # a blank line.  It that case, it will ask for another polynomial.  If the
26 # user types quit for either input, the program immediately exits.
27 #
28
29 #
30 # Function to evaluate a polynomial at x.  The polynomial is given
31 # as a list of coefficients, from the greatest to the least.
32 def polyval(x, coef)
33     sum = 0
34     coef = coef.clone           # Don't want to destroy the original
35     while true
36         sum += coef.shift       # Add and remove the next coef
37         break if coef.empty?    # If no more, done entirely.
38         sum *= x                # This happens the right number of times.
39     end
40     return sum
41 end
42
43 #
44 # Function to read a line containing a list of integers and return
45 # them as an array of integers.  If the string conversion fails, it
46 # throws TypeError.  If the input line is the word 'quit', then it
47 # converts it to an end-of-file exception
48 def readints(prompt)
49     # Read a line
50     print prompt
51     line = readline.chomp
52     raise EOFError.new if line == 'quit' # You can also use a real EOF.
53             
54     # Go through each item on the line, converting each one and adding it
55     # to retval.
56     retval = [ ]
57     for str in line.split(/\s+/)
58         if str =~ /^\-?\d+$/
59             retval.push(str.to_i)
60         else
61             raise TypeError.new
62         end
63     end
64
65     return retval
66 end
67
68 #
69 # Take a coeff and an exponent and return the string representation, ignoring
70 # the sign of the coefficient.
71 def term_to_str(coef, exp)
72     ret = ""
73
74     # Show coeff, unless it's 1 or at the right
75     coef = coef.abs
76     ret = coef.to_s     unless coef == 1 && exp > 0
77     ret += "x" if exp > 0                               # x if exponent not 0
78     ret += "^" + exp.to_s if exp > 1                    # ^exponent, if > 1.
79
80     return ret
81 end
82
83 #
84 # Create a string of the polynomial in sort-of-readable form.
85 def polystr(p)
86     # Get the exponent of first coefficient, plus 1.
87     exp = p.length
88
89     # Assign exponents to each term, making pairs of coeff and exponent,
90     # Then get rid of the zero terms.
91     p = (p.map { |c| exp -= 1; [ c, exp ] }).select { |p| p[0] != 0 }
92
93     # If there's nothing left, it's a zero
94     return "0" if p.empty?
95
96     # *** Now p is a non-empty list of [ coef, exponent ] pairs. ***
97
98     # Convert the first term, preceded by a "-" if it's negative.
99     result = (if p[0][0] < 0 then "-" else "" end) + term_to_str(*p[0])
100
101     # Convert the rest of the terms, in each case adding the appropriate
102     # + or - separating them.  
103     for term in p[1...p.length]
104         # Add the separator then the rep. of the term.
105         result += (if term[0] < 0 then " - " else " + " end) + 
106                 term_to_str(*term)
107     end
108
109     return result
110 end
111         
112 #
113 # Run until some kind of endfile.
114 begin
115     # Repeat until an exception or quit gets us out.
116     while true
117         # Read a poly until it works.  An EOF will except out of the
118         # program.
119         print "\n"
120         begin
121             poly = readints("Enter a polynomial coefficients: ")
122         rescue TypeError
123             print "Try again.\n"
124             retry
125         end
126         break if poly.empty?
127
128         # Read and evaluate x values until the user types a blank line.
129         # Again, an EOF will except out of the pgm.
130         while true
131             # Request an integer.
132             print "Enter x value or blank line: "
133             x = readline.chomp
134             break if x == ''
135             raise EOFError.new if x == 'quit'
136
137             # If it looks bad, let's try again.
138             if x !~ /^\-?\d+$/
139                 print "That doesn't look like an integer.  Please try again.\n"
140                 next
141             end
142
143             # Convert to an integer and print the result.
144             x = x.to_i
145             print "p(x) = ", polystr(poly), "\n"
146             print "p(", x, ") = ", polyval(x, poly), "\n"
147         end
148     end
149 rescue EOFError
150     print "\n=== EOF ===\n"
151 rescue Interrupt, SignalException
152     print "\n=== Interrupted ===\n"
153 else
154     print "--- Bye ---\n"
155 end
156 </textarea></form>
157     <script>
158       var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
159         mode: "text/x-ruby",
160         tabMode: "indent",
161         matchBrackets: true,
162         indentUnit: 4
163       });
164     </script>
165
166     <p><strong>MIME types defined:</strong> <code>text/x-ruby</code>.</p>
167
168     <p>Development of the CodeMirror Ruby mode was kindly sponsored
169     by <a href="http://ubalo.com/">Ubalo</a>, who hold
170     the <a href="LICENSE">license</a>.</p>
171
172   </body>
173 </html>