naksh 0.2.0
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/AUTHORS +1 -0
- data/COPYING +674 -0
- data/COPYING-DOCS +15 -0
- data/README.md +45 -0
- data/Rakefile +129 -0
- data/TODO +19 -0
- data/bin/naksh +2 -0
- data/data/default.mo +0 -0
- data/data/default.po +16 -0
- data/data/default2.mo +0 -0
- data/data/default2.po +27 -0
- data/data/starter.pot +29 -0
- data/docs/bash_flow.dia +0 -0
- data/docs/gtk_manual.txt +13 -0
- data/docs/master_plan.dia +0 -0
- data/docs/master_plan.png +0 -0
- data/docs/master_plan.svg +74 -0
- data/docs/master_plan.svgz +0 -0
- data/docs/userguide/en/about.xml +42 -0
- data/docs/userguide/en/gnu-fdl-1.2.xml +543 -0
- data/docs/userguide/en/naksh.xml +881 -0
- data/docs/userguide/en/naksh_start_window.png +0 -0
- data/docs/userguide/en/old_naksh.xml +180 -0
- data/docs/userguide/en/userguide-en.omf +37 -0
- data/lib/naksh.rb +165 -0
- data/lib/naksh/buffer.rb +153 -0
- data/lib/naksh/command_handling.rb +117 -0
- data/lib/naksh/commands/bash/README +2 -0
- data/lib/naksh/commands/bash/echo.nkc.rb +50 -0
- data/lib/naksh/commands/bsd.rb +25 -0
- data/lib/naksh/commands/dos.rb +25 -0
- data/lib/naksh/commands/fuse.rb +25 -0
- data/lib/naksh/commands/gnu.rb +30 -0
- data/lib/naksh/commands/gnu/README +2 -0
- data/lib/naksh/commands/gnu/cat.nkc.rb +50 -0
- data/lib/naksh/commands/gnu/cd.nkc.rb +11 -0
- data/lib/naksh/commands/gnu/echo.documentation.json +57 -0
- data/lib/naksh/commands/gnu/echo.documentation.rb +60 -0
- data/lib/naksh/commands/gnu/echo.nkc.rb +52 -0
- data/lib/naksh/commands/gnu/ls.nkc.rb +11 -0
- data/lib/naksh/commands/gnu/mv.nkc.rb +15 -0
- data/lib/naksh/commands/gnu/pwd.nkc.rb +11 -0
- data/lib/naksh/commands/naksh/addpath.nkc.rb +15 -0
- data/lib/naksh/commands/naksh/broken.nkc.rb +10 -0
- data/lib/naksh/commands/naksh/empty.nkc.rb +4 -0
- data/lib/naksh/commands/naksh/exit.nkc.rb +10 -0
- data/lib/naksh/commands/naksh/help.nkc.rb +21 -0
- data/lib/naksh/commands/naksh/man.nkc.rb +8 -0
- data/lib/naksh/commands/ruby.rb +31 -0
- data/lib/naksh/commands/unix.rb +131 -0
- data/lib/naksh/configuration/gconf.rb +51 -0
- data/lib/naksh/configuration/hash.rb +38 -0
- data/lib/naksh/defaults.rb +40 -0
- data/lib/naksh/history.rb +85 -0
- data/lib/naksh/interfaces.rb +59 -0
- data/lib/naksh/interfaces/fox.rb +49 -0
- data/lib/naksh/interfaces/gtk.rb +103 -0
- data/lib/naksh/interfaces/gtk/abtwin.rb +53 -0
- data/lib/naksh/interfaces/gtk/gnomeapp.rb +31 -0
- data/lib/naksh/interfaces/gtk/helpwin.rb +70 -0
- data/lib/naksh/interfaces/gtk/io.rb +94 -0
- data/lib/naksh/interfaces/gtk/mainwin.rb +56 -0
- data/lib/naksh/interfaces/gtk/mainwin/console.rb +121 -0
- data/lib/naksh/interfaces/gtk/mainwin/main_menu.rb +143 -0
- data/lib/naksh/interfaces/gtk/optwin.rb +78 -0
- data/lib/naksh/interfaces/tab.rb +58 -0
- data/lib/naksh/interfaces/wxw.rb +75 -0
- data/lib/naksh/interfaces/wxw/abtwin.rb +38 -0
- data/lib/naksh/interfaces/wxw/helpwin.rb +40 -0
- data/lib/naksh/interfaces/wxw/io.rb +61 -0
- data/lib/naksh/interfaces/wxw/mainwin.rb +42 -0
- data/lib/naksh/interfaces/wxw/mainwin/main_menu.rb +126 -0
- data/lib/naksh/interfaces/wxw/mainwin/sessions.rb +49 -0
- data/lib/naksh/interfaces/wxw/optwin.rb +37 -0
- data/lib/naksh/path_entry.rb +101 -0
- data/lib/naksh/regexp.rb +31 -0
- data/lib/naksh/session.rb +72 -0
- data/lib/naksh/stdlibext.rb +48 -0
- data/lib/naksh/syntax.rb +57 -0
- data/lib/naksh/syntax/bash.rb +219 -0
- data/lib/naksh/syntax/bash/antlr/bottomup.g +24 -0
- data/lib/naksh/syntax/bash/antlr/topdown.g +52 -0
- data/lib/naksh/syntax/bash/dhaka/evaluator.rb +353 -0
- data/lib/naksh/syntax/bash/dhaka/grammar.rb +71 -0
- data/lib/naksh/syntax/bash/dhaka/lexer.rb +52 -0
- data/lib/naksh/syntax/bash/treetop/bash.treetop +17 -0
- data/lib/old/abbr_call.rb +39 -0
- data/lib/old/autocomplete.rb +29 -0
- data/lib/old/command.rb +106 -0
- data/lib/old/command_holder.rb +75 -0
- data/lib/old/commands.rb +24 -0
- data/lib/old/old_executor.rb +95 -0
- data/lib/old/options.rb +31 -0
- data/lib/old/rush.rb +113 -0
- data/lib/old/sortissimo.rb +205 -0
- data/lib/old/systems.rb +25 -0
- data/lib/old/systems/ruby.rb +26 -0
- data/lib/old/un.rb +240 -0
- data/lib/rust/Rakefile +6 -0
- data/lib/rust/commands/builtins.rb +46 -0
- data/lib/rust/commands/builtins/cd.rb +17 -0
- data/lib/rust/commands/builtins/exit.rb +13 -0
- data/lib/rust/commands/builtins/help.rb +14 -0
- data/lib/rust/commands/builtins/parser.rb +13 -0
- data/lib/rust/commands/builtins/pwd.rb +13 -0
- data/lib/rust/commands/builtins/type.rb +13 -0
- data/lib/rust/commands/commands/ls.rb +13 -0
- data/lib/rust/commands/commands/read.rb +13 -0
- data/lib/rust/commands/commands/rm.rb +14 -0
- data/lib/rust/commands/commands/test.rb +20 -0
- data/lib/rust/helpers/array.rb +10 -0
- data/lib/rust/helpers/command_center.rb +78 -0
- data/lib/rust/helpers/constants.rb +58 -0
- data/lib/rust/helpers/io.rb +132 -0
- data/lib/rust/helpers/parser.rb +45 -0
- data/lib/rust/helpers/rush_control.rb +40 -0
- data/lib/rust/helpers/string.rb +50 -0
- data/lib/rust/helpers/trollop.rb +475 -0
- data/lib/rust/parsers/bash.rb +220 -0
- data/lib/rust/parsers/bash/stdlibext.rb +32 -0
- data/lib/rust/parsers/ren.rb +57 -0
- data/lib/rust/rust.rb +75 -0
- data/lib/rust/syntax/command.rb +23 -0
- data/lib/rust/syntax/paths.rb +31 -0
- data/lib/rust/syntax/pipes.rb +148 -0
- data/naksh.gemspec +55 -0
- data/test/naksh/arg_parser.rspec.rb +27 -0
- data/test/naksh/commands/bash/echo.rspec.rb +32 -0
- data/test/naksh/commands/sortlist +127 -0
- data/test/naksh/external_command.rspec.rb +46 -0
- data/test/naksh/send_error.rspec.rb +60 -0
- data/test/naksh/suggest.rspec.rb +38 -0
- data/test/naksh/syntax/bash.rspec.rb +69 -0
- data/test/naksh/syntax/bash/execute.rspec.rb +51 -0
- data/test/naksh/syntax/rspec.rb +63 -0
- data/test/run_rspecs.rb +20 -0
- metadata +217 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
grammar bash;
|
|
2
|
+
options {
|
|
3
|
+
language = Ruby;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
evaluate returns [result]: r=standard { result = r };
|
|
7
|
+
|
|
8
|
+
standard returns [result]: r=(str){result=r};
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
str returns [result]:
|
|
12
|
+
r=('"'! double_quoted_string_contents '"'!){result=r}
|
|
13
|
+
| r=('\''! single_quoted_string_contents '\''!){result=r}
|
|
14
|
+
| r=('""'|'\'\''){result=r};
|
|
15
|
+
|
|
16
|
+
double_quoted_string_contents returns [result]:
|
|
17
|
+
r=STRING_CONTENTS{result=r};
|
|
18
|
+
|
|
19
|
+
single_quoted_string_contents returns [result]:
|
|
20
|
+
r=STRING_CONTENTS{result=r};
|
|
21
|
+
|
|
22
|
+
STRING_CONTENTS: (~'\'')+;
|
|
23
|
+
|
|
24
|
+
MAINSET: ('a'..'z'|'A'..'Z'|'_'|'.'|'-'|'0'..'9')+;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
grammar bash;
|
|
2
|
+
options {
|
|
3
|
+
language = Ruby;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
evaluate returns [result]: r=script { result = r };
|
|
7
|
+
|
|
8
|
+
script returns [result]: r=pipeline (';' pipeline)? {result=r};
|
|
9
|
+
|
|
10
|
+
pipeline returns [result]: r=expression ('|' expression)? {result=r};
|
|
11
|
+
|
|
12
|
+
expression returns [result]: r=( command | command_list) {result=r};
|
|
13
|
+
|
|
14
|
+
command_list returns [result]: r= (or_list | and_list) {result=r};
|
|
15
|
+
|
|
16
|
+
or_list returns [result]: r= ( command '||' command ) {result=r};
|
|
17
|
+
|
|
18
|
+
and_list returns [result]: r= ( command '&&' command ) {result=r};
|
|
19
|
+
|
|
20
|
+
command returns [result]: r= ( command_name ba? ) {result=r};
|
|
21
|
+
|
|
22
|
+
arguments returns [result]: r= ( standard ( BLANK arguments )? ) {result=r};
|
|
23
|
+
|
|
24
|
+
command_name returns [result]: r= (standard) {result=r};
|
|
25
|
+
|
|
26
|
+
standard returns [result]: r= (ms | sq_str | dq_str) {result=r};
|
|
27
|
+
|
|
28
|
+
sq_str returns [result]: r= ('\'' SINGLE_QUOTED_STRING_CONTENTS '\'') {result=r};
|
|
29
|
+
|
|
30
|
+
dq_str returns [result]: r= (dq (nq NOQNOESC)? EVENESC dq) {result=r};
|
|
31
|
+
nq returns [result]: r= (NONQUOTES) {result=r};
|
|
32
|
+
dq returns [result]: r=DOUBLE_QUOTE { result=$r.text } ;
|
|
33
|
+
ba returns[result]: r= (BLANK arguments){result=r};
|
|
34
|
+
ms returns [result]: r= MAINSET {result=r};
|
|
35
|
+
//metacharacter is from bashref
|
|
36
|
+
metacharacter returns [result]: r= ( BLANK | '|' | '&' | ';' | '(' | ')' | '<' | '>' ) {result=r};
|
|
37
|
+
|
|
38
|
+
MAINSET: ('a'..'z'|'A'..'Z'|'_'|'.'|'-'|'0'..'9')+;
|
|
39
|
+
|
|
40
|
+
SINGLE_QUOTED_STRING_CONTENTS: ~'\'';
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
NONQUOTES: (~('"'))*;
|
|
44
|
+
NOQNOESC: (~('\\'|'"'));
|
|
45
|
+
EVENESC: ('\\\\')*;
|
|
46
|
+
SLASH: ('\\\\')*;
|
|
47
|
+
// the following are directly from bashref, probably not useful
|
|
48
|
+
BLANK: (' '|'\t') {channel=99};
|
|
49
|
+
|
|
50
|
+
CONTROL_OPERATOR: ( '\n' | '||' | '&&' | '&' | ';' | ';;' | '|' | '(' | ')' );
|
|
51
|
+
|
|
52
|
+
DOUBLE_QUOTE: '"';
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
#!/usr/local/bin/ruby
|
|
2
|
+
# 2011 Mahmut Bulut
|
|
3
|
+
|
|
4
|
+
# This file is part of Naksh.
|
|
5
|
+
#
|
|
6
|
+
# Naksh is free software: you can redistribute it and/or modify
|
|
7
|
+
# it under the terms of the GNU General Public License as published by
|
|
8
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
# (at your option) any later version.
|
|
10
|
+
#
|
|
11
|
+
# Naksh is distributed in the hope that it will be useful,
|
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
# GNU General Public License for more details.
|
|
15
|
+
#
|
|
16
|
+
# You should have received a copy of the GNU General Public License
|
|
17
|
+
# along with Naksh. If not, see <http://www.gnu.org/licenses/>.
|
|
18
|
+
|
|
19
|
+
require 'dhaka'
|
|
20
|
+
require 'naksh/syntax/bash/dhaka/grammar.rb'
|
|
21
|
+
|
|
22
|
+
class BashDhakaEvaluator < Dhaka::Evaluator
|
|
23
|
+
|
|
24
|
+
self.grammar = BashDhakaGrammar
|
|
25
|
+
|
|
26
|
+
define_evaluation_rules do
|
|
27
|
+
|
|
28
|
+
# No-ops
|
|
29
|
+
['no_terms', 'multiple_terms', 'single_term', 'some_terms'].each {|production_name| eval("for_#{production_name} do end")}
|
|
30
|
+
|
|
31
|
+
for_script do
|
|
32
|
+
evaluate(child_nodes[0])
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
for_nonpipe do
|
|
36
|
+
Naksh.buffer.speak
|
|
37
|
+
evaluate(child_nodes[0])
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
for_nonlist do
|
|
41
|
+
evaluate(child_nodes[0])
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
for_simple_script do
|
|
45
|
+
evaluate(child_nodes[0])
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
for_complex_script do
|
|
49
|
+
evaluate(child_nodes[0])
|
|
50
|
+
evaluate(child_nodes[2])
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
for_argless do
|
|
54
|
+
Naksh.send_command(evaluate(child_nodes[0]),Array.new)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
for_argumented do
|
|
58
|
+
Naksh.send_command(evaluate(child_nodes[0]),evaluate(child_nodes[2]))
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
for_single_arg do
|
|
62
|
+
[evaluate(child_nodes[0])]
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
for_multiple_args do
|
|
66
|
+
[evaluate(child_nodes[0]),evaluate(child_nodes[2])]
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
for_basic_argument do
|
|
70
|
+
evaluate child_nodes[0]
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
for_basic_command do
|
|
74
|
+
evaluate child_nodes[0]
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
for_realpipe do
|
|
78
|
+
STDOUT.puts "NOTICE: 1 silenced=#{Naksh.buffer.silenced?.inspect}"
|
|
79
|
+
Naksh.buffer.silence
|
|
80
|
+
STDOUT.puts "NOTICE: 2 silenced=#{Naksh.buffer.silenced?.inspect}"
|
|
81
|
+
input = evaluate(child_nodes[0])
|
|
82
|
+
output = *evaluate(child_nodes[2])
|
|
83
|
+
STDOUT.puts "NOTICE: input=(#{input.inspect}), output=(#{output.inspect})"
|
|
84
|
+
true
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
for_andlist do
|
|
88
|
+
Naksh.buffer.speak
|
|
89
|
+
evaluate(child_nodes[0]) and evaluate(child_nodes[2])
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
#======================================
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
for_function_definition do
|
|
98
|
+
function_name = evaluate(child_nodes[1])
|
|
99
|
+
arg_declarations = evaluate(child_nodes[3])
|
|
100
|
+
body = child_nodes[6]
|
|
101
|
+
@function_table[function_name] = Function.new(arg_declarations, body)
|
|
102
|
+
ChittagongSuccessResult.new(nil)
|
|
103
|
+
end
|
|
104
|
+
# lipi:function_defs
|
|
105
|
+
|
|
106
|
+
for_main_body_if_statement do
|
|
107
|
+
checked_if_statement
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
for_function_body_if_statement do
|
|
111
|
+
checked_if_statement
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def checked_if_statement
|
|
115
|
+
condition_eval = evaluate(child_nodes[1])
|
|
116
|
+
return condition_eval if condition_eval.exception
|
|
117
|
+
return evaluate(child_nodes[3]) if condition_eval.result
|
|
118
|
+
ChittagongSuccessResult.new(nil)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
for_main_body_if_else_statement do
|
|
122
|
+
checked_if_else_statement
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
for_function_body_if_else_statement do
|
|
126
|
+
checked_if_else_statement
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def checked_if_else_statement
|
|
130
|
+
condition_eval = evaluate(child_nodes[1])
|
|
131
|
+
return condition_eval if condition_eval.exception
|
|
132
|
+
if condition_eval.result
|
|
133
|
+
evaluate(child_nodes[3])
|
|
134
|
+
else
|
|
135
|
+
evaluate(child_nodes[7])
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
for_main_body_while_statement do
|
|
140
|
+
checked_while_statement
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
for_function_body_while_statement do
|
|
144
|
+
checked_while_statement
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def checked_while_statement
|
|
148
|
+
loop do
|
|
149
|
+
condition_eval = evaluate(child_nodes[1])
|
|
150
|
+
return condition_eval if condition_eval.exception
|
|
151
|
+
break unless condition_eval.result
|
|
152
|
+
body_eval = evaluate(child_nodes[3])
|
|
153
|
+
return body_eval if (body_eval.exception || body_eval.result)
|
|
154
|
+
end
|
|
155
|
+
ChittagongSuccessResult.new(nil)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
for_assignment_statement do
|
|
159
|
+
rhs = evaluate(child_nodes[2])
|
|
160
|
+
return rhs if rhs.exception
|
|
161
|
+
@stack[-1][evaluate(child_nodes[0])] = rhs.result
|
|
162
|
+
ChittagongSuccessResult.new(nil)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
for_print_statement do
|
|
166
|
+
rhs = evaluate(child_nodes[1])
|
|
167
|
+
return rhs if rhs.exception
|
|
168
|
+
@output_stream << rhs.result.to_s
|
|
169
|
+
ChittagongSuccessResult.new(nil)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
for_function_call_statement do
|
|
173
|
+
checked_function_call
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
for_function_call_expression do
|
|
177
|
+
checked_function_call
|
|
178
|
+
end
|
|
179
|
+
# lipi:checked_function_call
|
|
180
|
+
def checked_function_call
|
|
181
|
+
function_name = evaluate(child_nodes[0])
|
|
182
|
+
return ChittagongExceptionResult.new("Undefined function #{function_name}",
|
|
183
|
+
child_nodes[0]) unless @function_table.has_key?(function_name)
|
|
184
|
+
|
|
185
|
+
arg_values = evaluate(child_nodes[2])
|
|
186
|
+
return arg_values if arg_values.exception
|
|
187
|
+
|
|
188
|
+
function = @function_table[function_name]
|
|
189
|
+
return ChittagongExceptionResult.new(
|
|
190
|
+
"Wrong number of arguments", child_nodes[1]
|
|
191
|
+
) unless function.args.size == arg_values.result.size
|
|
192
|
+
new_frame = {}
|
|
193
|
+
|
|
194
|
+
function.args.zip(arg_values.result).each do |arg_name, arg_value|
|
|
195
|
+
new_frame[arg_name] = arg_value
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
@stack << new_frame
|
|
199
|
+
result = evaluate(function.body)
|
|
200
|
+
@stack.pop
|
|
201
|
+
|
|
202
|
+
result
|
|
203
|
+
end
|
|
204
|
+
# lipi:checked_function_call
|
|
205
|
+
|
|
206
|
+
for_return_statement do
|
|
207
|
+
checked_unary_operation(child_nodes[1]){|x| x}
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
for_single_arg_declaration do
|
|
211
|
+
[evaluate(child_nodes[0])]
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
for_no_arg_decl do
|
|
215
|
+
[]
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
for_multiple_arg_declarations do
|
|
219
|
+
evaluate(child_nodes[0]) + [evaluate(child_nodes[2])]
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
for_no_args do
|
|
223
|
+
ChittagongSuccessResult.new([])
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
for_single_arg do
|
|
227
|
+
checked_unary_operation(child_nodes[0]) {|x| [x]}
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
for_multiple_args do
|
|
231
|
+
checked_binary_operation(child_nodes[0], child_nodes[2]) {|a, b| a + [b]}
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
for_variable_reference do
|
|
235
|
+
variable_name = evaluate(child_nodes[0])
|
|
236
|
+
return ChittagongExceptionResult.new("Undefined variable #{variable_name}", child_nodes[0]) unless @stack[-1].has_key? variable_name
|
|
237
|
+
ChittagongSuccessResult.new(@stack[-1][variable_name])
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
for_subtraction do
|
|
241
|
+
checked_binary_operation(child_nodes[0], child_nodes[2]){|a, b| a - b}
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
for_addition do
|
|
245
|
+
checked_binary_operation(child_nodes[0], child_nodes[2]){|a, b| a + b}
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
for_division do
|
|
249
|
+
checked_binary_operation(child_nodes[0], child_nodes[2]){|a, b| a.to_f / b}
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
for_multiplication do
|
|
253
|
+
checked_binary_operation(child_nodes[0], child_nodes[2]){|a, b| a * b}
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
for_power do
|
|
257
|
+
checked_binary_operation(child_nodes[0], child_nodes[2]){|a, b| a ** b}
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
for_less_than_comparison do
|
|
261
|
+
checked_binary_operation(child_nodes[0], child_nodes[2]){|a, b| a < b}
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
for_greater_than_comparison do
|
|
265
|
+
checked_binary_operation(child_nodes[0], child_nodes[2]){|a, b| a > b}
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
for_equality_comparison do
|
|
269
|
+
checked_binary_operation(child_nodes[0], child_nodes[2]){|a, b| a == b}
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def checked_binary_operation node1, node2
|
|
273
|
+
node1_eval = evaluate(node1)
|
|
274
|
+
node2_eval = evaluate(node2)
|
|
275
|
+
return node1_eval if node1_eval.exception
|
|
276
|
+
return node2_eval if node2_eval.exception
|
|
277
|
+
ChittagongSuccessResult.new(yield(node1_eval.result, node2_eval.result))
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
for_negated_expression do
|
|
281
|
+
checked_unary_operation(child_nodes[1]){|x| -x}
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
for_negation do
|
|
285
|
+
checked_unary_operation(child_nodes[1]){|b| !b}
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
for_parenthetized_expression do
|
|
289
|
+
checked_unary_operation(child_nodes[1]){|x| x}
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def checked_unary_operation node
|
|
293
|
+
node_eval = evaluate(node)
|
|
294
|
+
return node_eval if node_eval.exception
|
|
295
|
+
ChittagongSuccessResult.new(yield(node_eval.result))
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
for_function_name do
|
|
299
|
+
token_value
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
for_variable_name do
|
|
303
|
+
token_value
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
for_arg_declaration do
|
|
307
|
+
token_value
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
for_literal do
|
|
311
|
+
ChittagongSuccessResult.new(token_value.to_f)
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
def token_value
|
|
315
|
+
child_nodes.first.token.value
|
|
316
|
+
end
|
|
317
|
+
# lipi:initialize
|
|
318
|
+
def initialize(stack, output_stream)
|
|
319
|
+
@stack = stack
|
|
320
|
+
@function_table = {}
|
|
321
|
+
@output_stream = output_stream
|
|
322
|
+
end
|
|
323
|
+
# lipi:initialize
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
class ChittagongResult
|
|
329
|
+
attr_reader :exception
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
class ChittagongSuccessResult < ChittagongResult
|
|
333
|
+
attr_reader :result
|
|
334
|
+
def initialize(result)
|
|
335
|
+
@result = result
|
|
336
|
+
end
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
class ChittagongExceptionResult < ChittagongResult
|
|
340
|
+
attr_reader :node
|
|
341
|
+
def initialize(exception, node)
|
|
342
|
+
@exception = exception
|
|
343
|
+
@node = node
|
|
344
|
+
end
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
class Function
|
|
348
|
+
attr_reader :args, :body
|
|
349
|
+
def initialize(args, body)
|
|
350
|
+
@args = args
|
|
351
|
+
@body = body
|
|
352
|
+
end
|
|
353
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
#!/usr/local/bin/ruby
|
|
2
|
+
# 2011 Mahmut Bulut
|
|
3
|
+
|
|
4
|
+
# This file is part of Naksh.
|
|
5
|
+
#
|
|
6
|
+
# Naksh is free software: you can redistribute it and/or modify
|
|
7
|
+
# it under the terms of the GNU General Public License as published by
|
|
8
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
# (at your option) any later version.
|
|
10
|
+
#
|
|
11
|
+
# Naksh is distributed in the hope that it will be useful,
|
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
# GNU General Public License for more details.
|
|
15
|
+
#
|
|
16
|
+
# You should have received a copy of the GNU General Public License
|
|
17
|
+
# along with Naksh. If not, see <http://www.gnu.org/licenses/>.
|
|
18
|
+
|
|
19
|
+
require 'dhaka'
|
|
20
|
+
|
|
21
|
+
class BashDhakaGrammar<Dhaka::Grammar
|
|
22
|
+
|
|
23
|
+
precedences do
|
|
24
|
+
left %w[ || && ]
|
|
25
|
+
left %w[ | > ]
|
|
26
|
+
left %w| $ |
|
|
27
|
+
left %w| * |
|
|
28
|
+
nonassoc %w| " ' |
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
for_symbol(Dhaka::START_SYMBOL_NAME) do
|
|
32
|
+
script %w[ body ]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
for_symbol 'body' do
|
|
36
|
+
simple_script %w[ list ]
|
|
37
|
+
complex_script %w[ body ; list]
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
for_symbol 'list' do
|
|
41
|
+
nonlist %w[ pipeline ]
|
|
42
|
+
andlist %w[ pipeline && pipeline ]
|
|
43
|
+
orlist %w[ pipeline || pipeline ]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
for_symbol 'pipeline' do
|
|
47
|
+
nonpipe %w[ statement ]
|
|
48
|
+
realpipe %w[ pipeline | statement ]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
for_symbol 'statement' do
|
|
52
|
+
argless %w[ command_reference ]
|
|
53
|
+
argumented %w[ command_reference space arguments ]
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
for_symbol 'arguments' do
|
|
57
|
+
single_arg %w[ argument ]
|
|
58
|
+
multiple_args %w[ argument space argument ]
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
for_symbol 'argument' do
|
|
62
|
+
basic_argument %w[ word ]
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
for_symbol 'command_reference' do
|
|
66
|
+
basic_command %w[ word ]
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
end
|