Example #1
0
void CCodeGenerator::call(Function* function, Expression* args) {
	std::vector<Operand> val;
	for (Expression::Ptr a = args; a; a = a->next()) {
		val.push_back(emit(a));
	}

    if (!function->type()->is_void()) {
        return_ = alloc_temp(function->type());
    } else {
        line();
    }
	out_ << function->label() << "(";
    
    Expression::Ptr arg = args;
    Formal::Ptr formal = function->formals();
	for (int i = 0; i < val.size(); i++) {
        if(!formal->is_self() && !formal->type()->equals(arg->type())) {
            // Cast to the appropriate C-type, since C doesn't know anything 
            // about subtypes, etc..
            out_ << "(";
            operator()(formal->type());
            out_ << ")";
        }
		out_ << val[i];
		if (i < val.size() - 1) {
			out_ << ", ";
		}
        formal = formal->next();
        arg = arg->next();
	}
	out_ << ");\n";
}
Example #2
0
void CCodeGenerator::operator()(Construct* expr) {
    // Look up the function by name in the current context.
    String::Ptr id = env_->name("@init");
    Class::Ptr clazz = expr->type()->clazz();
    Function::Ptr func = clazz->function(id);
    
    std::vector<Operand> args;
    for (Expression::Ptr a = expr->arguments(); a; a = a->next()) {
        args.push_back(emit(a));
    }

    return_ = alloc_temp(clazz->type());
    out_ << func->label() << "(";

    Formal::Ptr formal = func->formals();
    Expression::Ptr arg = expr->arguments();
    for (int i = 0; i < args.size(); i++) {
        if(!formal->is_self() && !formal->type()->equals(arg->type())) {
            // Cast to the appropriate C-type, since C doesn't know anything 
            // about subtypes, etc..
            out_ << "(";
            operator()(formal->type());
            out_ << ")";
        }
        out_ << args[i];
        if (i < args.size() - 1) {
            out_ << ", ";
        }
        formal = formal->next();
        arg = arg->next();
    }
    out_ << ");\n";
}
Example #3
0
void CCodeGenerator::native_operator(Call* expr) {

    std::string id = expr->function()->name()->string();  
    std::vector<Operand> args;
    for (Expression::Ptr a = expr->arguments(); a; a = a->next()) {
        args.push_back(emit(a));
    }   

    return_ = alloc_temp(expr->type());
    if (id == "@add") {
        out_ << args[0] << "+" << args[1] << ";\n";
    } else if (id == "@sub") {
        out_ << args[0] << "-" << args[1] << ";\n";
    } else if (id == "@mul") {
        out_ << args[0] << "*" << args[1] << ";\n";
    } else if (id == "@div") {
        out_ << args[0] << "/" << args[1] << ";\n";
    } else if (id == "@neg") {
        out_ << "-" << args[0] << ";\n";
    } else if (id == "@mod") {
        out_ << args[0] << "%" << args[1] << ";\n";
    } else if (id == "@compl") {
        out_ << "~" << args[0] << ";\n";
    } else if (id == "@equal") {
        out_ << args[0] << "==" << args[1] << ";\n";
    } else if (id == "@less") {
        out_ << args[0] << "<" << args[1] << ";\n";
    }
}
Example #4
0
void CCodeGenerator::operator()(Block* expr) {
	enter_scope();
	for (Expression::Ptr s = expr->children(); s; s = s->next()) {
        loc(s);
		s(this);
	}
	exit_scope();
}
Example #5
0
void TreePrinter::operator()(HashLiteral* expression) {
    indent_level_++;
    out_ << "HashLiteral\n";
    
    int i = 0;
    for (Expression::Ptr e = expression->arguments(); e; e = e->next()) {
        print_tabs(); out_ << "argument" << i << ": ";
        e(this);
        i++;
    }
    indent_level_--;
}
Example #6
0
void TreePrinter::operator()(Block* statement) {
    indent_level_++;
    Expression::Ptr children = statement->children();
    out_ << "Block\n";

    int i = 0;
    for (Expression::Ptr c = children; c; c = c->next()) {
        print_tabs(); out_ << "child" << i << ": ";
        c(this);
        i++;
    }
    indent_level_--;
}
Example #7
0
void TreePrinter::operator()(Match* statement) {
    indent_level_++;
    Expression::Ptr guard = statement->guard();
    out_ << "Match\n";
    print_tabs(); out_ << "guard: ";
    guard(this);

    int i = 0; 
    for (Expression::Ptr b = statement->cases(); b; b = b->next()) {
        print_tabs(); out_ << "case" << i << ": ";
        b(this);
        i++;
    }
    indent_level_--;
}
Example #8
0
void TreePrinter::operator()(Construct* expression) {
    indent_level_++;
    Expression::Ptr arguments = expression->arguments();
    out_ << "Construct\n";
    print_tabs(); out_ << "type: ";
    out_ << expression->type() << "\n";

    int i = 0;
    for (Expression::Ptr a = arguments; a; a = a->next()) {
        print_tabs(); out_ << "argument" << i << ": ";
        a(this);
        i++;
    }
    indent_level_--;
}
Example #9
0
void TreePrinter::operator()(Call* expression) {
    indent_level_++;
    Expression::Ptr arguments = expression->arguments();
    Expression::Ptr expr = expression->expression();
    out_ << "Call\n";
    print_tabs(); out_ << "expression: ";
    expr(this);

    int i = 0;
    for (Expression::Ptr a = arguments; a; a = a->next()) {
        print_tabs(); out_ << "argument" << i << ": ";
        a(this);
        i++;
    }
    indent_level_--;
}
Example #10
0
void TreePrinter::operator()(Let* expression) {
    indent_level_++;
    Expression::Ptr block = expression->block();
    out_ << "Let\n";
    
    int i = 0;
    for (Expression::Ptr v = expression->variables(); v; v = v->next()) {
        print_tabs(); out_ << "variable" << i << ": ";
        v(this);
        i++;
    }
    print_tabs(); out_ << "block: ";
    block(this);

    indent_level_--;
}