mirah 0.0.7-java → 0.0.8-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +181 -0
- data/README.txt +6 -10
- data/Rakefile +86 -9
- data/bin/mirah +2 -0
- data/bin/mirahc +2 -0
- data/bin/mirahp +2 -0
- data/{bin/dubyp → examples/interfaces.mirah} +16 -9
- data/examples/macros/square.mirah +12 -0
- data/examples/macros/square_int.mirah +12 -0
- data/examples/macros/string-each-char.mirah +14 -0
- data/examples/maven/README.txt +2 -0
- data/examples/maven/pom.xml +23 -0
- data/examples/maven/src/main/mirah/hello_mirah.mirah +9 -0
- data/examples/rosettacode/100-doors.mirah +44 -0
- data/examples/rosettacode/99-bottles-of-beer.mirah +13 -0
- data/examples/rosettacode/README.txt +9 -0
- data/examples/rosettacode/boolean-values.mirah +29 -0
- data/examples/rosettacode/comments.mirah +2 -0
- data/examples/rosettacode/copy-a-string.mirah +10 -0
- data/examples/rosettacode/count-occurrences-of-a-substring.mirah +40 -0
- data/examples/rosettacode/create-a-file.mirah +6 -0
- data/examples/rosettacode/empty-string.mirah +9 -0
- data/examples/rosettacode/factorial.mirah +10 -0
- data/examples/rosettacode/fibonacci.mirah +21 -0
- data/examples/rosettacode/file-size.mirah +5 -0
- data/examples/rosettacode/fizz-buzz.mirah +21 -0
- data/examples/rosettacode/flatten-a-list.mirah +24 -0
- data/examples/rosettacode/guess-the-number.mirah +21 -0
- data/examples/rosettacode/is-string-numeric.mirah +127 -0
- data/examples/rosettacode/palindrome.mirah +14 -0
- data/examples/rosettacode/repeat-a-string.mirah +9 -0
- data/examples/rosettacode/reverse-a-string.mirah +6 -0
- data/examples/rosettacode/rot-13.mirah +20 -0
- data/examples/rosettacode/user-input.mirah +4 -0
- data/examples/sort_closure.mirah +1 -1
- data/javalib/dynalink-0.2.jar +0 -0
- data/javalib/mirah-bootstrap.jar +0 -0
- data/lib/mirah.rb +7 -16
- data/lib/mirah/ast.rb +22 -92
- data/lib/mirah/ast/call.rb +41 -9
- data/lib/mirah/ast/class.rb +34 -6
- data/lib/mirah/ast/flow.rb +17 -5
- data/lib/mirah/ast/intrinsics.rb +50 -8
- data/lib/mirah/ast/literal.rb +7 -0
- data/lib/mirah/ast/local.rb +9 -1
- data/lib/mirah/ast/method.rb +21 -8
- data/lib/mirah/ast/scope.rb +1 -1
- data/lib/mirah/ast/structure.rb +81 -15
- data/lib/mirah/ast/type.rb +4 -0
- data/{bin/dubyc → lib/mirah/commands.rb} +4 -11
- data/lib/mirah/commands/base.rb +54 -0
- data/lib/mirah/commands/compile.rb +39 -0
- data/{examples/wiki/Rakefile → lib/mirah/commands/parse.rb} +18 -17
- data/lib/mirah/commands/run.rb +73 -0
- data/lib/mirah/compiler.rb +37 -417
- data/lib/mirah/compiler/call.rb +45 -0
- data/lib/mirah/compiler/class.rb +81 -0
- data/lib/mirah/compiler/flow.rb +109 -0
- data/lib/mirah/compiler/literal.rb +130 -0
- data/lib/mirah/compiler/local.rb +59 -0
- data/lib/mirah/compiler/method.rb +44 -0
- data/lib/mirah/compiler/structure.rb +65 -0
- data/lib/mirah/compiler/type.rb +27 -0
- data/lib/mirah/env.rb +4 -6
- data/lib/mirah/generator.rb +61 -0
- data/lib/mirah/jvm/compiler.rb +8 -867
- data/lib/mirah/jvm/compiler/base.rb +270 -0
- data/lib/mirah/jvm/compiler/java_source.rb +779 -0
- data/lib/mirah/jvm/compiler/jvm_bytecode.rb +851 -0
- data/lib/mirah/jvm/method_lookup.rb +21 -2
- data/lib/mirah/jvm/source_generator/builder.rb +10 -13
- data/lib/mirah/jvm/source_generator/loops.rb +99 -93
- data/lib/mirah/jvm/source_generator/precompile.rb +3 -2
- data/lib/mirah/jvm/typer.rb +3 -3
- data/lib/mirah/jvm/types.rb +10 -426
- data/lib/mirah/jvm/types/array_type.rb +62 -0
- data/lib/mirah/jvm/types/basic_types.rb +1 -0
- data/lib/mirah/jvm/types/dynamic_type.rb +46 -0
- data/lib/mirah/jvm/types/factory.rb +23 -5
- data/lib/mirah/jvm/types/interface_definition.rb +20 -0
- data/lib/mirah/jvm/types/intrinsics.rb +15 -3
- data/lib/mirah/jvm/types/meta_type.rb +45 -0
- data/lib/mirah/jvm/types/methods.rb +12 -5
- data/lib/mirah/jvm/types/null_type.rb +27 -0
- data/lib/mirah/jvm/types/primitive_type.rb +38 -0
- data/lib/mirah/jvm/types/source_mirror.rb +266 -0
- data/lib/mirah/jvm/types/type.rb +173 -0
- data/lib/mirah/jvm/types/type_definition.rb +55 -0
- data/lib/mirah/jvm/types/unreachable_type.rb +27 -0
- data/lib/mirah/jvm/types/void_type.rb +19 -0
- data/lib/mirah/parser.rb +90 -0
- data/lib/mirah/plugin/gwt.rb +5 -5
- data/lib/mirah/plugin/java.rb +1 -1
- data/lib/mirah/transform.rb +4 -321
- data/lib/mirah/transform/ast_ext.rb +63 -0
- data/lib/mirah/transform/error.rb +13 -0
- data/lib/mirah/transform/helper.rb +761 -0
- data/lib/mirah/transform/transformer.rb +255 -0
- data/lib/mirah/typer.rb +2 -383
- data/{bin/duby → lib/mirah/typer/base.rb} +12 -10
- data/lib/mirah/typer/simple.rb +377 -0
- data/lib/mirah/util/argument_processor.rb +114 -0
- data/lib/mirah/util/class_loader.rb +37 -0
- data/lib/mirah/util/compilation_state.rb +51 -0
- data/lib/mirah/util/process_errors.rb +33 -0
- data/lib/mirah/version.rb +1 -1
- data/lib/mirah_task.rb +3 -2
- data/test/{test_ast.rb → core/test_ast.rb} +6 -0
- data/test/{test_compilation.rb → core/test_compilation.rb} +0 -0
- data/test/{test_env.rb → core/test_env.rb} +24 -25
- data/test/{test_macros.rb → core/test_macros.rb} +2 -4
- data/test/{test_typer.rb → core/test_typer.rb} +0 -3
- data/test/jvm/bytecode_test_helper.rb +181 -0
- data/test/{test_javac_compiler.rb → jvm/javac_test_helper.rb} +38 -22
- data/test/jvm/test_enumerable.rb +304 -0
- data/test/{test_java_typer.rb → jvm/test_java_typer.rb} +2 -4
- data/test/{test_jvm_compiler.rb → jvm/test_jvm_compiler.rb} +146 -443
- data/test/jvm/test_macros.rb +147 -0
- data/test/jvm/test_main_method.rb +15 -0
- data/test/{test_gwt.rb → plugins/test_gwt.rb} +0 -2
- metadata +103 -91
- data/bin/jrubyp +0 -52
- data/examples/wiki/src/org/mirah/wiki/MirahWiki.duby +0 -339
- data/examples/wiki/src/org/mirah/wiki/edit.eduby.html +0 -42
- data/examples/wiki/src/org/mirah/wiki/error.eduby.html +0 -2
- data/examples/wiki/src/org/mirah/wiki/layout.eduby.html +0 -69
- data/examples/wiki/src/org/mirah/wiki/parser.eduby.html +0 -7
- data/examples/wiki/src/org/mirah/wiki/view.eduby.html +0 -15
- data/examples/wiki/war/WEB-INF/classes/test/HeredocContext.class +0 -0
- data/examples/wiki/war/WEB-INF/classes/test/MirahParser.class +0 -0
- data/examples/wiki/war/WEB-INF/lib/appengine-api.jar +0 -0
- data/examples/wiki/war/WEB-INF/lib/dubydatastore.jar +0 -0
- data/examples/wiki/war/WEB-INF/lib/jmeta-runtime.jar +0 -0
- data/examples/wiki/war/WEB-INF/lib/pegdown-stubs.jar +0 -0
- data/examples/wiki/war/WEB-INF/pegdown.jar +0 -0
- data/examples/wiki/war/app.yaml +0 -21
- data/examples/wiki/war/public/favicon.ico +0 -0
- data/examples/wiki/war/public/images/appengine_duby.png +0 -0
- data/examples/wiki/war/public/images/back.gif +0 -0
- data/examples/wiki/war/public/images/dir.gif +0 -0
- data/examples/wiki/war/public/images/file.gif +0 -0
- data/examples/wiki/war/public/javascripts/prettify.js +0 -61
- data/examples/wiki/war/public/robots.txt +0 -0
- data/examples/wiki/war/public/stylesheets/main.css +0 -156
- data/examples/wiki/war/public/stylesheets/prettify.css +0 -1
- data/examples/wiki/war/public/stylesheets/sh_style.css +0 -66
- data/examples/wiki/war/public/stylesheets/source.css +0 -21
- data/examples/wiki/war/public/wmd/images/bg-fill.png +0 -0
- data/examples/wiki/war/public/wmd/images/bg.png +0 -0
- data/examples/wiki/war/public/wmd/images/blockquote.png +0 -0
- data/examples/wiki/war/public/wmd/images/bold.png +0 -0
- data/examples/wiki/war/public/wmd/images/code.png +0 -0
- data/examples/wiki/war/public/wmd/images/h1.png +0 -0
- data/examples/wiki/war/public/wmd/images/hr.png +0 -0
- data/examples/wiki/war/public/wmd/images/img.png +0 -0
- data/examples/wiki/war/public/wmd/images/italic.png +0 -0
- data/examples/wiki/war/public/wmd/images/link.png +0 -0
- data/examples/wiki/war/public/wmd/images/ol.png +0 -0
- data/examples/wiki/war/public/wmd/images/redo.png +0 -0
- data/examples/wiki/war/public/wmd/images/separator.png +0 -0
- data/examples/wiki/war/public/wmd/images/ul.png +0 -0
- data/examples/wiki/war/public/wmd/images/undo.png +0 -0
- data/examples/wiki/war/public/wmd/images/wmd-on.png +0 -0
- data/examples/wiki/war/public/wmd/images/wmd.png +0 -0
- data/examples/wiki/war/public/wmd/showdown.js +0 -421
- data/examples/wiki/war/public/wmd/wmd-base.js +0 -1799
- data/examples/wiki/war/public/wmd/wmd-plus.js +0 -311
- data/examples/wiki/war/public/wmd/wmd.js +0 -73
- data/examples/wiki/war/src/org/mirah/wiki/MirahWiki.duby +0 -339
- data/examples/wiki/war/src/org/mirah/wiki/edit.eduby.html +0 -42
- data/examples/wiki/war/src/org/mirah/wiki/error.eduby.html +0 -2
- data/examples/wiki/war/src/org/mirah/wiki/layout.eduby.html +0 -69
- data/examples/wiki/war/src/org/mirah/wiki/parser.eduby.html +0 -7
- data/examples/wiki/war/src/org/mirah/wiki/view.eduby.html +0 -15
- data/javalib/dynalink-0.1.jar +0 -0
- data/javalib/jsr292-mock.jar +0 -0
- data/lib/mirah/class_loader.rb +0 -35
- data/lib/mirah/compilation_state.rb +0 -28
- data/lib/mirah/impl.rb +0 -273
- data/lib/mirah/jvm/base.rb +0 -267
- data/lib/mirah/jvm/source_compiler.rb +0 -760
- data/lib/mirah/transform2.rb +0 -752
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
<project xmlns="http://maven.apache.org/POM/5.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
|
2
|
+
<modelVersion>4.0.0</modelVersion>
|
|
3
|
+
<artifactId>yourArtifact</artifactId>
|
|
4
|
+
<groupId>yourGroup</groupId>
|
|
5
|
+
<version>0.0.0-SNAPSHOT</version>
|
|
6
|
+
<packaging>jar</packaging>
|
|
7
|
+
<name>Sample Maven build for Mirah source</name>
|
|
8
|
+
|
|
9
|
+
<build>
|
|
10
|
+
<plugins>
|
|
11
|
+
<plugin>
|
|
12
|
+
<groupId>org.mirah.maven</groupId>
|
|
13
|
+
<artifactId>maven-mirah-plugin</artifactId>
|
|
14
|
+
<version>1.0</version>
|
|
15
|
+
<executions>
|
|
16
|
+
<execution>
|
|
17
|
+
<goals><goal>compile</goal></goals>
|
|
18
|
+
</execution>
|
|
19
|
+
</executions>
|
|
20
|
+
</plugin>
|
|
21
|
+
</plugins>
|
|
22
|
+
</build>
|
|
23
|
+
</project>
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import java.util.ArrayList
|
|
2
|
+
|
|
3
|
+
class Door
|
|
4
|
+
:state
|
|
5
|
+
|
|
6
|
+
def initialize
|
|
7
|
+
@state=false
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def closed?; !@state; end
|
|
11
|
+
def open?; @state; end
|
|
12
|
+
|
|
13
|
+
def close; @state=false; end
|
|
14
|
+
def open; @state=true; end
|
|
15
|
+
|
|
16
|
+
def toggle
|
|
17
|
+
if closed?
|
|
18
|
+
open
|
|
19
|
+
else
|
|
20
|
+
close
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def toString; Boolean.toString(@state); end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
doors=ArrayList.new
|
|
28
|
+
1.upto(100) do
|
|
29
|
+
doors.add(Door.new)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
1.upto(100) do |multiplier|
|
|
33
|
+
index = 0
|
|
34
|
+
doors.each do |door|
|
|
35
|
+
Door(door).toggle if (index+1)%multiplier == 0
|
|
36
|
+
index += 1
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
i = 0
|
|
41
|
+
doors.each do |door|
|
|
42
|
+
puts "Door #{i+1} is #{door}."
|
|
43
|
+
i+=1
|
|
44
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
plural = 's'
|
|
2
|
+
99.downto(1) do |i|
|
|
3
|
+
puts "#{i} bottle#{plural} of beer on the wall,"
|
|
4
|
+
puts "#{i} bottle#{plural} of beer"
|
|
5
|
+
puts "Take one down, pass it around!"
|
|
6
|
+
plural = '' if i - 1 == 1
|
|
7
|
+
if i > 1
|
|
8
|
+
puts "#{i-1} bottle#{plural} of beer on the wall!"
|
|
9
|
+
puts
|
|
10
|
+
else
|
|
11
|
+
puts "No more bottles of beer on the wall!"
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
|
|
2
|
+
This example set is all the code for Mirah from the Rosetta Code wiki
|
|
3
|
+
(http://rosettacode.org/.
|
|
4
|
+
|
|
5
|
+
This code is the canonical source for the wiki. Please add as many more
|
|
6
|
+
samples as you'd like. Either add the sample here and send a pull request
|
|
7
|
+
to the project owners, or add a sample to the Rosetta Code wiki and send
|
|
8
|
+
a message to the project owners. Either way, we'd like to try and keep
|
|
9
|
+
the wiki page and this repository synchronized.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import java.util.ArrayList
|
|
2
|
+
import java.util.HashMap
|
|
3
|
+
|
|
4
|
+
# booleans
|
|
5
|
+
puts 'true is true' if true
|
|
6
|
+
puts 'false is false' if (!false)
|
|
7
|
+
|
|
8
|
+
# lists treated as booleans
|
|
9
|
+
x = ArrayList.new
|
|
10
|
+
puts "empty array is true" if x
|
|
11
|
+
x.add("an element")
|
|
12
|
+
puts "full array is true" if x
|
|
13
|
+
puts "isEmpty() is false" if !x.isEmpty()
|
|
14
|
+
|
|
15
|
+
# maps treated as booleans
|
|
16
|
+
map = HashMap.new
|
|
17
|
+
puts "empty map is true" if map
|
|
18
|
+
map.put('a', '1')
|
|
19
|
+
puts "full map is true" if map
|
|
20
|
+
puts "size() is 0 is false" if !(map.size() == 0)
|
|
21
|
+
|
|
22
|
+
# these things do not compile
|
|
23
|
+
# value = nil # ==> cannot assign nil to Boolean value
|
|
24
|
+
# puts 'nil is false' if false == nil # ==> cannot compare boolean to nil
|
|
25
|
+
# puts '0 is false' if (0 == false) # ==> cannot compare int to false
|
|
26
|
+
|
|
27
|
+
#puts 'TRUE is true' if TRUE # ==> TRUE does not exist
|
|
28
|
+
#puts 'FALSE is false' if !FALSE # ==> FALSE does not exist
|
|
29
|
+
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import java.util.regex.Pattern
|
|
2
|
+
import java.util.regex.Matcher
|
|
3
|
+
|
|
4
|
+
#The "remove and count the difference" method
|
|
5
|
+
def count_substring(pattern:string, source:string)
|
|
6
|
+
(source.length() - source.replace(pattern, "").length()) / pattern.length()
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
puts count_substring("th", "the three truths") # ==> 3
|
|
10
|
+
puts count_substring("abab", "ababababab") # ==> 2
|
|
11
|
+
puts count_substring("a*b", "abaabba*bbaba*bbab") # ==> 2
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# The "split and count" method
|
|
15
|
+
def count_substring2(pattern:string, source:string)
|
|
16
|
+
# the result of split() will contain one more element than the delimiter
|
|
17
|
+
# the "-1" second argument makes it not discard trailing empty strings
|
|
18
|
+
source.split(Pattern.quote(pattern), -1).length - 1
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
puts count_substring2("th", "the three truths") # ==> 3
|
|
22
|
+
puts count_substring2("abab", "ababababab") # ==> 2
|
|
23
|
+
puts count_substring2("a*b", "abaabba*bbaba*bbab") # ==> 2
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# This method does a match and counts how many times it matches
|
|
27
|
+
def count_substring3(pattern:string, source:string)
|
|
28
|
+
result = 0
|
|
29
|
+
Matcher m = Pattern.compile(Pattern.quote(pattern)).matcher(source);
|
|
30
|
+
while (m.find())
|
|
31
|
+
result = result + 1
|
|
32
|
+
end
|
|
33
|
+
result
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
puts count_substring3("th", "the three truths") # ==> 3
|
|
37
|
+
puts count_substring3("abab", "ababababab") # ==> 2
|
|
38
|
+
puts count_substring3("a*b", "abaabba*bbaba*bbab") # ==> 2
|
|
39
|
+
|
|
40
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
|
|
2
|
+
def fibonacci(n:int)
|
|
3
|
+
return n if n < 2
|
|
4
|
+
fibPrev = 1
|
|
5
|
+
fib = 1
|
|
6
|
+
3.upto(Math.abs(n)) do
|
|
7
|
+
oldFib = fib
|
|
8
|
+
fib = fib + fibPrev
|
|
9
|
+
fibPrev = oldFib
|
|
10
|
+
end
|
|
11
|
+
fib * (n<0 ? int(Math.pow(n+1, -1)) : 1)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
puts fibonacci 1
|
|
15
|
+
puts fibonacci 2
|
|
16
|
+
puts fibonacci 3
|
|
17
|
+
puts fibonacci 4
|
|
18
|
+
puts fibonacci 5
|
|
19
|
+
puts fibonacci 6
|
|
20
|
+
puts fibonacci 7
|
|
21
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
1.upto(100) do |n|
|
|
2
|
+
print "Fizz" if a = ((n % 3) == 0)
|
|
3
|
+
print "Buzz" if b = ((n % 5) == 0)
|
|
4
|
+
print n unless (a || b)
|
|
5
|
+
print "\n"
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
# a little more straight forward
|
|
9
|
+
1.upto(100) do |n|
|
|
10
|
+
if (n % 15) == 0
|
|
11
|
+
puts "FizzBuzz"
|
|
12
|
+
elsif (n % 5) == 0
|
|
13
|
+
puts "Buzz"
|
|
14
|
+
elsif (n % 3) == 0
|
|
15
|
+
puts "Fizz"
|
|
16
|
+
else
|
|
17
|
+
puts n
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import java.util.ArrayList
|
|
2
|
+
import java.util.List
|
|
3
|
+
import java.util.Collection
|
|
4
|
+
|
|
5
|
+
def flatten(list: Collection)
|
|
6
|
+
flatten(list, ArrayList.new)
|
|
7
|
+
end
|
|
8
|
+
def flatten(source: Collection, result: List)
|
|
9
|
+
|
|
10
|
+
source.each do |x|
|
|
11
|
+
if (Collection.class.isAssignableFrom(x.getClass()))
|
|
12
|
+
flatten(Collection(x), result)
|
|
13
|
+
else
|
|
14
|
+
result.add(x)
|
|
15
|
+
result # if branches must return same type
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
result
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# creating a list-of-list-of-list fails currently, so constructor calls are needed
|
|
22
|
+
source = [[1], 2, [[3, 4], 5], [[ArrayList.new]], [[[6]]], 7, 8, ArrayList.new]
|
|
23
|
+
|
|
24
|
+
puts flatten(source)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
def getInput:int
|
|
2
|
+
s = System.console.readLine()
|
|
3
|
+
Integer.parseInt(s)
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
number = int(Math.random() * 10 + 1)
|
|
8
|
+
|
|
9
|
+
puts "guess the number between 1 and 10"
|
|
10
|
+
guessed = false
|
|
11
|
+
while !guessed do
|
|
12
|
+
userNumber = getInput
|
|
13
|
+
if userNumber == number
|
|
14
|
+
guessed = true
|
|
15
|
+
puts "you guessed it"
|
|
16
|
+
elsif userNumber > number
|
|
17
|
+
puts "too high"
|
|
18
|
+
else
|
|
19
|
+
puts "too low"
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import java.text.NumberFormat
|
|
2
|
+
import java.text.ParsePosition
|
|
3
|
+
import java.util.Scanner
|
|
4
|
+
|
|
5
|
+
# this first example relies on catching an exception,
|
|
6
|
+
# which is bad style and poorly performing in Java
|
|
7
|
+
def is_numeric?(s:string)
|
|
8
|
+
begin
|
|
9
|
+
Double.parseDouble(s)
|
|
10
|
+
return true
|
|
11
|
+
rescue
|
|
12
|
+
return false
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
puts '123 is numeric' if is_numeric?('123')
|
|
17
|
+
puts '-123 is numeric' if is_numeric?('-123')
|
|
18
|
+
puts '123.1 is numeric' if is_numeric?('123.1')
|
|
19
|
+
|
|
20
|
+
puts 'nil is not numeric' unless is_numeric?(nil)
|
|
21
|
+
puts "'' is not numeric" unless is_numeric?('')
|
|
22
|
+
puts 'abc is not numeric' unless is_numeric?('abc')
|
|
23
|
+
puts '123- is not numeric' unless is_numeric?('123-')
|
|
24
|
+
puts '1.2.3 is not numeric' unless is_numeric?('1.2.3')
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# check every element of the string
|
|
28
|
+
def is_numeric2?(s: string)
|
|
29
|
+
if (s == nil || s.isEmpty())
|
|
30
|
+
return false
|
|
31
|
+
end
|
|
32
|
+
if (!s.startsWith('-'))
|
|
33
|
+
if s.contains('-')
|
|
34
|
+
return false
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
0.upto(s.length()-1) do |x|
|
|
39
|
+
c = s.charAt(x)
|
|
40
|
+
if ((x == 0) && (c == '-'.charAt(0)))
|
|
41
|
+
# negative number
|
|
42
|
+
elsif (c == '.'.charAt(0))
|
|
43
|
+
if (s.indexOf('.', x) > -1)
|
|
44
|
+
return false # more than one period
|
|
45
|
+
end
|
|
46
|
+
elsif (!Character.isDigit(c))
|
|
47
|
+
return false
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
true
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
puts '123 is numeric' if is_numeric2?('123')
|
|
55
|
+
puts '-123 is numeric' if is_numeric2?('-123')
|
|
56
|
+
puts '123.1 is numeric' if is_numeric2?('123.1')
|
|
57
|
+
|
|
58
|
+
puts 'nil is not numeric' unless is_numeric2?(nil)
|
|
59
|
+
puts "'' is not numeric" unless is_numeric2?('')
|
|
60
|
+
puts 'abc is not numeric' unless is_numeric2?('abc')
|
|
61
|
+
puts '123- is not numeric' unless is_numeric2?('123-')
|
|
62
|
+
puts '1.2.3 is not numeric' unless is_numeric2?('1.2.3')
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# use a regular expression
|
|
67
|
+
def is_numeric3?(s:string)
|
|
68
|
+
s == nil || s.matches("[-+]?\\d+(\\.\\d+)?")
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
puts '123 is numeric' if is_numeric3?('123')
|
|
72
|
+
puts '-123 is numeric' if is_numeric3?('-123')
|
|
73
|
+
puts '123.1 is numeric' if is_numeric3?('123.1')
|
|
74
|
+
|
|
75
|
+
puts 'nil is not numeric' unless is_numeric3?(nil)
|
|
76
|
+
puts "'' is not numeric" unless is_numeric3?('')
|
|
77
|
+
puts 'abc is not numeric' unless is_numeric3?('abc')
|
|
78
|
+
puts '123- is not numeric' unless is_numeric3?('123-')
|
|
79
|
+
puts '1.2.3 is not numeric' unless is_numeric3?('1.2.3')
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
# use the positional parser in the java.text.NumberFormat object
|
|
83
|
+
# (a more robust solution). If, after parsing, the parse position is at
|
|
84
|
+
# the end of the string, we can deduce that the entire string was a
|
|
85
|
+
# valid number.
|
|
86
|
+
def is_numeric4?(s:string)
|
|
87
|
+
return false if s == nil
|
|
88
|
+
formatter = NumberFormat.getInstance()
|
|
89
|
+
pos = ParsePosition.new(0)
|
|
90
|
+
formatter.parse(s, pos)
|
|
91
|
+
s.length() == pos.getIndex()
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
puts '123 is numeric' if is_numeric4?('123')
|
|
96
|
+
puts '-123 is numeric' if is_numeric4?('-123')
|
|
97
|
+
puts '123.1 is numeric' if is_numeric4?('123.1')
|
|
98
|
+
|
|
99
|
+
puts 'nil is not numeric' unless is_numeric4?(nil)
|
|
100
|
+
puts "'' is not numeric" unless is_numeric4?('')
|
|
101
|
+
puts 'abc is not numeric' unless is_numeric4?('abc')
|
|
102
|
+
puts '123- is not numeric' unless is_numeric4?('123-')
|
|
103
|
+
puts '1.2.3 is not numeric' unless is_numeric4?('1.2.3')
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
# use the java.util.Scanner object. Very useful if you have to
|
|
107
|
+
# scan multiple entries. Scanner also has similar methods for longs,
|
|
108
|
+
# shorts, bytes, doubles, floats, BigIntegers, and BigDecimals as well
|
|
109
|
+
# as methods for integral types where you may input a base/radix other than
|
|
110
|
+
# 10 (10 is the default, which can be changed using the useRadix method).
|
|
111
|
+
def is_numeric5?(s:string)
|
|
112
|
+
return false if s == nil
|
|
113
|
+
Scanner sc = Scanner.new(s)
|
|
114
|
+
sc.hasNextDouble()
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
puts '123 is numeric' if is_numeric5?('123')
|
|
118
|
+
puts '-123 is numeric' if is_numeric5?('-123')
|
|
119
|
+
puts '123.1 is numeric' if is_numeric5?('123.1')
|
|
120
|
+
|
|
121
|
+
puts 'nil is not numeric' unless is_numeric5?(nil)
|
|
122
|
+
puts "'' is not numeric" unless is_numeric5?('')
|
|
123
|
+
puts 'abc is not numeric' unless is_numeric5?('abc')
|
|
124
|
+
puts '123- is not numeric' unless is_numeric5?('123-')
|
|
125
|
+
puts '1.2.3 is not numeric' unless is_numeric5?('1.2.3')
|
|
126
|
+
|
|
127
|
+
|