Skip to content

Commit e272450

Browse files
authored
lex operation: largest exponent (#350)
1 parent 94e96a3 commit e272450

File tree

10 files changed

+123
-24
lines changed

10 files changed

+123
-24
lines changed

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ To install or update LODA, please follow the [installation instructions](https:/
22

33
## [Unreleased]
44

5+
## v24.10.20
6+
7+
### Features
8+
9+
* Added `lex` operation (largest exponent)
10+
511
## v24.9.15
612

713
### Bugfixes
814

9-
* Fix optimizer bug
10-
* Fix program folding bug
15+
* Fix optimizer bugs
16+
* Fix program folding bugs
1117

1218
## v24.9.7
1319

src/cmd/test.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -588,15 +588,11 @@ void Test::programUtil() {
588588
true);
589589
}
590590
p = parser.parse(ProgramUtil::getProgramPath(45));
591-
auto h = ProgramUtil::hash(p);
592-
size_t expected_hash = 12279585564253383018ULL;
593-
if (h != expected_hash) {
594-
Log::get().error("Unexpected program hash: " + std::to_string(h), true);
595-
}
591+
auto h1 = ProgramUtil::hash(p);
596592
ProgramUtil::removeOps(p, Operation::Type::NOP);
597-
h = ProgramUtil::hash(p);
598-
if (h != expected_hash) {
599-
Log::get().error("Unexpected program hash: " + std::to_string(h), true);
593+
auto h2 = ProgramUtil::hash(p);
594+
if (h2 != h1) {
595+
Log::get().error("Unexpected program hash: " + std::to_string(h2), true);
600596
}
601597
}
602598

src/eval/interpreter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ Number Interpreter::calc(const Operation::Type type, const Number& target,
6565
case Operation::Type::GCD: {
6666
return Semantics::gcd(target, source);
6767
}
68+
case Operation::Type::LEX: {
69+
return Semantics::lex(target, source);
70+
}
6871
case Operation::Type::BIN: {
6972
return Semantics::bin(target, source);
7073
}

src/eval/semantics.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,27 @@ Number Semantics::gcd(const Number& a, const Number& b) {
106106
return aa;
107107
}
108108

109+
Number Semantics::lex(const Number& a, const Number& b) {
110+
if (a == Number::INF || b == Number::INF) {
111+
return Number::INF;
112+
}
113+
if (b == Number::ZERO || b == Number::ONE) {
114+
return Number::ZERO;
115+
}
116+
auto r = Number::ZERO;
117+
auto aa = abs(a);
118+
auto bb = abs(b);
119+
while (true) {
120+
auto aaa = dif(aa, bb);
121+
if (aaa == aa) {
122+
break;
123+
}
124+
aa = aaa;
125+
r += Number::ONE;
126+
}
127+
return r;
128+
}
129+
109130
Number Semantics::bin(const Number& nn, const Number& kk) {
110131
if (nn == Number::INF || kk == Number::INF) {
111132
return Number::INF;

src/eval/semantics.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class Semantics {
2222

2323
static Number gcd(const Number& a, const Number& b);
2424

25+
static Number lex(const Number& a, const Number& b);
26+
2527
static Number bin(const Number& n, const Number& k);
2628

2729
static Number log(const Number& a, const Number& b);

src/lang/program.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
#include <stdexcept>
44

5-
const std::array<Operation::Type, 32> Operation::Types = {
5+
const std::array<Operation::Type, 33> Operation::Types = {
66
Operation::Type::NOP, Operation::Type::MOV, Operation::Type::ADD,
77
Operation::Type::SUB, Operation::Type::TRN, Operation::Type::MUL,
88
Operation::Type::DIV, Operation::Type::DIF, Operation::Type::MOD,
9-
Operation::Type::POW, Operation::Type::GCD, Operation::Type::BIN,
10-
Operation::Type::LOG, Operation::Type::NRT, Operation::Type::DIS,
11-
Operation::Type::DIR, Operation::Type::EQU, Operation::Type::NEQ,
12-
Operation::Type::LEQ, Operation::Type::GEQ, Operation::Type::MIN,
13-
Operation::Type::MAX, Operation::Type::BAN, Operation::Type::BOR,
14-
Operation::Type::BXO, Operation::Type::LPB, Operation::Type::LPE,
15-
Operation::Type::CLR, Operation::Type::SRT, Operation::Type::SEQ,
16-
Operation::Type::PRG, Operation::Type::DBG,
9+
Operation::Type::POW, Operation::Type::GCD, Operation::Type::LEX,
10+
Operation::Type::BIN, Operation::Type::LOG, Operation::Type::NRT,
11+
Operation::Type::DIS, Operation::Type::DIR, Operation::Type::EQU,
12+
Operation::Type::NEQ, Operation::Type::LEQ, Operation::Type::GEQ,
13+
Operation::Type::MIN, Operation::Type::MAX, Operation::Type::BAN,
14+
Operation::Type::BOR, Operation::Type::BXO, Operation::Type::LPB,
15+
Operation::Type::LPE, Operation::Type::CLR, Operation::Type::SRT,
16+
Operation::Type::SEQ, Operation::Type::PRG, Operation::Type::DBG,
1717
};
1818

1919
const Operation::Metadata& Operation::Metadata::get(Type t) {
@@ -39,6 +39,8 @@ const Operation::Metadata& Operation::Metadata::get(Type t) {
3939
Operation::Type::POW, "pow", 2, true, true, true};
4040
static Operation::Metadata gcd{
4141
Operation::Type::GCD, "gcd", 2, true, true, true};
42+
static Operation::Metadata lex{
43+
Operation::Type::LEX, "lex", 2, true, true, true};
4244
static Operation::Metadata bin{
4345
Operation::Type::BIN, "bin", 2, true, true, true};
4446
static Operation::Metadata log{
@@ -104,6 +106,8 @@ const Operation::Metadata& Operation::Metadata::get(Type t) {
104106
return pow;
105107
case Operation::Type::GCD:
106108
return gcd;
109+
case Operation::Type::LEX:
110+
return lex;
107111
case Operation::Type::BIN:
108112
return bin;
109113
case Operation::Type::LOG:

src/lang/program.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Operation {
4545
MOD, // modulo
4646
POW, // power
4747
GCD, // greatest common divisor
48+
LEX, // largest exponent
4849
BIN, // binomial coefficient
4950
LOG, // logarithm
5051
NRT, // n-th root
@@ -69,7 +70,7 @@ class Operation {
6970
__COUNT // number of operation types
7071
};
7172

72-
static const std::array<Type, 32> Types;
73+
static const std::array<Type, 33> Types;
7374

7475
class Metadata {
7576
public:

src/mine/generator.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ void Generator::ensureMeaningfulLoops(Program &p) {
264264
case Operation::Type::DIF:
265265
case Operation::Type::MOD:
266266
case Operation::Type::GCD:
267+
case Operation::Type::LEX:
267268
case Operation::Type::BIN:
268269
case Operation::Type::EQU:
269270
case Operation::Type::NEQ:

src/mine/iterator.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ bool Iterator::inc(Operation& op) {
9393
return true;
9494

9595
case Operation::Type::GCD:
96+
op.type = Operation::Type::LEX;
97+
return true;
98+
99+
case Operation::Type::LEX:
96100
op.type = Operation::Type::BIN;
97101
return true;
98102

@@ -173,20 +177,22 @@ bool Iterator::shouldSkip(const Operation& op) {
173177
op.type == Operation::Type::TRN || op.type == Operation::Type::MUL ||
174178
op.type == Operation::Type::DIV || op.type == Operation::Type::DIF ||
175179
op.type == Operation::Type::MOD || op.type == Operation::Type::GCD ||
176-
op.type == Operation::Type::BIN || op.type == Operation::Type::EQU ||
177-
op.type == Operation::Type::NEQ)) {
180+
op.type == Operation::Type::LEX || op.type == Operation::Type::BIN ||
181+
op.type == Operation::Type::EQU || op.type == Operation::Type::NEQ)) {
178182
return true;
179183
}
180184
if (op.source == CONSTANT_ZERO &&
181185
(op.type == Operation::Type::MUL || op.type == Operation::Type::DIV ||
182186
op.type == Operation::Type::DIF || op.type == Operation::Type::MOD ||
183187
op.type == Operation::Type::POW || op.type == Operation::Type::GCD ||
184-
op.type == Operation::Type::BIN || op.type == Operation::Type::LPB)) {
188+
op.type == Operation::Type::LEX || op.type == Operation::Type::BIN ||
189+
op.type == Operation::Type::LPB)) {
185190
return true;
186191
}
187192
if (op.source == CONSTANT_ONE &&
188193
(op.type == Operation::Type::MOD || op.type == Operation::Type::POW ||
189-
op.type == Operation::Type::GCD || op.type == Operation::Type::BIN)) {
194+
op.type == Operation::Type::GCD || op.type == Operation::Type::LEX ||
195+
op.type == Operation::Type::BIN)) {
190196
return true;
191197
}
192198

tests/semantics/lex.csv

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Op1,Op2,Result
2+
-4,-5,0
3+
-4,-4,1
4+
-4,-3,0
5+
-4,-2,2
6+
-4,-1,0
7+
-4,0,0
8+
-4,1,0
9+
-4,2,2
10+
-4,3,0
11+
-4,4,1
12+
-4,5,0
13+
-4,6,0
14+
-4,7,0
15+
-4,8,0
16+
0,-2,0
17+
0,-1,0
18+
0,0,0
19+
0,1,0
20+
0,2,0
21+
1,-1,0
22+
1,-1,0
23+
1,0,0
24+
1,1,0
25+
1,2,0
26+
2,-3,0
27+
2,-2,1
28+
2,-1,0
29+
2,0,0
30+
2,1,0
31+
2,2,1
32+
2,3,0
33+
2,4,0
34+
4,-5,0
35+
4,-4,1
36+
4,-3,0
37+
4,-2,2
38+
4,-1,0
39+
4,0,0
40+
4,1,0
41+
4,2,2
42+
4,3,0
43+
4,4,1
44+
4,5,0
45+
9,1,0
46+
9,2,0
47+
9,3,2
48+
9,4,0
49+
9,5,0
50+
9,9,1
51+
9,10,0
52+
16,2,4
53+
16,4,2
54+
36,2,2
55+
36,3,2
56+
36,4,1
57+
36,5,0
58+
36,6,2
59+
36,7,0

0 commit comments

Comments
 (0)