bakkdoor-blocktalk 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/LICENSE +165 -0
  2. data/README.markdown +114 -0
  3. data/TODO +9 -0
  4. data/benchmark.bt +18 -0
  5. data/evaluator.rb +19 -0
  6. data/examples/chained_method_call.bt +15 -0
  7. data/examples/classes_modules.bt +68 -0
  8. data/examples/exceptions.bt +28 -0
  9. data/examples/fac.bt +23 -0
  10. data/examples/inline_ruby.bt +20 -0
  11. data/examples/linecounter.bt +8 -0
  12. data/examples/multiple_methodcall.bt +1 -0
  13. data/examples/portscan.bt +39 -0
  14. data/examples/require.bt +8 -0
  15. data/examples/ruby_methods.bt +20 -0
  16. data/examples/string_interpol.bt +10 -0
  17. data/examples/string_test.bt +13 -0
  18. data/examples/test.bt +45 -0
  19. data/examples/test2.bt +125 -0
  20. data/examples/test3.bt +9 -0
  21. data/grammar/blocktalk.rb +5030 -0
  22. data/grammar/blocktalk.tt +463 -0
  23. data/language-spec/blocktalk-example.bt +38 -0
  24. data/language-spec/blocktalk-lang-spec.bt +232 -0
  25. data/lib/blocktalk/array.bt +60 -0
  26. data/lib/blocktalk/string.bt +9 -0
  27. data/lib/blocktalk.bt +3 -0
  28. data/lib/core.rb +12 -0
  29. data/lib/kernel/array.rb +9 -0
  30. data/lib/kernel/class.rb +46 -0
  31. data/lib/kernel/codeblock.rb +57 -0
  32. data/lib/kernel/console.rb +40 -0
  33. data/lib/kernel/error.rb +11 -0
  34. data/lib/kernel/module.rb +18 -0
  35. data/lib/kernel/object.rb +66 -0
  36. data/lib/kernel/string.rb +5 -0
  37. data/lib/kernel/system.rb +5 -0
  38. data/parser/helpers/method_definitions.rb +31 -0
  39. data/parser/helpers/methodcalls.rb +56 -0
  40. data/parser/nodes/block_literal.rb +42 -0
  41. data/parser/nodes/catch.rb +22 -0
  42. data/parser/nodes/class_method_definition.rb +15 -0
  43. data/parser/nodes/comment.rb +7 -0
  44. data/parser/nodes/ensure.rb +7 -0
  45. data/parser/nodes/expression.rb +7 -0
  46. data/parser/nodes/identifier.rb +7 -0
  47. data/parser/nodes/integer_literal.rb +7 -0
  48. data/parser/nodes/message_receiver.rb +7 -0
  49. data/parser/nodes/message_with_params.rb +8 -0
  50. data/parser/nodes/message_without_params.rb +10 -0
  51. data/parser/nodes/method_definition.rb +31 -0
  52. data/parser/nodes/methodcall.rb +37 -0
  53. data/parser/nodes/multiple_methodcall.rb +28 -0
  54. data/parser/nodes/operator_message.rb +8 -0
  55. data/parser/nodes/require.rb +25 -0
  56. data/parser/nodes/return.rb +7 -0
  57. data/parser/nodes/root.rb +15 -0
  58. data/parser/nodes/string.rb +7 -0
  59. data/parser/nodes/subexpression.rb +7 -0
  60. data/parser/nodes/super_call.rb +12 -0
  61. data/parser/nodes/try.rb +7 -0
  62. data/parser/nodes/yield.rb +18 -0
  63. data/parser/nodes.rb +29 -0
  64. metadata +70 -3
