jitcraft/SemanticAnalysis.cpp
changeset 23 8a31742781c1
parent 22 765ab1b672a3
child 25 23e8b3d8e4e6
     1.1 --- a/jitcraft/SemanticAnalysis.cpp	Sun Jul 01 16:36:39 2012 -0700
     1.2 +++ b/jitcraft/SemanticAnalysis.cpp	Sun Jul 01 18:21:15 2012 -0700
     1.3 @@ -211,30 +211,46 @@
     1.4      return left;
     1.5  }
     1.6  
     1.7 +Type *
     1.8 +SemanticAnalysis::visitForLValue(Expression *expression)
     1.9 +{
    1.10 +    if (expression->isIndexExpression()) {
    1.11 +        expression->accept(this);
    1.12 +        if (!expression->type())
    1.13 +            return NULL;
    1.14 +        return expression->type()->contained();
    1.15 +    }
    1.16 +
    1.17 +    if (!expression->isNameProxy()) {
    1.18 +        assert(false);
    1.19 +        return NULL;
    1.20 +    }
    1.21 +
    1.22 +    NameProxy *proxy = expression->toNameProxy();
    1.23 +    if (!proxy->binding()->kind() == BoundName::VARIABLE) {
    1.24 +        fprintf(stderr, "error\n");
    1.25 +        return NULL;
    1.26 +    }
    1.27 +
    1.28 +    Variable *var = proxy->binding()->toVariable();
    1.29 +    if (!var->type())
    1.30 +        return NULL;
    1.31 +
    1.32 +    if  (var->type()->isReference())
    1.33 +        return var->type()->contained();
    1.34 +    return var->type();
    1.35 +}
    1.36 +
    1.37  void
    1.38  SemanticAnalysis::visit(Assignment *node)
    1.39  {
    1.40      visitForLoad(node->expression());
    1.41  
    1.42 -    if (node->lvalue()->isIndexExpression()) {
    1.43 -        node->lvalue()->accept(this);
    1.44 -        node->setType(node->lvalue()->type()->contained());
    1.45 -    } else {
    1.46 -        NameProxy *proxy = node->lvalue()->toNameProxy();
    1.47 -        if (!proxy->binding()->kind() == BoundName::VARIABLE) {
    1.48 -            fprintf(stderr, "error\n");
    1.49 -            return;
    1.50 -        }
    1.51 -
    1.52 -        Variable *var = proxy->binding()->toVariable();
    1.53 -        if (!var->type())
    1.54 -            return;
    1.55 -
    1.56 -        if  (var->type()->isReference())
    1.57 -            node->setType(var->type()->contained());
    1.58 -        else
    1.59 -            node->setType(var->type());
    1.60 -    }
    1.61 +    // :TODO: use Local
    1.62 +    Type *type = visitForLValue(node->lvalue());
    1.63 +    if (!type)
    1.64 +        return;
    1.65 +    node->setType(type);
    1.66  
    1.67      if (node->token() != TOK_ASSIGN)
    1.68          assert(TypeCheckBinary(node->token(), node->type(), node->expression()->type()));
    1.69 @@ -243,6 +259,25 @@
    1.70  }
    1.71  
    1.72  void
    1.73 +SemanticAnalysis::visit(IncDecExpression *node)
    1.74 +{
    1.75 +    // :TODO: use Local
    1.76 +    Type *type = visitForLValue(node->expression());
    1.77 +    if (!type)
    1.78 +        return;
    1.79 +
    1.80 +    assert(!type->isReference());
    1.81 +    if (type->isArray()) {
    1.82 +        // can't a[]++
    1.83 +        assert(false);
    1.84 +        return;
    1.85 +    }
    1.86 +
    1.87 +    assert((type->isInt32() && !type->tag()) || type->isFloat());
    1.88 +    node->setType(type);
    1.89 +}
    1.90 +
    1.91 +void
    1.92  SemanticAnalysis::visit(BinaryExpression *node)
    1.93  {
    1.94      // :TODO: use Local