bitwise
This commit is contained in:
parent
2d0c39cd85
commit
0021ce04ac
12 changed files with 134 additions and 15 deletions
|
|
@ -67,7 +67,7 @@ ODE_LIBS=$(pkg-config --libs ode 2>/dev/null || echo "-lode -lm")
|
|||
|
||||
# Compile C to executable with libsuicmez, raylib, and ODE support
|
||||
echo "Compiling C code and linking with libsuicmez, raylib, and ODE..."
|
||||
gcc $OPTIMIZE_FLAG -I. -Ilibfishsoup/include "$C_FILE" libsuicmez/libsuicmez.c libsuicmez/suicmez_gc.c libfishsoup/src/*.c $RAYLIB_CFLAGS $RAYLIB_LIBS $ODE_CFLAGS $ODE_LIBS -lm -o "$OUTPUT_BINARY" || exit 1
|
||||
gcc $OPTIMIZE_FLAG -I. -Ilibfishsoup/include "$C_FILE" libsuicmez/libsuicmez.c libsuicmez/suicmez_gc.c libfishsoup/src/*.c $RAYLIB_CFLAGS $ODE_CFLAGS -o "$OUTPUT_BINARY" $RAYLIB_LIBS $ODE_LIBS -lz -lm || exit 1
|
||||
|
||||
echo "✓ Successfully created: $OUTPUT_BINARY"
|
||||
echo " Run with: ./$OUTPUT_BINARY"
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@ RAYLIB_LIBS=$(pkg-config --libs raylib 2>/dev/null || echo "-lraylib -lm")
|
|||
ODE_CFLAGS=$(pkg-config --cflags ode 2>/dev/null || echo "-I/usr/include")
|
||||
ODE_LIBS=$(pkg-config --libs ode 2>/dev/null || echo "-lode -lm")
|
||||
|
||||
if ! gcc $OPTIMIZE_FLAG -I. -Ilibfishsoup/include "$C_FILE" libsuicmez/libsuicmez.c libsuicmez/suicmez_gc.c libfishsoup/src/*.c $RAYLIB_CFLAGS $RAYLIB_LIBS $ODE_CFLAGS $ODE_LIBS -lm -o "$OUTPUT_BINARY" 2>&1; then
|
||||
if ! gcc $OPTIMIZE_FLAG -I. -Ilibfishsoup/include "$C_FILE" libsuicmez/libsuicmez.c libsuicmez/suicmez_gc.c libfishsoup/src/*.c $RAYLIB_CFLAGS $ODE_CFLAGS -o "$OUTPUT_BINARY" $RAYLIB_LIBS $ODE_LIBS -lz -lm 2>&1; then
|
||||
echo ""
|
||||
echo "✗ C compilation failed"
|
||||
exit 1
|
||||
|
|
|
|||
|
|
@ -29,19 +29,12 @@ int suic_main(void);
|
|||
|
||||
|
||||
int suic_main(void) {
|
||||
char* GAME_NAME = suic_alloc_array(NULL, sizeof(char), 5, "Soup");
|
||||
char* GAME_VERSION = suic_alloc_array(NULL, sizeof(char), 6, "0.1.0");
|
||||
int WINDOW_WIDTH = 1280;
|
||||
int WINDOW_HEIGHT = 720;
|
||||
char* WINDOW_TITLE = suic_alloc_array(NULL, sizeof(char), 27, "Soup - Voxel Battle Royale");
|
||||
char* SERVER_HOST = suic_alloc_array(NULL, sizeof(char), 10, "127.0.0.1");
|
||||
int SERVER_PORT = 8888;
|
||||
suic_init_window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE);
|
||||
suic_init_window(1280, 720, suic_alloc_array(NULL, sizeof(char), 27, "Soup - Voxel Battle Royale"));
|
||||
suic_enable_msaa_4x();
|
||||
int current_screen = 0;
|
||||
char* player_name = suic_alloc_array(NULL, sizeof(char), 7, "Player");
|
||||
char* server_host = SERVER_HOST;
|
||||
int server_port = SERVER_PORT;
|
||||
char* server_host = suic_alloc_array(NULL, sizeof(char), 10, "127.0.0.1");
|
||||
int server_port = 8888;
|
||||
suic_show_fps_meter((suic_vec2){ .x = 10.000000, .y = 10.000000 });
|
||||
while ((suic_window_should_close() == false)) {
|
||||
suic_begin_drawing();
|
||||
|
|
|
|||
|
|
@ -260,6 +260,8 @@ pub enum BinOp {
|
|||
Mod,
|
||||
And,
|
||||
Or,
|
||||
BitwiseAnd,
|
||||
BitwiseOr,
|
||||
Eq,
|
||||
Neq,
|
||||
Lt,
|
||||
|
|
|
|||
|
|
@ -114,6 +114,8 @@ pub enum CBinaryOp {
|
|||
Geq,
|
||||
And,
|
||||
Or,
|
||||
BitwiseAnd,
|
||||
BitwiseOr,
|
||||
}
|
||||
|
||||
impl CBinaryOp {
|
||||
|
|
@ -132,6 +134,8 @@ impl CBinaryOp {
|
|||
CBinaryOp::Geq => ">=",
|
||||
CBinaryOp::And => "&&",
|
||||
CBinaryOp::Or => "||",
|
||||
CBinaryOp::BitwiseAnd => "&",
|
||||
CBinaryOp::BitwiseOr => "|",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -454,6 +454,8 @@ impl StatementsTranspiler {
|
|||
BinOp::Geq => Ok(CBinaryOp::Geq),
|
||||
BinOp::And => Ok(CBinaryOp::And),
|
||||
BinOp::Or => Ok(CBinaryOp::Or),
|
||||
BinOp::BitwiseAnd => Ok(CBinaryOp::BitwiseAnd),
|
||||
BinOp::BitwiseOr => Ok(CBinaryOp::BitwiseOr),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -204,6 +204,12 @@ pub enum Token {
|
|||
#[token("or")]
|
||||
Or,
|
||||
|
||||
#[token("bitand")]
|
||||
BitAnd,
|
||||
|
||||
#[token("bitor")]
|
||||
BitOr,
|
||||
|
||||
#[token("xor")]
|
||||
Xor,
|
||||
|
||||
|
|
|
|||
|
|
@ -1114,13 +1114,13 @@ impl Parser {
|
|||
}
|
||||
|
||||
fn parse_and_expr(&mut self) -> Result<Expr, ParseError> {
|
||||
let mut left = self.parse_eq_expr()?;
|
||||
let mut left = self.parse_bitwise_or_expr()?;
|
||||
|
||||
loop {
|
||||
if matches!(self.peek(), Some(Token::And)) {
|
||||
let start = left.span.start;
|
||||
self.next();
|
||||
let right = self.parse_eq_expr()?;
|
||||
let right = self.parse_bitwise_or_expr()?;
|
||||
let end = right.span.end;
|
||||
left = Expr {
|
||||
kind: ExprKind::BinOp(Box::new(left), BinOp::And, Box::new(right)),
|
||||
|
|
@ -1135,6 +1135,50 @@ impl Parser {
|
|||
Ok(left)
|
||||
}
|
||||
|
||||
fn parse_bitwise_or_expr(&mut self) -> Result<Expr, ParseError> {
|
||||
let mut left = self.parse_bitwise_and_expr()?;
|
||||
|
||||
loop {
|
||||
if matches!(self.peek(), Some(Token::BitOr)) {
|
||||
let start = left.span.start;
|
||||
self.next();
|
||||
let right = self.parse_bitwise_and_expr()?;
|
||||
let end = right.span.end;
|
||||
left = Expr {
|
||||
kind: ExprKind::BinOp(Box::new(left), BinOp::BitwiseOr, Box::new(right)),
|
||||
span: Span::new(&(start..end), self.file.clone()),
|
||||
attributes: Vec::new(),
|
||||
};
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(left)
|
||||
}
|
||||
|
||||
fn parse_bitwise_and_expr(&mut self) -> Result<Expr, ParseError> {
|
||||
let mut left = self.parse_eq_expr()?;
|
||||
|
||||
loop {
|
||||
if matches!(self.peek(), Some(Token::BitAnd)) {
|
||||
let start = left.span.start;
|
||||
self.next();
|
||||
let right = self.parse_eq_expr()?;
|
||||
let end = right.span.end;
|
||||
left = Expr {
|
||||
kind: ExprKind::BinOp(Box::new(left), BinOp::BitwiseAnd, Box::new(right)),
|
||||
span: Span::new(&(start..end), self.file.clone()),
|
||||
attributes: Vec::new(),
|
||||
};
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(left)
|
||||
}
|
||||
|
||||
fn parse_eq_expr(&mut self) -> Result<Expr, ParseError> {
|
||||
let mut left = self.parse_comp_expr()?;
|
||||
|
||||
|
|
|
|||
|
|
@ -1840,7 +1840,7 @@ impl TypeChecker {
|
|||
let typed_right = self.typecheck_expr(right)?;
|
||||
|
||||
let result_type = match op {
|
||||
BinOp::Add | BinOp::Sub | BinOp::Mul | BinOp::Div | BinOp::Mod => {
|
||||
BinOp::Add | BinOp::Sub | BinOp::Mul | BinOp::Div | BinOp::Mod | BinOp::BitwiseAnd | BinOp::BitwiseOr => {
|
||||
if !self.types_compatible(&typed_left.ty, &typed_right.ty) {
|
||||
return Err(TypeError {
|
||||
kind: TypeErrorKind::TypeMismatch(
|
||||
|
|
|
|||
BIN
tests/arrays.o
BIN
tests/arrays.o
Binary file not shown.
54
tests/bitwise.c
Normal file
54
tests/bitwise.c
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#include "libsuicmez/libsuicmez.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
void* gc_alloc(const TypeInfo* type, size_t size);
|
||||
void gc_init(void);
|
||||
void gc_shutdown(void);
|
||||
|
||||
// Helper for allocating arrays
|
||||
static void* suic_alloc_array(const TypeInfo* type, size_t elem_size, size_t len, void* init_data) {
|
||||
void* ptr = gc_alloc(type, elem_size * len);
|
||||
if (init_data) memcpy(ptr, init_data, elem_size * len);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
// Helper for allocating structs
|
||||
static void* suic_alloc_struct(const TypeInfo* type, size_t size, void* init_data) {
|
||||
void* ptr = gc_alloc(type, size);
|
||||
if (init_data) memcpy(ptr, init_data, size);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int test_bitwise(void);
|
||||
int suic_main(void);
|
||||
|
||||
|
||||
int test_bitwise(void) {
|
||||
int a = 5;
|
||||
int b = 3;
|
||||
int and_result = (a & b);
|
||||
int or_result = (a | b);
|
||||
return (and_result + or_result);
|
||||
}
|
||||
|
||||
int suic_main(void) {
|
||||
return test_bitwise();
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
// Initialize GC
|
||||
gc_init();
|
||||
// init globals
|
||||
// init event loop
|
||||
int result = suic_main();
|
||||
// Shutdown GC
|
||||
gc_shutdown();
|
||||
return result;
|
||||
}
|
||||
|
||||
14
tests/bitwise.sui
Normal file
14
tests/bitwise.sui
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# Test bitwise operations
|
||||
fn test_bitwise() -> int do
|
||||
let a = 5; # 0b0101
|
||||
let b = 3; # 0b0011
|
||||
|
||||
let and_result = a bitand b; # Should be 1 (0b0001)
|
||||
let or_result = a bitor b; # Should be 7 (0b0111)
|
||||
|
||||
and_result + or_result # Should be 8
|
||||
end
|
||||
|
||||
fn main() -> int do
|
||||
test_bitwise()
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue