Skip to content

Commit

Permalink
nyan: Allow floats for int operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
heinezen committed Oct 17, 2020
1 parent 05f74f6 commit 1083219
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 28 deletions.
4 changes: 2 additions & 2 deletions nyan/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ bool ASTMemberType::exists() const {
}


ASTMemberArgument::ASTMemberArgument(TokenStream &tokens)
ASTMemberTypeArgument::ASTMemberTypeArgument(TokenStream &tokens)
:
has_key{false} {
auto token = tokens.next();
Expand Down Expand Up @@ -756,7 +756,7 @@ void ASTMemberType::strb(std::ostringstream &builder, int /*indentlevel*/) const
}


void ASTMemberArgument::strb(std::ostringstream &builder, int /*indentlevel*/) const {
void ASTMemberTypeArgument::strb(std::ostringstream &builder, int /*indentlevel*/) const {
if (this->has_key) {
builder << this->key.str() << "=";
}
Expand Down
6 changes: 3 additions & 3 deletions nyan/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ class ASTBase {
/**
* AST representation of a member type argument declaration.
*/
class ASTMemberArgument : public ASTBase {
class ASTMemberTypeArgument : public ASTBase {
friend class ASTMemberType;
friend class Database;
friend class Type;

public:
ASTMemberArgument(TokenStream &tokens);
ASTMemberTypeArgument(TokenStream &tokens);

void strb(std::ostringstream &builder, int indentlevel=0) const override;

Expand Down Expand Up @@ -86,7 +86,7 @@ class ASTMemberType : ASTBase {
IDToken name;

bool has_args;
std::vector<ASTMemberArgument> args;
std::vector<ASTMemberTypeArgument> args;
};


Expand Down
1 change: 0 additions & 1 deletion nyan/member_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class Type;

/**
* Stores information for a member of an Object.
* Also responsible for validating applied operators.
*/
class MemberInfo {
public:
Expand Down
2 changes: 1 addition & 1 deletion nyan/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ bool Type::is_container(container_t type) const {
}


bool Type::is_basic_compatible(const BasicType &type) const {
bool Type::is_basic_type_match(const BasicType &type) const {
return (this->basic_type == type);
}

Expand Down
4 changes: 2 additions & 2 deletions nyan/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ class Type {
bool is_container(container_t type) const;

/**
* Test if the basic type is compatbile, i. e. the same.
* Test if the basic type matches the given type, i. e. it's the same.
*/
bool is_basic_compatible(const BasicType &type) const;
bool is_basic_type_match(const BasicType &type) const;

/**
* Check if this type can be in the given other type.
Expand Down
41 changes: 27 additions & 14 deletions nyan/value/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,39 @@ Float::Number(const IDToken &token) {

template <typename T>
void Number<T>::apply_value(const Value &value, nyan_op operation) {
const Number &change = dynamic_cast<const Number &>(value);
auto applier = [](auto &member_value, auto operand, nyan_op operation) {
switch (operation) {
case nyan_op::ASSIGN:
member_value = operand; break;

switch (operation) {
case nyan_op::ASSIGN:
this->value = change.value; break;
case nyan_op::ADD_ASSIGN:
member_value += operand; break;

case nyan_op::ADD_ASSIGN:
this->value += change.value; break;
case nyan_op::SUBTRACT_ASSIGN:
member_value -= operand; break;

case nyan_op::SUBTRACT_ASSIGN:
this->value -= change.value; break;
case nyan_op::MULTIPLY_ASSIGN:
member_value *= operand; break;

case nyan_op::MULTIPLY_ASSIGN:
this->value *= change.value; break;
case nyan_op::DIVIDE_ASSIGN:
member_value /= operand; break;

case nyan_op::DIVIDE_ASSIGN:
this->value /= change.value; break;
default:
throw Error{"unknown operation requested"};
}
};

default:
throw Error{"unknown operation requested"};
if (typeid(Float&) == typeid(value)) {
const Float &change = dynamic_cast<const Float &>(value);
applier(this->value, change.get(), operation);
}
else if (typeid(Int&) == typeid(value)) {
const Int &change = dynamic_cast<const Int &>(value);
applier(this->value, change.get(), operation);
}
else {
InternalError("expected Number instance for operation, but got"
+ std::string(typeid(value).name()));
}
}

Expand Down
16 changes: 12 additions & 4 deletions nyan/value/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,18 @@ static ValueHolder value_from_value_token(const Type &target_type,
return {std::make_shared<Text>(value_token)};

case primitive_t::INT:
return {std::make_shared<Int>(value_token)};

case primitive_t::FLOAT:
return {std::make_shared<Float>(value_token)};
case primitive_t::FLOAT: {
if (value_token.get_type() == token_type::INT) {
return {std::make_shared<Int>(value_token)};
}
else if (value_token.get_type() == token_type::FLOAT) {
return {std::make_shared<Float>(value_token)};
}
throw LangError{
value_token,
"invalid value for number, expecting float or int"
};
}

case primitive_t::FILENAME: {
// TODO: make relative to current namespace
Expand Down
2 changes: 1 addition & 1 deletion test/test.nyan
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ FirstPatch<First>():
member += 3

Second(First):
member *= 5
member *= 5.5

NestingBase(First):

Expand Down

0 comments on commit 1083219

Please sign in to comment.