}
}
+//AST* normalize_fnapp_args(AST* tree)
+//{
+//}
+
AST* normalize_fnapp(AST* tree)
{
+ AST* normalized = tree;
AST* fn = fnapp_fn(tree);
+ /* Normalize the function */
if (!isatomic(fn)) {
AST* temp = TempVar();
fnapp_set_fn(tree, temp);
- return Let(temp, fn, tree);
- } else {
- return tree;
+ normalized = Let(temp, fn, tree);
+ }
+ /* Normalize the function arguments */
+ vec_t temps;
+ vec_init(&temps);
+ vec_t* args = fnapp_args(tree);
+ for (int i = 0; i < vec_size(args); i++) {
+ AST* arg = (AST*)vec_at(args, i);
+ if (!isatomic(arg)) {
+ AST* temp = TempVar();
+ vec_push_back(&temps, Let(temp, arg, NULL));
+ vec_set(args, i, temp);
+ }
+ }
+ /* Nest all the scopes and return the new form */
+ for (int i = vec_size(&temps); i > 0; i--) {
+ AST* let = (AST*)vec_at(&temps,i-1);
+ let_set_body(let, normalized);
+ normalized = let;
}
+ vec_deinit(&temps);
+ return normalized;
}
AST* normalize(AST* tree)
it "should normalize an application with a complex function" do
expect(anf('(foo())()')).to eq([['let', ['$:0', ['T_ID:foo']], ['$:0']]])
end
+
+ it "should normalize an application with a complex arg" do
+ expect(anf('foo(bar())')).to eq([['let', ['$:0', ['T_ID:bar']], ['T_ID:foo', '$:0']]])
+ end
+
+ it "should normalize an application with two complex args" do
+ expect(anf('foo(bar(),baz())')).to eq([
+ ['let', ['$:0', ['T_ID:bar']],
+ ['let', ['$:1', ['T_ID:baz']],
+ ['T_ID:foo', '$:0', '$:1']]]])
+ end
+
+ it "should normalize an application with three complex args" do
+ expect(anf('foo(bar(),baz(),boo())')).to eq([
+ ['let', ['$:0', ['T_ID:bar']],
+ ['let', ['$:1', ['T_ID:baz']],
+ ['let', ['$:2', ['T_ID:boo']],
+ ['T_ID:foo', '$:0', '$:1', '$:2']]]]])
+ end
+
+ it "should normalize an application with simple and complex args (s,c,c)" do
+ expect(anf('foo(a,bar(),baz())')).to eq([
+ ['let', ['$:0', ['T_ID:bar']],
+ ['let', ['$:1', ['T_ID:baz']],
+ ['T_ID:foo', 'T_ID:a', '$:0', '$:1']]]])
+ end
+
+ it "should normalize an application with simple and complex args (c,s,c)" do
+ expect(anf('foo(bar(),a,baz())')).to eq([
+ ['let', ['$:0', ['T_ID:bar']],
+ ['let', ['$:1', ['T_ID:baz']],
+ ['T_ID:foo', '$:0', 'T_ID:a', '$:1']]]])
+ end
+
+ it "should normalize an application with simple and complex args (c,c,s)" do
+ expect(anf('foo(bar(),baz(),a)')).to eq([
+ ['let', ['$:0', ['T_ID:bar']],
+ ['let', ['$:1', ['T_ID:baz']],
+ ['T_ID:foo', '$:0', '$:1', 'T_ID:a']]]])
+ end
end
end