add field access with tests
This commit is contained in:
parent
8e07a7568f
commit
be444bc687
@ -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
|
||||
|
@ -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 ) }
|
||||
|
||||
|
@ -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
|
||||
|
@ -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) },
|
||||
|
8
test/cases/call_site/field_basic.tst
Normal file
8
test/cases/call_site/field_basic.tst
Normal file
@ -0,0 +1,8 @@
|
||||
message.self
|
||||
-- -- --
|
||||
s(:expressions,
|
||||
s(:field_access,
|
||||
s(:receiver,
|
||||
s(:name, :message)),
|
||||
s(:field,
|
||||
s(:name, :self))))
|
@ -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))))))
|
||||
|
@ -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)))))
|
||||
|
@ -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)))))))
|
||||
|
Loading…
Reference in New Issue
Block a user