示例#1
0
//block -> {decls statements}
void block(){
	match('{');
	enviroment saved_env = top;
	top = Enviroment(top);
	decls();
	stmts();
	match('}');
	top = saved_env;
}
示例#2
0
int decls() {
  printf("%d\n", a);
  if (a > 0) {
    a = a - 1;
    return decls();
  }
  else {
    a = 0;
    return a;
  }
}
示例#3
0
LetRecNode::LetRecNode(Compiler &compiler, const iter_t &node, bool isProgram)
    : Node(compiler, Node_ExprLetRec), _isProgram(isProgram)
{
    unsigned j;
    unsigned jmax = node->children.size();

    /*
        If this an usual letrec, not the program, then don't parse the last
        child element as an "in" expression.
    */
    if (!isProgram) jmax -= 1;

    _expr = 0;

    // Parse RDecls
    map<string, RDecl*> vars;
    for (j = 0; j < jmax; j++) {
        RDecl *rd = new RDecl(compiler, node->children.begin() + j);
        if (vars.insert(make_pair(rd->alias, rd)).second == false) {
            throw logic_error("Multiple declarations of \"" + rd->alias + "\".");
        }
        if (isProgram && compiler.testFlag(Compiler::OptimizeMain) && rd->alias == "main") {
            _expr = rd->expr;
        } else {
            _decls.push_back(rd);
        }
    }
    vars.clear(); // We're being very memory efficient here. Embedded here we come!

    if (isProgram) {
        if (!compiler.testFlag(Compiler::OptimizeMain)) {
            _expr = new IdentNode(compiler, "main");
        }
    } else {
        _expr = Node::nodeFactory(compiler, node->children.begin() + j);
    }
    assert(_expr != 0);

    // Optimize out unused declarations
    if (!_decls.empty() && compiler.testFlag(Compiler::OptimizeUnused)) {
        DEBUGMSG("Optimizing unused variables:");
        std::vector<RDecl*> decls(_decls);
        _decls.clear();
        unsigned n = decls.size();
        BoolMatrix b(n + 1, n + 1, false);
        for (unsigned c = 0; c < n; c++) {
            for (unsigned r = 0; r < n; r++) {
                if (decls.at(r)->expr->usesVariable(decls.at(c)->alias)) {
                    b.data[r][c] = true;
                }
            }
            if (_expr->usesVariable(decls.at(c)->alias)) {
                b.data[n][c] = true;
            }
        }
        b.transformToTransitiveClosure();
        #ifdef DEBUG
        unsigned unused = 0;
        #endif
        for (unsigned i = 0; i < n; i++) {
            if (b.data[n][i]) {
                _decls.push_back(decls.at(i));
            } else {
                DEBUGMSG("Found unused variable \"" + decls.at(i)->alias + "\" in letrec.");
                #ifdef DEBUG
                unused++;
                #endif
            }
        }
        DEBUGMSG("Found " + intToString(unused) + ".");
    }

    // Reorder cyclic definitions
    if (_decls.size() > 1 && compiler.testFlag(Compiler::OptimizeLetRecDecls)) {
        BoolMatrix b(_decls.size(), _decls.size(), true);
        for (unsigned c = 0; c < _decls.size(); c++) {
            for (unsigned r = 0; r < _decls.size(); r++) {
                if (c == r) {
                    b.data[r][r] = false; // Set identity values
                } else if (_decls.at(r)->expr->usesVariable(_decls.at(c)->alias)) {
                    b.data[r][c] = false;
                }
            }
        }
        unsigned n = _decls.size();
        vector<RDecl*> d(_decls);
        _decls.clear();
        unsigned *ds = new unsigned[n];
        for (unsigned i = 0; i < n; i++) ds[i] = i;
        MySort mySort(&b);
        stable_sort(ds, ds + n, mySort);
        for (unsigned i = 0; i < n; i++) _decls.push_back(d.at(ds[i]));
        delete ds;
    }
}
示例#4
0
int main() {
  printf("Enter a:\n");
  // scanf("%d",a);
  a = 3;
  printf("%d\n", decls());
}