Skip to content

Commit

Permalink
Fix issues with constant arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
kaleidawave committed Aug 24, 2023
1 parent 62a16d2 commit 3057e53
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ members = [
"parser/generator",
"checker",
"checker/binary-serialize-derive",
# "checker/specification",
"checker/specification",
]

[package]
Expand Down
11 changes: 11 additions & 0 deletions checker/specification/specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,12 @@ try {

```ts
const x: 4 = 2 + 3
const y: 6 = 2 * 3
const z: 8 = (2 * 3) - 2
```

- Type 5 is not assignable to type 4
- Type 4 is not assignable to type 8

#### Equality

Expand All @@ -258,6 +261,14 @@ const x: 4 = 2 + 3

- Expected number found "HI"

#### Math operations

```ts
Math.cos(0) satisfies 0
```

- Expected 0 found 1

### Objects

#### Property exists
Expand Down
4 changes: 2 additions & 2 deletions checker/src/behavior/constant_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub(crate) fn call_constant_function(
types.new_constant_type(Constant::String(result))
} else {
let lhs = cast_as_number(c1, STRICT_CASTS).unwrap_or(f64::NAN);
let rhs = cast_as_number(c1, STRICT_CASTS).unwrap_or(f64::NAN);
let rhs = cast_as_number(c2, STRICT_CASTS).unwrap_or(f64::NAN);
let value = ordered_float::NotNan::try_from(lhs - rhs);
match value {
Ok(value) => types.new_constant_type(Constant::Number(value)),
Expand All @@ -95,7 +95,7 @@ pub(crate) fn call_constant_function(
match (first, second) {
(Type::Constant(c1), Type::Constant(c2)) => {
let lhs = cast_as_number(c1, STRICT_CASTS).unwrap_or(f64::NAN);
let rhs = cast_as_number(c1, STRICT_CASTS).unwrap_or(f64::NAN);
let rhs = cast_as_number(c2, STRICT_CASTS).unwrap_or(f64::NAN);
let value = ordered_float::NotNan::try_from(lhs * rhs);
let ty = match value {
Ok(value) => types.new_constant_type(Constant::Number(value)),
Expand Down
2 changes: 1 addition & 1 deletion checker/src/synthesis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn parser_binary_operator_to_others(operator: ParserBinaryOperator) -> Binar
ParserBinaryOperator::NotEqual => todo!(),
ParserBinaryOperator::Add => BinaryOperator::Add,
ParserBinaryOperator::Subtract => BinaryOperator::Subtract,
ParserBinaryOperator::Multiply => todo!(),
ParserBinaryOperator::Multiply => BinaryOperator::Multiply,
ParserBinaryOperator::Divide => todo!(),
ParserBinaryOperator::Modulo => todo!(),
ParserBinaryOperator::Exponent => todo!(),
Expand Down
4 changes: 2 additions & 2 deletions checker/src/types/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ pub fn evaluate_binary_operator(
// TODO automate with a macro
let function = match operator {
BinaryOperator::Add => operators.add.as_ref(),
BinaryOperator::Multiply => operators.add.as_ref(),
BinaryOperator::Multiply => operators.mul.as_ref(),
BinaryOperator::Modulo => None,
BinaryOperator::Exponent => None,
BinaryOperator::BitwiseOperators(_) => None,
BinaryOperator::RelationOperator(relation) => match relation {
RelationOperator::Equal => operators.equal.as_ref(),
RelationOperator::GreaterThan => None,
},
BinaryOperator::Subtract => operators.mul.as_ref(),
BinaryOperator::Subtract => operators.sub.as_ref(),
BinaryOperator::Divide => None,
BinaryOperator::LogicalOperator(_) => None,
BinaryOperator::InstanceOf => None,
Expand Down

0 comments on commit 3057e53

Please sign in to comment.