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

use safe memory lib and add memory leaks checks to CI #82

Merged
merged 5 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 11 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
run: |
bison -d -Wcounterexamples lang.y -o lang.tab.c
flex lang.l
gcc -o brainrot lib/hm.c lang.tab.c lex.yy.c ast.c -lfl -lm
gcc -o brainrot lib/hm.c lib/mem.c lang.tab.c lex.yy.c ast.c -lfl -lm

- name: Upload build artifacts
uses: actions/upload-artifact@v4
Expand All @@ -59,7 +59,7 @@ jobs:
- name: Install Python and dependencies
run: |
sudo apt-get update
sudo apt-get install python3 python3-pip -y
sudo apt-get install python3 python3-pip valgrind -y
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
Expand All @@ -70,3 +70,12 @@ jobs:
source .venv/bin/activate
pytest -v test_brainrot.py
working-directory: tests

- name: Run Valgrind on all .brainrot tests
run: |
for f in test_cases/*.brainrot; do
echo "Running Valgrind on $f..."
valgrind --leak-check=full --error-exitcode=1 ./brainrot < "$f"
echo
done
working-directory: .
85 changes: 79 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,83 @@
all:
bison -d -Wcounterexamples lang.y -o lang.tab.c
flex lang.l
gcc -o brainrot lib/hm.c lang.tab.c lex.yy.c ast.c -lfl -lm
# Compiler and tool configurations
CC := gcc
BISON := bison
FLEX := flex
PYTHON := python3

# Compiler and linker flags
CFLAGS := -Wall -Wextra -O2
LDFLAGS := -lfl -lm

# Source files and directories
SRC_DIR := lib
SRCS := $(SRC_DIR)/hm.c $(SRC_DIR)/mem.c ast.c
GENERATED_SRCS := lang.tab.c lex.yy.c
ALL_SRCS := $(SRCS) $(GENERATED_SRCS)

# Output files
TARGET := brainrot
BISON_OUTPUT := lang.tab.c
FLEX_OUTPUT := lex.yy.c

# Default target
.PHONY: all
all: $(TARGET)

# Main executable build
$(TARGET): $(ALL_SRCS)
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

# Generate parser files using Bison
$(BISON_OUTPUT): lang.y
$(BISON) -d -Wcounterexamples $< -o $@

# Generate lexer files using Flex
$(FLEX_OUTPUT): lang.l
$(FLEX) $<

# Run tests
.PHONY: test
test:
pytest -v
$(PYTHON) -m pytest -v

# Clean build artifacts
.PHONY: clean
clean:
rm -rf lang.lex.c lang.tab.c lang.tab.h lex.yy.c brainrot
rm -f $(TARGET) $(GENERATED_SRCS) lang.tab.h
rm -f *.o

# Check dependencies
.PHONY: check-deps
check-deps:
@command -v $(CC) >/dev/null 2>&1 || { echo "Error: gcc not found"; exit 1; }
@command -v $(BISON) >/dev/null 2>&1 || { echo "Error: bison not found"; exit 1; }
@command -v $(FLEX) >/dev/null 2>&1 || { echo "Error: flex not found"; exit 1; }
@command -v $(PYTHON) >/dev/null 2>&1 || { echo "Error: python3 not found"; exit 1; }
@$(PYTHON) -c "import pytest" >/dev/null 2>&1 || { echo "Error: pytest not found. Install with: pip install pytest"; exit 1; }

# Development helper to rebuild everything from scratch
.PHONY: rebuild
rebuild: clean all

# Format source files (requires clang-format)
.PHONY: format
format:
@command -v clang-format >/dev/null 2>&1 || { echo "Error: clang-format not found"; exit 1; }
find . -name "*.c" -o -name "*.h" | xargs clang-format -i

# Show help
.PHONY: help
help:
@echo "Available targets:"
@echo " all : Build the main executable (default target)"
@echo " test : Run the test suite"
@echo " clean : Remove all generated files"
@echo " check-deps : Verify all required dependencies are installed"
@echo " rebuild : Clean and rebuild the project"
@echo " format : Format source files using clang-format"
@echo " help : Show this help message"
@echo ""
@echo "Configuration:"
@echo " CC = $(CC)"
@echo " CFLAGS = $(CFLAGS)"
@echo " LDFLAGS = $(LDFLAGS)"
Loading
Loading