fix root, much tighter now

This commit is contained in:
Torsten Ruger 2015-10-09 17:32:34 +03:00
parent 3651856fc5
commit e9a7b1f6da
4 changed files with 110 additions and 77 deletions

View File

@ -3,7 +3,7 @@ require_relative "compound_types"
require_relative "tokens"
require_relative "keywords"
require_relative "control"
require_relative "expression"
require_relative "statement"
require_relative "call_site"
require_relative "function_definition"
require_relative "class_definition"
@ -25,13 +25,13 @@ module Parser
include Tokens
include Keywords
include Control
include Expression
include Statement
include CallSite
include FunctionDefinition
include Operators
include ModuleDef
rule(:root_body) {( class_definition | function_definition )}
rule(:root) { root_body.repeat.as(:expression_list) }
rule(:root) { root_body.repeat.as(:statement_list) }
end
end

View File

@ -1,15 +1,21 @@
class FooBo
Bar.call(35)
int main()
Bar.call(35)
end
end
-- -- --
s(:expressions,
s(:statements,
s(:class, :FooBo,
s(:derives, nil),
s(:expressions,
s(:call,
s(:name, :call),
s(:arguments,
s(:int, 35)),
s(:receiver,
s(:module, "Bar"))))))
s(:statements,
s(:function, :int,
s(:name, :main),
s(:parameters),
s(:statements,
s(:call,
s(:name, :call),
s(:arguments,
s(:int, 35)),
s(:receiver,
s(:class_name, :Bar))))))))

View File

@ -1,19 +1,35 @@
int foo(ref x)
a = 5
class Object
int foo(ref x)
a = 5
end
end
class Other < Object
foo( 3 )
int foo()
foo( 3 )
end
end
-- -- --
s(:expressions,
s(:function, :int,
s(:name, :foo),
s(:parameters,
s(:parameter, :ref, :x)),
s(:expressions,
s(:assign,
s(:name, :a),
s(:int, 5)))),
s(:call,
s(:name, :foo),
s(:arguments,
s(:int, 3))))
s(:statements,
s(:class, :Object,
s(:derives, nil),
s(:statements,
s(:function, :int,
s(:name, :foo),
s(:parameters,
s(:parameter, :ref, :x)),
s(:statements,
s(:assignment,
s(:name, :a),
s(:int, 5)))))),
s(:class, :Other,
s(:derives, :Object),
s(:statements,
s(:function, :int,
s(:name, :foo),
s(:parameters),
s(:statements,
s(:call,
s(:name, :foo),
s(:arguments,
s(:int, 3))))))))

View File

@ -1,56 +1,67 @@
int fibonaccit(int n)
a = 0
b = 1
while( n > 1 )
tmp = a
a = b
b = tmp + b
puts(b)
n = n - 1
class Object
int fibonaccit(int n)
a = 0
b = 1
while( n > 1 )
tmp = a
a = b
b = tmp + b
puts(b)
n = n - 1
end
end
int main()
fibonaccit( 10 )
end
end
fibonaccit( 10 )
-- -- --
s(:expressions,
s(:function, :int,
s(:name, :fibonaccit),
s(:parameters,
s(:parameter, :int, :n)),
s(:expressions,
s(:assign,
s(:name, :a),
s(:int, 0)),
s(:assign,
s(:name, :b),
s(:int, 1)),
s(:while,
s(:condition,
s(:operator, ">",
s(:name, :n),
s(:int, 1))),
s(:expressions,
s(:assign,
s(:name, :tmp),
s(:name, :a)),
s(:assign,
s(:statements,
s(:class, :Object,
s(:derives, nil),
s(:statements,
s(:function, :int,
s(:name, :fibonaccit),
s(:parameters,
s(:parameter, :int, :n)),
s(:statements,
s(:assignment,
s(:name, :a),
s(:name, :b)),
s(:assign,
s(:int, 0)),
s(:assignment,
s(:name, :b),
s(:operator, "+",
s(:name, :tmp),
s(:name, :b))),
s(:int, 1)),
s(:while_statement,
s(:condition,
s(:operator_value, :>,
s(:name, :n),
s(:int, 1))),
s(:statements,
s(:assignment,
s(:name, :tmp),
s(:name, :a)),
s(:assignment,
s(:name, :a),
s(:name, :b)),
s(:assignment,
s(:name, :b),
s(:operator_value, :+,
s(:name, :tmp),
s(:name, :b))),
s(:call,
s(:name, :puts),
s(:arguments,
s(:name, :b))),
s(:assignment,
s(:name, :n),
s(:operator_value, :-,
s(:name, :n),
s(:int, 1))))))),
s(:function, :int,
s(:name, :main),
s(:parameters),
s(:statements,
s(:call,
s(:name, :puts),
s(:name, :fibonaccit),
s(:arguments,
s(:name, :b))),
s(:assign,
s(:name, :n),
s(:operator, "-",
s(:name, :n),
s(:int, 1))))))),
s(:call,
s(:name, :fibonaccit),
s(:arguments,
s(:int, 10))))
s(:int, 10))))))))