mirah 0.0.4-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 +15 -0
- data/README.txt +51 -0
- data/Rakefile +86 -0
- data/bin/duby +10 -0
- data/bin/dubyc +10 -0
- data/bin/dubyp +10 -0
- data/bin/jrubyp +36 -0
- data/bin/mirah +9 -0
- data/bin/mirah.cmd +1 -0
- data/bin/mirahc +9 -0
- data/bin/mirahc.cmd +1 -0
- data/bin/mirahp +9 -0
- data/bin/mirahp.cmd +1 -0
- data/examples/ant/example-build.xml +7 -0
- data/examples/appengine/Rakefile +19 -0
- data/examples/appengine/Readme +29 -0
- data/examples/appengine/src/org/mirah/MirahApp.mirah +57 -0
- data/examples/appengine/src/org/mirah/list.dhtml +15 -0
- data/examples/appengine/war/WEB-INF/lib/dubydatastore.jar +0 -0
- data/examples/bintrees.mirah +66 -0
- data/examples/construction.mirah +8 -0
- data/examples/dynamic.mirah +17 -0
- data/examples/edb.mirah +3 -0
- data/examples/fib.mirah +16 -0
- data/examples/fields.mirah +22 -0
- data/examples/fractal.mirah +55 -0
- data/examples/java_thing.mirah +13 -0
- data/examples/plugins/appengine/Rakefile +55 -0
- data/examples/plugins/appengine/lib/com/google/appengine/ext/duby/db/datastore.rb +375 -0
- data/examples/plugins/appengine/src/com/google/appengine/ext/duby/db/Model.duby +336 -0
- data/examples/plugins/appengine/test/com/google/appengine/ext/duby/db/ModelTest.duby +113 -0
- data/examples/simple_class.mirah +12 -0
- data/examples/sort_closure.mirah +7 -0
- data/examples/swing.mirah +20 -0
- data/examples/tak.mirah +15 -0
- data/examples/test.edb +9 -0
- data/examples/wiki/Rakefile +18 -0
- data/examples/wiki/src/org/mirah/wiki/MirahWiki.duby +324 -0
- data/examples/wiki/src/org/mirah/wiki/edit.eduby.html +42 -0
- data/examples/wiki/src/org/mirah/wiki/error.eduby.html +2 -0
- data/examples/wiki/src/org/mirah/wiki/layout.eduby.html +69 -0
- data/examples/wiki/src/org/mirah/wiki/parser.eduby.html +7 -0
- data/examples/wiki/src/org/mirah/wiki/view.eduby.html +15 -0
- 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 +21 -0
- 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 +61 -0
- data/examples/wiki/war/public/robots.txt +0 -0
- data/examples/wiki/war/public/stylesheets/main.css +156 -0
- data/examples/wiki/war/public/stylesheets/prettify.css +1 -0
- data/examples/wiki/war/public/stylesheets/sh_style.css +66 -0
- data/examples/wiki/war/public/stylesheets/source.css +21 -0
- 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 +421 -0
- data/examples/wiki/war/public/wmd/wmd-base.js +1799 -0
- data/examples/wiki/war/public/wmd/wmd-plus.js +311 -0
- data/examples/wiki/war/public/wmd/wmd.js +73 -0
- data/javalib/JRubyParser.jar +0 -0
- data/javalib/dynalang-invoke-0.1.jar +0 -0
- data/javalib/mirah-bootstrap.jar +0 -0
- data/javalib/mirah-parser.jar +0 -0
- data/lib/duby.rb +2 -0
- data/lib/mirah.rb +338 -0
- data/lib/mirah/appengine_tasks.rb +146 -0
- data/lib/mirah/ast.rb +615 -0
- data/lib/mirah/ast/call.rb +307 -0
- data/lib/mirah/ast/class.rb +311 -0
- data/lib/mirah/ast/flow.rb +364 -0
- data/lib/mirah/ast/intrinsics.rb +470 -0
- data/lib/mirah/ast/literal.rb +154 -0
- data/lib/mirah/ast/local.rb +89 -0
- data/lib/mirah/ast/method.rb +360 -0
- data/lib/mirah/ast/scope.rb +208 -0
- data/lib/mirah/ast/structure.rb +226 -0
- data/lib/mirah/ast/type.rb +130 -0
- data/lib/mirah/compiler.rb +341 -0
- data/lib/mirah/env.rb +33 -0
- data/lib/mirah/jvm/base.rb +258 -0
- data/lib/mirah/jvm/compiler.rb +885 -0
- data/lib/mirah/jvm/method_lookup.rb +203 -0
- data/lib/mirah/jvm/source_compiler.rb +737 -0
- data/lib/mirah/jvm/source_generator/builder.rb +444 -0
- data/lib/mirah/jvm/source_generator/loops.rb +110 -0
- data/lib/mirah/jvm/source_generator/precompile.rb +188 -0
- data/lib/mirah/jvm/source_generator/typer.rb +11 -0
- data/lib/mirah/jvm/typer.rb +151 -0
- data/lib/mirah/jvm/types.rb +416 -0
- data/lib/mirah/jvm/types/basic_types.rb +33 -0
- data/lib/mirah/jvm/types/boolean.rb +17 -0
- data/lib/mirah/jvm/types/enumerable.rb +65 -0
- data/lib/mirah/jvm/types/extensions.rb +86 -0
- data/lib/mirah/jvm/types/factory.rb +186 -0
- data/lib/mirah/jvm/types/floats.rb +86 -0
- data/lib/mirah/jvm/types/integers.rb +171 -0
- data/lib/mirah/jvm/types/intrinsics.rb +376 -0
- data/lib/mirah/jvm/types/literals.rb +74 -0
- data/lib/mirah/jvm/types/methods.rb +614 -0
- data/lib/mirah/jvm/types/number.rb +143 -0
- data/lib/mirah/nbcompiler.rb +29 -0
- data/lib/mirah/plugin/edb.rb +29 -0
- data/lib/mirah/plugin/gwt.rb +173 -0
- data/lib/mirah/plugin/java.rb +55 -0
- data/lib/mirah/transform.rb +266 -0
- data/lib/mirah/transform2.rb +728 -0
- data/lib/mirah/typer.rb +407 -0
- data/lib/mirah_task.rb +107 -0
- data/test/test_ast.rb +359 -0
- data/test/test_compilation.rb +112 -0
- data/test/test_env.rb +42 -0
- data/test/test_gwt.rb +58 -0
- data/test/test_java_typer.rb +183 -0
- data/test/test_javac_compiler.rb +63 -0
- data/test/test_jvm_compiler.rb +2607 -0
- data/test/test_typer.rb +221 -0
- metadata +235 -0
data/History.txt
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
=== 0.0.3 / 2010-06-09
|
|
2
|
+
|
|
3
|
+
* Even more language features!
|
|
4
|
+
* invokedynamic support (sort of)
|
|
5
|
+
* Using mirror types to avoid class initialization
|
|
6
|
+
* Other stuff
|
|
7
|
+
|
|
8
|
+
=== 0.0.2 / 2010-02-15
|
|
9
|
+
|
|
10
|
+
* More language features added
|
|
11
|
+
|
|
12
|
+
=== 0.0.1 / 2009-10-27
|
|
13
|
+
|
|
14
|
+
* First gem release
|
|
15
|
+
* Most things necessary for simple apps, scripts, servlets are working
|
data/README.txt
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
= mirah
|
|
2
|
+
|
|
3
|
+
* http://groups.google.com/group/mirah
|
|
4
|
+
* http://kenai.com/projects/duby
|
|
5
|
+
|
|
6
|
+
== DESCRIPTION:
|
|
7
|
+
|
|
8
|
+
Mirah is a customizable programming language featuring static types,
|
|
9
|
+
local type inference and a heavily Ruby-inspired syntax. Mirah
|
|
10
|
+
currently includes a typer/compiler backend for the JVM which can
|
|
11
|
+
output either JVM bytecode or Java source files.
|
|
12
|
+
|
|
13
|
+
== FEATURES/PROBLEMS:
|
|
14
|
+
|
|
15
|
+
* Ruby syntax
|
|
16
|
+
* Compiles to .class or .java
|
|
17
|
+
* Fast as Java
|
|
18
|
+
|
|
19
|
+
== SYNOPSIS:
|
|
20
|
+
|
|
21
|
+
mirah <script.mirah>
|
|
22
|
+
mirah -e "inline script"
|
|
23
|
+
mirahc <script.mirah>
|
|
24
|
+
mirahc -e "inline script" # produces DashE.class
|
|
25
|
+
mirahc -java <script.mirah>
|
|
26
|
+
mirahc -java -e "inline script" # produces DashE.java
|
|
27
|
+
|
|
28
|
+
== REQUIREMENTS:
|
|
29
|
+
|
|
30
|
+
* JRuby 1.5.0 or higher.
|
|
31
|
+
* BiteScript 0.0.5 or higher
|
|
32
|
+
|
|
33
|
+
== INSTALL:
|
|
34
|
+
|
|
35
|
+
If your "gem" command is the one from JRuby:
|
|
36
|
+
|
|
37
|
+
* gem install mirah
|
|
38
|
+
|
|
39
|
+
Otherwise:
|
|
40
|
+
|
|
41
|
+
* jruby -S gem install mirah
|
|
42
|
+
|
|
43
|
+
Only JRuby is supported at this time.
|
|
44
|
+
|
|
45
|
+
== For Java tools:
|
|
46
|
+
|
|
47
|
+
To build the Mirah jars from source you should have a checkout of both jruby and
|
|
48
|
+
bitescript in Mirah's parent directory. Run "ant jar-complete" in jruby, then in
|
|
49
|
+
the mirah directory "../jruby/bin/jruby -S rake jar" to build the Mirah jar. Use
|
|
50
|
+
"jar:complete" instead to produce a free-standing jar file with JRuby and the
|
|
51
|
+
JRubyParser libraries included.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
require 'rake'
|
|
2
|
+
require 'rake/testtask'
|
|
3
|
+
require 'java'
|
|
4
|
+
$: << './lib'
|
|
5
|
+
require 'mirah'
|
|
6
|
+
require 'jruby/compiler'
|
|
7
|
+
require 'ant'
|
|
8
|
+
|
|
9
|
+
task :default => :test
|
|
10
|
+
|
|
11
|
+
Rake::TestTask.new :test do |t|
|
|
12
|
+
t.libs << "lib"
|
|
13
|
+
# This is hacky, I know
|
|
14
|
+
t.libs.concat Dir["../bitescript*/lib"]
|
|
15
|
+
t.test_files = FileList["test/**/*.rb"]
|
|
16
|
+
java.lang.System.set_property("jruby.duby.enabled", "true")
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
task :init do
|
|
20
|
+
mkdir_p 'dist'
|
|
21
|
+
mkdir_p 'build'
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
task :clean do
|
|
25
|
+
ant.delete :quiet => true, :dir => 'build'
|
|
26
|
+
ant.delete :quiet => true, :dir => 'dist'
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
task :compile => :init do
|
|
30
|
+
# build the Ruby sources
|
|
31
|
+
puts "Compiling Ruby sources"
|
|
32
|
+
JRuby::Compiler.compile_argv([
|
|
33
|
+
'-t', 'build',
|
|
34
|
+
'--javac',
|
|
35
|
+
'src/org/mirah/mirah_command.rb'
|
|
36
|
+
])
|
|
37
|
+
|
|
38
|
+
# build the Mirah sources
|
|
39
|
+
puts "Compiling Mirah sources"
|
|
40
|
+
Dir.chdir 'src' do
|
|
41
|
+
classpath = Duby::Env.encode_paths([
|
|
42
|
+
'javalib/jruby-complete.jar',
|
|
43
|
+
'javalib/JRubyParser.jar',
|
|
44
|
+
'build',
|
|
45
|
+
'/usr/share/ant/lib/ant.jar'
|
|
46
|
+
])
|
|
47
|
+
Duby.compile(
|
|
48
|
+
'-c', classpath,
|
|
49
|
+
'-d', '../build',
|
|
50
|
+
'org/mirah',
|
|
51
|
+
'duby/lang',
|
|
52
|
+
'mirah'
|
|
53
|
+
)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
task :jar => :compile do
|
|
58
|
+
ant.jar :jarfile => 'dist/mirah.jar' do
|
|
59
|
+
fileset :dir => 'lib'
|
|
60
|
+
fileset :dir => 'build'
|
|
61
|
+
fileset :dir => '.', :includes => 'bin/*'
|
|
62
|
+
fileset :dir => '../bitescript/lib'
|
|
63
|
+
manifest do
|
|
64
|
+
attribute :name => 'Main-Class', :value => 'org.mirah.MirahCommand'
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
namespace :jar do
|
|
70
|
+
task :complete => :jar do
|
|
71
|
+
ant.jar :jarfile => 'dist/mirah-complete.jar' do
|
|
72
|
+
zipfileset :src => 'dist/mirah.jar'
|
|
73
|
+
zipfileset :src => 'javalib/jruby-complete.jar'
|
|
74
|
+
zipfileset :src => 'javalib/mirah-parser.jar'
|
|
75
|
+
manifest do
|
|
76
|
+
attribute :name => 'Main-Class', :value => 'org.mirah.MirahCommand'
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
task :bootstrap => :compile do
|
|
82
|
+
ant.jar :jarfile => 'javalib/mirah-bootstrap.jar' do
|
|
83
|
+
fileset :dir => 'build'
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
data/bin/duby
ADDED
data/bin/dubyc
ADDED
data/bin/dubyp
ADDED
data/bin/jrubyp
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#!/usr/bin/env jruby
|
|
2
|
+
$: << File.dirname(File.dirname(__FILE__)) + '/lib'
|
|
3
|
+
require 'mirah'
|
|
4
|
+
|
|
5
|
+
module Java::OrgJrubyparserAst
|
|
6
|
+
class Node
|
|
7
|
+
def pp(indent = 0)
|
|
8
|
+
return if self.invisible?
|
|
9
|
+
name = ""
|
|
10
|
+
name = ":#{self.name}" if respond_to? :getName
|
|
11
|
+
name += " #{value.inspect}" if respond_to? :getValue
|
|
12
|
+
name += " (#{depth})" if respond_to? :getDepth
|
|
13
|
+
print "#{' '*indent}(#{node_name}#{name}"
|
|
14
|
+
if child_nodes.size == 0
|
|
15
|
+
puts ')'
|
|
16
|
+
else
|
|
17
|
+
puts
|
|
18
|
+
indent += 2
|
|
19
|
+
child_nodes.each {|n| n.pp(indent)}
|
|
20
|
+
puts "#{' '*indent})"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
filename = ARGV.shift
|
|
27
|
+
case filename
|
|
28
|
+
when '-e'
|
|
29
|
+
contents = ARGV.join(' ')
|
|
30
|
+
when '-', nil
|
|
31
|
+
contents = $stdin.read
|
|
32
|
+
else
|
|
33
|
+
contents = IO.read(filename)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
puts Duby::AST.parse_ruby(contents, filename).pp
|
data/bin/mirah
ADDED
data/bin/mirah.cmd
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
@jruby "%~dpn0" %*
|
data/bin/mirahc
ADDED
data/bin/mirahc.cmd
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
@jruby "%~dpn0" %*
|
data/bin/mirahp
ADDED
data/bin/mirahp.cmd
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
@jruby "%~dpn0" %*
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Hack to use the latest version instead of the gems when developing Mirah
|
|
2
|
+
if File.exist?('../../lib/mirah.rb')
|
|
3
|
+
$: << File.expand_path('../../lib')
|
|
4
|
+
end
|
|
5
|
+
if File.exist?('../../../bitescript/lib/bitescript.rb')
|
|
6
|
+
$: << File.expand_path('../../../bitescript/lib/')
|
|
7
|
+
end
|
|
8
|
+
require 'mirah/appengine_tasks'
|
|
9
|
+
|
|
10
|
+
appengine_app :app
|
|
11
|
+
|
|
12
|
+
MIRAH_APP = "#{Duby.dest_path}/org/mirah/MirahApp.class"
|
|
13
|
+
LIST = "#{Duby.source_path}/org/mirah/list.dhtml"
|
|
14
|
+
|
|
15
|
+
# We want to recompile the app if the template gets modified
|
|
16
|
+
Rake::Task[MIRAH_APP].enhance([LIST])
|
|
17
|
+
|
|
18
|
+
task :app => MIRAH_APP
|
|
19
|
+
task :default => :server
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Run rake to compile the application and start the development server:
|
|
2
|
+
|
|
3
|
+
$ jruby -I `pwd`/../../lib -S rake
|
|
4
|
+
|
|
5
|
+
Requirements:
|
|
6
|
+
- jruby -S gem install bitescript
|
|
7
|
+
- jruby -S gem install appengine-sdk -v=1.3.5.pre
|
|
8
|
+
|
|
9
|
+
Datastore API:
|
|
10
|
+
Model.get(key)
|
|
11
|
+
Model.delete(key)
|
|
12
|
+
Model.all -> Query
|
|
13
|
+
property foo, String # or any supported datastore type
|
|
14
|
+
# For numbers use Long or Double
|
|
15
|
+
|
|
16
|
+
instance.save
|
|
17
|
+
instance.key
|
|
18
|
+
|
|
19
|
+
instance.foo
|
|
20
|
+
instance.foo=
|
|
21
|
+
|
|
22
|
+
Query.count
|
|
23
|
+
Query.run -> Model[]
|
|
24
|
+
Query.first -> instance || nil
|
|
25
|
+
Query.sort(property_name)
|
|
26
|
+
Query.sort(property_name, true) # descending
|
|
27
|
+
Query.foo('foo').bar(3) # Only return instances where foo = 'foo' and bar = 3
|
|
28
|
+
Query.offset(10)
|
|
29
|
+
Query.limit(5)
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import java.util.HashMap
|
|
2
|
+
import java.util.regex.Pattern
|
|
3
|
+
|
|
4
|
+
import javax.servlet.http.HttpServlet
|
|
5
|
+
|
|
6
|
+
import com.google.appengine.api.datastore.Text
|
|
7
|
+
import com.google.appengine.ext.duby.db.Model
|
|
8
|
+
|
|
9
|
+
class Post < Model
|
|
10
|
+
property 'title', String
|
|
11
|
+
property 'body', Text
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class MirahApp < HttpServlet
|
|
15
|
+
def_edb(list, 'org/mirah/list.dhtml')
|
|
16
|
+
|
|
17
|
+
def doGet(request, response)
|
|
18
|
+
@posts = Post.all.run
|
|
19
|
+
response.getWriter.write(list)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def doPost(request, response)
|
|
23
|
+
post = Post.new
|
|
24
|
+
post.title = request.getParameter('title')
|
|
25
|
+
post.body = request.getParameter('body')
|
|
26
|
+
post.save
|
|
27
|
+
doGet(request, response)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def initialize
|
|
32
|
+
@escape_pattern = Pattern.compile("[<>&'\"]")
|
|
33
|
+
@escaped = HashMap.new
|
|
34
|
+
@escaped.put("<", "<")
|
|
35
|
+
@escaped.put(">", ">")
|
|
36
|
+
@escaped.put("&", "&")
|
|
37
|
+
@escaped.put("\"", """)
|
|
38
|
+
@escaped.put("'", "'")
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def h(text:String)
|
|
42
|
+
return "" unless text
|
|
43
|
+
matcher = @escape_pattern.matcher(text)
|
|
44
|
+
buffer = StringBuffer.new
|
|
45
|
+
while matcher.find
|
|
46
|
+
replacement = String(@escaped.get(matcher.group))
|
|
47
|
+
matcher.appendReplacement(buffer, replacement)
|
|
48
|
+
end
|
|
49
|
+
matcher.appendTail(buffer)
|
|
50
|
+
return buffer.toString
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def h(o:Object)
|
|
54
|
+
return "" unless o
|
|
55
|
+
h(o.toString)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<title>Posts</title>
|
|
2
|
+
<body>
|
|
3
|
+
<h1>All the Posts:</h1>
|
|
4
|
+
<% for post in @posts %>
|
|
5
|
+
<h2><%=h post.title %></h2>
|
|
6
|
+
<p><%=h post.body %></p>
|
|
7
|
+
<% end %>
|
|
8
|
+
<hr>
|
|
9
|
+
<h1>New Post:</h1>
|
|
10
|
+
<form method=post>
|
|
11
|
+
Title: <input type=text name=title><br>
|
|
12
|
+
Content: <textarea name=content></textarea><br>
|
|
13
|
+
<input type=submit>
|
|
14
|
+
</form>
|
|
15
|
+
</body>
|
|
Binary file
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
class BinaryTrees
|
|
2
|
+
def self.main(args:String[])
|
|
3
|
+
n = 0
|
|
4
|
+
n = Integer.parseInt(args[0]) if args.length > 0
|
|
5
|
+
|
|
6
|
+
maxDepth = (6 > n) ? 6 : n
|
|
7
|
+
stretchDepth = maxDepth + 1
|
|
8
|
+
|
|
9
|
+
check = TreeNode.bottomUpTree(0, stretchDepth).itemCheck
|
|
10
|
+
puts "stretch tree of depth #{stretchDepth}\t check: #{check}"
|
|
11
|
+
|
|
12
|
+
longLivedTree = TreeNode.bottomUpTree 0, maxDepth
|
|
13
|
+
|
|
14
|
+
depth = 4
|
|
15
|
+
while depth <= maxDepth
|
|
16
|
+
iterations = 1 << (maxDepth - depth + 4)
|
|
17
|
+
check = 0
|
|
18
|
+
|
|
19
|
+
i = 1
|
|
20
|
+
while i <= iterations
|
|
21
|
+
check += TreeNode.bottomUpTree(i, depth).itemCheck
|
|
22
|
+
check += TreeNode.bottomUpTree(-i,depth).itemCheck
|
|
23
|
+
i += 1
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
puts "#{iterations * 2}\t trees of depth #{depth}\t check: #{check}"
|
|
27
|
+
depth += 2
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
puts "long lived tree of depth #{maxDepth}\t check: #{longLivedTree.itemCheck}"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
class TreeNode
|
|
35
|
+
def initialize(left:TreeNode, right:TreeNode, item:int)
|
|
36
|
+
@item = item
|
|
37
|
+
@left = left
|
|
38
|
+
@right = right
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def initialize(item:int)
|
|
42
|
+
@left = TreeNode(nil)
|
|
43
|
+
@right = TreeNode(nil)
|
|
44
|
+
@item = item
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.bottomUpTree(item:int, depth:int)
|
|
48
|
+
if depth > 0
|
|
49
|
+
TreeNode.new(
|
|
50
|
+
TreeNode.bottomUpTree(2*item-1, depth-1),
|
|
51
|
+
TreeNode.bottomUpTree(2*item, depth-1),
|
|
52
|
+
item)
|
|
53
|
+
else
|
|
54
|
+
TreeNode.new(item)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def itemCheck
|
|
59
|
+
# if necessary deallocate here
|
|
60
|
+
if @left == nil
|
|
61
|
+
@item
|
|
62
|
+
else
|
|
63
|
+
@item + @left.itemCheck - @right.itemCheck
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|