data/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README.markdown ADDED
@@ -0,0 +1,114 @@
1
+ # ***Blocktalk*** #
2
+ ### **v0.1.2** ###
3
+
4
+ ## **Blocktalk** is a dynamic, object-oriented programming language somewhat in the tradition of Smalltalk and Ruby. ##
5
+
6
+ It has a syntax familiar to Smalltalk users, but also integrates some syntactic elements from Ruby,
7
+ mainly most literal syntax including literal syntax for blocks, hashes, arrays, symbols and regular expressions.
8
+ In contrast to Ruby, there are only very few predefined keywords in the language, everything else is achieved via
9
+ methodcalls on objects, similar to how it is done in Smalltalk.
10
+
11
+
12
+ ### Class definitions ###
13
+
14
+ For example, defining classes and modules in Blocktalk is also done via methodcalls to the Class and Module class
15
+ respectively:
16
+
17
+ Class >> :Foo do
18
+ def bar = do |baz|
19
+ Console puts: "In Foo#bar with baz = #{baz}"
20
+ end
21
+ end
22
+
23
+ Here, the ">>" method is called on the Class class (which also is an object - a class object), which takes the name
24
+ of the class as a Symbol and then a codeblock that gets evaluated in the context of the class. This can be done in Ruby
25
+ as well (in a similar way via `Class#new`, but in contrast to Ruby, this is the only way to do it.
26
+ In Smalltalk you would do it in a similar way: `Superclass subclass: Foo`.
27
+
28
+
29
+ ### Codeblocks ###
30
+
31
+ As in Ruby & Smalltalk, Blocktalk supports literal syntax for codeblocks (anonymous blocks of code / closures).
32
+ In contrast to Ruby, there's is no conceptual difference between codeblocks passed to methods as part of a method call
33
+ and storing them into variables - the syntax is the same. In Ruby you'd have to deal with converting blocks to Procs
34
+ and vice versa.
35
+ In constrast to Smalltalk, Blocktalk supports implicit passing of codeblocks to methods as it is done in Ruby. However,
36
+ since the syntax for an implicit codeblock passed to a method and creating them explicitly (to be stored in a variable or
37
+ or method argument, for example) is the same, you can also use the Smalltalk-ish approach of passing in codeblocks to
38
+ methodcalls explicitly as arguments.
39
+ This is also used in a few predefined methods in the standard library. An example is the if_true:if_false method, that
40
+ can take two explicit codeblocks for a if and then part, or just a block for the if part (either explicitly as an argument
41
+ or implicitly as a ruby-like method call with a passed in block):
42
+
43
+ (a < b) if_true: {
44
+ Console print: "a smaller than b!"
45
+ } if_false: {
46
+ Console print: "a greater than b!"
47
+ }
48
+
49
+ Since Blocktalk supports a very easy literal syntax for codeblocks, many special keywords aren't needed (as in Smalltalk).
50
+ The example above shows, how a typicall if-then conditional could be written.
51
+
52
+ Another example would be a while loop:
53
+
54
+ i = (Console gets: "Please enter a number!") to_i
55
+ {i < 10} while_true {
56
+ Console print: "a smaller than b!"
57
+ i = (Console gets: "Enter again!") to_i
58
+ }
59
+
60
+ In this case, while_true takes a ruby-like implicit block, noticeable by the absence of the colon after the methodname,
61
+ which indicates a method call with a passed in argument.
62
+
63
+
64
+ ### Exception Handling ###
65
+
66
+ Exception handling in Blocktalk is done similar to most programming languages, including Ruby:
67
+
68
+
69
+ i = Console gets: "Please enter a number!"
70
+ try {
71
+ Console print: "10 / i = #{(10 / (i to_i))}"
72
+
73
+ catch: ZeroDivisionError do |ex|
74
+ Console print: "got a exception: #{ex message}"
75
+ end
76
+
77
+ ensure {
78
+ Console print: "this will get done, no matter what value i has!"
79
+ }
80
+ }
81
+
82
+ This example will obviously fail if the we enter a zero. As in Ruby, the ensure-block gets run independent of an error
83
+ being raised or not (similar to e.g. `finally` in Java).
84
+
85
+
86
+ ### Other features ###
87
+
88
+ As Blocktalk is still in heavy development, new features may be added or old ones redesigned - I'm very open to discussion
89
+ if anyone has some cool ideas, just let me know.
90
+
91
+
92
+ ## Current status ##
93
+
94
+ I have most of the basic features done, Including Class & Module definitions (with class & instance methods etc), most
95
+ literal syntax (Integers, Floats, Symbols, Hashes, Arrays, Codeblocks, Regular Expressions ...) and a *working* interpreter
96
+ written in Ruby, that actually is somewhat of a compiler. As of now, the code gets parsed and translated into (quite ugly)
97
+ Ruby code, which then gets fed into Ruby and executed. I know this isn't the best way for now probably, but it works.
98
+ Something I'd like to add in the future is a bytecode compiler for Ruby 1.9.
99
+
100
+
101
+ ## Implementation ##
102
+
103
+ Blocktalk is implemented in Ruby. The goal is to be able to run any Ruby code available, making it easy to get something
104
+ real and working fast.
105
+ Another goal is to add some more unique features, that Ruby for example does not support. I'm very open for ideas on this.
106
+
107
+
108
+ ## Dependencies ##
109
+
110
+ - Treetop ([http://treetop.rubyforge.org](http://treetop.rubyforge.org)) for the parser.
111
+
112
+ - That's it for now :)
113
+
114
+ #### (C) 2009 Christopher Bertels / [http://www.adztec-independent.de](http://www.adztec-independent.de) ####
data/TODO ADDED
@@ -0,0 +1,9 @@
1
+ ##################
2
+ # TODO for v0.1: #
3
+ ##################
4
+
5
+ - add optimizations, when possible
6
+ - add more documentation & enhance standard library (as much as possible in Blocktalk itself)
7
+
8
+ ideas:
9
+ ------
data/benchmark.bt ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env blocktalk
2
+
3
+ # simple benchmarking script.
4
+ # runs a given blocktalk programm a specified amount of times (default 10).
5
+
6
+ amount = 10
7
+ args = ARGV join: " "
8
+
9
+ (args =~ /-n ([0-9]+)/) if {
10
+ amount = $1 to_i
11
+ }
12
+
13
+ script_file = ARGV at: 0
14
+
15
+ amount times do |i|
16
+ Kernel system: "blocktalk #{script_file} > /dev/null"
17
+ Console print: "."
18
+ end
data/evaluator.rb ADDED
@@ -0,0 +1,19 @@
1
+ class Evaluator
2
+ @load_path = File.dirname(__FILE__)
3
+ @expressions = ["require \"#{@load_path}/lib/core\""]
4
+
5
+ def self.add(code_str)
6
+ unless code_str.nil? or code_str.empty?
7
+ @expressions << code_str
8
+ end
9
+ end
10
+
11
+ def self.eval(argv = [])
12
+ # Kernel::eval @expressions.join(";")
13
+ system("/usr/bin/env ruby -e '#{@expressions.join(';')}' #{argv.join(' ')}")
14
+ end
15
+
16
+ def self.inspect
17
+ Kernel::puts @expressions.join("\n")
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ require: "lib/blocktalk/string"
2
+
3
+ str = "hello, world"
4
+ str2 = str upcase substitute: "," with: "::" downcase substitute: " " with: "--"
5
+ Console puts: str2
6
+
7
+ 10 < 5 if_true {
8
+ Console puts: "yoo"
9
+ } if_false {
10
+ Console puts: "noo"
11
+ }
12
+
13
+ (1 < 4) if_true {
14
+ Console puts: "1 < 4"
15
+ }
@@ -0,0 +1,68 @@
1
+ Module >> :ModuleA do
2
+ def method_a = do
3
+ Console puts: "in ModuleA#method_a!"
4
+ end
5
+ end
6
+
7
+
8
+ Module >> :ModuleB do
9
+ def method_b = do
10
+ Console puts: "in ModuleB#method_b!"
11
+ end
12
+
13
+ def method_c = do |param and: param2 plus: param3|
14
+ Console puts: "in ModuleB#method_c: #{param}, #{param2}, #{param3}"
15
+ end
16
+ end
17
+
18
+ Class >> :Place do
19
+ def self from_city = do |city_name|
20
+ # should do something useful here ...
21
+ Place new
22
+ end
23
+
24
+ def coordinates = do
25
+ # do some calculation here...
26
+ return (Kernel rand)
27
+ end
28
+ end
29
+
30
+ Class >> :Person {
31
+ self mixin: [ModuleA, ModuleB]
32
+ def initialize = do |name age: age city: city|
33
+ @name = name
34
+ @age = age
35
+ @city = city
36
+ end
37
+
38
+ def go_to = do |place with: vehicle|
39
+ ((place is_a?: Place) and: ((self distance_to: place) < 10.5)) if_true {
40
+ vehicle take: self to: place
41
+ }
42
+ end
43
+
44
+ def place = do
45
+ Place from_city: @city
46
+ end
47
+
48
+ def distance_to = do |place|
49
+ (place is_a?: Place) if_true: {
50
+ dist = ((self place) coordinates) - (place coordinates)
51
+ dist abs
52
+ } if_false: {
53
+ 0.0
54
+ }
55
+ end
56
+ }
57
+
58
+
59
+ chris = Person new: "Christopher Bertels" age: 22 city: "Osnabrück"
60
+ city = Place from_city: "Berlin"
61
+
62
+ Console puts: "Distance from chris to city:"
63
+ Console puts: (chris distance_to: city)
64
+
65
+ # Person mixed in ModuleA & ModuleB:
66
+ chris method_a
67
+ chris method_b
68
+ chris method_c: "hey" and: "ho" plus: "cool!"
@@ -0,0 +1,28 @@
1
+ try{
2
+ 0 / 0
3
+ catch: ZeroDivisionError do |ex|
4
+ Console puts: "in catching Error!"
5
+ Console puts: "something else ;)"
6
+ end
7
+
8
+ ensure {
9
+ Console puts: "error!"
10
+ }
11
+ }
12
+
13
+ # lets raise an exception ourselves
14
+
15
+ Class >> {:WeirdError => StandardError} do
16
+ def initialize = do |error_message|
17
+ super: error_message
18
+ end
19
+ end
20
+
21
+ try{
22
+ Console puts: "gonna raise an exception!"
23
+ Error raise: (WeirdError new: "catch me if you can!")
24
+
25
+ catch: WeirdError do |ex|
26
+ Console puts: "exception caught: #{ex.inspect}"
27
+ end
28
+ }
data/examples/fac.bt ADDED
@@ -0,0 +1,23 @@
1
+ Fixnum class_eval do
2
+ def fac_slow = do
3
+ (self == 1) if_true: {
4
+ 1
5
+ } if_false: {
6
+ (self * ((self - 1) fac_slow))
7
+ }
8
+ end
9
+
10
+ def fac_fast = do
11
+ val = self
12
+ acc = val
13
+ {val > 1} while_true {
14
+ val = val - 1
15
+ acc = acc * val
16
+ }
17
+ return acc
18
+ end
19
+ end
20
+
21
+ Console puts: (289 fac_slow)
22
+ Console puts
23
+ Console puts: (289 fac_fast)
@@ -0,0 +1,20 @@
1
+ # inline ruby test
2
+ Class >> :Foo do
3
+ %ruby{
4
+ # some inline ruby code ;)
5
+ puts "this gets run, when this classdefinition gets executed!"
6
+ puts "we can put any kind of ruby code here :)"
7
+ def ruby_method(arg)
8
+ puts "in Foo#ruby_method: #{arg}"
9
+ end
10
+ }%
11
+
12
+ def blocktalk_method = do
13
+ Console puts: "in Foo#blocktalk_method!"
14
+ Console puts: "calling Foo#ruby_method:"
15
+ self ruby_method: "chris"
16
+ end
17
+ end
18
+
19
+ f = Foo new
20
+ f blocktalk_method
@@ -0,0 +1,8 @@
1
+ ((ARGV size) > 0) if_true: {
2
+ filename = ARGV at: 0
3
+ File open: filename do |f|
4
+ Console puts: "#{filename} has #{f.readlines.size} lines!"
5
+ end
6
+ } if_false: {
7
+ Console puts: "Please specify a file to count lines on!"
8
+ }
@@ -0,0 +1 @@
1
+ Console puts: :cool; print: "hello\n"
@@ -0,0 +1,39 @@
1
+ System require: "socket"
2
+
3
+ host = "localhost"
4
+
5
+ (ARGV at: 0) if {
6
+ host = (ARGV at: 0)
7
+ }
8
+
9
+ max_port = 1024
10
+
11
+ ((ARGV join: " ") =~ /\-n ([0-9]+)/) if {
12
+ max_port = $1 to_i
13
+ }
14
+
15
+ open_ports = []
16
+
17
+ 1 upto: max_port do |port|
18
+ Thread new {
19
+ try{
20
+ t = TCPSocket new: host port: port
21
+ open_ports << port
22
+
23
+ s = Socket getnameinfo: ["AF_INET", port, host]
24
+ Console puts: ("\nPort #{port} (#{s[1]})" + " is open.")
25
+
26
+ catch{
27
+ Console print: "."
28
+ }
29
+
30
+ ensure{
31
+ t if { t close }
32
+ }
33
+ }
34
+ }
35
+ end
36
+
37
+ Console puts
38
+ Console puts: "Open ports on #{host} are:"
39
+ Console puts: (open_ports join: ", ")
@@ -0,0 +1,8 @@
1
+ # requiring Ruby code & libraries works like this:
2
+ System require: "socket"
3
+
4
+ # requiring Blocktalk code like this:
5
+ require: "examples/exceptions.bt"
6
+
7
+ Console puts: "back in require.bt"
8
+
@@ -0,0 +1,20 @@
1
+ # calling some ruby methods...
2
+
3
+ File open: "examples/test3.bt" mode: "r" do |f|
4
+ (f readlines) each do |l|
5
+ Console puts: l
6
+ end
7
+ end
8
+
9
+ # and again, this time cached!
10
+ File open: "examples/test3.bt" mode: "r" do |f|
11
+ (f readlines) each do |l|
12
+ Console puts: l
13
+ end
14
+ end
15
+
16
+
17
+ str = "hello, world, how are you?"
18
+ str2 = str gsub: "," with: "!"
19
+ Console puts: str;
20
+ puts: str2
@@ -0,0 +1,10 @@
1
+ greeted = "world!"
2
+ Console puts: "Hello, #{greeted}"
3
+
4
+ x = 0
5
+ {(x to_i) <= 0} while_true {
6
+ x = Console gets: "Please enter a positive number (> 0):"
7
+ }
8
+
9
+ Console puts: "entered number is: #{x}"
10
+ Console puts: "number (#{x to_i}) < 10: #{(x to_i) < 10}"
@@ -0,0 +1,13 @@
1
+ # Extend String class with ends_with? method
2
+ # that checks if a string ends with a given string.
3
+ String extend {
4
+ def ends_with? = do |end_string|
5
+ start_index = -1 * (end_string length)
6
+ end_index = end_string length
7
+ tail = self range_from: start_index to: end_index
8
+ tail == end_string
9
+ end
10
+ }
11
+
12
+ Console puts: ("hello, world!" ends_with?: "world!") # output: true
13
+ Console puts: ("hello, world!" ends_with?: "world") # output: false
data/examples/test.bt ADDED
@@ -0,0 +1,45 @@
1
+ # this is just for demonstration
2
+ # there probably won't be the need to require the console module from the
3
+ # standard library every time you want to print something to the screen ;)
4
+ System require: "console"
5
+
6
+ # we support ruby-style blocks with do ... end and curly braces { ... }
7
+ File open: "test.txt" mode: "w" do |f|
8
+ f puts: "what's up, dog?!"
9
+ f puts: "crazy shit, yo!"
10
+ f puts: "hahaha!!"
11
+ end
12
+
13
+ 10 to: 0 do |i|
14
+ Console puts: i
15
+ end
16
+
17
+ i = 0
18
+ (i < 10) while_true: do |i|
19
+ Console puts: i
20
+ i incr
21
+ end
22
+
23
+ numbers = [1,2,3,4,5] select: {|i| i < 3}
24
+ numbers each: do |i|
25
+ puts i
26
+ end
27
+
28
+ (1 .. 100) each: {|i| Console puts: i}
29
+
30
+ squares = (1 .. 100) collect: {|i| i * i}
31
+
32
+
33
+ # define a square method
34
+ square = { |x|
35
+ x * x
36
+ }
37
+
38
+ # or like this:
39
+ abs = { |num|
40
+ (num > 0) if_true {
41
+ return num
42
+ }
43
+
44
+ num * -1
45
+ }