Skip to content

Commit a74b7e0

Browse files
committed
cookie-compiler-1.0.0
1 parent 9149a5f commit a74b7e0

14 files changed

Lines changed: 318 additions & 33 deletions

File tree

Main_FrontendAndBackend/lang.cook

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
var a = 645;
2-
var b = 12943;
3-
print(a+b);
4-
5-
int x;
6-
input(x);
7-
if(x lesser than b){
8-
print("x is lesser than b");
9-
}
1+
loop 10000000 {
2+
print("hellp");
3+
}

Main_FrontendAndBackend/output.ll

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,28 @@
11
; ModuleID = 'GoofyLang'
22
source_filename = "GoofyLang"
33

4-
@.str_int = private constant [4 x i8] c"%d\0A\00"
5-
@strlit = private unnamed_addr constant [19 x i8] c"x is lesser than b\00", align 1
4+
@strlit = private unnamed_addr constant [6 x i8] c"hellp\00", align 1
65
@.str_string = private constant [4 x i8] c"%s\0A\00"
76

87
define i32 @main() {
98
global:
10-
%x = alloca i32, align 4
11-
%b = alloca i32, align 4
12-
%a = alloca i32, align 4
13-
store i32 645, ptr %a, align 4
14-
store i32 12943, ptr %b, align 4
15-
%a1 = load i32, ptr %a, align 4
16-
%b2 = load i32, ptr %b, align 4
17-
%addtmp = add i32 %a1, %b2
18-
%0 = call i32 (ptr, ...) @printf(ptr @.str_int, i32 %addtmp)
19-
store i32 0, ptr %x, align 4
20-
%readInt = call i32 @read_int()
21-
store i32 %readInt, ptr %x, align 4
22-
%x3 = load i32, ptr %x, align 4
23-
%b4 = load i32, ptr %b, align 4
24-
%cmptmp = icmp slt i32 %x3, %b4
25-
br i1 %cmptmp, label %then, label %ifcont
9+
%i = alloca i32, align 4
10+
store i32 0, ptr %i, align 4
11+
br label %loopcond
2612

27-
then: ; preds = %global
28-
%1 = call i32 (ptr, ...) @printf(ptr @.str_string, ptr @strlit)
29-
br label %ifcont
13+
loopcond: ; preds = %loopbody, %global
14+
%i1 = load i32, ptr %i, align 4
15+
%loopcond2 = icmp slt i32 %i1, 10000000
16+
br i1 %loopcond2, label %loopbody, label %afterloop
3017

31-
ifcont: ; preds = %then, %global
18+
loopbody: ; preds = %loopcond
19+
%0 = call i32 (ptr, ...) @printf(ptr @.str_string, ptr @strlit)
20+
%inc = add i32 %i1, 1
21+
store i32 %inc, ptr %i, align 4
22+
br label %loopcond
23+
24+
afterloop: ; preds = %loopcond
3225
ret i32 0
3326
}
3427

3528
declare i32 @printf(ptr, ...)
36-
37-
declare i32 @read_int()

cookie-compiler-1.0.0/.DS_Store

8 KB
Binary file not shown.

cookie-compiler-1.0.0/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Cookie Language Compiler - Standalone Distribution
2+
3+
This is a standalone distribution of the Cookie programming language compiler.
4+
5+
## Installation
6+
7+
1. Extract the package:
8+
```bash
9+
tar -xzf cookie-compiler-1.0.0.tar.gz
10+
cd cookie-compiler-1.0.0
11+
```
12+
13+
2. Add to PATH (optional):
14+
```bash
15+
export PATH=$PATH:$(pwd)/bin
16+
```
17+
18+
3. Or install system-wide:
19+
```bash
20+
sudo cp bin/* /usr/local/bin/
21+
sudo cp lib/* /usr/local/lib/
22+
```
23+
24+
## Usage
25+
26+
### Basic compilation:
27+
```bash
28+
./bin/cookie_compiler.sh program.cook
29+
```
30+
31+
### Specify output file:
32+
```bash
33+
./bin/cookie_compiler.sh -o myprogram program.cook
34+
```
35+
36+
### Verbose compilation:
37+
```bash
38+
./bin/cookie_compiler.sh -v program.cook
39+
```
40+
41+
### Keep intermediate files:
42+
```bash
43+
./bin/cookie_compiler.sh -k program.cook
44+
```
45+
46+
## Examples
47+
48+
Try the included examples:
49+
50+
```bash
51+
./bin/cookie_compiler.sh examples/cookie.cook
52+
./a.out
53+
54+
./bin/cookie_compiler.sh examples/lang.cook
55+
echo "10" | ./a.out
56+
```
57+
58+
## Requirements
59+
60+
- LLVM tools (llc command)
61+
- GCC compiler
62+
- Unix-like system (Linux, macOS)
63+
64+
## Language Features
65+
66+
Cookie supports:
67+
- Variables (int, float, bool, char, string)
68+
- Arrays and array operations
69+
- Control flow (if/else, loops, switch)
70+
- Functions with parameters and return values
71+
- Input/output operations
72+
- Type casting and conversions
73+
74+
## Documentation
75+
76+
For complete language documentation, visit:
77+
https://github.com/alphastar-avi/Cookie-lang
78+
79+
## Version
80+
81+
Cookie Compiler v1.0.0 - Standalone Distribution

cookie-compiler-1.0.0/VERSION

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Cookie Language Compiler
2+
Version: 1.0.0
3+
Build Date: Thu Jul 17 18:55:24 IST 2025
4+
Architecture: arm64
5+
Platform: Darwin

