Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[V-122] Bitwise operators #53

Merged
merged 4 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/parser/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ export const infixOps: OpMap = new Map([
["*", 2],
["/", 2],
["^", 3],
["and", 0],
["or", 0],
["xor", 0],
["as", 0],
["is", 0],
["in", 0],
["%", 2],
["==", 0],
["!=", 0],
["<", 0],
Expand All @@ -80,6 +75,14 @@ export const infixOps: OpMap = new Map([
["::", 0],
[";", 4],
["??", 3],
["and", 0],
["or", 0],
["xor", 0],
["as", 0],
["is", 0],
["is_subtype_of", 0],
["in", 0],
["has_trait", 0],
]);

export const isInfixOp = (op?: Expr): op is Identifier =>
Expand Down
85 changes: 75 additions & 10 deletions std/operators.voyd
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,103 @@ pub def_wasm_operator('>', gt_s, i32, bool)
pub def_wasm_operator('<=', le_s, i32, bool)
pub def_wasm_operator('>=', ge_s, i32, bool)
pub def_wasm_operator('==', eq, i32, bool)
pub def_wasm_operator('and', 'and', i32, i32)
pub def_wasm_operator('or', 'or', i32, i32)
pub def_wasm_operator('xor', 'xor', i32, i32)
pub def_wasm_operator('not', ne, i32, bool)
pub def_wasm_operator('!=', ne, i32, bool)
pub def_wasm_operator('+', add, i32, i32)
pub def_wasm_operator('-', sub, i32, i32)
pub def_wasm_operator('*', mul, i32, i32)
pub def_wasm_operator('/', div_s, i32, i32)
pub def_wasm_operator('%', rem_s, i32, i32)
pub def_wasm_operator('rem_u', rem_u, i32, i32)

pub def_wasm_operator('<', lt_s, i64, bool)
pub def_wasm_operator('>', gt_s, i64, bool)
pub def_wasm_operator('<=', le_s, i64, bool)
pub def_wasm_operator('>=', ge_s, i64, bool)
pub def_wasm_operator('==', eq, i64, bool)
pub def_wasm_operator('and', 'and', i64, i64)
pub def_wasm_operator('or', 'or', i64, i64)
pub def_wasm_operator('xor', 'xor', i64, i64)
pub def_wasm_operator('not', ne, i64, bool)
pub def_wasm_operator('!=', ne, i64, bool)
pub def_wasm_operator('+', add, i64, i64)
pub def_wasm_operator('-', sub, i64, i64)
pub def_wasm_operator('*', mul, i64, i64)
pub def_wasm_operator('/', div_s, i64, i64)
pub def_wasm_operator('%', rem_s, i64, i64)
pub def_wasm_operator('rem_u', rem_u, i64, i64)

// Floating-point operators for f32
pub def_wasm_operator('<', lt, f32, bool)
pub def_wasm_operator('>', gt, f32, bool)
pub def_wasm_operator('<=', le, f32, bool)
pub def_wasm_operator('>=', ge, f32, bool)
pub def_wasm_operator('==', eq, f32, bool)
pub def_wasm_operator('not', ne, f32, bool)
pub def_wasm_operator('!=', ne, f32, bool)
pub def_wasm_operator('+', add, f32, f32)
pub def_wasm_operator('-', sub, f32, f32)
pub def_wasm_operator('*', mul, f32, f32)
pub def_wasm_operator('/', div, f32, f32)
pub def_wasm_operator(sqrt, sqrt, f32, f32)
pub def_wasm_operator(trunc, trunc, f32, f32)
pub def_wasm_operator(floor, floor, f32, f32)
pub def_wasm_operator(ceil, ceil, f32, f32)
pub def_wasm_operator(nearest, nearest, f32, f32)
pub def_wasm_operator(min, min, f32, f32)
pub def_wasm_operator(max, max, f32, f32)
pub def_wasm_operator(copysign, copysign, f32, f32)

// Floating-point operators for f64
pub def_wasm_operator('<', lt, f64, bool)
pub def_wasm_operator('>', gt, f64, bool)
pub def_wasm_operator('<=', le, f64, bool)
pub def_wasm_operator('>=', ge, f64, bool)
pub def_wasm_operator('==', eq, f64, bool)
pub def_wasm_operator('not', ne, f64, bool)
pub def_wasm_operator('!=', ne, f64, bool)
pub def_wasm_operator('+', add, f64, f64)
pub def_wasm_operator('-', sub, f64, f64)
pub def_wasm_operator('*', mul, f64, f64)
pub def_wasm_operator('/', div, f64, f64)
pub def_wasm_operator(sqrt, sqrt, f64, f64)
pub def_wasm_operator(trunc, trunc, f64, f64)
pub def_wasm_operator(floor, floor, f64, f64)
pub def_wasm_operator(ceil, ceil, f64, f64)
pub def_wasm_operator(nearest, nearest, f64, f64)
pub def_wasm_operator(min, min, f64, f64)
pub def_wasm_operator(max, max, f64, f64)
pub def_wasm_operator(copysign, copysign, f64, f64)

// Bitwise operators
// i32 bitwise operators
pub def_wasm_operator(shift_l, shl, i32, i32)
pub def_wasm_operator(shift_r, shr_s, i32, i32)
pub def_wasm_operator(shift_lu, shl, i32, i32)
pub def_wasm_operator(shift_ru, shr_u, i32, i32)
pub def_wasm_operator(rotate_l, rotl, i32, i32)
pub def_wasm_operator(rotate_r, rotr, i32, i32)
pub def_wasm_operator(count_leading_zeros, clz, i32, i32)
pub def_wasm_operator(count_trailing_zeros, ctz, i32, i32)
pub def_wasm_operator(pop_count, popcnt, i32, i32)
pub def_wasm_operator(lt_u, lt_u, i32, bool)
pub def_wasm_operator(gt_u, gt_u, i32, bool)
pub def_wasm_operator(lte_u, le_u, i32, bool)
pub def_wasm_operator(gte_u, ge_u, i32, bool)
pub def_wasm_operator(bit_and, 'and', i32, i32)
pub def_wasm_operator(bit_or, 'or', i32, i32)
pub def_wasm_operator(bit_xor, 'xor', i32, i32)

// i64 bitwise operators
pub def_wasm_operator(shift_l, shl, i64, i64)
pub def_wasm_operator(shift_r, shr_s, i64, i64)
pub def_wasm_operator(shift_lu, shl, i64, i64)
pub def_wasm_operator(shift_ru, shr_u, i64, i64)
pub def_wasm_operator(rotate_l, rotl, i64, i64)
pub def_wasm_operator(rotate_r, rotr, i64, i64)
pub def_wasm_operator(count_leading_zeros, clz, i64, i64)
pub def_wasm_operator(count_trailing_zeros, ctz, i64, i64)
pub def_wasm_operator(pop_count, popcnt, i64, i64)
pub def_wasm_operator(lt_u, lt_u, i64, bool)
pub def_wasm_operator(gt_u, gt_u, i64, bool)
pub def_wasm_operator(lte_u, le_u, i64, bool)
pub def_wasm_operator(gte_u, ge_u, i64, bool)
pub def_wasm_operator(bit_and, 'and', i64, i64)
pub def_wasm_operator(bit_or, 'or', i64, i64)
pub def_wasm_operator(bit_xor, 'xor', i64, i64)

pub fn '=='(left: bool, right: bool) -> bool
binaryen
Expand All @@ -76,3 +129,15 @@ pub fn 'or'(left: bool, right: bool) -> bool
func: 'or'
namespace: i32
args: [left, right]

pub fn 'xor'(left: bool, right: bool) -> bool
binaryen
func: 'xor'
namespace: i32
args: [left, right]

pub fn not(value: bool) -> bool
binaryen
func: eqz
namespace: i32
args: [value]