Skip to content

Commit

Permalink
Enums in compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
cgswords authored and tzakian committed Jan 24, 2024
1 parent 2273133 commit 721f9fa
Show file tree
Hide file tree
Showing 151 changed files with 9,067 additions and 585 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 2 tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//# init --edition 2024.alpha

//# publish
module 0x42::m {

public enum ABC<T> has drop {
A(T),
B,
C(T)
}

fun t0(): u64 {
match (ABC::C(0)) {
ABC::C(x) => x,
ABC::A(x) => x,
ABC::B => 1,
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 2 tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//# init --edition 2024.alpha

//# publish
module 0x42::m {

public enum ABC<T> has drop {
A(T),
B,
C(T)
}

fun t0(x: ABC<u64>): ABC<u64> {
match (x) {
ABC::C(c) => ABC::C(c),
ABC::A(a) => ABC::A(a),
y => y,
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 2 tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//# init --edition 2024.alpha

//# publish
module 0x42::m {

public enum ABC<T> has drop {
A(T),
B,
C(T)
}

fun t0(abc: &mut ABC<u64>, default: &mut u64): &mut u64 {
match (abc) {
ABC::C(x) => x,
ABC::A(x) => x,
ABC::B => default,
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 2 tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//# init --edition 2024.alpha

//# publish
module 0x42::m {

public enum ABC<T> {
A(T),
B,
C(T)
}

fun t0(): u64 {
match (ABC::C(0)) {
ABC::C(x) => x,
ABC::A(x) => x,
ABC::B => 1,
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 2 tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//# init --edition 2024.alpha

//# publish
module 0x42::m {

public enum ABC<T> has drop {
A(T),
B,
C(T)
}

fun t0(abc: &ABC<u64>, default: &u64): &u64 {
match (abc) {
ABC::C(x) => x,
ABC::A(x) => x,
ABC::B => default,
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 2 tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//# init --edition 2024.alpha

//# publish
module 0x42::m {

public enum ABC<T> has drop {
A(T),
B,
C(T)
}

fun t0(): u64 {
let default = 1;
let x = ABC::C(0);
let y = match (&x) {
ABC::C(x) => x,
ABC::A(x) => x,
ABC::B => &default,
};
let y = *y;
let z = match (x) {
ABC::C(x) => y + x,
ABC::A(x) => y + x,
ABC::B => y,
};
z
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 2 tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//# init --edition 2024.alpha

//# publish
module 0x42::m {

public enum ABC<T> has drop {
A(T),
B,
C(T)
}

fun t0(): u64 {
match (ABC::C(0)) {
ABC::C(x) => x,
_ => 1,
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 3 tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//# init --edition 2024.alpha

//# publish

module 0x42::t {

use std::vector;
use fun std::vector::pop_back as vector.pop_back;
use fun std::vector::push_back as vector.push_back;
use fun std::vector::is_empty as vector.is_empty;

public enum Exp has drop {
Done,
Add,
Mul,
Num(u64),
}

const EINVALIDEXP: u64 = 0;

public fun evaluate(mut expressions: vector<Exp>): u64 {
let mut stack = vector[];
while (!expressions.is_empty()) {
match (expressions.pop_back()) {
Exp::Done => break,
Exp::Add => {
let e1 = stack.pop_back();
let e2 = stack.pop_back();
stack.push_back(e1 + e2);
},
Exp::Mul => {
let e1 = stack.pop_back();
let e2 = stack.pop_back();
stack.push_back(e1 * e2);
},
Exp::Num(number) => {
stack.push_back(number);
}
}
};
let result = vector::pop_back(&mut stack);
assert!(expressions.is_empty(), EINVALIDEXP);
assert!(stack.is_empty(), EINVALIDEXP);
result
}

use fun evaluate as vector.evaluate;

public fun test() {
let input = vector[Exp::Done, Exp::Add, Exp::Num(5), Exp::Num(5)];
assert!(input.evaluate() == 10, 0);
}

}

//# run 0x42::t::test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 2 tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//# init --edition 2024.alpha

//# publish
module 0x42::m {

public enum Either<L,R> has drop {
Left(L),
Right(R),
}

fun t0(in: Either<u64, u64>): u64 {
match (in) {
Either::Left(y) => y,
Either::Right(y) => y,
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 2 tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//# init --edition 2024.alpha

//# publish
module 0x42::m {

public enum Threes has drop {
One(u64),
Two(u64, u64),
Three(u64, u64, u64)
}

fun t0(): u64 {
match (Threes::One(0)) {
Threes::Three(x, _, _) => x,
Threes::One(x) => x,
Threes::Two(_, _) => 1,
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 2 tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//# init --edition 2024.alpha

//# publish
module 0x42::m {

public enum Threes<T> has drop {
Zero,
One(T),
Two(T, T),
Three(T, T, T)
}

fun t0(): u64 {
match (Threes::One(0)) {
Threes::Three(x, _, _) => x,
Threes::One(x) => x,
Threes::Two(_, _) => 1,
Threes::Zero => 64,
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 3 tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//# init --edition 2024.alpha

//# publish
module 0x42::m {

public enum ABC<T> has drop {
A(T),
B,
C(T)
}

fun fib(x: u64): u64 {
match (x) {
0 => 1,
1 => 1,
x => fib(x-1) + fib(x-2),
}
}

fun test() {
assert!(fib(5) == 8, 0);
}

}

//# run 0x42::m::test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 2 tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//# init --edition 2024.alpha

//# publish
module 0x42::m {

public enum ABC<Q,R> has drop {
A(Q,R),
B,
C(Q,R),
}

public enum QEither<L,R> has drop {
Left(L),
Right(R),
}

fun t0(in: ABC<u64, QEither<u64, u64>>): u64 {
match (in) {
ABC::C(x, QEither::Left(y)) => x + y,
ABC::C(x, QEither::Right(y)) => x + y,
ABC::A(b, QEither::Left(d)) => b + d,
ABC::A(c, QEither::Right(e)) => c + e,
ABC::B => 1,
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 2 tasks
Loading

0 comments on commit 721f9fa

Please sign in to comment.