-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support euros (€) and operations/printing on currencies (#23)
This introduces `LoxValue` and makes `CurrencyValue` a first-class citizen. The binary/unary operators have varying behaviors, `$123 + $245` is OK but `$123 * $245` is not. Whereas `$123 * 2` is fine but `$123 + 2` is not. I don't love the type guards here. It seems like this should work: ```ts type NumsOrStrs = | { left: string; right: string } | { left: number; right: number }; declare let pair: NumsOrStrs; if (typeof pair.left === 'number') { const sum = pair.left + pair.right; // ~~~~~~~~~~~~~~~~~~~~~~ // Operator '+' cannot be applied to types 'number' and 'string | number'. } ```
- Loading branch information
Showing
22 changed files
with
251 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
$100,000 at 7% for 10 years: | ||
196715.1357289567 | ||
$196,715.136 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
$1,234.56 | ||
$3,579 | ||
$617 | ||
$2,468 | ||
$-1,234 | ||
true | ||
€1,234 | ||
€2,468 | ||
€3,579 | ||
true | ||
true | ||
true | ||
false | ||
true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Operands must be the same currency. | ||
[line 1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Operands must be the same currency. | ||
[line 1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Operand must be a number. | ||
[line 1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
print $1,234.56; | ||
print $1,234 + $2,345; | ||
print $1,234 / 2; | ||
print $1,234 * 2; | ||
// print $-1,234; // this is how it prints, but it does not parse. | ||
print -$1,234; | ||
print !!$1,234; | ||
print €1,234; | ||
print €1,234 * 2; | ||
print €1,234 + €2,345; | ||
|
||
print $1,234 > $999; | ||
print $1,234 == $1,234; | ||
print $1,234 >= $1,234; | ||
print $1,234 == €1,234; // false! | ||
print $1,234 != €1,234; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
print $1,234 + €1,234; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
print $1,234 > €1,234; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
print $1,234 * $1,234; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { Interpreter } from "./interpreter.js"; | ||
import { LoxValue } from "./lox-value.js"; | ||
|
||
export abstract class LoxCallable { | ||
abstract arity(): number; | ||
abstract call(interpreter: Interpreter, args: unknown[]): unknown; | ||
abstract call(interpreter: Interpreter, args: LoxValue[]): LoxValue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.