add field access with tests

This commit is contained in:
Torsten Ruger 2015-09-20 12:50:06 +03:00
parent 8e07a7568f
commit be444bc687
8 changed files with 84 additions and 50 deletions

View File

@ -12,6 +12,7 @@ module Parser
rule(:call_site) { (basic_type.as(:receiver) >> str(".")).maybe >> #possibly qualified
name.as(:call_site) >> argument_list >> comment.maybe}
rule(:field_access) { name.as(:receiver) >> str(".") >> name.as(:field) }
end
end

View File

@ -2,7 +2,7 @@ module Parser
module Expression
include Parslet
rule(:value_expression) { call_site | basic_type }
rule(:value_expression) { call_site | field_access |basic_type }
rule(:expression) { (simple_return | while_do | small_conditional | conditional | operator_expression | call_site ) }

View File

@ -10,10 +10,10 @@ require_relative "module_definition"
require_relative "operators"
module Parser
# obviously a work in progress !!
# We "compose" the parser from bits, divide and hopefully conquer
# a note about .maybe : .maybe is almost every respect the same as .repeat(0,1)
# so either 0, or 1, in other words maybe. Nice feature, but there are strings attached:
# a maybe removes the 0 a sequence (array) to a single (hash). Thus 2 transformations are needed
@ -31,8 +31,9 @@ module Parser
include Operators
include ModuleDef
rule(:root_body) {(module_definition | class_definition | function_definition | expression |
operator_expression | call_site | basic_type | hash_constant | array_constant )}
rule(:root_body) {(module_definition | class_definition | function_definition |
expression | operator_expression | call_site | field_access |
basic_type | hash_constant | array_constant )}
rule(:root) { root_body.repeat.as(:expression_list) }
end
end

View File

@ -36,6 +36,9 @@ module Parser
:argument_list => sequence(:argument_list)) do
s(:call , call_site, s(:arguments , *argument_list) , s(:receiver , receiver))
end
rule( :receiver => simple(:receiver) , :field => simple(:field) ) do
s(:field_access , s(:receiver , receiver) , s(:field , field) )
end
rule(:if => simple(:if), :conditional => simple(:conditional),
:if_true => {:expressions => sequence(:if_true) , :else => simple(:else) },

View File

@ -0,0 +1,8 @@
message.self
-- -- --
s(:expressions,
s(:field_access,
s(:receiver,
s(:name, :message)),
s(:field,
s(:name, :self))))

View File

@ -1,22 +1,33 @@
class Pifi
ofthen(3 , var)
int ofthen(int n , ref m)
44
n = n + m.index
return n
end
end
-- -- --
s(:expressions,
s(:class, :Pifi,
s(:derives, nil),
s(:call,
s(:name, :ofthen),
s(:arguments,
s(:int, 3),
s(:name, :var))),
s(:function, :int,
s(:name, :ofthen),
s(:parameters,
s(:field, :int, :n),
s(:field, :ref, :m)),
s(:expressions,
s(:int, 44)))))
s(:expressions,
s(:class, :Pifi,
s(:derives, nil),
s(:call,
s(:name, :ofthen),
s(:arguments,
s(:int, 3),
s(:name, :var))),
s(:function, :int,
s(:name, :ofthen),
s(:parameters,
s(:field, :int, :n),
s(:field, :ref, :m)),
s(:expressions,
s(:assign,
s(:name, :n),
s(:operator, "+",
s(:name, :n),
s(:field_access,
s(:receiver,
s(:name, :m)),
s(:field,
s(:name, :index))))),
s(:return,
s(:name, :n))))))

View File

@ -1,17 +1,21 @@
int retvar(ref n)
int i = 5
int i = n.layout
return i
end
-- -- --
s(:expressions,
s(:function, :int,
s(:name, :retvar),
s(:parameters,
s(:field, :ref, :n)),
s(:expressions,
s(:name, :int),
s(:assign,
s(:name, :i),
s(:int, 5)),
s(:return,
s(:name, :i)))))
s(:expressions,
s(:function, :int,
s(:name, :retvar),
s(:parameters,
s(:field, :ref, :n)),
s(:expressions,
s(:name, :int),
s(:assign,
s(:name, :i),
s(:field_access,
s(:receiver,
s(:name, :n)),
s(:field,
s(:name, :layout)))),
s(:return,
s(:name, :i)))))

View File

@ -1,22 +1,28 @@
module Opers
int foo(int x)
int abba = 5
int abba = x + self.index
return abba + 5
end
end
-- -- --
s(:expressions,
s(:module, :Opers,
s(:function, :int,
s(:name, :foo),
s(:parameters,
s(:field, :int, :x)),
s(:expressions,
s(:name, :int),
s(:assign,
s(:name, :abba),
s(:int, 5)),
s(:return,
s(:operator, "+",
s(:name, :abba),
s(:int, 5)))))))
s(:expressions,
s(:module, :Opers,
s(:function, :int,
s(:name, :foo),
s(:parameters,
s(:field, :int, :x)),
s(:expressions,
s(:name, :int),
s(:assign,
s(:name, :abba),
s(:operator, "+",
s(:name, :x),
s(:field_access,
s(:receiver,
s(:name, :self)),
s(:field,
s(:name, :index))))),
s(:return,
s(:operator, "+",
s(:name, :abba),
s(:int, 5)))))))