Skip to content

Commit 601b15a

Browse files
committed
add tan/asin/acos/atan
1 parent 0f32abf commit 601b15a

File tree

11 files changed

+66
-22
lines changed

11 files changed

+66
-22
lines changed

build/gen_macros.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ pub fn gen_ast_macros() -> String {
9999
"expr_unop", &[
100100
("op", ArgKind::Token(&[
101101
"-", "!",
102-
"sin", "cos", "sqrt",
102+
"sin", "cos", "tan",
103+
"asin", "acos", "atan",
104+
"sqrt",
103105
"_S", "_f", // $ has to be written as _S since it's a macro meta-char
104106
"%", "int", "float",
105107
])),

doc/syntax.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ Literals can be:
4141

4242
* **Integers:** `123`, `0x123`, `-0b00101`, `true`, `false`. There is no octal syntax. Literal numbers have no optional leading plus.
4343
* **Floats:** `1.0`, `-1.3f`, `2f`. Again, no leading plus.
44-
* **Strings:** `"The quick brown fox\njumped over the lazy dog"`. The control characters for strings are `\0`, `\n`, `\r`, `\\`, and `\"`.
44+
* **Strings:** `"The quick brown fox\njumped over the lazy dog"`. The control characters for strings are `\0`, `\n`, `\r`, `\\`, and `\"`.
4545

46-
Source text files for `truth` **must** be encoded in UTF-8. `truth` handles the conversion between the Shift-JIS encoding used in the binary files and the UTF-8 encoding used in source text. (in the future, you may even be able to configure which encoding is used in the binary files... but source text will always be UTF-8!)
46+
Source text files for `truth` **must** be encoded in UTF-8. `truth` handles the conversion between the Shift-JIS encoding used in the binary files and the UTF-8 encoding used in source text. (in the future, you may even be able to configure which encoding is used in the binary files... but source text will always be UTF-8!)
4747

4848
### Variables
4949

@@ -139,12 +139,12 @@ Other things present in expressions:
139139
140140
* The **ternary operator** `a ? b : c`. Right associative, [the way it should be](https://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/#operators).
141141
* **Unary negation** `-x`.
142-
* **Special functions** `sin(x)` and `cos(x)`.
142+
* **Special functions** `sin(x)` and `cos(x)`. Some languages support `acos`, `asin`, `tan`, and `sqrt`.
143143
* **Logical right shift `>>>`**.
144144
* All ints in truth are signed so `>>` is an arithmetic right shift (sign-extended) for consistency.
145145
`>>>` is provided as a separate operator for doing logical right shifts (zero-extended).
146146
* **Explicit casts** `_S` (float to int) and `_f` (int to float): `I0 = _S(F0);`
147-
* That example is identical to `I0 = $F0`, but `_S` is also more generally usable in larger expressions where it may introduce a temporary
147+
* That example is identical to `I0 = $F0`, but `_S` is also more generally usable in larger expressions where it may introduce a temporary
148148
149149
### Assignments
150150
@@ -217,7 +217,7 @@ times(I2 = I0) {
217217
```
218218
219219
#### About conditions
220-
220+
221221
In the desugared form of `times()` above, you'll notice that there is a `while (--var)`. This `--var` is not an expression!
222222
223223
Basically, a **condition** can either be an expression `<expr>` or a predecrement operation `--<var>`. The latter compiles to a special kind of jump available in ANM and early ECL that decrements a variable and jumps if it is nonzero. (*every single `while` loop in vanilla ANM files uses this form of jump,* so the ability to decompile it seemed... kind of important!)
@@ -290,7 +290,7 @@ Notice that, unlike conditional blocks, *the conditional jump has no braces*; it
290290
```C
291291
// this compiles to a single instruction in anm
292292
if (I0 != 0) goto else_label;
293-
293+
294294
// this typically compiles to more than one instruction
295295
if (I0 != 0) {
296296
goto else_label;
@@ -317,7 +317,7 @@ However, this is generally obsolete. Normally, the only reason the game ever us
317317
label: // has time label 15
318318
bar()
319319
goto label @ 10;
320-
320+
321321

322322
// is equivalent to
323323
10:

map/v2.anmm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
61 fsin
7272
62 fcos
7373
63 ftan
74-
64 acos
74+
64 facos
7575
65 fatan
7676
66 validRad
7777

src/ast/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,10 @@ string_enum! {
695695
#[strum(serialize = "~")] BitNot,
696696
#[strum(serialize = "sin")] Sin,
697697
#[strum(serialize = "cos")] Cos,
698+
#[strum(serialize = "tan")] Tan,
699+
#[strum(serialize = "asin")] Asin,
700+
#[strum(serialize = "acos")] Acos,
701+
#[strum(serialize = "atan")] Atan,
698702
#[strum(serialize = "sqrt")] Sqrt,
699703
#[strum(serialize = "$")] EncodeI,
700704
#[strum(serialize = "%")] EncodeF,
@@ -711,6 +715,10 @@ impl UnOpKind {
711715
UnOpKind::BitNot => OpClass::Bitwise,
712716
UnOpKind::Sin => OpClass::FloatMath,
713717
UnOpKind::Cos => OpClass::FloatMath,
718+
UnOpKind::Tan => OpClass::FloatMath,
719+
UnOpKind::Asin => OpClass::FloatMath,
720+
UnOpKind::Acos => OpClass::FloatMath,
721+
UnOpKind::Atan => OpClass::FloatMath,
714722
UnOpKind::Sqrt => OpClass::FloatMath,
715723
UnOpKind::CastI => OpClass::Cast,
716724
UnOpKind::CastF => OpClass::Cast,

src/core_mapfiles/anm.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ static ANM_INS_07_09: &'static CoreSignatures = &CoreSignatures {
150150
(Th07, 60, Some(("ff", None))),
151151
(Th07, 61, Some(("ff", Some(IKind::UnOp(U::Sin, Ty::Float))))),
152152
(Th07, 62, Some(("ff", Some(IKind::UnOp(U::Cos, Ty::Float))))),
153-
(Th07, 63, Some(("ff", None))),
154-
(Th07, 64, Some(("ff", None))),
155-
(Th07, 65, Some(("ff", None))),
153+
(Th07, 63, Some(("ff", Some(IKind::UnOp(U::Tan, Ty::Float))))),
154+
(Th07, 64, Some(("ff", Some(IKind::UnOp(U::Acos, Ty::Float))))),
155+
(Th07, 65, Some(("ff", Some(IKind::UnOp(U::Atan, Ty::Float))))),
156156
(Th07, 66, Some(("f", None))),
157157
(Th07, 67, Some(("SSot", Some(IKind::CondJmp(B::Eq, Ty::Int))))),
158158
(Th07, 68, Some(("ffot", Some(IKind::CondJmp(B::Eq, Ty::Float))))),
@@ -235,9 +235,9 @@ static ANM_INS_095_128: &'static CoreSignatures = &CoreSignatures {
235235
(Th095, 41, Some(("ff", None))),
236236
(Th095, 42, Some(("ff", Some(IKind::UnOp(U::Sin, Ty::Float))))),
237237
(Th095, 43, Some(("ff", Some(IKind::UnOp(U::Cos, Ty::Float))))),
238-
(Th095, 44, Some(("ff", None))),
239-
(Th095, 45, Some(("ff", None))),
240-
(Th095, 46, Some(("ff", None))),
238+
(Th095, 44, Some(("ff", Some(IKind::UnOp(U::Tan, Ty::Float))))),
239+
(Th095, 45, Some(("ff", Some(IKind::UnOp(U::Acos, Ty::Float))))),
240+
(Th095, 46, Some(("ff", Some(IKind::UnOp(U::Atan, Ty::Float))))),
241241
(Th095, 47, Some(("f", None))),
242242
(Th095, 48, Some(("fff", None))),
243243
(Th095, 49, Some(("fff", None))),
@@ -353,9 +353,9 @@ static ANM_INS_13_185: &CoreSignatures = &CoreSignatures {
353353
(Th13, 123, Some(("ff", None))),
354354
(Th13, 124, Some(("ff", Some(IKind::UnOp(U::Sin, Ty::Float))))),
355355
(Th13, 125, Some(("ff", Some(IKind::UnOp(U::Cos, Ty::Float))))),
356-
(Th13, 126, Some(("ff", None))),
357-
(Th13, 127, Some(("ff", None))),
358-
(Th13, 128, Some(("ff", None))),
356+
(Th13, 126, Some(("ff", Some(IKind::UnOp(U::Tan, Ty::Float))))),
357+
(Th13, 127, Some(("ff", Some(IKind::UnOp(U::Acos, Ty::Float))))),
358+
(Th13, 128, Some(("ff", Some(IKind::UnOp(U::Atan, Ty::Float))))),
359359
(Th13, 129, Some(("f", None))),
360360
(Th13, 130, Some(("ffff", None))),
361361
(Th13, 131, Some(("ffff", None))),

src/fmt.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -886,12 +886,14 @@ impl Format for ast::Expr {
886886
})
887887
},
888888
ast::Expr::UnOp(op, x) => match op.value {
889-
token![unop -] | token![!] | token![~]
889+
| token![unop -] | token![!] | token![~]
890890
=> out.fmt_optional_parens(|out| out.fmt((op, x))),
891891

892-
token![unop $] | token![unop %] |
893-
token![unop int] | token![unop float] |
894-
token![sin] | token![cos] | token![sqrt]
892+
| token![unop $] | token![unop %]
893+
| token![unop int] | token![unop float]
894+
| token![sin] | token![cos] | token![tan]
895+
| token![asin] | token![acos] | token![atan]
896+
| token![sqrt]
895897
=> out.fmt((op, "(", SuppressParens(x), ")")),
896898
},
897899
ast::Expr::XcrementOp { order: ast::XcrementOpOrder::Pre, op, var } => out.fmt((op, var)),

src/parse/lalrparser.lalrpop

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ extern {
107107
"timeof" => Token::TimeOf,
108108
"sin" => Token::Sin,
109109
"cos" => Token::Cos,
110+
"tan" => Token::Tan,
111+
"asin" => Token::Asin,
112+
"acos" => Token::Acos,
113+
"atan" => Token::Atan,
110114
"sqrt" => Token::Sqrt,
111115
"_S" => Token::LegacyEncodeI,
112116
"_f" => Token::LegacyEncodeF,
@@ -570,6 +574,10 @@ OpXcrement: ast::XcrementOpKind = {
570574
FuncUnOpKeyword: ast::UnOpKind = {
571575
"sin" => token![sin],
572576
"cos" => token![cos],
577+
"tan" => token![tan],
578+
"asin" => token![asin],
579+
"acos" => token![acos],
580+
"atan" => token![atan],
573581
"sqrt" => token![sqrt],
574582
// FIXME: differentiate read sigils from type casts
575583
"_S" => token![$],

src/parse/lexer.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ define_token_enum! {
9999
#[token("timeof")] TimeOf,
100100
#[token("sin")] Sin,
101101
#[token("cos")] Cos,
102+
#[token("tan")] Tan,
103+
#[token("asin")] Asin,
104+
#[token("acos")] Acos,
105+
#[token("atan")] Atan,
102106
#[token("sqrt")] Sqrt,
103107
#[token("_S")] LegacyEncodeI,
104108
#[token("_f")] LegacyEncodeF,

src/passes/const_simplify.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ impl ast::UnOpKind {
5151
token![unop ~] => Some(ScalarValue::Int(!x)),
5252
token![unop sin] |
5353
token![unop cos] |
54+
token![unop tan] |
55+
token![unop asin] |
56+
token![unop acos] |
57+
token![unop atan] |
5458
token![unop sqrt] => uncaught_type_error(),
5559
token![unop int] => Some(ScalarValue::Int(x)),
5660
token![unop float] => Some(ScalarValue::Float(x as f32)),
@@ -64,6 +68,10 @@ impl ast::UnOpKind {
6468
token![unop ~] => uncaught_type_error(),
6569
token![unop sin] => Some(ScalarValue::Float(x.sin())),
6670
token![unop cos] => Some(ScalarValue::Float(x.cos())),
71+
token![unop tan] => Some(ScalarValue::Float(x.tan())),
72+
token![unop asin] => Some(ScalarValue::Float(x.asin())),
73+
token![unop acos] => Some(ScalarValue::Float(x.acos())),
74+
token![unop atan] => Some(ScalarValue::Float(x.atan())),
6775
token![unop sqrt] => Some(ScalarValue::Float(x.sqrt())),
6876
token![unop int] => Some(ScalarValue::Int(x as i32)),
6977
token![unop float] => Some(ScalarValue::Float(x)),

src/passes/type_check.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,10 @@ impl ExprTypeChecker<'_, '_> {
652652

653653
| token![unop sin]
654654
| token![unop cos]
655+
| token![unop tan]
656+
| token![unop asin]
657+
| token![unop acos]
658+
| token![unop atan]
655659
| token![unop sqrt]
656660
=> self.require_float(arg_ty, op.span, arg_span),
657661
}
@@ -681,6 +685,10 @@ impl ast::Expr {
681685

682686
token![unop sin] |
683687
token![unop cos] |
688+
token![unop tan] |
689+
token![unop asin] |
690+
token![unop acos] |
691+
token![unop atan] |
684692
token![unop sqrt] => ScalarType::Float,
685693

686694
token![unop $] => ScalarType::Int,

0 commit comments

Comments
 (0)