cookie-compiler-1.0.0/a.out

32.6 KB
Binary file not shown.

cookie-compiler-1.0.0/bin/cookie

8.09 MB
Binary file not shown.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#!/bin/bash
2+
3+
# Cookie Language Compiler Wrapper
4+
# This script compiles Cookie source code to native executables
5+
6+
set -e
7+
8+
# Default settings
9+
INPUT_FILE=""
10+
OUTPUT_FILE="a.out"
11+
VERBOSE=false
12+
KEEP_INTERMEDIATE=false
13+
RUNTIME_PATH="$(dirname "$0")/../lib/libruntime.a"
14+
15+
# Help function
16+
show_help() {
17+
echo "Cookie Language Compiler"
18+
echo "Usage: $0 [OPTIONS] input.cook"
19+
echo ""
20+
echo "Options:"
21+
echo " -o FILE Output executable name (default: a.out)"
22+
echo " -v Verbose output"
23+
echo " -k Keep intermediate files"
24+
echo " -h Show this help message"
25+
echo ""
26+
echo "Examples:"
27+
echo " $0 program.cook"
28+
echo " $0 -o myprogram program.cook"
29+
echo " $0 -v -k program.cook"
30+
}
31+
32+
# Parse command line arguments
33+
while getopts "o:vkh" opt; do
34+
case $opt in
35+
o)
36+
OUTPUT_FILE="$OPTARG"
37+
;;
38+
v)
39+
VERBOSE=true
40+
;;
41+
k)
42+
KEEP_INTERMEDIATE=true
43+
;;
44+
h)
45+
show_help
46+
exit 0
47+
;;
48+
\?)
49+
echo "Invalid option: -$OPTARG" >&2
50+
show_help
51+
exit 1
52+
;;
53+
esac
54+
done
55+
56+
# Get the input file
57+
shift $((OPTIND-1))
58+
if [ $# -eq 0 ]; then
59+
echo "Error: No input file specified" >&2
60+
show_help
61+
exit 1
62+
fi
63+
64+
INPUT_FILE="$1"
65+
66+
# Check if input file exists
67+
if [ ! -f "$INPUT_FILE" ]; then
68+
echo "Error: Input file '$INPUT_FILE' not found" >&2
69+
exit 1
70+
fi
71+
72+
# Check if Cookie compiler exists
73+
COOKIE_COMPILER="$(dirname "$0")/cookie"
74+
if [ ! -f "$COOKIE_COMPILER" ]; then
75+
echo "Error: Cookie compiler not found at '$COOKIE_COMPILER'" >&2
76+
echo "Please run 'make' to build the compiler first" >&2
77+
exit 1
78+
fi
79+
80+
# Check if runtime library exists
81+
if [ ! -f "$RUNTIME_PATH" ]; then
82+
echo "Error: Runtime library not found at '$RUNTIME_PATH'" >&2
83+
echo "Please run 'make' to build the runtime library first" >&2
84+
exit 1
85+
fi
86+
87+
# Verbose output function
88+
log() {
89+
if [ "$VERBOSE" = true ]; then
90+
echo "[INFO] $1"
91+
fi
92+
}
93+
94+
# Clean up intermediate files
95+
cleanup() {
96+
if [ "$KEEP_INTERMEDIATE" = false ]; then
97+
rm -f output.ll output.o
98+
fi
99+
}
100+
101+
# Set up cleanup trap
102+
trap cleanup EXIT
103+
104+
# Start compilation process
105+
log "Compiling Cookie source: $INPUT_FILE"
106+
107+
# Step 1: Compile Cookie source to LLVM IR
108+
log "Generating LLVM IR..."
109+
if ! cat "$INPUT_FILE" | "$COOKIE_COMPILER" > output.ll 2>&1; then
110+
echo "Error: Failed to compile Cookie source to LLVM IR" >&2
111+
exit 1
112+
fi
113+
114+
# Step 2: Compile LLVM IR to object file
115+
log "Compiling LLVM IR to object code..."
116+
if ! llc -filetype=obj output.ll -o output.o 2>&1; then
117+
echo "Error: Failed to compile LLVM IR to object code" >&2
118+
echo "Make sure 'llc' is installed and in your PATH" >&2
119+
exit 1
120+
fi
121+
122+
# Step 3: Link with runtime library to create executable
123+
log "Linking with runtime library..."
124+
RUNTIME_DIR="$(dirname "$RUNTIME_PATH")"
125+
if ! gcc output.o "$RUNTIME_PATH" -Wl,-rpath,"$RUNTIME_DIR" -o "$OUTPUT_FILE" 2>&1; then
126+
echo "Error: Failed to link object code with runtime library" >&2
127+
echo "Make sure gcc is installed and runtime library is built" >&2
128+
exit 1
129+
fi
130+
131+
# Make executable
132+
chmod +x "$OUTPUT_FILE"
133+
134+
echo "Successfully compiled '$INPUT_FILE' to '$OUTPUT_FILE'"
135+
136+
# Show file info if verbose
137+
if [ "$VERBOSE" = true ]; then
138+
log "Output file info:"
139+
ls -la "$OUTPUT_FILE"
140+
log "File type:"
141+
file "$OUTPUT_FILE"
142+
fi
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
loop 5{
2+
print("dorito");
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var a = 645;
2+
var b = 12943;
3+
print(a+b);
4+
5+
int x;
6+
input(x);
7+
if(x lesser than b){
8+
print("x is lesser than b");
9+
}

0 commit comments

Comments
 (0)