there goes the module
it was ruby after all should be reborn as twins: namespace and aspect
This commit is contained in:
parent
58b3553251
commit
486e24514b
@ -44,7 +44,7 @@ module Parser
|
||||
rule(:field) { type >> name >> (assign >> value_expression.as(:value) ).maybe}
|
||||
rule(:class_field) { keyword_field >> field }
|
||||
# and class/module names must start with capital
|
||||
rule(:module_name) { keyword.absent? >> (match['A-Z'] >> match['a-zA-Z0-9_'].repeat).as(:module_name) >> space? }
|
||||
rule(:class_name) { keyword.absent? >> (match['A-Z'] >> match['a-zA-Z0-9_'].repeat).as(:class_name) >> space? }
|
||||
|
||||
rule(:escape) { str('\\') >> any.as(:esc) }
|
||||
rule(:string) { quote >> (
|
||||
@ -56,7 +56,7 @@ module Parser
|
||||
|
||||
rule(:float) { integer >> dot >> integer >>
|
||||
(exponent >> sign.maybe >> digit.repeat(1,3)).maybe >> space?}
|
||||
rule(:basic_type){ integer | name | string | float | field | module_name |
|
||||
rule(:basic_type){ integer | name | string | float | field | class_name |
|
||||
keyword_true | keyword_false | keyword_nil }
|
||||
end
|
||||
end
|
||||
|
11
lib/parser/class_definition.rb
Normal file
11
lib/parser/class_definition.rb
Normal file
@ -0,0 +1,11 @@
|
||||
module Parser
|
||||
module ModuleDef
|
||||
include Parslet
|
||||
|
||||
rule(:class_definition) do
|
||||
keyword_class >> class_name >> (smaller >> class_name).maybe.as(:derived_name) >>
|
||||
( (keyword_end.absent? >> root_body).repeat()).as(:class_expressions) >> keyword_end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -3,7 +3,7 @@ module Parser
|
||||
include Parslet
|
||||
|
||||
rule(:function_definition) {
|
||||
type >> ((module_name|name).as(:receiver) >> str(".")).maybe >> #possibly qualified
|
||||
type >> ((class_name|name).as(:receiver) >> str(".")).maybe >> #possibly qualified
|
||||
name.as(:function_name) >> left_parenthesis >>
|
||||
parameter_list.maybe >> right_parenthesis >> expressions_end >> space?
|
||||
}
|
||||
|
@ -1,15 +0,0 @@
|
||||
module Parser
|
||||
module ModuleDef
|
||||
include Parslet
|
||||
rule(:module_definition) do
|
||||
keyword_module >> module_name >>
|
||||
( (keyword_end.absent? >> root_body).repeat()).as(:module_expressions) >> keyword_end
|
||||
end
|
||||
|
||||
rule(:class_definition) do
|
||||
keyword_class >> module_name >> (smaller >> module_name).maybe.as(:derived_name) >>
|
||||
( (keyword_end.absent? >> root_body).repeat()).as(:class_expressions) >> keyword_end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
@ -6,7 +6,7 @@ require_relative "control"
|
||||
require_relative "expression"
|
||||
require_relative "call_site"
|
||||
require_relative "function_definition"
|
||||
require_relative "module_definition"
|
||||
require_relative "class_definition"
|
||||
require_relative "operators"
|
||||
|
||||
module Parser
|
||||
@ -31,7 +31,7 @@ module Parser
|
||||
include Operators
|
||||
include ModuleDef
|
||||
|
||||
rule(:root_body) {(module_definition | class_definition | function_definition |
|
||||
rule(:root_body) {( class_definition | function_definition |
|
||||
expression | operator_expression | call_site )}
|
||||
rule(:root) { root_body.repeat.as(:expression_list) }
|
||||
end
|
||||
|
@ -24,7 +24,7 @@ module Parser
|
||||
rule(:field => simple(:field) , :type => simple(:type), :name => simple(:name) , :value => simple(:value)) {
|
||||
s(:class_field , type.to_sym , name.to_sym , value ) }
|
||||
|
||||
rule(:module_name => simple(:module_name)) { s(:module,module_name.to_s) }
|
||||
rule(:class_name => simple(:class_name)) { s(:module,class_name.to_s) }
|
||||
|
||||
rule(:array_constant => sequence(:array_constant) ) { s(:array , *array_constant) }
|
||||
rule(:array_element => simple(:array_element)) { array_element }
|
||||
@ -99,13 +99,13 @@ module Parser
|
||||
end
|
||||
|
||||
#modules and classes are understandibly quite similar Class < Module
|
||||
rule( :module_name => simple(:module_name) , :derived_name => simple(:derived_name) , :class_expressions => sequence(:class_expressions) , :end=>"end") do
|
||||
s(:class , module_name.to_s.to_sym ,
|
||||
rule( :class_name => simple(:class_name) , :derived_name => simple(:derived_name) , :class_expressions => sequence(:class_expressions) , :end=>"end") do
|
||||
s(:class , class_name.to_s.to_sym ,
|
||||
s(:derives, derived_name ? derived_name.to_a.first.to_sym : nil) ,
|
||||
s(:expressions, *class_expressions) )
|
||||
end
|
||||
rule( :module_name => simple(:module_name) , :module_expressions => sequence(:module_expressions) , :end=>"end") do
|
||||
s(:module , module_name.to_s.to_sym , s(:expressions, *module_expressions))
|
||||
rule( :class_name => simple(:class_name) , :module_expressions => sequence(:module_expressions) , :end=>"end") do
|
||||
s(:module , class_name.to_s.to_sym , s(:expressions, *module_expressions))
|
||||
end
|
||||
|
||||
rule(:expression_list => sequence(:expression_list)) {
|
||||
|
@ -1,17 +0,0 @@
|
||||
class Foo
|
||||
module Boo
|
||||
funcall(3 , var)
|
||||
end
|
||||
end
|
||||
-- -- --
|
||||
s(:expressions,
|
||||
s(:class, :Foo,
|
||||
s(:derives, nil),
|
||||
s(:expressions,
|
||||
s(:module, :Boo,
|
||||
s(:expressions,
|
||||
s(:call,
|
||||
s(:name, :funcall),
|
||||
s(:arguments,
|
||||
s(:int, 3),
|
||||
s(:name, :var))))))))
|
@ -1,11 +0,0 @@
|
||||
module Opers
|
||||
field int abba = 5
|
||||
field ref baab
|
||||
end
|
||||
-- -- --
|
||||
s(:expressions,
|
||||
s(:module, :Opers,
|
||||
s(:expressions,
|
||||
s(:class_field, :int, :abba,
|
||||
s(:int, 5)),
|
||||
s(:class_field, :ref, :baab))))
|
@ -1,17 +0,0 @@
|
||||
module Foo
|
||||
class Bar
|
||||
funcall(3 , var)
|
||||
end
|
||||
end
|
||||
-- -- --
|
||||
s(:expressions,
|
||||
s(:module, :Foo,
|
||||
s(:expressions,
|
||||
s(:class, :Bar,
|
||||
s(:derives, nil),
|
||||
s(:expressions,
|
||||
s(:call,
|
||||
s(:name, :funcall),
|
||||
s(:arguments,
|
||||
s(:int, 3),
|
||||
s(:name, :var))))))))
|
@ -1,23 +0,0 @@
|
||||
module Soho
|
||||
ofthen(3 , var)
|
||||
int ofthen(int n,ref m )
|
||||
return 44
|
||||
end
|
||||
end
|
||||
-- -- --
|
||||
s(:expressions,
|
||||
s(:module, :Soho,
|
||||
s(:expressions,
|
||||
s(:call,
|
||||
s(:name, :ofthen),
|
||||
s(:arguments,
|
||||
s(:int, 3),
|
||||
s(:name, :var))),
|
||||
s(:function, :int,
|
||||
s(:name, :ofthen),
|
||||
s(:parameters,
|
||||
s(:parameter, :int, :n),
|
||||
s(:parameter, :ref, :m)),
|
||||
s(:expressions,
|
||||
s(:return,
|
||||
s(:int, 44)))))))
|
@ -1,29 +0,0 @@
|
||||
module Foo
|
||||
ref ofthen(int n)
|
||||
if(0)
|
||||
isit = 42
|
||||
else
|
||||
maybenot = 667
|
||||
end
|
||||
end
|
||||
end
|
||||
-- -- --
|
||||
s(:expressions,
|
||||
s(:module, :Foo,
|
||||
s(:expressions,
|
||||
s(:function, :ref,
|
||||
s(:name, :ofthen),
|
||||
s(:parameters,
|
||||
s(:parameter, :int, :n)),
|
||||
s(:expressions,
|
||||
s(:if,
|
||||
s(:condition,
|
||||
s(:int, 0)),
|
||||
s(:if_true,
|
||||
s(:assign,
|
||||
s(:name, :isit),
|
||||
s(:int, 42))),
|
||||
s(:if_false,
|
||||
s(:assign,
|
||||
s(:name, :maybenot),
|
||||
s(:int, 667)))))))))
|
@ -1,25 +0,0 @@
|
||||
module Opers
|
||||
int foo(int x)
|
||||
int abba = self.index
|
||||
return abba + 5
|
||||
end
|
||||
end
|
||||
-- -- --
|
||||
s(:expressions,
|
||||
s(:module, :Opers,
|
||||
s(:expressions,
|
||||
s(:function, :int,
|
||||
s(:name, :foo),
|
||||
s(:parameters,
|
||||
s(:parameter, :int, :x)),
|
||||
s(:expressions,
|
||||
s(:field_def, :int, :abba,
|
||||
s(:field_access,
|
||||
s(:receiver,
|
||||
s(:name, :self)),
|
||||
s(:field,
|
||||
s(:name, :index)))),
|
||||
s(:return,
|
||||
s(:operator, "+",
|
||||
s(:name, :abba),
|
||||
s(:int, 5))))))))
|
@ -1,8 +0,0 @@
|
||||
module Simple
|
||||
5
|
||||
end
|
||||
-- -- --
|
||||
s(:expressions,
|
||||
s(:module, :Simple,
|
||||
s(:expressions,
|
||||
s(:int, 5))))
|
@ -1,20 +0,0 @@
|
||||
module Fibo
|
||||
a = 5 + foo
|
||||
bar( b , a , r)
|
||||
end
|
||||
|
||||
-- -- --
|
||||
s(:expressions,
|
||||
s(:module, :Fibo,
|
||||
s(:expressions,
|
||||
s(:assign,
|
||||
s(:name, :a),
|
||||
s(:operator, "+",
|
||||
s(:int, 5),
|
||||
s(:name, :foo))),
|
||||
s(:call,
|
||||
s(:name, :bar),
|
||||
s(:arguments,
|
||||
s(:name, :b),
|
||||
s(:name, :a),
|
||||
s(:name, :r))))))
|
@ -1,15 +1,14 @@
|
||||
module Fibo
|
||||
class Fibo
|
||||
int fibonaccit(int n)
|
||||
int a = 0
|
||||
return a
|
||||
end
|
||||
|
||||
fibonaccit( 10 )
|
||||
end
|
||||
|
||||
-- -- --
|
||||
s(:expressions,
|
||||
s(:module, :Fibo,
|
||||
s(:class, :Fibo,
|
||||
s(:derives, nil),
|
||||
s(:expressions,
|
||||
s(:function, :int,
|
||||
s(:name, :fibonaccit),
|
||||
@ -19,8 +18,4 @@ s(:expressions,
|
||||
s(:field_def, :int, :a,
|
||||
s(:int, 0)),
|
||||
s(:return,
|
||||
s(:name, :a)))),
|
||||
s(:call,
|
||||
s(:name, :fibonaccit),
|
||||
s(:arguments,
|
||||
s(:int, 10))))))
|
||||
s(:name, :a)))))))
|
||||
|
@ -1,18 +0,0 @@
|
||||
module FooBo
|
||||
class Bar
|
||||
a = 5 + foo
|
||||
end
|
||||
end
|
||||
|
||||
-- -- --
|
||||
s(:expressions,
|
||||
s(:module, :FooBo,
|
||||
s(:expressions,
|
||||
s(:class, :Bar,
|
||||
s(:derives, nil),
|
||||
s(:expressions,
|
||||
s(:assign,
|
||||
s(:name, :a),
|
||||
s(:operator, "+",
|
||||
s(:int, 5),
|
||||
s(:name, :foo))))))))
|
Loading…
Reference in New Issue
Block a user