emfrp 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +45 -12
- data/bin/emfrp +4 -1
- data/examples/LCDClock/LCDClock.mfrp +93 -93
- data/examples/LCDClock/LCDClock_LPC1768.bin +0 -0
- data/examples/LCDClock/README.md +24 -24
- data/examples/LCDPositioner/LCDPositioner.mfrp +30 -30
- data/examples/LCDPositioner/LCDPositionerMain.c +15 -15
- data/examples/MostDistantPoint/MostDistantPoint.mfrp +25 -25
- data/examples/MostDistantPoint/MostDistantPointMain.c +14 -14
- data/lib/emfrp/compile/c/alloc.rb +200 -200
- data/lib/emfrp/compile/c/codegen.rb +18 -18
- data/lib/emfrp/compile/c/codegen_context.rb +218 -218
- data/lib/emfrp/compile/c/monofy.rb +185 -185
- data/lib/emfrp/compile/c/syntax_codegen.rb +364 -364
- data/lib/emfrp/compile/c/syntax_exp_codegen.rb +119 -119
- data/lib/emfrp/compile/graphviz/graphviz.rb +53 -53
- data/lib/emfrp/compile_error.rb +95 -95
- data/lib/emfrp/interpreter/command_manager.rb +367 -367
- data/lib/emfrp/interpreter/evaluater.rb +146 -146
- data/lib/emfrp/interpreter/file_loader.rb +52 -52
- data/lib/emfrp/interpreter/interpreter.rb +200 -195
- data/lib/emfrp/parser/expression.rb +386 -386
- data/lib/emfrp/parser/misc.rb +184 -184
- data/lib/emfrp/parser/newnode_convert.rb +72 -72
- data/lib/emfrp/parser/operator.rb +25 -25
- data/lib/emfrp/parser/parser.rb +150 -150
- data/lib/emfrp/parser/parsing_error.rb +49 -49
- data/lib/emfrp/parser/toplevel.rb +555 -555
- data/lib/emfrp/pre_convert/pre_convert.rb +32 -32
- data/lib/emfrp/syntax.rb +171 -171
- data/lib/emfrp/typing/typing_error.rb +47 -47
- data/lib/emfrp/typing/union_type.rb +197 -197
- data/lib/emfrp/version.rb +1 -1
- data/mfrp_include/Std.mfrp +122 -122
- data/tests/Rakefile +8 -8
- data/tests/Rakefile.common +27 -27
- data/tests/command/Rakefile +2 -2
- data/tests/command/ReplaceNode.mfrp +39 -39
- data/tests/compiler/ComplexDataType/ComplexDataType.mfrp +14 -14
- data/tests/compiler/ComplexDataType/ComplexDataTypeMain.c +15 -15
- data/tests/compiler/ComplexDataType/Rakefile +2 -2
- data/tests/compiler/ComplexDataType/expected_out.txt +0 -0
- data/tests/compiler/ComplexDataType/in.txt +5 -5
- data/tests/compiler/LCDClock/LCDClock.mfrp +90 -90
- data/tests/compiler/LCDClock/LCDClockMain.c +0 -0
- data/tests/compiler/LCDClock/Rakefile +2 -2
- data/tests/compiler/LCDClock/expected_out.txt +0 -0
- data/tests/compiler/LCDClock/in.txt +0 -0
- data/tests/compiler/LCDPositioner/LCDPositioner.mfrp +30 -30
- data/tests/compiler/LCDPositioner/LCDPositionerMain.c +15 -15
- data/tests/compiler/LCDPositioner/Rakefile +2 -2
- data/tests/compiler/LCDPositioner/graph.dot +0 -0
- data/tests/compiler/LCDPositioner/graph.png +0 -0
- data/tests/compiler/Rakefile +8 -8
- data/tests/compiler/Rakefile.common +23 -23
- data/tests/compiler/UseData/Rakefile +2 -2
- data/tests/compiler/UseData/UseData.mfrp +8 -8
- data/tests/compiler/UseSubModule/Rakefile +2 -2
- data/tests/compiler/UseSubModule/SubModule.mfrp +8 -8
- data/tests/compiler/UseSubModule/SubModule2.mfrp +5 -5
- data/tests/compiler/UseSubModule/UseSubModule.mfrp +11 -11
- data/tests/core/FromAnnotation.mfrp +18 -18
- data/tests/core/Last.mfrp +10 -10
- data/tests/core/Rakefile +2 -2
- data/tests/core/TypingTest.mfrp +11 -11
- data/tests/core/WithoutInputs.mfrp +19 -19
- data/tests/load_time_error/Rakefile +32 -32
- data/tests/load_time_error/TypeMismatch.mfrp +4 -4
- metadata +3 -3
@@ -1,185 +1,185 @@
|
|
1
|
-
module Emfrp
|
2
|
-
class Monofy
|
3
|
-
IType = Struct.new(:typing, :type_def) do
|
4
|
-
def ==(other)
|
5
|
-
self.typing.to_uniq_str == other.typing.to_uniq_str && self.type_def[:name] == other.type_def[:name]
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
IFunc = Struct.new(:typing, :param_typings, :func_def) do
|
10
|
-
def ==(other)
|
11
|
-
typing_name_array == other.typing_name_array && self.func_def[:name] == other.func_def[:name]
|
12
|
-
end
|
13
|
-
|
14
|
-
def typing_name_array
|
15
|
-
([typing] + param_typings).map{|x| x.to_uniq_str}
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.monofy(top)
|
20
|
-
m = new(top)
|
21
|
-
m.monofy()
|
22
|
-
m.sort_nodes()
|
23
|
-
end
|
24
|
-
|
25
|
-
def initialize(top)
|
26
|
-
@top = top
|
27
|
-
@datas = []
|
28
|
-
@itypes = []
|
29
|
-
@ifuncs = []
|
30
|
-
@update = false
|
31
|
-
end
|
32
|
-
|
33
|
-
def used_nodes
|
34
|
-
used = []
|
35
|
-
f = proc do |n|
|
36
|
-
used << n
|
37
|
-
n[:params].each do |p|
|
38
|
-
pn = @top[:dict][:node_space][p[:name][:desc]].get
|
39
|
-
if pn.is_a?(NodeDef)
|
40
|
-
if p[:last]
|
41
|
-
used << pn
|
42
|
-
else
|
43
|
-
f.call(pn)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
@top[:outputs].each{|x| f.call(@top[:dict][:node_space][x[:name][:desc]].get)}
|
49
|
-
return used.uniq
|
50
|
-
end
|
51
|
-
|
52
|
-
def sort_nodes
|
53
|
-
evaluated = Hash[@top[:inputs].map{|x| [x[:name], true]}]
|
54
|
-
nodes = used_nodes()
|
55
|
-
que = nodes.select{|n| n[:params].all?{|p| p[:last] || evaluated[p[:name]]}}
|
56
|
-
res = []
|
57
|
-
until que.empty?
|
58
|
-
node = que.shift
|
59
|
-
next if evaluated[node[:name]]
|
60
|
-
evaluated[node[:name]] = true
|
61
|
-
que += nodes.select do |n|
|
62
|
-
n[:params].all?{|p| p[:last] || evaluated[p[:name]]}
|
63
|
-
end
|
64
|
-
res << node
|
65
|
-
end
|
66
|
-
@top[:dict][:sorted_nodes] = res.map{|x| Link.new(x)}
|
67
|
-
end
|
68
|
-
|
69
|
-
def monofy
|
70
|
-
visited = {}
|
71
|
-
@top[:outputs].each do |x|
|
72
|
-
monofy_node(@top[:dict][:node_space][x[:name][:desc]].get, visited)
|
73
|
-
end
|
74
|
-
while @update
|
75
|
-
@update = false
|
76
|
-
@datas.each do |d|
|
77
|
-
key = Link.new(d)
|
78
|
-
unless @top[:dict][:sorted_datas].find{|x| x == key}
|
79
|
-
monofy_exp(d[:exp])
|
80
|
-
@top[:dict][:sorted_datas] << key
|
81
|
-
end
|
82
|
-
end
|
83
|
-
@ifuncs.each do |ifunc|
|
84
|
-
key = ifunc.typing_name_array + [ifunc.func_def[:name][:desc]]
|
85
|
-
unless @top[:dict][:ifunc_space][key]
|
86
|
-
new_f = copy_def(ifunc.func_def)
|
87
|
-
xs = ([new_f] + new_f[:params]).map{|x| x[:typing]}
|
88
|
-
ys = [ifunc.typing] + ifunc.param_typings
|
89
|
-
xs.zip(ys).each{|x, y| x.unify(y)}
|
90
|
-
@top[:dict][:ifunc_space][key] = Link.new(new_f)
|
91
|
-
monofy_exp(new_f[:exp])
|
92
|
-
end
|
93
|
-
end
|
94
|
-
@itypes.each do |itype|
|
95
|
-
key = itype.typing.to_uniq_str
|
96
|
-
unless @top[:dict][:itype_space][key]
|
97
|
-
new_t = copy_def(itype.type_def)
|
98
|
-
new_t[:tvalues].each{|x| x[:typing].unify(itype.typing)}
|
99
|
-
@top[:dict][:itype_space][key] = Link.new(new_t)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
@top[:dict][:sorted_datas] = @datas.map{|x| Link.new(x)}
|
104
|
-
end
|
105
|
-
|
106
|
-
def monofy_node(node, visited)
|
107
|
-
return if visited[node]
|
108
|
-
visited[node] = true
|
109
|
-
monofy_exp(node[:init_exp]) if node[:init_exp]
|
110
|
-
if node.is_a?(NodeDef)
|
111
|
-
monofy_exp(node[:exp])
|
112
|
-
node[:params].each do |param|
|
113
|
-
monofy_node(@top[:dict][:node_space][param[:name][:desc]].get, visited)
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
def monofy_exp(exp, visited={})
|
119
|
-
if exp.is_a?(Syntax) && exp.has_key?(:typing)
|
120
|
-
case type_def = @top[:dict][:type_space][exp[:typing].typename].get
|
121
|
-
when TypeDef
|
122
|
-
itype = IType.new(exp[:typing], type_def)
|
123
|
-
unless @itypes.find{|x| x == itype}
|
124
|
-
@itypes << itype
|
125
|
-
@update = true
|
126
|
-
end
|
127
|
-
when PrimTypeDef
|
128
|
-
# do nothing
|
129
|
-
else
|
130
|
-
raise
|
131
|
-
end
|
132
|
-
end
|
133
|
-
case exp
|
134
|
-
when FuncCall
|
135
|
-
case f = @top[:dict][:func_space][exp[:name][:desc]].get
|
136
|
-
when FuncDef
|
137
|
-
ifunc = IFunc.new(exp[:typing], exp[:args].map{|x| x[:typing]}, f)
|
138
|
-
unless @ifuncs.find{|x| x == ifunc}
|
139
|
-
@ifuncs << ifunc
|
140
|
-
@update = true
|
141
|
-
end
|
142
|
-
when PrimFuncDef
|
143
|
-
@top[:dict][:used_pfuncs] << Link.new(f)
|
144
|
-
@top[:dict][:used_pfuncs].uniq!
|
145
|
-
end
|
146
|
-
monofy_exp(exp[:args])
|
147
|
-
when VarRef
|
148
|
-
d = exp[:binder].get
|
149
|
-
if d.is_a?(DataDef) && !@datas.find{|x| x[:name] == d[:name]}
|
150
|
-
@datas << d
|
151
|
-
@update = true
|
152
|
-
end
|
153
|
-
when Syntax
|
154
|
-
monofy_exp(exp.values)
|
155
|
-
when Array
|
156
|
-
exp.each{|x| monofy_exp(x)}
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
def copy_def(x, mapping={}, tbl={})
|
161
|
-
case x
|
162
|
-
when Syntax
|
163
|
-
new_x = x.dup
|
164
|
-
mapping[x] = new_x
|
165
|
-
x.keys.each do |k|
|
166
|
-
new_x[k] = copy_def(x[k], mapping, tbl)
|
167
|
-
end
|
168
|
-
if new_x.has_key?(:typing)
|
169
|
-
new_x[:typing] = x[:typing].clone_utype(tbl)
|
170
|
-
end
|
171
|
-
if new_x.has_key?(:binder) && mapping[new_x[:binder].get]
|
172
|
-
new_x[:binder] = Link.new(mapping[new_x[:binder].get])
|
173
|
-
end
|
174
|
-
if new_x.has_key?(:type_def) && mapping[new_x[:type_def].get]
|
175
|
-
new_x[:type_def] = Link.new(mapping[new_x[:type_def].get])
|
176
|
-
end
|
177
|
-
return new_x
|
178
|
-
when Array
|
179
|
-
return x.map{|a| copy_def(a, mapping, tbl)}
|
180
|
-
else
|
181
|
-
return x
|
182
|
-
end
|
183
|
-
end
|
184
|
-
end
|
185
|
-
end
|
1
|
+
module Emfrp
|
2
|
+
class Monofy
|
3
|
+
IType = Struct.new(:typing, :type_def) do
|
4
|
+
def ==(other)
|
5
|
+
self.typing.to_uniq_str == other.typing.to_uniq_str && self.type_def[:name] == other.type_def[:name]
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
IFunc = Struct.new(:typing, :param_typings, :func_def) do
|
10
|
+
def ==(other)
|
11
|
+
typing_name_array == other.typing_name_array && self.func_def[:name] == other.func_def[:name]
|
12
|
+
end
|
13
|
+
|
14
|
+
def typing_name_array
|
15
|
+
([typing] + param_typings).map{|x| x.to_uniq_str}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.monofy(top)
|
20
|
+
m = new(top)
|
21
|
+
m.monofy()
|
22
|
+
m.sort_nodes()
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize(top)
|
26
|
+
@top = top
|
27
|
+
@datas = []
|
28
|
+
@itypes = []
|
29
|
+
@ifuncs = []
|
30
|
+
@update = false
|
31
|
+
end
|
32
|
+
|
33
|
+
def used_nodes
|
34
|
+
used = []
|
35
|
+
f = proc do |n|
|
36
|
+
used << n
|
37
|
+
n[:params].each do |p|
|
38
|
+
pn = @top[:dict][:node_space][p[:name][:desc]].get
|
39
|
+
if pn.is_a?(NodeDef)
|
40
|
+
if p[:last]
|
41
|
+
used << pn
|
42
|
+
else
|
43
|
+
f.call(pn)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
@top[:outputs].each{|x| f.call(@top[:dict][:node_space][x[:name][:desc]].get)}
|
49
|
+
return used.uniq
|
50
|
+
end
|
51
|
+
|
52
|
+
def sort_nodes
|
53
|
+
evaluated = Hash[@top[:inputs].map{|x| [x[:name], true]}]
|
54
|
+
nodes = used_nodes()
|
55
|
+
que = nodes.select{|n| n[:params].all?{|p| p[:last] || evaluated[p[:name]]}}
|
56
|
+
res = []
|
57
|
+
until que.empty?
|
58
|
+
node = que.shift
|
59
|
+
next if evaluated[node[:name]]
|
60
|
+
evaluated[node[:name]] = true
|
61
|
+
que += nodes.select do |n|
|
62
|
+
n[:params].all?{|p| p[:last] || evaluated[p[:name]]}
|
63
|
+
end
|
64
|
+
res << node
|
65
|
+
end
|
66
|
+
@top[:dict][:sorted_nodes] = res.map{|x| Link.new(x)}
|
67
|
+
end
|
68
|
+
|
69
|
+
def monofy
|
70
|
+
visited = {}
|
71
|
+
@top[:outputs].each do |x|
|
72
|
+
monofy_node(@top[:dict][:node_space][x[:name][:desc]].get, visited)
|
73
|
+
end
|
74
|
+
while @update
|
75
|
+
@update = false
|
76
|
+
@datas.each do |d|
|
77
|
+
key = Link.new(d)
|
78
|
+
unless @top[:dict][:sorted_datas].find{|x| x == key}
|
79
|
+
monofy_exp(d[:exp])
|
80
|
+
@top[:dict][:sorted_datas] << key
|
81
|
+
end
|
82
|
+
end
|
83
|
+
@ifuncs.each do |ifunc|
|
84
|
+
key = ifunc.typing_name_array + [ifunc.func_def[:name][:desc]]
|
85
|
+
unless @top[:dict][:ifunc_space][key]
|
86
|
+
new_f = copy_def(ifunc.func_def)
|
87
|
+
xs = ([new_f] + new_f[:params]).map{|x| x[:typing]}
|
88
|
+
ys = [ifunc.typing] + ifunc.param_typings
|
89
|
+
xs.zip(ys).each{|x, y| x.unify(y)}
|
90
|
+
@top[:dict][:ifunc_space][key] = Link.new(new_f)
|
91
|
+
monofy_exp(new_f[:exp])
|
92
|
+
end
|
93
|
+
end
|
94
|
+
@itypes.each do |itype|
|
95
|
+
key = itype.typing.to_uniq_str
|
96
|
+
unless @top[:dict][:itype_space][key]
|
97
|
+
new_t = copy_def(itype.type_def)
|
98
|
+
new_t[:tvalues].each{|x| x[:typing].unify(itype.typing)}
|
99
|
+
@top[:dict][:itype_space][key] = Link.new(new_t)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
@top[:dict][:sorted_datas] = @datas.map{|x| Link.new(x)}
|
104
|
+
end
|
105
|
+
|
106
|
+
def monofy_node(node, visited)
|
107
|
+
return if visited[node]
|
108
|
+
visited[node] = true
|
109
|
+
monofy_exp(node[:init_exp]) if node[:init_exp]
|
110
|
+
if node.is_a?(NodeDef)
|
111
|
+
monofy_exp(node[:exp])
|
112
|
+
node[:params].each do |param|
|
113
|
+
monofy_node(@top[:dict][:node_space][param[:name][:desc]].get, visited)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def monofy_exp(exp, visited={})
|
119
|
+
if exp.is_a?(Syntax) && exp.has_key?(:typing)
|
120
|
+
case type_def = @top[:dict][:type_space][exp[:typing].typename].get
|
121
|
+
when TypeDef
|
122
|
+
itype = IType.new(exp[:typing], type_def)
|
123
|
+
unless @itypes.find{|x| x == itype}
|
124
|
+
@itypes << itype
|
125
|
+
@update = true
|
126
|
+
end
|
127
|
+
when PrimTypeDef
|
128
|
+
# do nothing
|
129
|
+
else
|
130
|
+
raise
|
131
|
+
end
|
132
|
+
end
|
133
|
+
case exp
|
134
|
+
when FuncCall
|
135
|
+
case f = @top[:dict][:func_space][exp[:name][:desc]].get
|
136
|
+
when FuncDef
|
137
|
+
ifunc = IFunc.new(exp[:typing], exp[:args].map{|x| x[:typing]}, f)
|
138
|
+
unless @ifuncs.find{|x| x == ifunc}
|
139
|
+
@ifuncs << ifunc
|
140
|
+
@update = true
|
141
|
+
end
|
142
|
+
when PrimFuncDef
|
143
|
+
@top[:dict][:used_pfuncs] << Link.new(f)
|
144
|
+
@top[:dict][:used_pfuncs].uniq!
|
145
|
+
end
|
146
|
+
monofy_exp(exp[:args])
|
147
|
+
when VarRef
|
148
|
+
d = exp[:binder].get
|
149
|
+
if d.is_a?(DataDef) && !@datas.find{|x| x[:name] == d[:name]}
|
150
|
+
@datas << d
|
151
|
+
@update = true
|
152
|
+
end
|
153
|
+
when Syntax
|
154
|
+
monofy_exp(exp.values)
|
155
|
+
when Array
|
156
|
+
exp.each{|x| monofy_exp(x)}
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def copy_def(x, mapping={}, tbl={})
|
161
|
+
case x
|
162
|
+
when Syntax
|
163
|
+
new_x = x.dup
|
164
|
+
mapping[x] = new_x
|
165
|
+
x.keys.each do |k|
|
166
|
+
new_x[k] = copy_def(x[k], mapping, tbl)
|
167
|
+
end
|
168
|
+
if new_x.has_key?(:typing)
|
169
|
+
new_x[:typing] = x[:typing].clone_utype(tbl)
|
170
|
+
end
|
171
|
+
if new_x.has_key?(:binder) && mapping[new_x[:binder].get]
|
172
|
+
new_x[:binder] = Link.new(mapping[new_x[:binder].get])
|
173
|
+
end
|
174
|
+
if new_x.has_key?(:type_def) && mapping[new_x[:type_def].get]
|
175
|
+
new_x[:type_def] = Link.new(mapping[new_x[:type_def].get])
|
176
|
+
end
|
177
|
+
return new_x
|
178
|
+
when Array
|
179
|
+
return x.map{|a| copy_def(a, mapping, tbl)}
|
180
|
+
else
|
181
|
+
return x
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|