fix class defs

This commit is contained in:
Torsten Ruger 2015-10-09 17:32:05 +03:00
parent 60746383ee
commit 3651856fc5
9 changed files with 70 additions and 96 deletions

View File

@ -2,9 +2,14 @@ module Parser
module ModuleDef
include Parslet
rule(:class_field) { keyword_field >> field_def }
rule(:class_body) {
function_definition | class_field
}
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
keyword_class >> class_name >> (str("<") >> space >> class_name).maybe.as(:derived_name) >>
( (keyword_end.absent? >> class_body).repeat()).as(:class_statements) >> keyword_end
end
end

View File

@ -32,18 +32,18 @@ class String
end
-- -- --
s(:expressions,
s(:statements,
s(:class, :String,
s(:derives, nil),
s(:expressions,
s(:statements,
s(:function, :ref,
s(:name, :new_string),
s(:parameters,
s(:parameter, :int, :len)),
s(:expressions,
s(:assign,
s(:statements,
s(:assignment,
s(:name, :len),
s(:operator, "<<",
s(:operator_value, :<<,
s(:name, :len),
s(:int, 2))),
s(:return,
@ -57,7 +57,7 @@ s(:expressions,
s(:function, :int,
s(:name, :length),
s(:parameters),
s(:expressions,
s(:statements,
s(:return,
s(:field_access,
s(:receiver,
@ -68,27 +68,27 @@ s(:expressions,
s(:name, :plus),
s(:parameters,
s(:parameter, :ref, :str)),
s(:expressions,
s(:assign,
s(:statements,
s(:assignment,
s(:name, :my_length),
s(:field_access,
s(:receiver,
s(:name, :self)),
s(:field,
s(:name, :length)))),
s(:assign,
s(:assignment,
s(:name, :str_len),
s(:call,
s(:name, :length),
s(:arguments),
s(:receiver,
s(:name, :str)))),
s(:assign,
s(:assignment,
s(:name, :my_length),
s(:operator, "+",
s(:operator_value, :+,
s(:name, :str_len),
s(:name, :my_length))),
s(:assign,
s(:assignment,
s(:name, :new_string),
s(:call,
s(:name, :new_string),
@ -96,16 +96,16 @@ s(:expressions,
s(:name, :my_length)),
s(:receiver,
s(:name, :self)))),
s(:assign,
s(:assignment,
s(:name, :i),
s(:int, 0)),
s(:while,
s(:while_statement,
s(:condition,
s(:operator, "<",
s(:operator_value, :<,
s(:name, :i),
s(:name, :my_length))),
s(:expressions,
s(:assign,
s(:statements,
s(:assignment,
s(:name, :char),
s(:call,
s(:name, :get),
@ -118,21 +118,21 @@ s(:expressions,
s(:name, :char)),
s(:receiver,
s(:name, :new_string))),
s(:assign,
s(:assignment,
s(:name, :i),
s(:operator, "+",
s(:operator_value, :+,
s(:name, :i),
s(:int, 1))))),
s(:assign,
s(:assignment,
s(:name, :i),
s(:int, 0)),
s(:while,
s(:while_statement,
s(:condition,
s(:operator, "<",
s(:operator_value, :<,
s(:name, :i),
s(:name, :str_len))),
s(:expressions,
s(:assign,
s(:statements,
s(:assignment,
s(:name, :char),
s(:call,
s(:name, :get),
@ -140,9 +140,9 @@ s(:expressions,
s(:name, :i)),
s(:receiver,
s(:name, :str)))),
s(:assign,
s(:assignment,
s(:name, :len),
s(:operator, "+",
s(:operator_value, :+,
s(:name, :i),
s(:name, :my_length))),
s(:call,
@ -152,9 +152,9 @@ s(:expressions,
s(:name, :char)),
s(:receiver,
s(:name, :new_string))),
s(:assign,
s(:assignment,
s(:name, :i),
s(:operator, "+",
s(:operator_value, :+,
s(:name, :i),
s(:int, 1))))),
s(:return,

View File

@ -3,27 +3,21 @@ class Foo < Object
int func()
return self.fff
end
ofthen(3 , var)
end
-- -- --
s(:expressions,
s(:statements,
s(:class, :Foo,
s(:derives, :Object),
s(:expressions,
s(:statements,
s(:class_field, :int, :fff,
s(:int, 3)),
s(:function, :int,
s(:name, :func),
s(:parameters),
s(:expressions,
s(:statements,
s(:return,
s(:field_access,
s(:receiver,
s(:name, :self)),
s(:field,
s(:name, :fff)))))),
s(:call,
s(:name, :ofthen),
s(:arguments,
s(:int, 3),
s(:name, :var))))))
s(:name, :fff)))))))))

View File

@ -1,5 +1,4 @@
class Pifi
ofthen(3 , var)
int self.ofthen(int n , ref m)
n = n + m.index
@ -7,24 +6,19 @@ class Pifi
end
end
-- -- --
s(:expressions,
s(:statements,
s(:class, :Pifi,
s(:derives, nil),
s(:expressions,
s(:call,
s(:name, :ofthen),
s(:arguments,
s(:int, 3),
s(:name, :var))),
s(:statements,
s(:function, :int,
s(:name, :ofthen),
s(:parameters,
s(:parameter, :int, :n),
s(:parameter, :ref, :m)),
s(:expressions,
s(:assign,
s(:statements,
s(:assignment,
s(:name, :n),
s(:operator, "+",
s(:operator_value, :+,
s(:name, :n),
s(:field_access,
s(:receiver,

View File

@ -8,23 +8,23 @@ class Ifi
end
end
-- -- --
s(:expressions,
s(:statements,
s(:class, :Ifi,
s(:derives, nil),
s(:expressions,
s(:statements,
s(:function, :int,
s(:name, :ofthen),
s(:parameters,
s(:parameter, :int, :n)),
s(:expressions,
s(:if,
s(:statements,
s(:if_statement,
s(:condition,
s(:int, 0)),
s(:if_true,
s(:assign,
s(:true_statements,
s(:assignment,
s(:name, :isit),
s(:int, 42))),
s(:if_false,
s(:assign,
s(:false_statements,
s(:assignment,
s(:name, :maybenot),
s(:int, 667)))))))))

View File

@ -1,15 +1,16 @@
class Foo < Object
int test()
43
return 43
end
end
-- -- --
s(:expressions,
s(:statements,
s(:class, :Foo,
s(:derives, :Object),
s(:expressions,
s(:statements,
s(:function, :int,
s(:name, :test),
s(:parameters),
s(:expressions,
s(:int, 43))))))
s(:statements,
s(:return,
s(:int, 43)))))))

View File

@ -1,21 +1,22 @@
class Opers
int foo(int x)
int abba = 5
abba + 5
return abba + 5
end
end
-- -- --
s(:expressions,
s(:statements,
s(:class, :Opers,
s(:derives, nil),
s(:expressions,
s(:statements,
s(:function, :int,
s(:name, :foo),
s(:parameters,
s(:parameter, :int, :x)),
s(:expressions,
s(:statements,
s(:field_def, :int, :abba,
s(:int, 5)),
s(:operator, "+",
s(:name, :abba),
s(:int, 5)))))))
s(:return,
s(:operator_value, :+,
s(:name, :abba),
s(:int, 5))))))))

View File

@ -1,9 +1,9 @@
class Foo
5
field int x
end
-- -- --
s(:expressions,
s(:statements,
s(:class, :Foo,
s(:derives, nil),
s(:expressions,
s(:int, 5))))
s(:statements,
s(:class_field, :int, :x))))

View File

@ -1,21 +0,0 @@
class Fibo
int fibonaccit(int n)
int a = 0
return a
end
end
-- -- --
s(:expressions,
s(:class, :Fibo,
s(:derives, nil),
s(:expressions,
s(:function, :int,
s(:name, :fibonaccit),
s(:parameters,
s(:parameter, :int, :n)),
s(:expressions,
s(:field_def, :int, :a,
s(:int, 0)),
s(:return,
s(:name, :a)))))))