Skip to content

Commit

Permalink
more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiayang committed Mar 22, 2019
1 parent daf5bb0 commit 5dfc71c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 20 deletions.
17 changes: 10 additions & 7 deletions build/supertiny.flx
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ struct foo
{
do {
var addr: ipv4
addr.raw = 123445;
// addr.raw = -16668480;
addr.raw = 0xff01a8c0;

addr.bytes[0] = 192
addr.bytes[1] = 168
addr.bytes[2] = 1
addr.bytes[3] = 1
printf("%d.%d.%d.%d\n", addr._.bytes[0], addr._.bytes[1], addr._.bytes[2], addr._.bytes[3]);

// addr._.bytes[0] = 192
// addr._.bytes[1] = 168
// addr._.bytes[2] = 1
// addr._.bytes[3] = 1
}


do {
/* do {
var x: @raw union {
bar: f64
foo: i64
Expand All @@ -49,7 +52,7 @@ struct foo

printf("x = %.2lf\n", x.bar)
printf("x = %d\n", x.foo)
}
} */
}


Expand Down
14 changes: 7 additions & 7 deletions source/codegen/autocasting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ namespace cgn
}
else
{
if(ty->getMinBits() <= fir::Type::getInt64()->getBitWidth() - 1)
return fir::ConstantInt::getInt64(cn->getInt64());
if(ty->getMinBits() <= fir::Type::getInt64()->getBitWidth() - 1)
return fir::ConstantInt::getInt64(cn->getInt64());

else if(ty->isSigned() && ty->getMinBits() <= fir::Type::getUint64()->getBitWidth())
return fir::ConstantInt::getUint64(cn->getUint64());
else if(ty->isSigned() && ty->getMinBits() <= fir::Type::getUint64()->getBitWidth())
return fir::ConstantInt::getUint64(cn->getUint64());

else
error("int overflow");
else
error("int overflow");
}
}

Expand Down Expand Up @@ -65,7 +65,7 @@ namespace cgn
if(target->toPrimitiveType()->getBitWidth() < ty->getMinBits())
{
// TODO: actually do what we say.
warn(cs->loc(), "Casting literal to type '%s' will cause an overflow; resulting value will be the limit of the casted type",
warn(cs->loc(), "Casting literal to type '%s' will cause an overflow; value will be truncated bitwise to fit",
target);
}

Expand Down
5 changes: 5 additions & 0 deletions source/fir/Types/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,11 @@ namespace fir
return this->isIntegerType() && this->toPrimitiveType()->isSigned();
}

bool Type::isUnsignedIntType()
{
return this->isIntegerType() && !this->toPrimitiveType()->isSigned();
}

bool Type::isFunctionType()
{
return this->kind == TypeKind::Function;
Expand Down
11 changes: 7 additions & 4 deletions source/frontend/parser/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,19 @@ namespace parser
{
switch(t)
{
// () and [] have the same precedence.
// not sure if this should stay -- works for now.
case TT::DoubleColon:
return 5000;

case TT::LParen:
case TT::LSquare:
return 2000;

case TT::Period:
case TT::DoubleColon:
return 1500;

case TT::LSquare:
return 1000;


// unary !
// unary +/-
// bitwise ~
Expand Down
1 change: 1 addition & 0 deletions source/include/ir/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ namespace fir
bool isIntegerType();
bool isFunctionType();
bool isSignedIntType();
bool isUnsignedIntType();
bool isFloatingPointType();

bool isArraySliceType();
Expand Down
9 changes: 7 additions & 2 deletions source/typecheck/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ TCResult ast::LitNumber::typecheck(sst::TypecheckState* fs, fir::Type* infer)
fs->pushLoc(this);
defer(fs->popLoc());

// set base = 0 to autodetect.
auto number = mpfr::mpreal(this->num, mpfr_get_default_prec(), /* base: */ 0);
// i don't think mpfr auto-detects base, LMAO
int base = 10;
if(this->num.find("0x") == 0 || this->num.find("0X") == 0)
base = 16;

auto number = mpfr::mpreal(this->num, mpfr_get_default_prec(), base);
bool sgn = mpfr::signbit(number);
bool flt = !mpfr::isint(number);

Expand All @@ -35,6 +39,7 @@ TCResult ast::LitNumber::typecheck(sst::TypecheckState* fs, fir::Type* infer)

size_t bits = mpfr_min_prec(mpfr::mpreal(this->num.substr(0, this->num.size() - 1) + "9").mpfr_ptr());

printf("number %s: %d / %d\n", number.toString().c_str(), sgn, bits);
auto ret = util::pool<sst::LiteralNumber>(this->loc, (infer && infer->isPrimitiveType()) ? infer : fir::ConstantNumberType::get(sgn, flt, bits));
ret->num = mpfr::mpreal(this->num);

Expand Down

0 comments on commit 5dfc71c

Please sign in to comment.